File: player.c

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (297 lines) | stat: -rw-r--r-- 8,035 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Crossfire -- cooperative multi-player graphical RPG and adventure game
 *
 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
 * Copyright (c) 1992 Frank Tore Johansen
 *
 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
 * welcome to redistribute it under certain conditions. For details, please
 * see COPYING and LICENSE.
 *
 * The authors can be reached via e-mail at <crossfire@metalforge.org>.
 */

/**
 * @file
 * Player-structure related functions.
 */

#include "global.h"

#include <stdlib.h>
#include <string.h>

/**
 * Clears data in player structure.
 *
 * Socket isn't touched. Nor is anything that doesn't need to be freed. So you
 * may need to do a memset() to clear out values.
 *
 * @param pl
 * player to clear. Pointer is still valid, and can be reused for "play again".
 */
void clear_player(player *pl) {
    client_spell *info;
    client_spell *next;

    /* Clear item stack (used by DMs only) */
    free(pl->stack_items);
    pl->stack_position = 0;

    info = pl->spell_state;
    while (info) {
        next = info->next;
        free(info);
        info = next;
    }
    if (pl->unarmed_skill)
        FREE_AND_CLEAR_STR(pl->unarmed_skill);
}

/**
 * Frees player structure, including pointed object (through object_free_drop_inventory()).
 *
 * @param pl
 * player to clear. Pointer is invalid after this call.
 */
void free_player(player *pl) {
    if (first_player != pl) {
        player *prev = first_player;

        while (prev != NULL && prev->next != NULL && prev->next != pl)
            prev = prev->next;
        if (prev->next != pl) {
            LOG(llevError, "Free_player: Can't find previous player.\n");
            exit(1);
        }
        prev->next = pl->next;
    } else
        first_player = pl->next;

    if (pl->ob != NULL) {
        if (!QUERY_FLAG(pl->ob, FLAG_REMOVED))
            object_remove(pl->ob);
        object_free_drop_inventory(pl->ob);
    }

    clear_player(pl);

    free(pl->socket.faces_sent);

    free(pl);
}

/**
 * Determine if the attacktype represented by the
 * specified attack-number is enabled for dragon players.
 * A dragon player (quetzal) can gain resistances for
 * all enabled attacktypes.
 *
 * @param attacknr
 * attacktype to check
 * @return
 * TRUE if player can gain resistances in that, FALSE else.
 */
int atnr_is_dragon_enabled(int attacknr) {
    if (attacknr == ATNR_MAGIC
    || attacknr == ATNR_FIRE
    || attacknr == ATNR_ELECTRICITY
    || attacknr == ATNR_COLD
    || attacknr == ATNR_ACID
    || attacknr == ATNR_POISON)
        return 1;
    return 0;
}

/**
 * Checks if player is a dragon.
 *
 * @param op
 * player to check. Can be NULL.
 * @return
 * TRUE if the adressed object 'ob' is a player of the dragon race.
 */
int is_dragon_pl(const object *op) {
    if (op != NULL
    && op->type == PLAYER
    && op->arch != NULL
    && op->arch->clone.race != NULL
    && strcmp(op->arch->clone.race, "dragon") == 0)
        return 1;
    return 0;
}

/**
 * Gets the (client-side) spell state for specified spell. Will be created to empty state if not found.
 *
 * @note
 * will fatal() in case of memory allocation failure.
 * @param pl
 * player we're handling.
 * @param spell
 * spell for which to search data.
 * @return
 * state information for that spell.
 */
client_spell *get_client_spell_state(player *pl, object *spell) {
    client_spell *info = pl->spell_state;

    while (info) {
        if (info->spell == spell)
            return info;
        info = info->next;
    }
    /*
     * Why take the time to malloc() and then memset()?
     * Just calloc and its good to go!
     */
    info = (client_spell *)calloc(1, sizeof(client_spell));
    if (info == NULL)
        fatal(OUT_OF_MEMORY);
    info->next = pl->spell_state;
    info->spell = spell;
    pl->spell_state = info;
    return info;
}

/**
 * Tests if a player is a wraith.
 *
 * @param op
 * player to check.
 * @return
 * true if the adressed object 'ob' is a wraith player, false else.
 */
