Debian Codegraph @

plugins/cfrhg/cfrhg.c

raw symbols SCIP
1 /*****************************************************************************/
2 /* Template for version 2.0 plugins. */
3 /* Contact: [email protected] */
4 /*****************************************************************************/
5 /* That code is placed under the GNU General Public Licence (GPL) */
6 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints) */
7 /*****************************************************************************/
8 /* CrossFire, A Multiplayer game for X-windows */
9 /* */
10 /* Copyright (C) 2008 the Crossfire development team */
11 /* */
12 /* This program is free software; you can redistribute it and/or modify */
13 /* it under the terms of the GNU General Public License as published by */
14 /* the Free Software Foundation; either version 2 of the License, or */
15 /* (at your option) any later version. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* */
26 /*****************************************************************************/
27
28 /**
29 * @defgroup plugin_rhg Random house generation plugin
30 * This plugin links unused exits to random maps.
31 * The random map parameters vary based on the map it is in, but each exit will always use
32 * the same random seed to have the same layout and content.
33 *
34 * Exits will only point to a random map if both their @ref obj::slaying "slaying" and
35 * @ref obj::msg "msg" fields are empty.
36 *
37 * @todo
38 * - make more parameters vary based on maps
39 * - add exits to all towns
40 *
41 * @{
42 */
43
44 /**
45 * @file
46 * This file is part of the @ref plugin_rhg "random house generation plugin".
47 * See this page for more information.
48 */
49
50 #include <stdarg.h>
51 #include <assert.h>
52 #include <string.h>
53
54 #include <cfrhg.h>
55 #include <cfrhg_proto.h>
56 #include <svnversion.h>
57
58 CF_PLUGIN char SvnRevPlugin[] = SVN_REV;
59
60 /** Link between a map and the exits to generate for it. */
61 typedef struct house_zone_struct {
62 const char *mappath; /**< Full map path. */
63 const char *monsterstyle; /**< Style of monsters. */
64 } house_zone_struct;
65
66 /** Maps we work on. */
67 static const house_zone_struct zones[] = {
68 /* Scorn */
69 { "/world/world_104_115", "city" },
70 { "/world/world_105_115", "city" },
71 { "/world/world_104_116", "city" },
72 { "/world/world_105_116", "city" },
73 /* Navar */
74 { "/world/world_122_116", "city" },
75 { "/world/world_121_116", "city" },
76 { "/world/world_122_117", "city" },
77 { "/world/world_121_117", "city" },
78 { NULL, NULL }
79 };
80
81 /**
82 * Get the random map parameters of a map.
83 * @param map
84 * map to get the zone of.
85 * @return
86 * NULL if the map shouldn't be processed, else its parameters.
87 */
88 static const house_zone_struct *get_map_zone(const mapstruct *map) {
89 int zone;
90
91 for (zone = 0; zones[zone].mappath != NULL; zone++) {
92 if (strcmp(zones[zone].mappath, map->path) == 0)
93 return &zones[zone];
94 }
95
96 return NULL;
97 }
98
99 /**
100 * Should we add a random map to this exit?
101 * @param exit
102 * exit to check.
103 * @return
104 * 1 if a map should be set, 0 else.
105 */
106 static int is_suitable_exit(object *exit) {
107 assert(exit);
108
109 if (cf_object_get_int_property(exit, CFAPI_OBJECT_PROP_TYPE) != EXIT)
110 return 0;
111 if (cf_object_get_sstring_property(exit, CFAPI_OBJECT_PROP_SLAYING) || cf_object_get_sstring_property(exit, CFAPI_OBJECT_PROP_MESSAGE))
112 return 0;
113
114 return 1;
115 }
116
117 /**
118 * Get the random map seed. Will always yield the same value for the same (position included) exit.
119 * @param exit
120 * exit to get the seed of.
121 * @param map
122 * map the exit is on.
123 * @return
124 * random seed.
125 */
126 static int get_exit_seed(const object *exit, const mapstruct *map) {
127 char r[500];
128 int seed = 0, len, w = 0;
129
130 snprintf(r, sizeof(r), "%s!%d,%d*%s", exit->arch->name, exit->x, exit->y, map->path);
131
132 len = strlen(r)-1;
133 while (len >= 0) {
134 seed ^= ((int)r[len])<<w;
135 w += 8;
136 w = w%32;
137 len--;
138 }
139
140 return seed;
141 }
142
143 /**
144 * Change an empty exit to point to a random map.
145 * @param exit
146 * exit to alter.
147 * @param zone
148 * zone we're part of, to know the random map parameters.
149 * @param map
150 * map the exit is on.
151 */
152 static void add_exit_to_item(object *exit, const house_zone_struct *zone, const mapstruct *map) {
153 char params[MAX_BUF];
154
155 assert(exit);
156 assert(zone);
157
158 snprintf(params, sizeof(params), "layoutstyle onion\n"
159 "floorstyle indoor\n"
160 "wallstyle wooden\n"
161 "monsterstyle %s\n"
162 "dungeon_level 1\n"
163 "dungeon_depth 1\n"
164 "decorstyle furniture\n"
165 "random_seed %d\n",
166 zone->monsterstyle,
167 get_exit_seed(exit, map));
168
169 cf_object_set_string_property(exit, CFAPI_OBJECT_PROP_SLAYING, "/!");
170 cf_object_set_string_property(exit, CFAPI_OBJECT_PROP_MESSAGE, params);
171 }
172
173 /**
174 * Checks if the map should be processed, and if so process it.
175 * @param map
176 * map to work on.
177 */
178 static void add_exits_to_map(const mapstruct *map) {
179 int x, y;
180 const house_zone_struct *zone = get_map_zone(map);
181
182 if (!zone)
183 return;
184
185 for (x = 0; x < MAP_WIDTH(map); x++) {
186 for (y = 0; y < MAP_HEIGHT(map); y++) {
187 FOR_MAP_PREPARE(map, x, y, item) {
188 if (is_suitable_exit(item))
189 add_exit_to_item(item, zone, map);
190 } FOR_MAP_FINISH();
191 }
192 }
193 }
194
195 /**
196 * Global server event handling. Only uses EVENT_MAPLOAD.
197 * @param type
198 * unused.
199 * @return
200 * 0.
201 */
202 CF_PLUGIN int cfrhg_globalEventListener(int *type, ...) {
203 va_list args;
204 int rv = 0;
205 mapstruct *map;
206 int code;
207
208 va_start(args, type);
209 code = va_arg(args, int);
210
211 switch (code) {
212 case EVENT_MAPLOAD:
213 map = va_arg(args, mapstruct *);
214 add_exits_to_map(map);
215 break;
216 }
217 va_end(args);
218
219 return rv;
220 }
221
222 /**
223 * Unused.
224 * @param type
225 * unused.
226 * @return
227 * 0.
228 */
229 CF_PLUGIN int eventListener(int *type, ...) {
230 return 0;
231 }
232
233 /**
234 * Plugin initialization.
235 * @param iversion
236 * server version.
237 * @param gethooksptr
238 * function to get hooks.
239 * @return
240 * 0.
241 */
242 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
243 cf_init_plugin(gethooksptr);
244
245 cf_log(llevDebug, PLUGIN_VERSION " init\n");
246
247 return 0;
248 }
249
250 /**
251 * Get the plugin identification or full name.
252 * @param type
253 * unused.
254 * @return
255 * NULL.
256 */
257 CF_PLUGIN void *getPluginProperty(int *type, ...) {
258 va_list args;
259 const char *propname;
260 int size;
261 char *buf;
262
263 va_start(args, type);
264 propname = va_arg(args, const char *);
265
266 if (!strcmp(propname, "Identification")) {
267 buf = va_arg(args, char *);
268 size = va_arg(args, int);
269 va_end(args);
270 snprintf(buf, size, PLUGIN_NAME);
271 return NULL;
272 } else if (!strcmp(propname, "FullName")) {
273 buf = va_arg(args, char *);
274 size = va_arg(args, int);
275 va_end(args);
276 snprintf(buf, size, PLUGIN_VERSION);
277 return NULL;
278 }
279 va_end(args);
280 return NULL;
281 }
282
283 /**
284 * Unused.
285 * @param op
286 * unused.
287 * @param params
288 * unused.
289 * @return
290 * -1.
291 */
292 CF_PLUGIN int cfrhg_runPluginCommand(object *op, char *params) {
293 return -1;
294 }
295
296 /**
297 * Plugin initialisation.
298 * @return
299 * 0.
300 */
301 CF_PLUGIN int postInitPlugin(void) {
302 cf_log(llevDebug, PLUGIN_VERSION " post init\n");
303
304 cf_system_register_global_event(EVENT_MAPLOAD, PLUGIN_NAME, cfrhg_globalEventListener);
305
306 return 0;
307 }
308
309 /**
310 * Unloading of plugin.
311 * @return
312 * 0.
313 */
314 CF_PLUGIN int closePlugin(void) {
315 cf_log(llevDebug, PLUGIN_VERSION " closing\n");
316 return 0;
317 }
318 /*@}*/