Debian Codegraph @

types/detector/detector.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * The implementation of @ref page_type_51 "detector" objects.
17 */
18
19 #include "global.h"
20
21 #include <string.h>
22
23 #include "ob_methods.h"
24 #include "ob_types.h"
25 #include "sounds.h"
26 #include "sproto.h"
27
28 static method_ret detector_type_process(ob_methods *context, object *op);
29
30 /**
31 * Initializer for the @ref page_type_51 "detector" object type.
32 */
33 void init_type_detector(void) {
34 register_process(DETECTOR, detector_type_process);
35 }
36
37 /**
38 * Move a @ref page_type_51 "detector".
39 *
40 * - slaying: name of the thing the detector is to look for
41 * - speed: frequency of 'glances'
42 * - connected: connected value of detector
43 * - sp: 1 if detection sets buttons
44 * -1 if detection unsets buttons
45 *
46 * @param op
47 * detector to move.
48 */
49 static void move_detector(object *op) {
50 object *tmp;
51 int last = op->value;
52 int detected;
53 detected = 0;
54
55 if (!op->slaying) {
56 if (op->map)
57 LOG(llevError, "Detector with no slaying set at %s (%d,%d)\n", op->map->path, op->x, op->y);
58 else if (op->env)
59 LOG(llevError, "Detector with no slaying in %s\n", op->env->name);
60 else
61 LOG(llevError, "Detector with no slaying nowhere?\n");
62 op->speed = 0;
63 object_update_speed(op);
64 return;
65 }
66
67 for (tmp = GET_MAP_OB(op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) {
68 if (op->stats.hp) {
69 if (object_find_by_name(tmp, op->slaying) != NULL
70 || object_find_by_type_and_slaying(tmp, FORCE, op->slaying)) {
71 detected = 1;
72 break;
73 }
74 }
75 if (op->slaying == tmp->name) {
76 detected = 1;
77 break;
78 }
79 if (tmp->type == PLAYER && !strcmp(op->slaying, "player")) {
80 detected = 1;
81 break;
82 }
83 if (tmp->type == SPECIAL_KEY && tmp->slaying == op->slaying) {
84 detected = 1;
85 break;
86 }
87 }
88
89 /* the detector sets the button if detection is found */
90 if (op->stats.sp == 1) {
91 if (detected && last == 0) {
92 op->value = 1;
93 push_button(op);
94 }
95 if (!detected && last == 1) {
96 op->value = 0;
97 push_button(op);
98 }
99 } else { /* in this case, we unset buttons */
100 if (detected && last == 1) {
101 op->value = 0;
102 push_button(op);
103 }
104 if (!detected && last == 0) {
105 op->value = 1;
106 push_button(op);
107 }
108 }
109 }
110
111 /**
112 * Processes a @ref page_type_51 "detector".
113 * @param context The method context, ignored
114 * @param op The detector to process
115 * @retval METHOD_OK
116 */
117 static method_ret detector_type_process(ob_methods *context, object *op) {
118 move_detector(op);
119 return METHOD_OK;
120 }