Debian Codegraph @

plugins/citylife/citylife.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) 2000 Mark Wedel */
11 /* Copyright (C) 1992 Frank Tore Johansen */
12 /* */
13 /* This program is free software; you can redistribute it and/or modify */
14 /* it under the terms of the GNU General Public License as published by */
15 /* the Free Software Foundation; either version 2 of the License, or */
16 /* (at your option) any later version. */
17 /* */
18 /* This program is distributed in the hope that it will be useful, */
19 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
20 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
21 /* GNU General Public License for more details. */
22 /* */
23 /* You should have received a copy of the GNU General Public License */
24 /* along with this program; if not, write to the Free Software */
25 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* */
27 /*****************************************************************************/
28
29 /**
30 * @defgroup plugin_citylife City life plugin
31 * This plugin adds random NPCs to town, and makes them enter houses, spawns new
32 * ones.
33 *
34 * When a map is loaded, NPCs are randomly added so they appear already. During the
35 * course of the server, some will enter houses (disappear), others will exit from
36 * houses (appear on a house).
37 *
38 * For each map to be processed, two things are defined:
39 * - spawn zones, where NPCs should be added when the map is loaded
40 * - spawn points, from which to add new NPCs while the map is in memory.
41 * Should probably be houses and such.
42 *
43 * NPCs use a key/value to prevent them from immediately entering the building
44 * they exited.
45 *
46 * @todo
47 * - define spawn points/zones for other towns
48 * - vary NPCs based on time of day
49 * - define "objectives" to go to
50 * - make NPCs pause when player talks to them
51 *
52 * @{
53 */
54
55 /**
56 * @file
57 * This file is part of the @ref plugin_citylife "city life plugin".
58 * See this page for more information.
59 */
60
61 #include <stdarg.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include "citylife.h"
66 #include "citylife_proto.h"
67 #include "svnversion.h"
68
69 CF_PLUGIN char SvnRevPlugin[] = SVN_REV;
70
71 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
72 cf_init_plugin(gethooksptr);
73
74 cf_log(llevDebug, PLUGIN_VERSION " init\n");
75
76 return 0;
77 }
78
79 CF_PLUGIN void *getPluginProperty(int *type, ...) {
80 va_list args;
81 const char *propname;
82 int size;
83 char *buf;
84
85 va_start(args, type);
86 propname = va_arg(args, const char *);
87
88 if (!strcmp(propname, "Identification")) {
89 buf = va_arg(args, char *);
90 size = va_arg(args, int);
91 va_end(args);
92 snprintf(buf, size, PLUGIN_NAME);
93 return NULL;
94 } else if (!strcmp(propname, "FullName")) {
95 buf = va_arg(args, char *);
96 size = va_arg(args, int);
97 va_end(args);
98 snprintf(buf, size, PLUGIN_VERSION);
99 return NULL;
100 }
101 va_end(args);
102 return NULL;
103 }
104
105 CF_PLUGIN int citylife_runPluginCommand(object *op, char *params) {
106 return -1;
107 }
108
109 /** Key to contain whether it's the first move of the NPC or not. */
110 #define FIRST_MOVE_KEY "citylife_first_move"
111
112 /**
113 * Point from which a NPC can come when the map is loaded.
114 */
115 typedef struct {
116 int x;
117 int y;
118 } spawn_point;
119
120 /**
121 * Zone in which to add NPCs when the map was just loaded.
122 * NPC will be added in [sx, ex[ and [sy, ey[.
123 */
124 typedef struct {
125 int sx, sy, ex, ey;
126 } spawn_zone;
127
128 /**
129 * Options for a map.
130 */
131 typedef struct {
132 const spawn_point *points; /**< Points to spawn from when there is a player on the map. */
133 int count_points; /**< How many items in points. */
134 const spawn_zone *zones; /**< Zones where to spawn at load time. */
135 int count_zones; /**< How many items in zones. */
136 int population; /**< Maximum of NPCs to add at load time. */
137 const char *mapname; /**< Map path. */
138 const char *const *available_archetypes; /**< What archetypes can we chose from for an NPC? */
139 int archetypes_count; /**< Number of items in available_archetypes. */
140 } mapzone;
141 /*@}*/
142 /**
143 * @defgroup citylife_scorn Scorn parameters
144 * Parameters for the @ref plugin_citylife "City life" plugin for Scorn.
145 *
146 * The city is pretty rectangular, so quite easy to define large zones to add to.
147 * @ingroup plugin_citylife
148 */
149 /*@{*/
150 /** Zones for map 104_115. */
151 static const spawn_zone scorn_nw_zones[] = {
152 { 40, 26, 50, 50 }
153 };
154
155 /** Points for map 104_115. */
156 static const spawn_point scorn_nw_points[] = {
157 { 41, 39 },
158 { 48, 35 },
159 { 49, 40 },
160 { 47, 22 },
161 { 49, 37 }
162 };
163
164 /** Zones for map 105_115. */
165 static const spawn_zone scorn_ne_zones[] = {
166 { 0, 26, 22, 50 }
167 };
168
169 /** Points for map 105_115. */
170 static const spawn_point scorn_ne_points[] = {
171 { 15, 42 },
172 { 10, 34 },
173 { 15, 29 },
174 { 1, 25 },
175 { 1, 29 }
176 };
177
178 /** Zones for map 104_116. */
179 static const spawn_zone scorn_sw_zones[] = {
180 { 41, 0, 50, 10 }
181 };
182
183 /** Points for map 104_116. */
184 static const spawn_point scorn_sw_points[] = {
185 { 41, 2 },
186 { 46, 8 },
187 { 42, 8 }
188 };
189
190 /** Zones for map 105_116. */
191 static const spawn_zone scorn_se_zones[] = {
192 { 0, 0, 13, 10 }
193 };
194
195 /** Points for map 105_116. */
196 static const spawn_point scorn_se_points[] = {
197 { 2, 8 },
198 { 11, 8 },
199 { 8, 1 },
200 { 5, 8 }
201 };
202
203 /** Archetypes to spawn in Scorn. */
204 static const char *const scorn_archs[] = {
205 "c_man",
206 "c_woman",
207 "child",
208 "farmer",
209 "fatman",
210 "fatwoman",
211 "guard",
212 "knight",
213 "man",
214 "nun",
215 "sage",
216 "woman"
217 };
218
219 /*@}*/
220 /**
221 * @defgroup citylife_darcap darcap parameters
222 * Parameters for the @ref plugin_citylife "City life" plugin for Darcap.
223 *
224 * The city is triangluar, so we will try 3 rectangles that exclude the inn and docks.
225 * @ingroup plugin_citylife
226 */
227 /*@{*/
228 /** Zones for map 116_102. */
229 static const spawn_zone darcap_zones[] = {
230 { 19, 24, 32, 33 },
231 { 19, 35, 30, 44 },
232 { 31, 14, 41, 25 }
233 };
234
235 /** Points for map 116_102. */
236 static const spawn_point darcap_points[] = {
237 { 23, 25 },
238 { 19, 27 },
239 { 28, 25 },
240 { 28, 30 },
241 { 26, 33 },
242 { 28, 35 },
243 { 19, 37 },
244 { 25, 37 },
245 { 19, 43 },
246 { 32, 44 },
247 { 25, 43 },
248 { 31, 16 },
249 { 31, 18 },
250 { 31, 20 },
251 { 41, 16 },
252 { 41, 18 },
253 { 41, 20 },
254 { 34, 17 },
255 { 37, 22 }
256 };
257
258
259 /** Archetypes to spawn in Darcap. */
260 static const char *const darcap_archs[] = {
261 "c_man",
262 "c_woman",
263 "child",
264 "farmer",
265 "fatman",
266 "fatwoman",
267 "guard",
268 "knight",
269 "man",
270 "nun",
271 "sage",
272 "woman"
273 };
274
275
276 /*@}*/
277 /**
278 * @defgroup citylife_navar Navar parameters
279 * Parameters for the @ref plugin_citylife "City life" plugin for navar.
280 *
281 * The city is lumpy with some internal walls and divisions so we need a bunch oa zones.
282 * @ingroup plugin_citylife
283 */
284 /*@{*/
285 /** Zones for map 121_116. */
286 static const spawn_zone navar_nw_zones[] = {
287 { 38, 29, 48, 35 },
288 { 36, 38, 49, 44 },
289 { 39, 45, 49, 49 },
290 { 36, 48, 38, 49 }
291 };
292
293 /** Points for map 121_116. */
294 static const spawn_point navar_nw_points[] = {
295 { 39, 29 },
296 { 41, 31 },
297 { 48, 29 },
298 { 38, 35 },
299 { 43, 39 },
300 { 38, 46 },
301 { 36, 48 },
302 { 45, 47 }
303 };
304
305 /** Zones for map 122_116. */
306 static const spawn_zone navar_ne_zones[] = {
307 { 0, 33, 7, 49 },
308 { 10, 38, 19, 44},
309 { 11, 45, 19, 49}
310 };
311
312 /** Points for map 122_116. */
313 static const spawn_point navar_ne_points[] = {
314 { 4, 34 },
315 { 2, 39 },
316 { 2, 45 },
317 { 8, 46 },
318 { 13, 47 },
319 { 21, 42 }
320 };
321
322 /** Zones for map 121_117. */
323 static const spawn_zone navar_sw_zones[] = {
324 { 36, 0, 49, 5 },
325 { 39, 6, 49, 15}
326 };
327
328 /** Points for map 121_117. */
329 static const spawn_point navar_sw_points[] = {
330 { 42, 7 },
331 { 37, 4 },
332 { 46, 11 }
333 };
334
335 /** Zones for map 122_117. */
336 static const spawn_zone navar_se_zones[] = {
337 { 0, 0, 8, 15 },
338 { 4, 16, 13, 18},
339 { 9, 4, 18, 14},
340 { 10, 0, 18, 1}
341 };
342
343 /** Points for map 122_117. */
344 static const spawn_point navar_se_points[] = {
345 { 3, 0 },
346 { 1, 1 },
347 { 3, 9 },
348 { 5, 16 },
349 { 18, 14 },
350 { 12, 11 },
351 { 14, 4 },
352 { 12, 0 }
353 };
354
355 /** Archetypes to spawn in Navar. */
356 static const char *const navar_archs[] = {
357 "c_man",
358 "c_woman",
359 "child",
360 "elf_man",
361 "courier",
362 "fatwoman",
363 "guard",
364 "knight",
365 "dwarf_wiz",
366 "clown",
367 "sage",
368 "woman",
369 "fighter",
370 "halfling",
371 "sailor"
372 };
373
374
375 /*@}*/
376 /**
377 * @defgroup citylife_port_joseph Port Joseph parameters
378 * Parameters for the @ref plugin_citylife "City life" plugin for Port Joseph.
379 *
380 * The city is pretty small, so one zone ought to do it.
381 * @ingroup plugin_citylife
382 */
383 /*@{*/
384 /** Zones for map 101_114. */
385 static const spawn_zone port_joseph_zones[] = {
386 { 10, 27, 19, 43 }
387 };
388
389 /** Points for map 101_114. */
390 static const spawn_point port_joseph_points[] = {
391 { 14, 31 },
392 { 17, 32 },
393 { 13, 35 },
394 { 17, 42 },
395 { 13, 38 },
396 { 17, 36 }
397 };
398
399
400 /** Archetypes to spawn in Port Joseph. */
401 static const char *const port_joseph_archs[] = {
402 "c_man",
403 "c_woman",
404 "pirate",
405 "fatman",
406 "fatwoman",
407 "man",
408 "sailor",
409 "woman3"
410 };
411
412 /*@}*/
413 /**
414 * @defgroup citylife_stoneville stoneville parameters
415 * Parameters for the @ref plugin_citylife "City life" plugin for stoneville.
416 *
417 * The city does not have a wall or small boundries, but one zone ought to do it.
418 * @ingroup plugin_citylife
419 */
420 /*@{*/
421 /** Zones for map 103_127. */
422 static const spawn_zone stoneville_zones[] = {
423 { 0, 8, 14, 23 }
424 };
425
426 /** Points for map 103_127. */
427 static const spawn_point stoneville_points[] = {
428 { 3, 9 },
429 { 8, 15 },
430 { 1, 21 },
431 { 2, 17 },
432 { 12, 9 }
433 };
434
435
436 /** Archetypes to spawn in stoneville. */
437 static const char *const stoneville_archs[] = {
438 "c_man",
439 "c_woman",
440 "pirate",
441 "fatman",
442 "fatwoman",
443 "man",
444 "sailor",
445 "woman3"
446 };
447
448
449 /*@}*/
450 /**
451 * @defgroup citylife_wolfsburg Wolfsburg parameters
452 * Parameters for the @ref plugin_citylife "City life" plugin for Wolfsburg.
453 *
454 * The city is a big rectangle, so one zone ought to do it.
455 * @ingroup plugin_citylife
456 */
457 /*@{*/
458 /** Zones for map 128_109. */
459 static const spawn_zone wolfsburg_zones[] = {
460 { 13, 1, 45, 17 }
461 };
462
463 /** Points for map 128_109. */
464 static const spawn_point wolfsburg_points[] = {
465 { 15, 1 },
466 { 19, 7 },
467 { 22, 10 },
468 { 29, 7 },
469 { 42, 2 },
470 { 35, 1 },
471 { 43, 8 },
472 { 33, 11 }
473 };
474
475
476 /** Archetypes to spawn in wolfsburg. */
477 static const char *const wolfsburg_archs[] = {
478 "c_man",
479 "c_woman",
480 /* "prisoner", Why? They don't move. They look ridiculous spawning on the main map. */
481 "fatman",
482 "fatwoman",
483 "man",
484 "sailor",
485 "woman3",
486 "merchant"
487 };
488
489
490 /*@}*/
491 /**
492 * @defgroup citylife_santo_dominion Santo Dominion parameters
493 * Parameters for the @ref plugin_citylife "City life" plugin for Santo Dominion.
494 *
495 * The city is a big rectangle, so one zone ought to do it.
496 * @ingroup plugin_citylife
497 */
498 /*@{*/
499 /** Zones for map 102_108. */
500 static const spawn_zone santo_dominion_zones[] = {
501 { 9, 14, 25, 20 },
502 { 15, 6, 23, 13 }
503 };
504
505 /** Points for map 102_108. */
506 static const spawn_point santo_dominion_points[] = {
507 { 15, 6 },
508 { 18, 5 },
509 { 14, 9 },
510 { 15, 16 },
511 { 31, 15 },
512 { 22, 20 },
513 { 11, 19 },
514 { 26, 11 }
515 };
516
517
518 /** Archetypes to spawn in santo_dominion. */
519 static const char *const santo_dominion_archs[] = {
520 "c_man",
521 "c_woman",
522 /* "prisoner", */
523 "fatman",
524 "fatwoman",
525 "man",
526 "sailor",
527 "woman3",
528 "merchant"
529 };
530
531 /*@}*/
532
533 /**
534 * @defgroup citylife_scorn_county Scorn County parameters.
535 * Parameters for the @ref plugin_citylife "City life" plugin for Scorn County.
536 * @ingroup plugin_citylife
537 */
538 /*@{*/
539 static const spawn_zone scorncounty_zones[] = {
540 {24, 27, 27, 35}, // village
541 {25, 12, 27, 18}, // field
542 {34, 12, 36, 16}, // field
543 {38, 0, 45, 2}, // field
544
545 };
546
547 static const spawn_point scorncounty_points[] = {
548 {24, 27},
549 {26, 27},
550 {24, 29},
551 {27, 29},
552 {24, 31},
553 {24, 34},
554 {34, 32},
555 {41, 1},
556 };
557
558 static const char *const scorncounty_archs[] = {
559 "child",
560 "farmer",
561 "fatman",
562 "fatwoman",
563 };
564 /*@}*/
565
566 /** @ingroup plugin_citylife
567 @{*/
568 /**
569 * All maps we work on.
570 */
571 static const mapzone available_zones[] = {
572 { scorn_nw_points, 5, scorn_nw_zones, 1, 2, "/world/world_104_115", scorn_archs, 12 },
573 { scorn_ne_points, 5, scorn_ne_zones, 1, 5, "/world/world_105_115", scorn_archs, 12 },
574 { scorn_sw_points, 3, scorn_sw_zones, 1, 5, "/world/world_104_116", scorn_archs, 12 },
575 { scorn_se_points, 4, scorn_se_zones, 1, 5, "/world/world_105_116", scorn_archs, 12 },
576 { scorncounty_points, 8, scorncounty_zones, 4, 8, "/world/world_105_116", scorncounty_archs, 4},
577 { darcap_points, 19, darcap_zones, 3, 4, "/world/world_116_102", darcap_archs, 12},
578 { navar_nw_points, 8, navar_nw_zones, 4, 2, "/world/world_121_116", navar_archs, 15 },
579 { navar_ne_points, 6, navar_ne_zones, 3, 5, "/world/world_122_116", navar_archs, 15 },
580 { navar_sw_points, 3, navar_sw_zones, 2, 3, "/world/world_121_117", navar_archs, 15 },
581 { navar_se_points, 8, navar_se_zones, 4, 5, "/world/world_122_117", navar_archs, 15 },
582 { port_joseph_points, 6, port_joseph_zones, 1, 3, "/world/world_101_114", port_joseph_archs, 8 },
583 { stoneville_points, 5, stoneville_zones, 1, 2, "/world/world_103_127", stoneville_archs, 8 },
584 { wolfsburg_points, 8, wolfsburg_zones, 1, 4, "/world/world_128_109", wolfsburg_archs, 8 },
585 { santo_dominion_points, 8, santo_dominion_zones, 2, 3, "/world/world_102_108", santo_dominion_archs, 8 },
586 { NULL, -1, NULL, -1, 1, "", NULL, 0 },
587 };
588
589 /**
590 * Finds if a map has a zone defined.
591 *
592 * @param map
593 * candidat map.
594 * @return
595 * map zone, NULL if not defined.
596 */
597 static const mapzone *get_zone_for_map(mapstruct *map) {
598 int test;
599
600 for (test = 0; available_zones[test].count_points != -1; test++) {
601 if (strcmp(available_zones[test].mapname, map->path) == 0)
602 return &available_zones[test];
603 }
604 return NULL;
605 }
606
607 /**
608 * Creates a NPC for the specified zone, and do needed initialization.
609 * @param zone
610 * what NPCs to create.
611 * @return
612 * new NPC, with event handled for time. NULL if invalid archetype in the zone.
613 */
614 static object *get_npc(const mapzone *zone) {
615 int arch = RANDOM()%zone->archetypes_count;
616 object *npc = cf_create_object_by_name(zone->available_archetypes[arch]);
617 object *evt;
618
619 if (!npc) {
620 cf_log(llevError, PLUGIN_NAME ": get_npc() got NULL object for %s!\n", zone->available_archetypes[arch]);
621 return NULL;
622 }
623
624 cf_object_set_flag(npc, FLAG_RANDOM_MOVE, 1);
625 /* Prevent disease spreading in cities, mostly rabies. */
626 cf_object_set_flag(npc, FLAG_UNDEAD, 1);
627 /* add a key so NPC will not disappear in the house it came from */
628 cf_object_set_key(npc, FIRST_MOVE_KEY, "1", 1);
629
630 evt = cf_create_object_by_name("event_time");
631 evt->slaying = cf_add_string(PLUGIN_NAME);
632 evt->title = cf_add_string(PLUGIN_NAME);
633 cf_object_insert_object(evt, npc);
634
635 return npc;
636 }
637
638 /**
639 * Add an NPC somewhere in a spawn zone.
640 * @param zone
641 * map zone definition from which to get a spawn zone.
642 * @param map
643 * map to insert into.
644 */
645 static void add_npc_to_zone(const mapzone *zone, mapstruct *map) {
646 int which;
647 object *npc = get_npc(zone);
648
649 if (!npc)
650 return;
651 which = RANDOM()%zone->count_zones;
652 if (cf_object_teleport(npc, map, zone->zones[which].sx+RANDOM()%(zone->zones[which].ex-zone->zones[which].sx), zone->zones[which].sy+RANDOM()%(zone->zones[which].ey-zone->zones[which].sy))) {
653 cf_object_free_drop_inventory(npc);
654 }
655 }
656
657 /**
658 * Add an NPC somewhere at a spawn point.
659 * @param zone
660 * map zone definition from which to get a spawn point.
661 * @param map
662 * map to insert into.
663 */
664 static void add_npc_to_point(const mapzone *zone, mapstruct *map) {
665 int which;
666 object *npc = get_npc(zone);
667
668 which = RANDOM()%zone->count_points;
669 if (cf_object_teleport(npc, map, zone->points[which].x, zone->points[which].y)) {
670 cf_object_free_drop_inventory(npc);
671 }
672 }
673
674 /**
675 * Add some NPCs to the map, based on the zone definition.
676 * @param map
677 * map to add to.
678 */
679 static void add_npcs_to_map(mapstruct *map) {
680 int add;
681 const mapzone *zone = get_zone_for_map(map);
682
683 if (!zone)
684 return;
685
686 add = 1+RANDOM()%zone->population;
687 cf_log(llevDebug, PLUGIN_NAME ": adding %d NPC to map %s.\n", add, map->path);
688
689 while (--add >= 0) {
690 add_npc_to_zone(zone, map);
691 }
692 }
693
694 /**
695 * Find a suitable map loaded and add an NPC to it.
696 */
697 static void add_npc_to_random_map(void) {
698 int count, test;
699 mapstruct *list[50];
700 int zones[50];
701 count = 0;
702
703 for (test = 0; available_zones[test].count_points != -1 && count < 50; test++) {
704 if ((list[count] = cf_map_has_been_loaded(available_zones[test].mapname)) && (list[count]->in_memory == MAP_IN_MEMORY)) {
705 zones[count] = test;
706 count++;
707 }
708 }
709 if (!count)
710 return;
711
712 test = RANDOM()%count;
713 add_npc_to_point(&available_zones[zones[test]], list[test]);
714 }
715
716 CF_PLUGIN int citylife_globalEventListener(int *type, ...) {
717 va_list args;
718 int rv = 0;
719 mapstruct *map;
720 int code;
721
722 va_start(args, type);
723 code = va_arg(args, int);
724
725 switch (code) {
726 case EVENT_MAPLOAD:
727 map = va_arg(args, mapstruct *);
728 add_npcs_to_map(map);
729 break;
730
731 case EVENT_CLOCK:
732 if (RANDOM()%40 == 0)
733 add_npc_to_random_map();
734 }
735 va_end(args);
736
737 return rv;
738 }
739
740 CF_PLUGIN int postInitPlugin(void) {
741 cf_log(llevDebug, PLUGIN_VERSION " post init\n");
742
743 /* Pick the global events you want to monitor from this plugin */
744
745 /*
746 cf_system_register_global_event(EVENT_BORN, PLUGIN_NAME, citylife_globalEventListener);
747 cf_system_register_global_event(EVENT_CRASH, PLUGIN_NAME, citylife_globalEventListener);
748 cf_system_register_global_event(EVENT_PLAYER_DEATH, PLUGIN_NAME, citylife_globalEventListener);
749 cf_system_register_global_event(EVENT_GKILL, PLUGIN_NAME, citylife_globalEventListener);
750 cf_system_register_global_event(EVENT_LOGIN, PLUGIN_NAME, citylife_globalEventListener);
751 cf_system_register_global_event(EVENT_LOGOUT, PLUGIN_NAME, citylife_globalEventListener);
752 cf_system_register_global_event(EVENT_MAPENTER, PLUGIN_NAME, citylife_globalEventListener);
753 cf_system_register_global_event(EVENT_MAPLEAVE, PLUGIN_NAME, citylife_globalEventListener);
754 cf_system_register_global_event(EVENT_MAPRESET, PLUGIN_NAME, citylife_globalEventListener);
755 cf_system_register_global_event(EVENT_REMOVE, PLUGIN_NAME, citylife_globalEventListener);
756 cf_system_register_global_event(EVENT_SHOUT, PLUGIN_NAME, citylife_globalEventListener);
757 cf_system_register_global_event(EVENT_TELL, PLUGIN_NAME, citylife_globalEventListener);
758 cf_system_register_global_event(EVENT_MUZZLE, PLUGIN_NAME, citylife_globalEventListener);
759 cf_system_register_global_event(EVENT_KICK, PLUGIN_NAME, citylife_globalEventListener);
760 */
761 cf_system_register_global_event(EVENT_CLOCK, PLUGIN_NAME, citylife_globalEventListener);
762 cf_system_register_global_event(EVENT_MAPLOAD, PLUGIN_NAME, citylife_globalEventListener);
763 /*
764 cf_system_register_global_event(EVENT_MAPRESET, PLUGIN_NAME, citylife_globalEventListener);
765 */
766
767 return 0;
768 }
769
770 CF_PLUGIN int eventListener(int *type, ...) {
771 int rv = 1;
772 va_list args;
773 /*char *buf;*/
774 object *ground, *who/*, *activator, *third*/, *event;
775 /*int fix;*/
776 const char *value;
777
778 va_start(args, type);
779
780 who = va_arg(args, object *);
781 /*activator =*/ va_arg(args, object *);
782 /*third =*/ va_arg(args, object *);
783 /*buf =*/ va_arg(args, char *);
784 /*fix =*/ va_arg(args, int);
785 event = va_arg(args, object *);
786 va_arg(args, talk_info *); /* ignored for now */
787 va_end(args);
788
789 object *inv;
790 value = cf_object_get_key(who, FIRST_MOVE_KEY);
791 if (!value) {
792 /**
793 * If the key doesn't exist, it means 'who' wasn't created by us,
794 * since we do set the key each time.
795 * So play it safe and totally remove the event.
796 */
797 if (event) {
798 cf_log(llevInfo, "citylife: removing event from object which we didn't generate\n");
799 cf_object_remove(event);
800 }
801 return rv;
802 }
803 // Set the flag regardless of whether we tried to move through an exit
804 if (strcmp(value, "1") == 0) {
805 cf_object_set_key(who, FIRST_MOVE_KEY, "0", 1);
806
807 /* must set inventory as no drop, else it'll just drop on the ground */
808 for (inv = cf_object_get_object_property(who, CFAPI_OBJECT_PROP_INVENTORY); inv; inv = cf_object_get_object_property(inv, CFAPI_OBJECT_PROP_OB_BELOW))
809 cf_object_set_flag(inv, FLAG_NO_DROP, 1);
810 }
811 /* should our npc disappear? -- Only attempt this if not first move */
812 else if (RANDOM()%100 < 30) {
813 for (ground = cf_map_get_object_at(who->map, who->x, who->y); ground; ground = cf_object_get_object_property(ground, CFAPI_OBJECT_PROP_OB_ABOVE)) {
814 if (ground->type == EXIT) {
815
816 cf_object_remove(who);
817 cf_object_free_drop_inventory(who);
818 return rv;
819 }
820 }
821 }
822
823 /* we have to move manually, because during the night NPCs don't move. */
824 cf_object_move(who, 1+RANDOM()%8, NULL);
825
826 return rv;
827 }
828
829 CF_PLUGIN int closePlugin(void) {
830 cf_log(llevDebug, PLUGIN_VERSION " closing\n");
831 return 0;
832 }
833 /*@}*/