Debian Codegraph @

plugins/cfanim/cfanim.c

raw symbols SCIP
1 /*****************************************************************************/
2 /* Crossfire Animator v2.0a */
3 /* Contacts: [email protected], [email protected] */
4 /*****************************************************************************/
5 /* That code is placed under the GNU General Public Licence (GPL) */
6 /* */
7 /* (C) 2001 David Delbecq for the original code version. */
8 /*****************************************************************************/
9 /* CrossFire, A Multiplayer game for X-windows */
10 /* */
11 /* Copyright (C) 2000 Mark Wedel */
12 /* Copyright (C) 1992 Frank Tore Johansen */
13 /* */
14 /* This program is free software; you can redistribute it and/or modify */
15 /* it under the terms of the GNU General Public License as published by */
16 /* the Free Software Foundation; either version 2 of the License, or */
17 /* (at your option) any later version. */
18 /* */
19 /* This program is distributed in the hope that it will be useful, */
20 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
21 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
22 /* GNU General Public License for more details. */
23 /* */
24 /* You should have received a copy of the GNU General Public License */
25 /* along with this program; if not, write to the Free Software */
26 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* */
28 /*****************************************************************************/
29
30 /* First let's include the header file needed */
31
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "cfanim.h"
38 #include "svnversion.h"
39
40 CF_PLUGIN char SvnRevPlugin[] = SVN_REV;
41
42 static CFanimation *first_animation = NULL; /**< Animations we're currently processing. */
43
44 static int get_boolean(const char *strg, int *bl);
45
46 /**
47 * Returns the direction from its name.
48 * @param name direction's name
49 * @return direction or -1 if unknown.
50 */
51 static int get_dir_from_name(const char *name) {
52 if (!strcmp(name, "north"))
53 return 1;
54 if (!strcmp(name, "north_east"))
55 return 2;
56 if (!strcmp(name, "east"))
57 return 3;
58 if (!strcmp(name, "south_east"))
59 return 4;
60 if (!strcmp(name, "south"))
61 return 5;
62 if (!strcmp(name, "south_west"))
63 return 6;
64 if (!strcmp(name, "west"))
65 return 7;
66 if (!strcmp(name, "north_west"))
67 return 8;
68 return -1;
69 }
70
71 static long int initmovement(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
72 int dir;
73
74 dir = get_dir_from_name(name);
75 move_entity->parameters = NULL;
76 return dir;
77 }
78
79 static anim_move_result runmovement(struct CFanimation_struct *animation, long int id, void *parameters) {
80 object *op = animation->victim;
81 int dir = id;
82
83 if (animation->verbose)
84 cf_log(llevDebug, "CFAnim: Moving in direction %ld\n", id);
85 if (op->type == PLAYER)
86 cf_player_move(op->contr, dir);
87 else
88 cf_object_move(op, dir, op);
89 return mr_finished;
90 }
91
92 static long int initfire(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
93 int dir;
94
95 dir = get_dir_from_name(&(name[5]));
96 move_entity->parameters = NULL;
97 return dir;
98 }
99
100 /** @todo fix */
101 static anim_move_result runfire(struct CFanimation_struct *animation, long int id, void *parameters) {
102 if (animation->verbose)
103 cf_log(llevDebug, "CFAnim: Firing in direction %ld\n", id);
104 return mr_finished;
105 }
106
107 static long int initturn(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
108 int dir;
109
110 dir = get_dir_from_name(&(name[5]));
111 move_entity->parameters = NULL;
112 return dir;
113 }
114
115 static anim_move_result runturn(struct CFanimation_struct *animation, long int id, void *parameters) {
116 object *op = animation->victim;
117 int dir = id;
118 /*int face;*/
119
120 if (animation->verbose)
121 cf_log(llevDebug, "CFAnim: Turning in direction %ld\n", id);
122 op->facing = dir;
123 /** @todo fix suspicious or missing call */
124 /* cf_object_set_int_property(op, CFAPI_OBJECT_PROP_ANIMATION, face);*/
125 return mr_finished;
126 }
127
128 static long int initcamera(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
129 int dir;
130
131 dir = get_dir_from_name(&(name[7]));
132 move_entity->parameters = NULL;
133 return dir;
134 }
135
136 /** @todo fix */
137 static anim_move_result runcamera(struct CFanimation_struct *animation, long int id, void *parameters) {
138 if (animation->verbose)
139 cf_log(llevDebug, "CFAnim: Moving the camera in direction %ld\n", id);
140 return mr_finished;
141 /*if (animation->victim->type == PLAYER)
142 hook_scroll_map(animation->victim, id);
143 else
144 printf("CFAnim: Not a player\n");
145 return 1;*/
146 }
147
148 static long int initvisible(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
149 int result;
150
151 if (get_boolean(parameters, &result))
152 return result;
153 cf_log(llevError, "CFAnim: Error in animation - possible values for 'invisible' are 'yes' and 'no'\n");
154 return -1;
155 }
156
157 static anim_move_result runvisible(struct CFanimation_struct *animation, long int id, void *parameters) {
158 if (id == -1)
159 return mr_finished;
160 animation->invisible = id;
161 return mr_finished;
162 }
163
164 static long int initwizard(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
165 int result;
166
167 if (get_boolean(parameters, &result))
168 return result;
169 cf_log(llevError, "CFAnim: Error in animation - possible values for 'wizard' are 'yes' and 'no'\n");
170 return -1;
171 }
172
173 static anim_move_result runwizard(struct CFanimation_struct *animation, long int id, void *parameters) {
174 if (id == -1)
175 return 1;
176 animation->wizard = id;
177 return mr_finished;
178 }
179
180 static long int initsay(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
181 if (parameters)
182 move_entity->parameters = cf_strdup_local(parameters);
183 else
184 move_entity->parameters = NULL;
185 if (move_entity->parent->verbose)
186 cf_log(llevDebug, "CFAnim: init say: parameters: %s\n", parameters ? parameters : "null");
187 return 1;
188 }
189
190 static anim_move_result runsay(struct CFanimation_struct *animation, long int id, void *parameters) {
191 if (parameters) {
192 cf_object_say(animation->victim, parameters);
193 free(parameters);
194 } else
195 cf_log(llevError, "CFAnim: Error in animation: nothing to say with say function\n");
196 return mr_finished;
197 }
198
199 static long int initapply(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
200 return 1;
201 }
202
203 static anim_move_result runapply(struct CFanimation_struct *animation, long int id, void *parameters) {
204 object *current_container;
205
206 if (animation->victim->type != PLAYER)
207 return mr_finished;
208 current_container = animation->victim->container;
209 animation->victim->container = NULL;
210 cf_object_apply_below(animation->victim);
211 animation->victim->container = current_container;
212 return mr_finished;
213 }
214
215 static long int initapplyobject(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
216 move_entity->parameters = parameters ? (void*)cf_add_string(parameters) : NULL;
217 return 1;
218 }
219
220 static anim_move_result runapplyobject(struct CFanimation_struct *animation, long int id, void *parameters) {
221 object *current;
222 int aflag;
223
224 if (!parameters)
225 return mr_finished;
226 current = animation->victim->below;
227 FOR_OB_AND_BELOW_PREPARE(current)
228 if (current->name == parameters)
229 break;
230 FOR_OB_AND_BELOW_FINISH();
231 if (!current)
232 current = cf_object_find_by_name(animation->victim, parameters);
233 if (!current) {
234 cf_free_string(parameters);
235 return mr_finished;
236 }
237 aflag = AP_APPLY;
238 cf_object_apply(animation->victim, current, aflag);
239 cf_free_string(parameters);
240 return mr_finished;
241 }
242
243 static long int initdropobject(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
244 move_entity->parameters = parameters ?
245 (void *)cf_add_string(parameters) : NULL;
246 return 1;
247 }
248
249 static anim_move_result rundropobject(struct CFanimation_struct *animation, long int id, void *parameters) {
250 object *what;
251 if (!parameters)
252 return mr_finished;
253 what = cf_object_find_by_name(animation->victim, parameters);
254 if (what != NULL)
255 cf_object_drop(what, animation->victim);
256 cf_free_string(parameters);
257 return mr_finished;
258 }
259
260 static long int initpickup(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
261 return 1;
262 }
263
264 static anim_move_result runpickup(struct CFanimation_struct *animation, long int id, void *parameters) {
265 object *current;
266
267 current = animation->victim->below;
268 if (!current)
269 return mr_finished;
270 cf_object_pickup(animation->victim, current);
271 return mr_finished;
272 }
273
274 static long int initpickupobject(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
275 move_entity->parameters = parameters ? (void*)cf_add_string(parameters) : NULL;
276 return 1;
277 }
278
279 static anim_move_result runpickupobject(struct CFanimation_struct *animation, long int id, void *parameters) {
280 if (!parameters)
281 return mr_finished;
282 FOR_BELOW_PREPARE(animation->victim, current)
283 if (current->name == parameters) {
284 cf_object_pickup(animation->victim, current);
285 break;
286 }
287 FOR_BELOW_FINISH();
288 cf_free_string(parameters);
289 return mr_finished;
290 }
291
292 static long int initghosted(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
293 int result;
294
295 if (get_boolean(parameters, &result))
296 return result;
297 cf_log(llevError, "CFAnim: Error in animation: possible values for 'ghosted' are 'yes' and 'no'\n");
298 return -1;
299 }
300
301 static anim_move_result runghosted(struct CFanimation_struct *animation, long int id, void *parameters) {
302 object *corpse;
303
304 if ((id && animation->ghosted)
305 || (!id && !animation->ghosted))
306 runghosted(animation, !id, parameters);
307 if (id) { /*Create a ghost/corpse pair*/
308 corpse = cf_object_clone(animation->victim, 1);
309 corpse->x = animation->victim->x;
310 corpse->y = animation->victim->y;
311 corpse->type = 0;
312 CLEAR_FLAG(corpse, FLAG_WIZ);
313 corpse->contr = NULL;
314 cf_map_insert_object_there(corpse, animation->victim->map, NULL, 0);
315 animation->wizard = 1;
316 animation->invisible = 1;
317 animation->corpse = corpse;
318 } else { /*Remove a corpse, make current player visible*/
319 animation->wizard = 0;
320 animation->invisible = 0;
321 cf_object_remove(animation->corpse);
322 cf_object_free_drop_inventory(animation->corpse);
323 animation->corpse = NULL;
324 animation->victim->invisible = 0;
325 cf_player_move(animation->victim->contr, 0);
326 }
327 animation->ghosted = id;
328 return mr_finished;
329 }
330
331 typedef struct {
332 char *mapname;
333 int mapx;
334 int mapy;
335 } teleport_params;
336
337 static long int initteleport(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
338 char *mapname;
339 int mapx;
340 int mapy;
341 teleport_params *teleport;
342
343 move_entity->parameters = NULL;
344 cf_log(llevDebug, ".(%s)\n", parameters);
345 if (!parameters) {
346 cf_log(llevError, "CFAnim: Error - no parameters for teleport\n");
347 return 0;
348 }
349 mapname = strstr(parameters, " ");
350 cf_log(llevDebug, ".(%s)\n", parameters);
351 if (!mapname)
352 return 0;
353 *mapname = '\0';
354 mapx = atoi(parameters);
355 mapname++;
356 parameters = mapname;
357 assert(parameters != NULL);
358 cf_log(llevDebug, ".(%s)\n", parameters);
359 mapname = strstr(parameters, " ");
360 cf_log(llevDebug, ".\n");
361 if (!mapname)
362 return 0;
363 *mapname = '\0';
364 mapy = atoi(parameters);
365 mapname++;
366 if (mapname[0] == '\0')
367 return 0;
368 teleport = (teleport_params *)malloc(sizeof(teleport_params));
369 teleport->mapname = cf_strdup_local(mapname);
370 teleport->mapx = mapx;
371 teleport->mapy = mapy;
372 move_entity->parameters = teleport;
373 return 1;
374 }
375
376 static anim_move_result runteleport(struct CFanimation_struct *animation, long int id, void *parameters) {
377 teleport_params *teleport = (teleport_params *)parameters;
378
379 if (!parameters)
380 return mr_finished;
381 cf_object_teleport(animation->victim, cf_map_get_map(teleport->mapname, 0), teleport->mapx, teleport->mapy);
382 free(parameters);
383 return mr_finished;
384 }
385
386 static long int initnotice(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
387 move_entity->parameters = parameters ? cf_strdup_local(parameters) : NULL;
388 return 1;
389 }
390 static anim_move_result runnotice(struct CFanimation_struct *animation, long int id, void *parameters) {
391 int val;
392
393 val = NDI_NAVY|NDI_UNIQUE;
394
395 cf_player_message(animation->victim, parameters, val);
396 return mr_finished;
397 }
398
399 static long int initstop(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
400 return 1;
401 }
402
403 /** @todo fix */
404 static anim_move_result runstop(struct CFanimation_struct *animation, long int id, void *parameters) {
405 if (animation->verbose)
406 cf_log(llevDebug, "CFAnim: stop encountered\n");
407 return mr_finished;
408 }
409
410 /** Destination for moveto command. */
411 typedef struct {
412 int x, y; /**< Coordinates. */
413 } param_moveto;
414
415 static long int initmoveto(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
416 param_moveto *moveto;
417 int x, y;
418
419 if (sscanf(parameters, "%d %d", &x, &y) != 2)
420 return 0;
421
422 moveto = (param_moveto *)calloc(1, sizeof(param_moveto));
423 moveto->x = x;
424 moveto->y = y;
425 move_entity->parameters = moveto;
426
427 return 1;
428 }
429
430 static anim_move_result runmoveto(struct CFanimation_struct *animation, long int id, void *parameters) {
431 int move;
432 param_moveto *dest = (param_moveto *)parameters;
433
434 if (!dest)
435 return mr_finished;
436
437 move = cf_object_move_to(animation->victim, dest->x, dest->y);
438
439 if (animation->victim->x == dest->x && animation->victim->y == dest->y) {
440 free(parameters);
441 return mr_finished;
442 }
443
444 if (move == 1)
445 return mr_again;
446
447 return mr_finished;
448 }
449
450 static long int initmessage(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
451 if (parameters)
452 move_entity->parameters = strdup(parameters);
453 else
454 move_entity->parameters = NULL;
455 return 1;
456 }
457
458 static anim_move_result runmessage(struct CFanimation_struct *animation, long int id, void *parameters) {
459 if (parameters && animation->victim->map) {
460 cf_map_message(animation->victim->map, (const char *)parameters, NDI_UNIQUE|NDI_GREEN);
461 free(parameters);
462 }
463
464 return mr_finished;
465 }
466
467 static long int inittrigger(const char *name, char *parameters, struct CFmovement_struct *move_entity) {
468 long int connection;
469
470 move_entity->parameters = NULL;
471 if (sscanf(parameters, "%ld", &connection) != 1 || connection <= 0) {
472 cf_log(llevError, "CFAnim: invalid connection %s\n", parameters);
473 return 0;
474 }
475 return connection;
476 }
477
478 static anim_move_result runtrigger(struct CFanimation_struct *animation, long int id, void *parameters) {
479 oblinkpt *olp;
480 mapstruct *map;
481 objectlink *ol = NULL;
482
483 if (animation->victim == NULL || animation->victim->map == NULL) {
484 cf_log(llevError, "CFAnim: trigger for victim not on map or NULL.\n");
485 return mr_finished;
486 }
487
488 map = animation->victim->map;
489
490 /* locate objectlink for this connected value */
491 if (!map->buttons) {
492 cf_log(llevError, "Map %s called for trigger on connected %d but there ain't any button list for that map!\n", cf_map_get_sstring_property(map, CFAPI_MAP_PROP_PATH), id);
493 return mr_finished;
494 }
495 for (olp = map->buttons; olp; olp = olp->next) {
496 if (olp->value == id) {
497 ol = olp->link;
498 break;
499 }
500 }
501 if (ol == NULL) {
502 cf_log(llevError, "Map %s called for trigger on connected %d but there ain't any button list for that map!\n", cf_map_get_sstring_property(map, CFAPI_MAP_PROP_PATH), id);
503 return mr_finished;
504 }
505 /* run the object link */
506 cf_map_trigger_connected(ol, NULL, 1);
507
508 return mr_finished;
509 }
510
511 /** Available animation commands. */
512 CFanimationHook animationbox[] = {
513 { "north", initmovement, runmovement },
514 { "north_east", initmovement, runmovement },
515 { "east", initmovement, runmovement },
516 { "south_east", initmovement, runmovement },
517 { "south", initmovement, runmovement },
518 { "south_west", initmovement, runmovement },
519 { "west", initmovement, runmovement },
520 { "north_west", initmovement, runmovement },
521 { "fire_north", initfire, runfire },
522 { "fire_north_east", initfire, runfire },
523 { "fire_east", initfire, runfire },
524 { "fire_south_east", initfire, runfire },
525 { "fire_south", initfire, runfire },
526 { "fire_south_west", initfire, runfire },
527 { "fire_west", initfire, runfire },
528 { "fire_north_west", initfire, runfire },
529 { "turn_north", initturn, runturn },
530 { "turn_north_east", initturn, runturn },
531 { "turn_east", initturn, runturn },
532 { "turn_south_east", initturn, runturn },
533 { "turn_south", initturn, runturn },
534 { "turn_south_west", initturn, runturn },
535 { "turn_west", initturn, runturn },
536 { "turn_north_west", initturn, runturn },
537 { "camera_north", initcamera, runcamera },
538 { "camera_north_east", initcamera, runcamera },
539 { "camera_east", initcamera, runcamera },
540 { "camera_south_east", initcamera, runcamera },
541 { "camera_south", initcamera, runcamera },
542 { "camera_south_west", initcamera, runcamera },
543 { "camera_west", initcamera, runcamera },
544 { "camera_north_west", initcamera, runcamera },
545 { "invisible", initvisible, runvisible },
546 { "wizard", initwizard, runwizard },
547 { "say", initsay, runsay },
548 { "apply", initapply, runapply },
549 { "apply_object", initapplyobject, runapplyobject },
550 { "drop_object", initdropobject, rundropobject },
551 { "pickup", initpickup, runpickup },
552 { "pickup_object", initpickupobject, runpickupobject },
553 { "ghosted", initghosted, runghosted },
554 { "teleport", initteleport, runteleport },
555 { "notice", initnotice, runnotice },
556 { "stop", initstop, runstop },
557 { "moveto", initmoveto, runmoveto },
558 { "message", initmessage, runmessage },
559 { "trigger", inittrigger, runtrigger }
560 };
561
562 int animationcount = sizeof(animationbox)/sizeof(CFanimationHook);
563
564 static int ordered_commands = 0;
565
566 static int compareAnims(const void *a, const void *b) {
567 return strcmp(((const CFanimationHook *)a)->name, ((const CFanimationHook *)b)->name);
568 }
569
570 static void prepare_commands(void) {
571 qsort(animationbox, animationcount, sizeof(CFanimationHook), compareAnims);
572 ordered_commands = 1;
573 }
574
575 static CFanimationHook *get_command(char *command) {
576 CFanimationHook dummy;
577
578 dummy.name = command;
579 if (!ordered_commands)
580 prepare_commands();
581 return (CFanimationHook *)bsearch(&dummy, animationbox, animationcount, sizeof(CFanimationHook), compareAnims);
582 }
583
584 /**
585 * Parse an animation block in the animation file.
586 * @param buffer buffer to read data info, will have been modified when function exits.
587 * @param buffer_size size of buffer.
588 * @param fichier file to read from.
589 * @param parent animation we're reading the block for.
590 * @return one animation frame.
591 */
592 static CFmovement *parse_animation_block(char *buffer, size_t buffer_size, FILE *fichier, CFanimation *parent) {
593 CFmovement *first = NULL;
594 CFmovement *current = NULL;
595 CFmovement *next;
596 char *time;
597 char *name;
598 char *parameters;
599 int tick;
600 CFanimationHook *animationhook;
601
602 if (parent->verbose)
603 cf_log(llevDebug, "CFAnim: In parse block for %s\n", buffer);
604 while (fgets(buffer, buffer_size, fichier)) {
605 if (buffer[0] == '[')
606 break;
607 if (buffer[0] == '#')
608 continue;
609 buffer[strlen(buffer)-strlen("\n")] = '\0';
610 while (buffer[strlen(buffer)-1] == ' ')
611 buffer[strlen(buffer)-1] = '\0';
612 if (strlen(buffer) <= 0)
613 continue;
614 time = buffer;
615
616 name = strstr(buffer, " ");
617 if (!name)
618 continue;
619 *name = '\0';
620 name++;
621 while (*name == ' ')
622 name++;
623
624 tick = atoi(time);
625 if (tick < 0)
626 continue;
627
628 parameters = strstr(name, " ");
629 if (parameters) { /*Parameters may be nul*/
630 *parameters = '\0';
631 parameters++;
632 while (*parameters == ' ')
633 parameters++;
634 if (*parameters == '\0')
635 parameters = NULL;
636 }
637 animationhook = get_command(name);
638 if (!animationhook)
639 cf_log(llevError, "CFAnim: %s - Unknown animation command\n", name);
640 else if (parent->verbose) {
641 cf_log(llevDebug, "CFAnim: Parsed %s -> %p\n", name, animationhook);
642 }
643 if (!animationhook) {
644 if (parent->errors_allowed)
645 continue;
646 else
647 break;
648 }
649 next = (CFmovement *)malloc(sizeof(CFmovement));
650 if (!next)
651 continue;
652 next->parent = parent;
653 next->tick = tick;
654 next->next = NULL;
655 if (animationhook->funcinit)
656 next->id = animationhook->funcinit(name, parameters, next);
657 next->func = animationhook->funcrun;
658 if (current)
659 current->next = next;
660 else
661 first = next;
662 current = next;
663 }
664 return first;
665 }
666
667 /**
668 * This function take buffer with a value like "blabla= things" and extracts some things.
669 *
670 * @param buffer where equality is written
671 * @param[out] variable will be positionned to where in buffer the
672 * variable name starts. leading spaces will be converted to \0
673 * @param[out] value same as above but for the value part
674 * @note variable and value become pointers to internals of
675 * buffer. If buffer chages, they will change too and/or become invalid!
676 */
677 static int equality_split(char *buffer, char **variable, char **value) {
678 if (!strcmp(&buffer[strlen(buffer)-strlen("\n")], "\n"))
679 buffer[strlen(buffer)-strlen("\n")] = '\0';
680 *value = strstr(buffer, "=");
681 if (!*value)
682 return 0;
683 **value = '\0';
684 *variable = buffer;
685 (*value)++;
686 while ((strlen(*variable) > 0) && ((*variable)[strlen(*variable)-1] == ' '))
687 (*variable)[strlen(*variable)-1] = '\0';
688 while ((strlen(*value) > 0) && ((*value)[strlen(*value)-1] == ' '))
689 (*value)[strlen(*value)-1] = '\0';
690 while (**value == ' ')
691 (*value)++;
692 if ((**variable == '\0') || (**value == '\0'))
693 return 0;
694 return 1;
695 }
696
697 /**
698 * This function gets a string containing
699 * [Y/y](es)/[N/n](o), 1/0
700 * and set bl according to what's read
701 * if return value is true, strg was set successfully
702 * else, an error occured and bl was not touched
703 *
704 * @param strg string to process.
705 * @param bl value strg meant.
706 * @return 1 if strg was processed, 0 else.
707 */
708 static int get_boolean(const char *strg, int *bl) {
709 /*
710 * We're only parsing the first character, so we
711 * really don't need to call strncmp().
712 * Should future checks need to check multiple characters,
713 * strncmp() would be a good choice. For that reason,
714 * I won't optimize this down to a switch statement.
715 * It makes it clearer how to handle multi-character checks.
716 */
717 if (*strg == 'y')
718 *bl = 1;
719 else if (*strg == 'n')
720 *bl = 0;
721 else if (*strg == 'Y')
722 *bl = 1;
723 else if (*strg == 'N')
724 *bl = 0;
725 else if (*strg == '1')
726 *bl = 1;
727 else if (*strg == '0')
728 *bl = 0;
729 else
730 return 0;
731 return 1;
732 }
733
734 /**
735 * Is specified object currently being animated?
736 * @param ob object to search for.
737 * @return 1 if ob is part of animation, 0 else.
738 */
739 static int is_animated_object(const object *ob) {
740 CFanimation *current;
741
742 for (current = first_animation; current; current = current->nextanimation)
743 if (current->victim == ob) {
744 return 1;
745 }
746 return 0;
747 }
748
749 /**
750 * Create a new animation.
751 * @return new animation pointer inserted in the list of animations.
752 */
753 static CFanimation *create_animation(void) {
754 CFanimation *new;
755 CFanimation *current;
756
757 new = (CFanimation *)malloc(sizeof(CFanimation));
758 if (!new)
759 return NULL;
760 new->name = NULL;
761 new->victim = NULL;
762 new->event = NULL;
763 new->nextmovement = NULL;
764 new->nextanimation = NULL;
765 new->delete_end = 0;
766 for (current = first_animation; (current && current->nextanimation); current = current->nextanimation)
767 ;
768 if (!current)
769 first_animation = new;
770 else
771 current->nextanimation = new;
772 return new;
773 }
774
775 static object *find_by_name(object *origin, const char *name) {
776 int x, y, w, h;
777 mapstruct *map;
778 const char *sname;
779
780 sname = cf_find_string(name);
781 if (!sname)
782 return NULL;
783
784 while (origin && !origin->map)
785 origin = origin->env;
786
787 if (!origin || !origin->map)
788 return NULL;
789
790 map = origin->map;
791
792 w = cf_map_get_width(map);
793 h = cf_map_get_height(map);
794
795 for (x = 0; x < w; x++) {
796 for (y = 0; y < h; y++) {
797 FOR_MAP_PREPARE(map, x, y, ob) {
798 if (/*cf_object_get_sstring_property(ob, CFAPI_OBJECT_PROP_NAME)*/ob->name == sname)
799 return ob;
800 } FOR_MAP_FINISH();
801 }
802 }
803
804 return NULL;
805 }
806
807 /**
808 * Create a new animation object according to file, option and activator (who)
809 *
810 * @param who object that raised the event leading to the plugin.
811 * @param activator object that caused who to get an event.
812 * @param event actual event object linking who and this plugin. Can be removed.
813 * @param file file name to read from, should be accessible from the current path.
814 * @param message if non empty, will be the name of the used animation instead of the one specified in the file.
815 * @return 1 if the animation was created, 0 else.
816 * @todo fix memory leaks in case of errors.
817 */
818 static int start_animation(object *who, object *activator, object *event, const char *file, const char *message) {
819 FILE *fichier;
820 char *name = NULL;
821 int victimtype = 0;
822 object *victim = NULL;
823 int unique = 0;
824 int always_delete = 0;
825 int delete_end = 0;
826 int parallel = 0;
827 int paralyzed = 1;
828 int invisible = 0;
829 int wizard = 0;
830 enum time_enum timetype;
831 int errors_allowed = 0;
832 int verbose = 0;
833 const char *animationitem = NULL;
834 char buffer[HUGE_BUF];
835 char *variable;
836 char *value;
837 int errors_found = 0;
838 CFanimation *current_anim;
839
840 fichier = fopen(file, "r");
841 if (fichier == NULL) {
842 cf_log(llevDebug, "CFAnim: Unable to open %s\n", file);
843 return 0;
844 }
845 while (fgets(buffer, HUGE_BUF, fichier)) {
846 if (buffer[0] == '[')
847 break;
848 if (buffer[0] == '#')
849 continue;
850 if (!strcmp(buffer, "\n"))
851 continue;
852 errors_found = 1;
853 cf_log(llevError, "CFAnim: '%s' has an invalid syntax.\n", buffer);
854 }
855 if (feof(fichier)) {
856 fclose(fichier);
857 return 0;
858 }
859 if (strncmp(buffer, "[Config]", 8)) {
860 cf_log(llevError, "CFAnim: Fatal error in %s: [Config] must be the first group defined.\n", file);
861 fclose(fichier);
862 return 0;
863 }
864 while (fgets(buffer, HUGE_BUF, fichier)) {
865 if (buffer[0] == '[')
866 break;
867 if (buffer[0] == '#')
868 continue;
869 if (!strcmp(buffer, "\n"))
870 continue;
871 if (!equality_split(buffer, &variable, &value))
872 errors_found = 1;
873 else {
874 if (!strcmp(variable, "name")) {
875 if (*value == '"')
876 value++;
877 if (value[strlen(value)-1] == '"')
878 value[strlen(value)-1] = '\0';
879 name = cf_strdup_local(value);
880 } else if (!strcmp(variable, "victimtype")) {
881 if (!strcmp(value, "player"))
882 victimtype = 0;
883 else if (!strcmp(value, "object"))
884 victimtype = 1;
885 else if (!strcmp(value, "any"))
886 victimtype = 2;
887 else if (!strcmp(value, "byname"))
888 victimtype = 3;
889 else
890 errors_found = 1;
891 } else if (!strcmp(variable, "victim")) {
892 cf_log(llevDebug, "CFAnim: Setting victim to %s\n", value);
893 if (!strcmp(value, "who"))
894 victim = who;
895 else if (!strcmp(value, "activator"))
896 victim = activator;
897 else if (!strcmp(value, "who_owner"))
898 if (!who) {
899 errors_found = 1;
900 cf_log(llevError, "CFAnim: Warning: object \"who\" doesn't exist and you're victimized it's owner\n");
901 } else
902 victim = who->env;
903 else if (!strcmp(value, "activator_owner"))
904 if (!activator) {
905 errors_found = 1;
906 cf_log(llevError, "CFAnim: Warning: object \"activator\" doesn't exist and you're victimized it's owner\n");
907 } else
908 victim = activator->env;
909 else if (victimtype == 3) {
910 victim = find_by_name(who, value);
911 } else
912 errors_found = 1;
913 } else if (!strcmp(variable, "unique")) {
914 if (!get_boolean(value, &unique))
915 errors_found = 1;
916 } else if (!strcmp(variable, "always_delete")) {
917 if (!get_boolean(value, &always_delete))
918 errors_found = 1;
919 } else if (!strcmp(variable, "delete_event_end")) {
920 if (!get_boolean(value, &delete_end))
921 errors_found = 1;
922 } else if (!strcmp(variable, "parallel")) {
923 if (!get_boolean(value, &parallel))
924 errors_found = 1;
925 } else if (!strcmp(variable, "paralyzed")) {
926 if (!get_boolean(value, &paralyzed))
927 errors_found = 1;
928 } else if (!strcmp(variable, "invisible")) {
929 if (!get_boolean(value, &invisible))
930 errors_found = 1;
931 } else if (!strcmp(variable, "wizard")) {
932 if (!get_boolean(value, &wizard))
933 errors_found = 1;
934 } else if (!strcmp(variable, "errors_allowed")) {
935 if (!get_boolean(value, &errors_allowed))
936 errors_found = 1;
937 } else if (!strcmp(variable, "verbose")) {
938 if (!get_boolean(value, &verbose))
939 errors_found = 1;
940 } else if (!strcmp(variable, "time_representation")) {
941 if (!strcmp(value, "second"))
942 timetype = time_second;
943 else if (!strcmp(value, "tick"))
944 timetype = time_tick;
945 else
946 errors_found = 1;
947 } else if (!strcmp(variable, "animation")) {
948 animationitem = cf_add_string(value);
949 } else
950 errors_found = 1;
951 }
952 }
953
954 if (message && message[0] != '\0') {
955 cf_free_string(animationitem);
956 animationitem = cf_add_string(message);
957 }
958
959 if (buffer[0] == '\0') {
960 if (animationitem)
961 cf_free_string(animationitem);
962 cf_log(llevError, "CFAnim: Errors occurred during the parsing of %s\n", file);
963 fclose(fichier);
964 return 0;
965 }
966 if (!animationitem) {
967 cf_log(llevError, "CFAnim: no animation specified when using %s\n", file);
968 fclose(fichier);
969 return 0;
970 }
971 if (!victim) {
972 if (animationitem)
973 cf_free_string(animationitem);
974 cf_log(llevError, "CFAnim: Fatal error - victim is NULL");
975 fclose(fichier);
976 return 0;
977 }
978 if (!(current_anim = create_animation())) {
979 if (animationitem)
980 cf_free_string(animationitem);
981 cf_log(llevError, "CFAnim: Fatal error - Not enough memory.\n");
982 fclose(fichier);
983 return 0;
984 }
985 if (always_delete) {
986 /*if (verbose) printf("CFAnim: Freeing event nr. %d for %s.\n", current_event, who->name);*/
987 cf_object_remove(event);
988 event = NULL;
989 }
990 if (((victim->type == PLAYER) && (victimtype == 1))
991 || ((victim->type != PLAYER) && (victimtype == 0))
992 || (errors_found && !errors_allowed)) {
993 if (verbose)
994 cf_log(llevError, "CFAnim: No correct victim found or errors found, aborting.\n");
995 if (animationitem)
996 cf_free_string(animationitem);
997 free(current_anim);
998 fclose(fichier);
999 return 0;
1000 }
1001 if (unique && !always_delete) {
1002 /*if (verbose) printf("CFAnim: Freeing event nr. %d for %s.\n", current_event, who->name);*/
1003 cf_object_remove(event);
1004 event = NULL;
1005 }
1006 current_anim->name = name;
1007 current_anim->victim = victim;
1008 current_anim->event = event;
1009 current_anim->paralyze = paralyzed;
1010 current_anim->invisible = invisible;
1011 current_anim->wizard = wizard;
1012 current_anim->unique = unique;
1013 current_anim->delete_end = delete_end;
1014 current_anim->ghosted = 0;
1015 current_anim->corpse = NULL;
1016 current_anim->time_representation = timetype;
1017 current_anim->verbose = verbose;
1018 current_anim->tick_left = 0;
1019 current_anim->errors_allowed = errors_allowed;
1020
1021 while (buffer[0] == '[') {
1022 while (strncmp(&buffer[1], animationitem, strlen(animationitem))) {
1023 while ((value = fgets(buffer, HUGE_BUF, fichier)) != NULL)
1024 if (buffer[0] == '[')
1025 break;
1026 if (value == NULL) {
1027 cf_log(llevError, "CFAnim: no matching animation %s in file.\n", animationitem);
1028 cf_free_string(animationitem);
1029 fclose(fichier);
1030 return 0;
1031 }
1032 }
1033 current_anim->nextmovement = parse_animation_block(buffer, HUGE_BUF, fichier, current_anim);
1034 if (current_anim->nextmovement)
1035 break;
1036 }
1037 fclose(fichier);
1038 return 1;
1039 }
1040
1041 /**
1042 * Checks if an animation can execute one or more moves, and if so does them.
1043 * @param animation animation to check
1044 * @param milliseconds time elapsed since the last time this function was called.
1045 */
1046 static void animate_one(CFanimation *animation, long int milliseconds) {
1047 CFmovement *current;
1048 int mult = 1;
1049 anim_move_result result;
1050
1051 if (animation->time_representation == time_second) {
1052 animation->tick_left += milliseconds;
1053 mult = 1000;
1054 } else
1055 animation->tick_left++;
1056
1057 if (animation->verbose)
1058 cf_log(llevDebug, "CFAnim: Ticking %s for %s. Tickleft is %ld\n", animation->name, animation->victim->name, animation->tick_left);
1059 if (animation->invisible)
1060 animation->victim->invisible = 10;
1061 if (animation->wizard && animation->victim->type == PLAYER) {
1062 /* setting FLAG_WIZ *on non player leads to issues, as many functions expect contr to not be NULL in this case. */
1063 if (animation->verbose)
1064 cf_log(llevDebug, "CFAnim: Setting wizard flags\n");
1065 cf_object_set_flag(animation->victim, FLAG_WIZPASS, 1);
1066 cf_object_set_flag(animation->victim, FLAG_WIZCAST, 1);
1067 cf_object_set_flag(animation->victim, FLAG_WIZ, 1);
1068 if (animation->verbose)
1069 cf_log(llevDebug, "CFAnim: Setting wizard flags done\n");
1070
1071 }
1072 if (animation->paralyze)
1073 animation->victim->speed_left = -99999;
1074
1075 cf_object_update(animation->victim, UP_OBJ_CHANGE);
1076
1077 if (animation->nextmovement)
1078 while (animation->tick_left > animation->nextmovement->tick*mult) {
1079 animation->tick_left -= animation->nextmovement->tick*mult;
1080 result = animation->nextmovement->func(animation, animation->nextmovement->id, animation->nextmovement->parameters);
1081 if (result == mr_again)
1082 continue;
1083
1084 current = animation->nextmovement;
1085 animation->nextmovement = animation->nextmovement->next;
1086 free(current);
1087 if (!animation->nextmovement)
1088 break;
1089 }
1090 cf_object_set_flag(animation->victim, FLAG_WIZPASS, 0);
1091 cf_object_set_flag(animation->victim, FLAG_WIZCAST, 0);
1092 cf_object_set_flag(animation->victim, FLAG_WIZ, 0);
1093 }
1094
1095 /**
1096 * Return the number of microseconds between two timespec structures.
1097 * This function was copied from common/time.c since linking is too hard.
1098 */
1099 long usec_elapsed(struct timespec first, struct timespec second) {
1100 time_t sec_elapsed = second.tv_sec - first.tv_sec;
1101 long nsec_elapsed = second.tv_nsec - first.tv_nsec;
1102 return (sec_elapsed * 1e6) + (nsec_elapsed / 1e3);
1103 }
1104
1105 /**
1106 * Animates all currently running animations.
1107 */
1108 static void animate(void) {
1109 CFanimation *current;
1110 CFanimation *next;
1111 CFanimation *previous_anim = NULL;
1112 struct timespec now;
1113 static struct timespec yesterday;
1114 static int already_passed = 0;
1115 long int delta_milli;
1116
1117 clock_gettime(CLOCK_MONOTONIC, &now);
1118 if (!already_passed) {
1119 already_passed = 1;
1120 yesterday = now;
1121 return;
1122 }
1123 delta_milli = usec_elapsed(yesterday, now) / 1e3;
1124 /*printf("Working for %ld milli seconds\n", delta_milli);*/
1125 yesterday = now;
1126 for (current = first_animation; current; current = current->nextanimation)
1127 animate_one(current, delta_milli);
1128 current = first_animation;
1129 while (current) {
1130 if (!current->nextmovement) {
1131 if (current->paralyze)
1132 current->victim->speed_left = current->victim->speed;
1133 next = current->nextanimation;
1134 if (first_animation == current)
1135 first_animation = next;
1136 else {
1137 previous_anim->nextanimation = next;
1138 }
1139 if (current->delete_end && current->event != NULL)
1140 cf_object_remove(current->event);
1141 free(current->name);
1142 free(current);
1143 current = next;
1144 } else {
1145 previous_anim = current;
1146 current = current->nextanimation;
1147 }
1148 }
1149 }
1150
1151 /**
1152 * Plugin initialisation function.
1153 * @param iversion server version.
1154 * @param gethooksptr function to get the hooks.
1155 * @return 0
1156 */
1157 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
1158 cf_init_plugin(gethooksptr);
1159 cf_log(llevDebug, "CFAnim 2.0a init\n");
1160
1161 /* Place your initialization code here */
1162 return 0;
1163 }
1164
1165 CF_PLUGIN void *getPluginProperty(int *type, ...) {
1166 va_list args;
1167 const char *propname;
1168 char *buf;
1169 int size;
1170
1171 va_start(args, type);
1172 propname = va_arg(args, const char *);
1173
1174 if (!strcmp(propname, "Identification")) {
1175 buf = va_arg(args, char *);
1176 size = va_arg(args, int);
1177 va_end(args);
1178 snprintf(buf, size, PLUGIN_NAME);
1179 return NULL;
1180 } else if (!strcmp(propname, "FullName")) {
1181 buf = va_arg(args, char *);
1182 size = va_arg(args, int);
1183 va_end(args);
1184 snprintf(buf, size, PLUGIN_VERSION);
1185 return NULL;
1186 }
1187 va_end(args);
1188 return NULL;
1189 }
1190
1191 CF_PLUGIN anim_move_result cfanim_runPluginCommand(object *op, char *params) {
1192 return -1;
1193 }
1194
1195 CF_PLUGIN int postInitPlugin(void) {
1196 cf_log(llevDebug, "CFAnim 2.0a post init\n");
1197 /* Pick the global events you want to monitor from this plugin */
1198 cf_system_register_global_event(EVENT_CLOCK, PLUGIN_NAME, cfanim_globalEventListener);
1199 return 0;
1200 }
1201
1202 CF_PLUGIN int cfanim_globalEventListener(int *type, ...) {
1203 va_list args;
1204 int rv = 0;
1205 int event_code;
1206
1207 va_start(args, type);
1208 event_code = va_arg(args, int);
1209 assert(event_code == EVENT_CLOCK);
1210
1211 animate();
1212
1213 va_end(args);
1214
1215 return rv;
1216 }
1217
1218 CF_PLUGIN int eventListener(int *type, ...) {
1219 int rv = 0;
1220 va_list args;
1221 char *buf, message[MAX_BUF], script[MAX_BUF];
1222 object *who, *activator/*, *third*/, *event;
1223 int query;
1224
1225 va_start(args, type);
1226
1227 who = va_arg(args, object *);
1228 activator = va_arg(args, object *);
1229 /*third =*/ va_arg(args, object *);
1230 buf = va_arg(args, char *);
1231
1232 if (buf != NULL) {
1233 snprintf(message, sizeof(message), "%s", buf);
1234 } else {
1235 message[0] = '\0';
1236 }
1237
1238 query = va_arg(args, int); /* 'fix', ignored */
1239 event = va_arg(args, object *);
1240
1241 if (query == 1 && strcmp(message, "query_object_is_animated") == 0) {
1242 rv = is_animated_object(who);
1243 va_end(args);
1244 return rv;
1245 }
1246
1247 /** @todo build from current map's path, probably */
1248 cf_get_maps_directory(event->slaying, script, sizeof(script));
1249 va_end(args);
1250
1251 /* Put your plugin action(s) here */
1252 if (activator != NULL) {
1253 cf_log(llevDebug, "CFAnim: %s called animator script %s\n", activator->name, script);
1254 } else if (who != NULL) {
1255 activator = who;
1256 cf_log(llevDebug, "CFAnim: %s called animator script %s\n", who->name, script);
1257 }
1258
1259 rv = start_animation(who, activator, event, script, message);
1260
1261 return rv;
1262 }
1263
1264 CF_PLUGIN int closePlugin(void) {
1265 cf_log(llevDebug, "CFAnim 2.0a closing\n");
1266 return 0;
1267 }