Crossfire Server, Branch 1.12  R12190
cfrhg.c
Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /* Template for version 2.0 plugins.                                         */
00003 /* Contact: yann.chachkoff@myrealbox.com                                     */
00004 /*****************************************************************************/
00005 /* That code is placed under the GNU General Public Licence (GPL)            */
00006 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints)     */
00007 /*****************************************************************************/
00008 /*  CrossFire, A Multiplayer game for X-windows                              */
00009 /*                                                                           */
00010 /*  Copyright (C) 2008 the Crossfire development team                        */
00011 /*                                                                           */
00012 /*  This program is free software; you can redistribute it and/or modify     */
00013 /*  it under the terms of the GNU General Public License as published by     */
00014 /*  the Free Software Foundation; either version 2 of the License, or        */
00015 /*  (at your option) any later version.                                      */
00016 /*                                                                           */
00017 /*  This program is distributed in the hope that it will be useful,          */
00018 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
00019 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
00020 /*  GNU General Public License for more details.                             */
00021 /*                                                                           */
00022 /*  You should have received a copy of the GNU General Public License        */
00023 /*  along with this program; if not, write to the Free Software              */
00024 /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00025 /*                                                                           */
00026 /*****************************************************************************/
00027 
00050 #include <stdarg.h>
00051 #include <assert.h>
00052 
00053 #include <cfrhg.h>
00054 #ifndef __CEXTRACT__
00055 #include <cfrhg_proto.h>
00056 #endif
00057 
00059 typedef struct house_zone_struct {
00060     const char *mappath;        
00061     const char *monsterstyle;   
00062 } house_zone_struct;
00063 
00065 static const house_zone_struct zones[] = {
00066     /* Scorn */
00067     { "/world/world_104_115", "city" },
00068     { "/world/world_105_115", "city" },
00069     { "/world/world_104_116", "city" },
00070     { "/world/world_105_116", "city" },
00071     /* Navar */
00072     { "/world/world_122_116", "city" },
00073     { "/world/world_121_116", "city" },
00074     { "/world/world_122_117", "city" },
00075     { "/world/world_121_117", "city" },
00076     { NULL, NULL }
00077 };
00078 
00086 static const house_zone_struct *get_map_zone(const mapstruct *map) {
00087     int zone;
00088 
00089     for (zone = 0; zones[zone].mappath != NULL; zone++) {
00090         if (strcmp(zones[zone].mappath, map->path) == 0)
00091             return &zones[zone];
00092     }
00093 
00094     return NULL;
00095 }
00096 
00104 static int is_suitable_exit(object *exit) {
00105     assert(exit);
00106 
00107     if (cf_object_get_int_property(exit, CFAPI_OBJECT_PROP_TYPE) != EXIT)
00108         return 0;
00109     if (cf_object_get_sstring_property(exit, CFAPI_OBJECT_PROP_SLAYING) || cf_object_get_sstring_property(exit, CFAPI_OBJECT_PROP_MESSAGE))
00110         return 0;
00111 
00112     return 1;
00113 }
00114 
00124 static int get_exit_seed(const object *exit, const mapstruct *map) {
00125     char r[500];
00126     int seed = 0, len, w = 0;
00127 
00128     snprintf(r, sizeof(r), "%s!%d,%d*%s", exit->arch->name, exit->x, exit->y, map->path);
00129 
00130     len = strlen(r)-1;
00131     while (len >= 0) {
00132         seed ^= ((int)r[len])<<w;
00133         w += 8;
00134         w = w%32;
00135         len--;
00136     }
00137 
00138     return seed;
00139 }
00140 
00150 static void add_exit_to_item(object *exit, const house_zone_struct *zone, const mapstruct *map) {
00151     char params[MAX_BUF];
00152 
00153     assert(exit);
00154     assert(zone);
00155 
00156     snprintf(params, sizeof(params), "layoutstyle onion\n"
00157         "floorstyle indoor\n"
00158         "wallstyle wooden\n"
00159         "monsterstyle %s\n"
00160         "dungeon_level 1\n"
00161         "dungeon_depth 1\n"
00162         "decorstyle furniture\n"
00163         "random_seed %d\n",
00164         zone->monsterstyle,
00165         get_exit_seed(exit, map));
00166 
00167     cf_object_set_string_property(exit, CFAPI_OBJECT_PROP_SLAYING, "/!");
00168     cf_object_set_string_property(exit, CFAPI_OBJECT_PROP_MESSAGE, params);
00169 }
00170 
00176 static void add_exits_to_map(const mapstruct *map) {
00177     int x, y;
00178     object *item;
00179     const house_zone_struct *zone = get_map_zone(map);
00180 
00181     if (!zone)
00182         return;
00183 
00184     for (x = 0; x < MAP_WIDTH(map); x++) {
00185         for (y = 0; y < MAP_HEIGHT(map); y++) {
00186             item = GET_MAP_OB(map, x, y);
00187             while (item) {
00188                 if (is_suitable_exit(item))
00189                     add_exit_to_item(item, zone, map);
00190 
00191                 item = item->above;
00192             }
00193         }
00194     }
00195 }
00196 
00204 CF_PLUGIN void *cfrhg_globalEventListener(int *type, ...) {
00205     va_list args;
00206     static int rv = 0;
00207     mapstruct *map;
00208     int code;
00209 
00210     va_start(args, type);
00211     code = va_arg(args, int);
00212 
00213     rv = 0;
00214 
00215     switch (code) {
00216     case EVENT_MAPLOAD:
00217         map = va_arg(args, mapstruct *);
00218         add_exits_to_map(map);
00219         break;
00220     }
00221     va_end(args);
00222 
00223     return &rv;
00224 }
00225 
00233 CF_PLUGIN void *eventListener(int *type, ...) {
00234     return NULL;
00235 }
00236 
00246 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
00247     cf_init_plugin(gethooksptr);
00248 
00249     cf_log(llevDebug, PLUGIN_VERSION " init\n");
00250 
00251     return 0;
00252 }
00253 
00261 CF_PLUGIN void *getPluginProperty(int *type, ...) {
00262     va_list args;
00263     const char *propname;
00264     int size;
00265     char *buf;
00266 
00267     va_start(args, type);
00268     propname = va_arg(args, const char *);
00269 
00270     if (!strcmp(propname, "Identification")) {
00271         buf = va_arg(args, char *);
00272         size = va_arg(args, int);
00273         va_end(args);
00274         snprintf(buf, size, PLUGIN_NAME);
00275         return NULL;
00276     } else if (!strcmp(propname, "FullName")) {
00277         buf = va_arg(args, char *);
00278         size = va_arg(args, int);
00279         va_end(args);
00280         snprintf(buf, size, PLUGIN_VERSION);
00281         return NULL;
00282     }
00283     va_end(args);
00284     return NULL;
00285 }
00286 
00296 CF_PLUGIN int cfrhg_runPluginCommand(object *op, char *params) {
00297     return -1;
00298 }
00299 
00305 CF_PLUGIN int postInitPlugin(void) {
00306     cf_log(llevDebug, PLUGIN_VERSION " post init\n");
00307 
00308     cf_system_register_global_event(EVENT_MAPLOAD, PLUGIN_NAME, cfrhg_globalEventListener);
00309 
00310     return 0;
00311 }
00312 
00318 CF_PLUGIN int closePlugin(void) {
00319     cf_log(llevDebug, PLUGIN_VERSION " closing\n");
00320     return 0;
00321 }