Crossfire Client, Trunk
init.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 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 "client.h"
20 #include "mapdata.h"
21 #include "metaserver.h"
22 #include "p_cmd.h"
23 
24 /* Makes the load/save code trivial - basically, the
25  * entries here match the same numbers as the CONFIG_ values defined
26  * in common/client.h - this means the load and save just does
27  * something like a fprintf(outifle, "%s: %d", config_names[i],
28  * want_config[i]);
29  */
30 const char *const config_names[CONFIG_NUMS] = {
31  NULL, "download_all_images", "echo_bindings",
32  "fasttcpsend", "command_window", "cacheimages", "fog_of_war", "iconscale",
33  "mapscale", "popups", "displaymode", "showicon", "tooltips", "sound", "splitinfo",
34  "split", "show_grid", "lighting", "trim_info_window",
35  "map_width", "map_height", "foodbeep", "darkness", "port",
36  "grad_color_bars", "resistances", "smoothing", "nosplash",
37  "auto_apply_container", "mapscroll", "sign_popups", "message_timestamping", "auto_afk",
38  "inv_menu"
39 };
40 
42 
43 #define FREE_AND_CLEAR(xyz) { free(xyz); xyz=NULL; }
44 
45 void VersionCmd(char *data, int len) {
46  char *cp;
47 
48  csocket.cs_version = atoi(data);
49  /* set sc_version in case it is an old server supplying only one version */
51  if (csocket.cs_version != VERSION_CS) {
52  LOG(LOG_WARNING, "common::VersionCmd", "Differing C->S version numbers (%d,%d)",
54  /* exit(1);*/
55  }
56  cp = strchr(data, ' ');
57  if (!cp) {
58  return;
59  }
60  csocket.sc_version = atoi(cp);
61  if (csocket.sc_version != VERSION_SC) {
62  LOG(LOG_WARNING, "common::VersionCmd", "Differing S->C version numbers (%d,%d)",
64  }
65  cp = strchr(cp + 1, ' ');
66  if (cp) {
67  LOG(LOG_DEBUG, "common::VersionCmd", "Playing on server type %s", cp);
68  }
69 }
70 
72  cs_print_string(csock.fd, "version %d %d %s",
74 }
75 
76 void SendAddMe(ClientSocket csock) {
77  cs_print_string(csock.fd, "addme");
78 }
79 
80 static void init_paths() {
81  // Set and create configuration and cache directories.
82  GString *app_config_dir = g_string_new(g_get_user_config_dir());
83  g_string_append(app_config_dir, "/crossfire");
84  config_dir = g_string_free(app_config_dir, FALSE);
85  g_mkdir_with_parents(config_dir, 0755);
86 
87  GString *app_cache_dir = g_string_new(g_get_user_cache_dir());
88  g_string_append(app_cache_dir, "/crossfire");
89  cache_dir = g_string_free(app_cache_dir, FALSE);
90  g_mkdir_with_parents(cache_dir, 0755);
91 }
92 
97 static void reset_vars_common() {
98  cpl.count_left = 0;
99  cpl.container = NULL;
100  memset(&cpl.stats, 0, sizeof(Stats));
101  cpl.stats.maxsp = 1; /* avoid div by 0 errors */
102  cpl.stats.maxhp = 1; /* ditto */
103  cpl.stats.maxgrace = 1; /* ditto */
104 
105  /* ditto - displayed weapon speed is weapon speed/speed */
106  cpl.stats.speed = 1;
107  cpl.input_text[0] = '\0';
108  cpl.title[0] = '\0';
109  cpl.range[0] = '\0';
110  cpl.last_command[0] = '\0';
111 
112  for (int i = 0; i < range_size; i++) {
113  cpl.ranges[i] = NULL;
114  }
115 
116  csocket.command_sent = 0;
118  csocket.command_time = 0;
119 
120  cpl.magicmap = NULL;
121  cpl.showmagic = 0;
122 
124  face_info.cache_hits = 0;
126  face_info.faceset = 0;
128  face_info.num_images = 0;
129 
130  stat_points = 0;
131  stat_min = 0;
132  stat_maximum = 0;
133 
134  mapdata_free();
136 }
137 
141 static void init_config() {
143  want_config[CONFIG_CACHE] = FALSE;
147  want_config[CONFIG_DOWNLOAD] = FALSE;
148  want_config[CONFIG_ECHO] = FALSE;
149  want_config[CONFIG_FASTTCP] = TRUE;
150  want_config[CONFIG_FOGWAR] = TRUE;
151  want_config[CONFIG_FOODBEEP] = FALSE;
159  want_config[CONFIG_POPUPS] = FALSE;
163  want_config[CONFIG_SHOWGRID] = FALSE;
164  want_config[CONFIG_SHOWICON] = FALSE;
167  want_config[CONFIG_SOUND] = TRUE;
168  want_config[CONFIG_SPLASH] = TRUE;
169  want_config[CONFIG_SPLITINFO] = FALSE;
170  want_config[CONFIG_SPLITWIN] = FALSE;
171  want_config[CONFIG_TIMESTAMP] = FALSE;
173  want_config[CONFIG_TRIMINFO] = FALSE;
176 
177  for (int i = 0; i < CONFIG_NUMS; i++) {
178  use_config[i] = want_config[i];
179  }
180 }
181 
187 void client_init() {
188  // Initialize experience tables.
189  exp_table = NULL;
190  exp_table_max = 0;
191 
193 
194  // Clear out variables related to image face caching.
196  face_info.want_faceset = NULL;
197 
198  for (int i = 0; i < MAX_FACE_SETS; i++) {
199  face_info.facesets[i].prefix = NULL;
200  face_info.facesets[i].fullname = NULL;
201  face_info.facesets[i].fallback = 0;
202  face_info.facesets[i].size = NULL;
203  face_info.facesets[i].extension = NULL;
204  face_info.facesets[i].comment = NULL;
205  }
206 
207  // Allocate memory for player-related objects.
208  cpl.ob = player_item();
209  cpl.below = map_item();
210 
212 
213  for (int i = 0; i < MAX_SKILL; i++) {
214  skill_names[i] = NULL;
215  last_used_skills[i] = -1;
216  }
217 
218  init_commands();
219  init_config();
220  init_paths();
221 
222  // Paths must be set before metaserver can be initialized.
223  ms_init();
224 }
225 
230  int i;
231 
232  for (i = 0; i < MAX_SKILL; i++) {
233  cpl.stats.skill_exp[i] = 0;
234  cpl.stats.skill_level[i] = 0;
235  }
236 }
237 
242 void client_reset() {
243  int i;
244 
245  /* Keep old checksum to compare it with the next server's checksum. */
247 
248  for (i = 0; i < MAX_FACE_SETS; i++) {
251  face_info.facesets[i].fallback = 0;
255  }
256 
258 
259  for (i = 0; i < MAX_SKILL; i++) {
261  }
262 
263  if (motd) {
265  }
266 
267  if (news) {
269  }
270 
271  if (rules) {
273  }
274 
275  if (races) {
277  num_races = 0;
278  used_races = 0;
279  races = NULL;
280  }
281 
282  if (classes) {
284  num_classes = 0;
285  used_classes = 0;
286  classes = NULL;
287  }
288 
289  serverloginmethod = 0;
290 }
Player_Struct::stats
Stats stats
Definition: client.h:347
Stat_struct::skill_level
gint16 skill_level[MAX_SKILL]
Definition: client.h:287
CONFIG_CWINDOW
#define CONFIG_CWINDOW
Definition: client.h:186
VERSION_CS
#define VERSION_CS
Definition: client.h:67
FaceSets_struct::comment
char * comment
Definition: client.h:398
news
char * news
Definition: client.h:545
num_classes
int num_classes
Definition: client.h:547
LOG_WARNING
@ LOG_WARNING
Warning that something might not work.
Definition: client.h:435
map_item
item * map_item(void)
Definition: item.c:571
CONFIG_PORT
#define CONFIG_PORT
Definition: client.h:205
CONFIG_TRIMINFO
#define CONFIG_TRIMINFO
Definition: client.h:200
Stat_struct::skill_exp
gint64 skill_exp[MAX_SKILL]
Definition: client.h:288
metaserver.h
Player_Struct::title
char title[MAX_BUF]
Definition: client.h:349
ClientSocket::command_received
guint16 command_received
Definition: client.h:127
races
Race_Class_Info * races
Definition: commands.c:98
used_classes
int used_classes
Definition: client.h:547
CFG_LT_PIXEL
#define CFG_LT_PIXEL
Definition: client.h:228
ClientSocket::fd
GSocketConnection * fd
Definition: client.h:124
Face_Information_struct::num_images
gint16 num_images
Definition: client.h:408
cache_dir
const char * cache_dir
Definition: client.c:53
client_reset
void client_reset()
Definition: init.c:242
free_all_race_class_info
void free_all_race_class_info(Race_Class_Info *data, int num_entries)
Definition: commands.c:420
CONFIG_DOWNLOAD
#define CONFIG_DOWNLOAD
Definition: client.h:183
used_races
int used_races
Definition: client.h:547
init_commands
void init_commands()
Definition: p_cmd.c:407
FREE_AND_CLEAR
#define FREE_AND_CLEAR(xyz)
Definition: init.c:43
ClientSocket::command_time
int command_time
Definition: client.h:132
face_info
Face_Information face_info
Definition: image.c:169
CONFIG_FOGWAR
#define CONFIG_FOGWAR
Definition: client.h:188
FaceSets_struct::size
char * size
Definition: client.h:396
Stat_struct::maxgrace
gint16 maxgrace
Definition: client.h:265
CONFIG_MAPSCALE
#define CONFIG_MAPSCALE
Definition: client.h:190
CONFIG_GRAD_COLOR
#define CONFIG_GRAD_COLOR
Definition: client.h:206
CONFIG_LIGHTING
#define CONFIG_LIGHTING
Definition: client.h:199
Player_Struct::count_left
guint16 count_left
Definition: client.h:338
Player_Struct::input_text
char input_text[MAX_BUF]
Definition: client.h:341
ClientSocket::sc_version
int sc_version
Definition: client.h:125
Face_Information_struct::bmaps_checksum
guint32 bmaps_checksum
Definition: client.h:409
CONFIG_TOOLTIPS
#define CONFIG_TOOLTIPS
Definition: client.h:194
exp_table_max
guint16 exp_table_max
Definition: client.c:64
want_config
gint16 want_config[CONFIG_NUMS]
Definition: init.c:41
FaceSets_struct::prefix
char * prefix
Definition: client.h:394
VERSION_INFO
char VERSION_INFO[MAX_BUF]
Definition: client.c:48
range_size
@ range_size
Definition: client.h:154
init_paths
static void init_paths()
Definition: init.c:80
exp_table
guint64 * exp_table
Definition: client.c:65
mapdata.h
last_used_skills
int last_used_skills[MAX_SKILL+1]
Definition: client.c:55
Face_Information_struct::cache_misses
gint16 cache_misses
Definition: client.h:416
stat_min
int stat_min
Definition: client.h:548
CONFIG_FOODBEEP
#define CONFIG_FOODBEEP
Definition: client.h:203
CONFIG_CACHE
#define CONFIG_CACHE
Definition: client.h:187
CONFIG_NUMS
#define CONFIG_NUMS
Definition: client.h:217
Player_Struct::container
item * container
Definition: client.h:337
SendAddMe
void SendAddMe(ClientSocket csock)
Definition: init.c:76
Face_Information_struct::facesets
FaceSets facesets[MAX_FACE_SETS]
Definition: client.h:420
Stat_struct::speed
gint32 speed
Definition: client.h:273
config_names
const char *const config_names[CONFIG_NUMS]
Definition: init.c:30
CFG_DM_PIXMAP
#define CFG_DM_PIXMAP
Definition: client.h:237
CONFIG_SPLITINFO
#define CONFIG_SPLITINFO
Definition: client.h:196
Stat_struct::maxhp
gint16 maxhp
Definition: client.h:261
Player_Struct::last_command
char last_command[MAX_BUF]
Definition: client.h:340
Player_Struct::ranges
item * ranges[range_size]
Definition: client.h:342
CONFIG_SHOWGRID
#define CONFIG_SHOWGRID
Definition: client.h:198
CONFIG_TIMESTAMP
#define CONFIG_TIMESTAMP
Definition: client.h:214
Player_Struct::ob
item * ob
Definition: client.h:334
mapdata_free
void mapdata_free(void)
Definition: mapdata.c:615
Player_Struct::range
char range[MAX_BUF]
Definition: client.h:350
Stat_struct::maxsp
gint16 maxsp
Definition: client.h:263
CONFIG_MAPSCROLL
#define CONFIG_MAPSCROLL
Definition: client.h:211
stat_maximum
int stat_maximum
Definition: client.h:548
CONFIG_SIGNPOPUP
#define CONFIG_SIGNPOPUP
Definition: client.h:213
MAX_SKILL
#define MAX_SKILL
Definition: client.h:84
Face_Information_struct::want_faceset
char * want_faceset
Definition: client.h:407
LOG
void LOG(LogLevel level, const char *origin, const char *format,...)
Definition: misc.c:111
cs_print_string
int cs_print_string(GSocketConnection *fd, const char *str,...)
Definition: newsocket.c:252
config_dir
const char * config_dir
Definition: client.c:52
csocket
ClientSocket csocket
Definition: client.c:70
ms_init
void ms_init()
Definition: metaserver.c:257
EPORT
#define EPORT
Definition: client.h:62
CONFIG_SHOWICON
#define CONFIG_SHOWICON
Definition: client.h:193
MAX_FACE_SETS
#define MAX_FACE_SETS
Definition: client.h:381
FaceSets_struct::extension
char * extension
Definition: client.h:397
Player_Struct::magicmap
guint8 * magicmap
Definition: client.h:361
init_config
static void init_config()
Definition: init.c:141
CONFIG_SPLASH
#define CONFIG_SPLASH
Definition: client.h:209
SendVersion
void SendVersion(ClientSocket csock)
Definition: init.c:71
CONFIG_AUTO_AFK
#define CONFIG_AUTO_AFK
Definition: client.h:215
CONFIG_INV_MENU
#define CONFIG_INV_MENU
Definition: client.h:216
p_cmd.h
reset_vars_common
static void reset_vars_common()
Definition: init.c:97
ClientSocket
Definition: client.h:123
CONFIG_RESISTS
#define CONFIG_RESISTS
Definition: client.h:207
Player_Struct::showmagic
guint8 showmagic
Definition: client.h:362
Face_Information_struct::old_bmaps_checksum
guint32 old_bmaps_checksum
Definition: client.h:409
CONFIG_SOUND
#define CONFIG_SOUND
Definition: client.h:195
player_item
item * player_item(void)
Definition: item.c:565
FaceSets_struct::fullname
char * fullname
Definition: client.h:395
CONFIG_MAPWIDTH
#define CONFIG_MAPWIDTH
Definition: client.h:201
cpl
Client_Player cpl
Definition: client.c:69
ClientSocket::cs_version
int cs_version
Definition: client.h:125
VERSION_SC
#define VERSION_SC
Definition: client.h:68
reset_player_data
void reset_player_data()
Definition: init.c:229
CONFIG_POPUPS
#define CONFIG_POPUPS
Definition: client.h:191
CONFIG_SMOOTH
#define CONFIG_SMOOTH
Definition: client.h:208
Player_Struct::below
item * below
Definition: client.h:335
COMMAND_WINDOW
#define COMMAND_WINDOW
Definition: client.h:76
Stat_struct
Definition: client.h:249
Face_Information_struct::faceset
guint8 faceset
Definition: client.h:406
rules
char * rules
Definition: client.h:545
motd
char * motd
Definition: client.h:546
num_races
int num_races
Definition: commands.c:87
stat_points
int stat_points
Definition: commands.c:93
Face_Information_struct::have_faceset_info
guint8 have_faceset_info
Definition: client.h:417
CONFIG_APPLY_CONTAINER
#define CONFIG_APPLY_CONTAINER
Definition: client.h:210
Face_Information_struct::cache_hits
gint16 cache_hits
Definition: client.h:416
CONFIG_ICONSCALE
#define CONFIG_ICONSCALE
Definition: client.h:189
ClientSocket::command_sent
guint16 command_sent
Definition: client.h:127
client_init
void client_init()
Definition: init.c:187
CONFIG_ECHO
#define CONFIG_ECHO
Definition: client.h:184
CONFIG_MAPHEIGHT
#define CONFIG_MAPHEIGHT
Definition: client.h:202
CONFIG_FASTTCP
#define CONFIG_FASTTCP
Definition: client.h:185
CONFIG_SPLITWIN
#define CONFIG_SPLITWIN
Definition: client.h:197
CONFIG_DARKNESS
#define CONFIG_DARKNESS
Definition: client.h:204
serverloginmethod
int serverloginmethod
Definition: client.c:62
use_config
gint16 use_config[CONFIG_NUMS]
Definition: init.c:41
skill_names
char * skill_names[MAX_SKILL]
Definition: client.c:50
LOG_DEBUG
@ LOG_DEBUG
Useful debugging information.
Definition: client.h:433
VersionCmd
void VersionCmd(char *data, int len)
Definition: init.c:45
client.h
FaceSets_struct::fallback
guint8 fallback
Definition: client.h:393
classes
Race_Class_Info * classes
Definition: client.h:608
CONFIG_DISPLAYMODE
#define CONFIG_DISPLAYMODE
Definition: client.h:192