Crossfire Server, Trunk  1.75.0
player.cpp
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
19 #include "global.h"
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
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)
49 
50  for (uint8_t db = 0; db < pl->delayed_buffers_allocated; db++) {
51  free(pl->delayed_buffers[db]);
52  }
55  pl->delayed_buffers_used = 0;
56  if (pl->last_exit != NULL) {
58  pl->last_exit = NULL;
59  }
60 }
61 
68 void free_player(player *pl) {
69  if (first_player != pl) {
70  player *prev = first_player;
71 
72  while (prev != NULL && prev->next != NULL && prev->next != pl)
73  prev = prev->next;
74  if (!prev || prev->next != pl) {
75  LOG(llevError, "Free_player: Can't find previous player.\n");
76  exit(1);
77  }
78  prev->next = pl->next;
79  } else
80  first_player = pl->next;
81 
82  if (pl->ob != NULL) {
83  if (!QUERY_FLAG(pl->ob, FLAG_REMOVED))
84  object_remove(pl->ob);
86  }
87 
88  clear_player(pl);
89  free(pl);
90 }
91 
103 int atnr_is_dragon_enabled(int attacknr) {
104  if (attacknr == ATNR_MAGIC
105  || attacknr == ATNR_FIRE
106  || attacknr == ATNR_ELECTRICITY
107  || attacknr == ATNR_COLD
108  || attacknr == ATNR_ACID
109  || attacknr == ATNR_POISON)
110  return 1;
111  return 0;
112 }
113 
122 int is_dragon_pl(const object *op) {
123  if (op != NULL
124  && op->type == PLAYER
125  && op->arch != NULL
126  && op->arch->clone.race != NULL
127  && strcmp(op->arch->clone.race, "dragon") == 0)
128  return 1;
129  return 0;
130 }
131 
145  client_spell *info = pl->spell_state;
146 
147  while (info) {
148  if (info->spell == spell)
149  return info;
150  info = info->next;
151  }
152  /*
153  * Why take the time to malloc() and then memset()?
154  * Just calloc and its good to go!
155  */
156  info = (client_spell *)calloc(1, sizeof(client_spell));
157  if (info == NULL)
159  info->next = pl->spell_state;
160  info->spell = spell;
161  pl->spell_state = info;
162  return info;
163 }
164 
173 int is_wraith_pl(object *op) {
174  return op != NULL && op->type == PLAYER && op->arch != NULL && op->contr && op->contr->is_wraith;
175 }
176 
185 int is_old_wraith_pl(object *op) {
186  return op != NULL && op->type == PLAYER && op->arch != NULL && op->contr && op->contr->is_old_wraith && !is_wraith_pl(op);
187 }
188 
202 void player_set_dragon_title(player *pl, int level, const char *attack, int skin_resist) {
203  if (level == 0)
204  snprintf(pl->title, sizeof(pl->title), "%s hatchling", attack);
205  else if (level == 1)
206  snprintf(pl->title, sizeof(pl->title), "%s wyrm", attack);
207  else if (level == 2)
208  snprintf(pl->title, sizeof(pl->title), "%s wyvern", attack);
209  else if (level == 3)
210  snprintf(pl->title, sizeof(pl->title), "%s dragon", attack);
211  /* special titles for extra high resistance! */
212  else if (skin_resist > 80)
213  snprintf(pl->title, sizeof(pl->title), "legendary %s dragon", attack);
214  else if (skin_resist > 50)
215  snprintf(pl->title, sizeof(pl->title), "ancient %s dragon", attack);
216  else
217  snprintf(pl->title, sizeof(pl->title), "big %s dragon", attack);
218  pl->own_title[0] = '\0';
219 }
220 
232 void player_get_title(const player *pl, char *buf, size_t bufsize) {
233  if (pl->own_title[0] == '\0')
234  snprintf(buf, bufsize, "the %s", pl->title);
235  else
236  strlcpy(buf, pl->own_title, bufsize);
237 }
238 
247 int player_has_own_title(const struct player *pl) {
248  return pl->own_title[0] != '\0';
249 }
250 
260 const char *player_get_own_title(const struct player *pl) {
261  return pl->own_title;
262 }
263 
272 void player_set_own_title(struct player *pl, const char *title) {
273  strlcpy(pl->own_title, title, sizeof(pl->own_title));
275 }
276 
287 void link_player_skills(object *op) {
288  int skill;
289  FOR_INV_PREPARE(op, tmp) {
290  if (tmp->type == SKILL) {
291  for (skill = 0; skill < MAX_SKILLS; skill++) {
292  if (op->contr->last_skill_ob[skill] == NULL) {
293  /* Didn't find the skill, so add it */
294  op->contr->last_skill_ob[skill] = tmp;
295  op->contr->last_skill_exp[skill] = -1;
296  break;
297  }
298  if (op->contr->last_skill_ob[skill] == tmp) {
299  /* Skill already linked, nothing to do */
300  break;
301  }
302  }
303  }
304  } FOR_INV_FINISH();
305 }
306 
307 void commit_crime(object *op, const char *description) {
308  object *force = add_force(op, "criminal", 25); // 5 minutes
309  object_set_msg(force, description);
310 }
311 
312 bool is_criminal(object *op) {
313  return find_force(op, "criminal");
314 }
player::stack_items
tag_t * stack_items
Item stack for patch/dump/...
Definition: player.h:217
PLAYER
@ PLAYER
Definition: object.h:112
player::next
player * next
Pointer to next player, NULL if this is last.
Definition: player.h:106
global.h
FREE_OBJ_NO_DESTROY_CALLBACK
#define FREE_OBJ_NO_DESTROY_CALLBACK
Do not run the destroy callback.
Definition: object.h:545
first_player
player * first_player
First player.
Definition: init.cpp:106
player::unarmed_skill
const char * unarmed_skill
Prefered skill to use in unarmed combat.
Definition: player.h:221
llevError
@ llevError
Error, serious thing.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
client_spell
This stores, for a spell a player knows, the last sp/gr/dam information sent to client.
Definition: player.h:87
player
One player.
Definition: player.h:105
ATNR_ACID
#define ATNR_ACID
Definition: attack.h:55
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
player::is_wraith
bool is_wraith
Whether this player is a wraith or not, initialized at load time.
Definition: player.h:227
player::spell_state
client_spell * spell_state
Spell information sent to client.
Definition: player.h:215
object::arch
struct archetype * arch
Pointer to archetype.
Definition: object.h:424
client_spell::next
struct client_spell * next
Next spell information.
Definition: player.h:92
player::ob
object * ob
The object representing the player.
Definition: player.h:177
player_set_own_title
void player_set_own_title(struct player *pl, const char *title)
Sets the custom title.
Definition: player.cpp:272
player::delayed_buffers
SockList ** delayed_buffers
Buffers which will be sent after the player's tick completes.
Definition: player.h:226
SKILL
@ SKILL
Also see SKILL_TOOL (74) below.
Definition: object.h:148
player_has_own_title
int player_has_own_title(const struct player *pl)
Returns whether the player has a custom title.
Definition: player.cpp:247
buf
StringBuffer * buf
Definition: readable.cpp:1565
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects,...
Definition: object.cpp:1560
object::contr
struct player * contr
Pointer to the player which control this object.
Definition: object.h:284
is_criminal
bool is_criminal(object *op)
Definition: player.cpp:312
archetype::clone
object clone
An object from which to do object_copy()
Definition: object.h:487
player::delayed_buffers_used
uint8_t delayed_buffers_used
Used items in delayed_buffers_used.
Definition: player.h:225
commit_crime
void commit_crime(object *op, const char *description)
Definition: player.cpp:307
player::last_skill_exp
int64_t last_skill_exp[MAX_SKILLS]
Last exp sent to client.
Definition: player.h:154
client_spell::spell
object * spell
Spell object this structure is about.
Definition: player.h:88
description
spell prayer lvl t sp speed range duration short description
Definition: spell-summary.txt:2
object::type
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
spell
with a maximum of six This is not so if you are wearing plate you receive no benefit Armour is additive with all the supplementry forms of which means that it lasts until the next semi permanent spell effect is cast upon the character spell
Definition: tome-of-magic.txt:44
object_free
void object_free(object *ob, int flags)
Frees everything allocated by an object, removes it from the list of used objects,...
Definition: object.cpp:1592
title
Information on one title.
Definition: readable.cpp:108
FOR_INV_FINISH
#define FOR_INV_FINISH()
Finishes FOR_INV_PREPARE().
Definition: define.h:671
ATNR_POISON
#define ATNR_POISON
Definition: attack.h:59
MAX_SKILLS
#define MAX_SKILLS
This is the maximum number of skills the game may handle.
Definition: skills.h:70
object::race
sstring race
Human, goblin, dragon, etc.
Definition: object.h:326
replace_unprintable_chars
void replace_unprintable_chars(char *buf)
Replaces any unprintable character in the given buffer with a space.
Definition: utils.cpp:447
fatal
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.cpp:590
player::last_skill_ob
object * last_skill_ob[MAX_SKILLS]
Exp objects sent to client.
Definition: player.h:153
strlcpy
size_t strlcpy(char *dst, const char *src, size_t size)
Portable implementation of strlcpy(3).
Definition: porting.cpp:222
is_wraith_pl
int is_wraith_pl(object *op)
Tests if a player is a wraith.
Definition: player.cpp:173
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Release the shared string, and set it to NULL.
Definition: global.h:200
ATNR_FIRE
#define ATNR_FIRE
Definition: attack.h:51
FLAG_REMOVED
#define FLAG_REMOVED
Object is not in any map or invenory.
Definition: define.h:232
get_client_spell_state
client_spell * get_client_spell_state(player *pl, object *spell)
Gets the (client-side) spell state for specified spell.
Definition: player.cpp:144
FREE_AND_CLEAR
#define FREE_AND_CLEAR(xyz)
Free the pointer and then set it to NULL.
Definition: global.h:195
is_dragon_pl
int is_dragon_pl(const object *op)
Checks if player is a dragon.
Definition: player.cpp:122
player::is_old_wraith
bool is_old_wraith
Whether this player is a "old" wraith, initialized at load time and updated when eating.
Definition: player.h:228
player::own_title
char own_title[MAX_NAME]
Title the player has chosen for themself.
Definition: player.h:182
object_set_msg
void object_set_msg(object *op, const char *msg)
Set the message field of an object.
Definition: object.cpp:4811
ATNR_MAGIC
#define ATNR_MAGIC
Definition: attack.h:50
add_force
object * add_force(object *op, const char *name, int duration)
Add or return an existing force inside 'op' with the given 'name' and 'duration' in units of 100 tick...
Definition: object.cpp:5430
atnr_is_dragon_enabled
int atnr_is_dragon_enabled(int attacknr)
Determine if the attacktype represented by the specified attack-number is enabled for dragon players.
Definition: player.cpp:103
level
int level
Definition: readable.cpp:1563
find_force
object * find_force(object *op, const char *name)
Find a force with the given 'name' in the slaying field.
Definition: object.cpp:5425
player_set_dragon_title
void player_set_dragon_title(player *pl, int level, const char *attack, int skin_resist)
Updates the title of a dragon player to reflect the current level, attack type, and resistances.
Definition: player.cpp:202
skill
skill
Definition: arch-handbook.txt:585
player::delayed_buffers_allocated
uint8_t delayed_buffers_allocated
Number of items in delayed_buffers_used.
Definition: player.h:224
object_remove
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to.
Definition: object.cpp:1833
ATNR_COLD
#define ATNR_COLD
Definition: attack.h:53
player::stack_position
int stack_position
Current stack position, 0 for no item.
Definition: player.h:219
is_old_wraith_pl
int is_old_wraith_pl(object *op)
Checks if player is a wraith without the 'wraith feed' skill.
Definition: player.cpp:185
player::title
char title[BIG_NAME]
Default title, like fighter, wizard, etc.
Definition: player.h:184
player_get_own_title
const char * player_get_own_title(const struct player *pl)
Returns the player's own title.
Definition: player.cpp:260
player::last_exit
object * last_exit
Last exit used by player or NULL.
Definition: player.h:208
ATNR_ELECTRICITY
#define ATNR_ELECTRICITY
Definition: attack.h:52
clear_player
void clear_player(player *pl)
Clears data in player structure.
Definition: player.cpp:33
link_player_skills
void link_player_skills(object *op)
This function goes through the player inventory and sets up the last_skills[] array in the player obj...
Definition: player.cpp:287
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
FOR_INV_PREPARE
#define FOR_INV_PREPARE(op_, it_)
Constructs a loop iterating over the inventory of an object.
Definition: define.h:664
free_player
void free_player(player *pl)
Frees player structure, including pointed object (through object_free_drop_inventory()).
Definition: player.cpp:68
player_get_title
void player_get_title(const player *pl, char *buf, size_t bufsize)
Returns the player's title.
Definition: player.cpp:232