int is_wraith_pl(object *op) {
    return op != NULL && op->type == PLAYER && op->arch != NULL && object_find_by_name(op, "wraith feed") != NULL;
}

/**
 * Checks if player is a wraith without the 'wraith feed' skill.
 *
 * @param op
 * player to check.
 * @return
 * true if the adressed object 'ob' is an old wraith player, false else.
 */
int is_old_wraith_pl(object *op) {
    return op != NULL && op->type == PLAYER && op->arch != NULL && object_find_by_name(op, "Wraith_Force") != NULL && !is_wraith_pl(op);
}

/**
 * Updates the title of a dragon player to reflect the current level, attack
 * type, and resistances.
 *
 * @param pl
 * the player to update
 * @param level
 * the dragon's current level
 * @param attack
 * the dragon's current attack focus
 * @param skin_resist
 * the dragon's skin resistance for attack
 */
void player_set_dragon_title(struct pl *pl, int level, const char *attack, int skin_resist) {
    if (level == 0)
        snprintf(pl->title, sizeof(pl->title), "%s hatchling", attack);
    else if (level == 1)
        snprintf(pl->title, sizeof(pl->title), "%s wyrm", attack);
    else if (level == 2)
        snprintf(pl->title, sizeof(pl->title), "%s wyvern", attack);
    else if (level == 3)
        snprintf(pl->title, sizeof(pl->title), "%s dragon", attack);
    /* special titles for extra high resistance! */
    else if (skin_resist > 80)
        snprintf(pl->title, sizeof(pl->title), "legendary %s dragon", attack);
    else if (skin_resist > 50)
        snprintf(pl->title, sizeof(pl->title), "ancient %s dragon", attack);
    else
        snprintf(pl->title, sizeof(pl->title), "big %s dragon", attack);
    pl->own_title[0] = '\0';
}

/**
 * Returns the player's title. The returned title is never empty and includes a
 * "the" prefix if necessary.
 *
 * @param pl
 * the player to return the title of
 * @param buf
 * returns the title
 * @param bufsize
 * the size of buf in byte
 */
void player_get_title(const struct pl *pl, char *buf, size_t bufsize) {
    if (pl->own_title[0] == '\0')
        snprintf(buf, bufsize, "the %s", pl->title);
    else
        strlcpy(buf, pl->own_title, bufsize);
}

/**
 * Returns whether the player has a custom title.
 *
 * @param pl
 * the player to check
 * @return
 * whether the player has a custom title
 */
int player_has_own_title(const struct pl *pl) {
    return pl->own_title[0] != '\0';
}

/**
 * Returns the player's own title. The returned value must not be modified and
 * points into the player structure.
 *
 * @param pl
 * the player
 * @return
 * the own title
 */
const char *player_get_own_title(const struct pl *pl) {
    return pl->own_title;
}

/**
 * Sets the custom title.
 *
 * @param pl
 * the player to modify
 * @param title
 * the new title to set; empty string to unset
 */
void player_set_own_title(struct pl *pl, const char *title) {
    strlcpy(pl->own_title, title, sizeof(pl->own_title));
    replace_unprintable_chars(pl->own_title);
}

/**
 * This function goes through the player inventory and sets
 * up the last_skills[] array in the player object.
 * The last_skills[] is used to more quickly lookup skills -
 * mostly used for sending exp.
 * This function should be called anytime the player gains a skill.
 *
 * @param op
 * player to link skills for. Must be a player.
 */
void link_player_skills(object *op) {
    int skill;
    FOR_INV_PREPARE(op, tmp) {
        if (tmp->type == SKILL) {
            for (skill = 0; skill < MAX_SKILLS; skill++) {
                if (op->contr->last_skill_ob[skill] == NULL) {
                    /* Didn't find the skill, so add it */
                    op->contr->last_skill_ob[skill] = tmp;
                    op->contr->last_skill_exp[skill] = -1;
                    break;
                }
                if (op->contr->last_skill_ob[skill] == tmp) {
                    /* Skill already linked, nothing to do */
                    break;
                }
            }
        }
    } FOR_INV_FINISH();
}