Debian Codegraph @

types/creator/creator.c

raw symbols SCIP
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2008 Crossfire Development Team
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 The authors can be reached via e-mail at [email protected]
21 */
22
23 /**
24 * @file
25 * The implementation of @ref page_type_42 "creator" objects.
26 */
27
28 #include <global.h>
29 #include <ob_methods.h>
30 #include <ob_types.h>
31 #include <sounds.h>
32 #include <sproto.h>
33
34 static method_ret creator_type_process(ob_methods *context, object *op);
35 static method_ret creator_type_trigger(ob_methods *context, object *op, object *cause, int state);
36
37 /**
38 * Initializer for the @ref page_type_42 "creator" object type.
39 */
40 void init_type_creator(void) {
41 register_process(CREATOR, creator_type_process);
42 register_trigger(CREATOR, creator_type_trigger);
43 }
44
45 /**
46 * Move function for @ref page_type_42 "creator" objects.
47 *
48 * It has the creator object create it's other_arch right on top of it.
49 * connected: what will trigger it
50 * - hp: how many times it may create before stopping
51 * - lifesave: if set, it'll never disappear but will go on creating
52 * everytime it's triggered
53 * - other_arch: the object to create
54 *
55 * Note this can create large objects, however, in that case, it
56 * has to make sure that there is in fact space for the object.
57 * It should really do this for small objects also, but there is
58 * more concern with large objects, most notably a part being placed
59 * outside of the map which would cause the server to crash
60 *
61 * @param creator
62 * creator to move.
63 */
64 static void move_creator(object *creator) {
65 object *new_ob;
66
67 if (!QUERY_FLAG(creator, FLAG_LIFESAVE) && --creator->stats.hp < 0) {
68 creator->stats.hp = -1;
69 return;
70 }
71
72 if (creator->inv != NULL) {
73 int i;
74 object *ob_to_copy;
75
76 /* select random object from inventory to copy */
77 ob_to_copy = creator->inv;
78 i = 1;
79 FOR_BELOW_PREPARE(creator->inv, ob) {
80 if (rndm(0, i) == 0) {
81 ob_to_copy = ob;
82 }
83 i++;
84 } FOR_BELOW_FINISH();
85 new_ob = object_create_clone(ob_to_copy);
86 CLEAR_FLAG(new_ob, FLAG_IS_A_TEMPLATE);
87 object_unset_flag_inv(new_ob, FLAG_IS_A_TEMPLATE);
88 } else {
89 if (creator->other_arch == NULL) {
90 LOG(llevError, "move_creator: Creator doesn't have other arch set: %s (%s, %d, %d)\n", creator->name ? creator->name : "(null)", creator->map->path, creator->x, creator->y);
91 return;
92 }
93
94 new_ob = object_create_arch(creator->other_arch);
95 fix_generated_item(new_ob, creator, 0, 0, GT_MINIMAL);
96 }
97
98 /* Make sure this multipart object fits */
99 if (new_ob->arch->more && ob_blocked(new_ob, creator->map, creator->x, creator->y)) {
100 object_free_drop_inventory(new_ob);
101 return;
102 }
103
104 if (creator->level != 0)
105 new_ob->level = creator->level;
106
107 object_insert_in_map_at(new_ob, creator->map, creator, 0, creator->x, creator->y);
108 if (QUERY_FLAG(new_ob, FLAG_FREED))
109 return;
110
111 if (creator->slaying) {
112 FREE_AND_COPY(new_ob->name, creator->slaying);
113 if (new_ob->title) {
114 FREE_AND_CLEAR_STR(new_ob->title);
115 }
116 }
117 }
118
119 /**
120 * Processes a @ref page_type_42 "creator".
121 * @param context The method context
122 * @param op The teleporter to process
123 * @retval METHOD_OK
124 */
125 static method_ret creator_type_process(ob_methods *context, object *op) {
126 move_creator(op);
127 return METHOD_OK;
128 }
129
130 /**
131 * A @ref page_type_42 "creator" is triggered.
132 * @param context Ignored.
133 * @param op The object being triggered
134 * @param cause Ignored.
135 * @param state Ignored.
136 * @retval METHOD_OK
137 */
138 static method_ret creator_type_trigger(ob_methods *context, object *op, object *cause, int state) {
139 move_creator(op);
140 return METHOD_OK;
141 }