Debian Codegraph @

common/player.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 * Player-structure related functions.
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 /**
25 * Clears data in player structure.
26 *
27 * Socket isn't touched. Nor is anything that doesn't need to be freed. So you
28 * may need to do a memset() to clear out values.
29 *
30 * @param pl
31 * player to clear. Pointer is still valid, and can be reused for "play again".
32 */
33 void clear_player(player *pl) {
34 client_spell *info;
35 client_spell *next;
36
37 /* Clear item stack (used by DMs only) */
38 free(pl->stack_items);
39 pl->stack_position = 0;
40
41 info = pl->spell_state;
42 while (info) {
43 next = info->next;
44 free(info);
45 info = next;
46 }
47 if (pl->unarmed_skill)
48 FREE_AND_CLEAR_STR(pl->unarmed_skill);
49 }
50
51 /**
52 * Frees player structure, including pointed object (through object_free_drop_inventory()).
53 *
54 * @param pl
55 * player to clear. Pointer is invalid after this call.
56 */
57 void free_player(player *pl) {
58 if (first_player != pl) {
59 player *prev = first_player;
60
61 while (prev != NULL && prev->next != NULL && prev->next != pl)
62 prev = prev->next;
63 if (prev->next != pl) {
64 LOG(llevError, "Free_player: Can't find previous player.\n");
65 exit(1);
66 }
67 prev->next = pl->next;
68 } else
69 first_player = pl->next;
70
71 if (pl->ob != NULL) {
72 if (!QUERY_FLAG(pl->ob, FLAG_REMOVED))
73 object_remove(pl->ob);
74 object_free_drop_inventory(pl->ob);
75 }
76
77 clear_player(pl);
78
79 free(pl->socket.faces_sent);
80
81 free(pl);
82 }
83
84 /**
85 * Determine if the attacktype represented by the
86 * specified attack-number is enabled for dragon players.
87 * A dragon player (quetzal) can gain resistances for
88 * all enabled attacktypes.
89 *
90 * @param attacknr
91 * attacktype to check
92 * @return
93 * TRUE if player can gain resistances in that, FALSE else.
94 */
95 int atnr_is_dragon_enabled(int attacknr) {
96 if (attacknr == ATNR_MAGIC
97 || attacknr == ATNR_FIRE
98 || attacknr == ATNR_ELECTRICITY
99 || attacknr == ATNR_COLD
100 || attacknr == ATNR_ACID
101 || attacknr == ATNR_POISON)
102 return 1;
103 return 0;
104 }
105
106 /**
107 * Checks if player is a dragon.
108 *
109 * @param op
110 * player to check. Can be NULL.
111 * @return
112 * TRUE if the adressed object 'ob' is a player of the dragon race.
113 */
114 int is_dragon_pl(const object *op) {
115 if (op != NULL
116 && op->type == PLAYER
117 && op->arch != NULL
118 && op->arch->clone.race != NULL
119 && strcmp(op->arch->clone.race, "dragon") == 0)
120 return 1;
121 return 0;
122 }
123
124 /**
125 * Gets the (client-side) spell state for specified spell. Will be created to empty state if not found.
126 *
127 * @note
128 * will fatal() in case of memory allocation failure.
129 * @param pl
130 * player we're handling.
131 * @param spell
132 * spell for which to search data.
133 * @return
134 * state information for that spell.
135 */
136 client_spell *get_client_spell_state(player *pl, object *spell) {
137 client_spell *info = pl->spell_state;
138
139 while (info) {
140 if (info->spell == spell)
141 return info;
142 info = info->next;
143 }
144 /*
145 * Why take the time to malloc() and then memset()?
146 * Just calloc and its good to go!
147 */
148 info = (client_spell *)calloc(1, sizeof(client_spell));
149 if (info == NULL)
150 fatal(OUT_OF_MEMORY);
151 info->next = pl->spell_state;
152 info->spell = spell;
153 pl->spell_state = info;
154 return info;
155 }
156
157 /**
158 * Tests if a player is a wraith.
159 *
160 * @param op
161 * player to check.
162 * @return
163 * true if the adressed object 'ob' is a wraith player, false else.
164 */
165 int is_wraith_pl(object *op) {
166 return op != NULL && op->type == PLAYER && op->arch != NULL && object_find_by_name(op, "wraith feed") != NULL;
167 }
168
169 /**
170 * Checks if player is a wraith without the 'wraith feed' skill.
171 *
172 * @param op
173 * player to check.
174 * @return
175 * true if the adressed object 'ob' is an old wraith player, false else.
176 */
177 int is_old_wraith_pl(object *op) {
178 return op != NULL && op->type == PLAYER && op->arch != NULL && object_find_by_name(op, "Wraith_Force") != NULL && !is_wraith_pl(op);
179 }
180
181 /**
182 * Updates the title of a dragon player to reflect the current level, attack
183 * type, and resistances.
184 *
185 * @param pl
186 * the player to update
187 * @param level
188 * the dragon's current level
189 * @param attack
190 * the dragon's current attack focus
191 * @param skin_resist
192 * the dragon's skin resistance for attack
193 */
194 void player_set_dragon_title(struct pl *pl, int level, const char *attack, int skin_resist) {
195 if (level == 0)
196 snprintf(pl->title, sizeof(pl->title), "%s hatchling", attack);
197 else if (level == 1)
198 snprintf(pl->title, sizeof(pl->title), "%s wyrm", attack);
199 else if (level == 2)
200 snprintf(pl->title, sizeof(pl->title), "%s wyvern", attack);
201 else if (level == 3)
202 snprintf(pl->title, sizeof(pl->title), "%s dragon", attack);
203 /* special titles for extra high resistance! */
204 else if (skin_resist > 80)
205 snprintf(pl->title, sizeof(pl->title), "legendary %s dragon", attack);
206 else if (skin_resist > 50)
207 snprintf(pl->title, sizeof(pl->title), "ancient %s dragon", attack);
208 else
209 snprintf(pl->title, sizeof(pl->title), "big %s dragon", attack);
210 pl->own_title[0] = '\0';
211 }
212
213 /**
214 * Returns the player's title. The returned title is never empty and includes a
215 * "the" prefix if necessary.
216 *
217 * @param pl
218 * the player to return the title of
219 * @param buf
220 * returns the title
221 * @param bufsize
222 * the size of buf in byte
223 */
224 void player_get_title(const struct pl *pl, char *buf, size_t bufsize) {
225 if (pl->own_title[0] == '\0')
226 snprintf(buf, bufsize, "the %s", pl->title);
227 else
228 strlcpy(buf, pl->own_title, bufsize);
229 }
230
231 /**
232 * Returns whether the player has a custom title.
233 *
234 * @param pl
235 * the player to check
236 * @return
237 * whether the player has a custom title
238 */
239 int player_has_own_title(const struct pl *pl) {
240 return pl->own_title[0] != '\0';
241 }
242
243 /**
244 * Returns the player's own title. The returned value must not be modified and
245 * points into the player structure.
246 *
247 * @param pl
248 * the player
249 * @return
250 * the own title
251 */
252 const char *player_get_own_title(const struct pl *pl) {
253 return pl->own_title;
254 }
255
256 /**
257 * Sets the custom title.
258 *
259 * @param pl
260 * the player to modify
261 * @param title
262 * the new title to set; empty string to unset
263 */
264 void player_set_own_title(struct pl *pl, const char *title) {
265 strlcpy(pl->own_title, title, sizeof(pl->own_title));
266 replace_unprintable_chars(pl->own_title);
267 }
268
269 /**
270 * This function goes through the player inventory and sets
271 * up the last_skills[] array in the player object.
272 * The last_skills[] is used to more quickly lookup skills -
273 * mostly used for sending exp.
274 * This function should be called anytime the player gains a skill.
275 *
276 * @param op
277 * player to link skills for. Must be a player.
278 */
279 void link_player_skills(object *op) {
280 int skill;
281 FOR_INV_PREPARE(op, tmp) {
282 if (tmp->type == SKILL) {
283 for (skill = 0; skill < MAX_SKILLS; skill++) {
284 if (op->contr->last_skill_ob[skill] == NULL) {
285 /* Didn't find the skill, so add it */
286 op->contr->last_skill_ob[skill] = tmp;
287 op->contr->last_skill_exp[skill] = -1;
288 break;
289 }
290 if (op->contr->last_skill_ob[skill] == tmp) {
291 /* Skill already linked, nothing to do */
292 break;
293 }
294 }
295 }
296 } FOR_INV_FINISH();
297 }