Crossfire Client, Trunk  R20330
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"
38 };
39 
41 
42 #define FREE_AND_CLEAR(xyz) { free(xyz); xyz=NULL; }
43 
44 void VersionCmd(char *data, int len) {
45  char *cp;
46 
47  csocket.cs_version = atoi(data);
48  /* set sc_version in case it is an old server supplying only one version */
50  if (csocket.cs_version != VERSION_CS) {
51  LOG(LOG_WARNING, "common::VersionCmd", "Differing C->S version numbers (%d,%d)",
53  /* exit(1);*/
54  }
55  cp = strchr(data, ' ');
56  if (!cp) {
57  return;
58  }
59  csocket.sc_version = atoi(cp);
60  if (csocket.sc_version != VERSION_SC) {
61  LOG(LOG_WARNING, "common::VersionCmd", "Differing S->C version numbers (%d,%d)",
63  }
64  cp = strchr(cp + 1, ' ');
65  if (cp) {
66  LOG(LOG_DEBUG, "common::VersionCmd", "Playing on server type %s", cp);
67  }
68 }
69 
71  cs_print_string(csock.fd, "version %d %d %s",
73 }
74 
75 void SendAddMe(ClientSocket csock) {
76  cs_print_string(csock.fd, "addme");
77 }
78 
79 static void init_paths() {
80  // Set and create configuration and cache directories.
81  GString *app_config_dir = g_string_new(g_get_user_config_dir());
82  g_string_append(app_config_dir, "/crossfire");
83  config_dir = g_string_free(app_config_dir, FALSE);
84  g_mkdir_with_parents(config_dir, 0755);
85 
86  GString *app_cache_dir = g_string_new(g_get_user_cache_dir());
87  g_string_append(app_cache_dir, "/crossfire");
88  cache_dir = g_string_free(app_cache_dir, FALSE);
89  g_mkdir_with_parents(cache_dir, 0755);
90 }
91 
96 static void reset_vars_common() {
97  cpl.count_left = 0;
98  cpl.container = NULL;
99  memset(&cpl.stats, 0, sizeof(Stats));
100  cpl.stats.maxsp = 1; /* avoid div by 0 errors */
101  cpl.stats.maxhp = 1; /* ditto */
102  cpl.stats.maxgrace = 1; /* ditto */
103 
104  /* ditto - displayed weapon speed is weapon speed/speed */
105  cpl.stats.speed = 1;
106  cpl.input_text[0] = '\0';
107  cpl.title[0] = '\0';
108  cpl.range[0] = '\0';
109  cpl.last_command[0] = '\0';
110 
111  for (int i = 0; i < range_size; i++) {
112  cpl.ranges[i] = NULL;
113  }
114 
115  csocket.command_sent = 0;
117  csocket.command_time = 0;
118 
119  cpl.magicmap = NULL;
120  cpl.showmagic = 0;
121 
123  face_info.cache_hits = 0;
125  face_info.faceset = 0;
127  face_info.num_images = 0;
128 
129  stat_points = 0;
130  stat_min = 0;
131  stat_maximum = 0;
132 
133  mapdata_init();
135 }
136 
140 static void init_config() {
142  want_config[CONFIG_CACHE] = FALSE;
146  want_config[CONFIG_DOWNLOAD] = FALSE;
147  want_config[CONFIG_ECHO] = FALSE;
148  want_config[CONFIG_FASTTCP] = TRUE;
149  want_config[CONFIG_FOGWAR] = TRUE;
150  want_config[CONFIG_FOODBEEP] = FALSE;
158  want_config[CONFIG_POPUPS] = FALSE;
162  want_config[CONFIG_SHOWGRID] = FALSE;
163  want_config[CONFIG_SHOWICON] = FALSE;
166  want_config[CONFIG_SOUND] = TRUE;
167  want_config[CONFIG_SPLASH] = TRUE;
168  want_config[CONFIG_SPLITINFO] = FALSE;
169  want_config[CONFIG_SPLITWIN] = FALSE;
170  want_config[CONFIG_TIMESTAMP] = FALSE;
172  want_config[CONFIG_TRIMINFO] = FALSE;
173 
174  for (int i = 0; i < CONFIG_NUMS; i++) {
175  use_config[i] = want_config[i];
176  }
177 }
178 
184 void client_init() {
185  // Initialize experience tables.
186  exp_table = NULL;
187  exp_table_max = 0;
188 
190 
191  // Clear out variables related to image face caching.
193  face_info.want_faceset = NULL;
194 
195  for (int i = 0; i < MAX_FACE_SETS; i++) {
196  face_info.facesets[i].prefix = NULL;
197  face_info.facesets[i].fullname = NULL;
198  face_info.facesets[i].fallback = 0;
199  face_info.facesets[i].size = NULL;
200  face_info.facesets[i].extension = NULL;
201  face_info.facesets[i].comment = NULL;
202  }
203 
204  // Allocate memory for player-related objects.
205  cpl.ob = player_item();
206  cpl.below = map_item();
207 
209 
210  for (int i = 0; i < MAX_SKILL; i++) {
211  skill_names[i] = NULL;
212  last_used_skills[i] = -1;
213  }
214 
215  init_commands();
216  init_config();
217  init_paths();
218 
219  // Paths must be set before metaserver can be initialized.
220  ms_init();
221 }
222 
227  int i;
228 
229  for (i = 0; i < MAX_SKILL; i++) {
230  cpl.stats.skill_exp[i] = 0;
231  cpl.stats.skill_level[i] = 0;
232  }
233 }
234 
239 void client_reset() {
240  int i;
241 
242  /* Keep old checksum to compare it with the next server's checksum. */
244 
245  for (i = 0; i < MAX_FACE_SETS; i++) {
248  face_info.facesets[i].fallback = 0;
252  }
253 
255 
256  for (i = 0; i < MAX_SKILL; i++) {
258  }
259 
260  if (motd) {
262  }
263 
264  if (news) {
266  }
267 
268  if (rules) {
270  }
271 
272  if (races) {
274  num_races = 0;
275  used_races = 0;
276  races = NULL;
277  }
278 
279  if (classes) {
281  num_classes = 0;
282  used_classes = 0;
283  classes = NULL;
284  }
285 
286  serverloginmethod = 0;
287 }
#define VERSION_CS
Definition: client.h:65
void free_all_race_class_info(Race_Class_Info *data, int num_entries)
This function clears the data from the Race_Class_Info array.
Definition: commands.c:420
char * comment
Definition: client.h:405
char * news
Definition: client.h:578
#define CONFIG_FASTTCP
Definition: client.h:186
gint16 cache_hits
Just for debugging/logging purposes.
Definition: client.h:423
void VersionCmd(char *data, int len)
Definition: init.c:44
void SendAddMe(ClientSocket csock)
Definition: init.c:75
#define COMMAND_WINDOW
Do not send more than this many outstanding commands to the server this is only a default value...
Definition: client.h:74
int command_time
Time (in ms) players commands currently take to execute.
Definition: client.h:127
int used_classes
Definition: commands.c:91
#define CONFIG_TOOLTIPS
Definition: client.h:195
int stat_points
Definition: commands.c:93
#define CONFIG_RESISTS
Definition: client.h:208
void client_init()
Called ONCE during client startup to initialize configuration and other variables to reasonable defau...
Definition: init.c:184
gint16 want_config[CONFIG_NUMS]
Definition: init.c:40
#define CONFIG_SHOWGRID
Definition: client.h:199
void ms_init()
Initialize metaserver client.
Definition: metaserver.c:300
guint8 fallback
Definition: client.h:400
gint64 skill_exp[MAX_SKILL]
Experience points for skills.
Definition: client.h:289
Race_Class_Info * races
Definition: commands.c:98
#define FREE_AND_CLEAR(xyz)
Definition: init.c:42
int sc_version
Server versions of these.
Definition: client.h:120
guint8 have_faceset_info
Simple value to know if there is data in facesets[].
Definition: client.h:424
#define CONFIG_LIGHTING
Definition: client.h:200
#define MAX_SKILL
How many skill types server supports/client will get sent to it.
Definition: client.h:82
#define CONFIG_POPUPS
Definition: client.h:192
char * rules
Definition: client.h:578
gint16 maxsp
Maximum spell points.
Definition: client.h:264
item * ob
Player object.
Definition: client.h:335
#define CONFIG_DISPLAYMODE
Definition: client.h:193
int num_classes
Definition: commands.c:90
Warning that something might not work.
Definition: client.h:442
gint32 speed
Speed (is displayed as a float)
Definition: client.h:274
ClientSocket csocket
Definition: client.c:70
FaceSets facesets[MAX_FACE_SETS]
Definition: client.h:427
#define CONFIG_FOODBEEP
Definition: client.h:204
Face_Information face_info
Definition: image.c:169
Stats stats
Player stats.
Definition: client.h:348
static void init_paths()
Definition: init.c:79
#define CONFIG_SHOWICON
Definition: client.h:194
const char * cache_dir
Definition: client.c:54
#define CONFIG_SPLITINFO
Definition: client.h:197
guint32 bmaps_checksum
Definition: client.h:416
char * prefix
Definition: client.h:401
int serverloginmethod
Definition: client.c:60
guint8 * magicmap
Magic map data.
Definition: client.h:362
char VERSION_INFO[MAX_BUF]
Definition: client.c:49
char * skill_names[MAX_SKILL]
Definition: client.c:51
#define EPORT
Definition: client.h:60
guint64 * exp_table
Definition: client.c:65
#define CONFIG_MAPSCROLL
Use bitmap operations for map scrolling.
Definition: client.h:212
Metaserver functions and data structures.
Basic support for socket communications, including the file descriptor, input buffer, server, server, version, etc.
Definition: client.h:117
void reset_player_data()
Reset player experience data.
Definition: init.c:226
void LOG(LogLevel level, const char *origin, const char *format,...)
Log messages of a certain importance to stderr.
Definition: misc.c:112
char * size
Definition: client.h:403
#define CONFIG_SPLASH
Definition: client.h:210
#define CFG_DM_PIXMAP
Definition: client.h:238
int stat_maximum
Definition: commands.c:95
void init_commands()
Fills some internal arrays.
Definition: p_cmd.c:376
Includes and prototypes for p_cmd.c for player-commands like &#39;/magicmap&#39;.
int cs_print_string(int fd, const char *str,...)
Send a printf-formatted packet to the socket.
Definition: newsocket.c:310
gint16 use_config[CONFIG_NUMS]
Definition: init.c:40
void SendVersion(ClientSocket csock)
Definition: init.c:70
item * below
Items below the player (pl.below->inv)
Definition: client.h:336
Client_Player cpl
Player object.
Definition: client.c:69
#define CONFIG_GRAD_COLOR
Definition: client.h:207
#define CONFIG_SIGNPOPUP
Definition: client.h:215
item * container
open container
Definition: client.h:338
#define CONFIG_APPLY_CONTAINER
Reapply container.
Definition: client.h:211
#define CONFIG_DARKNESS
Definition: client.h:205
int stat_min
Definition: commands.c:94
#define CONFIG_MAPHEIGHT
Definition: client.h:203
void mapdata_init(void)
Initializes the module.
Definition: mapdata.c:556
#define CONFIG_PORT
Is this useful any more?
Definition: client.h:206
#define CONFIG_CWINDOW
Definition: client.h:187
#define CONFIG_CACHE
Definition: client.h:188
char last_command[MAX_BUF]
Last command entered.
Definition: client.h:341
const char * config_dir
Definition: client.c:53
#define CONFIG_NUMS
This should always be the last value in the CONFIG_xxx list.
Definition: client.h:217
guint16 count_left
count for commands
Definition: client.h:339
int num_races
Definition: commands.c:87
#define MAX_FACE_SETS
Definition: client.h:382
#define CONFIG_DOWNLOAD
Definition: client.h:184
int last_used_skills[MAX_SKILL+1]
maps position to skill id with trailing zero as stop mark.
Definition: client.c:56
char * extension
Definition: client.h:404
#define CONFIG_TRIMINFO
Definition: client.h:201
#define CFG_LT_TILE
Definition: client.h:228
gint16 skill_level[MAX_SKILL]
Level of known skills.
Definition: client.h:288
guint8 showmagic
If 0, show the normal map, otherwise show the magic map.
Definition: client.h:363
guint32 old_bmaps_checksum
Definition: client.h:416
#define CONFIG_ICONSCALE
Definition: client.h:190
#define CONFIG_TIMESTAMP
Definition: client.h:216
item * ranges[range_size]
Object that is used for that.
Definition: client.h:343
#define CONFIG_SMOOTH
Definition: client.h:209
int used_races
Definition: commands.c:88
#define VERSION_SC
Definition: client.h:66
char * fullname
Definition: client.h:402
int command_sent
Definition: client.h:122
item * map_item(void)
Definition: item.c:633
const char *const config_names[CONFIG_NUMS]
See common/init.c - number mapping used when loading/saving the values.
Definition: init.c:30
char range[MAX_BUF]
Range attack chosen.
Definition: client.h:351
int command_received
These are used for the newer &#39;windowing&#39; method of commands - number of last command sent...
Definition: client.h:122
gint16 maxhp
Maximum hit points.
Definition: client.h:262
#define CONFIG_SOUND
Definition: client.h:196
void client_reset()
Clear client variables between connections to different servers.
Definition: init.c:239
char title[MAX_BUF]
Title of character.
Definition: client.h:350
Includes various dependencies header files needed by most everything.
char input_text[MAX_BUF]
keys typed (for long commands)
Definition: client.h:342
item * player_item(void)
Definition: item.c:627
#define CONFIG_ECHO
Definition: client.h:185
Useful debugging information.
Definition: client.h:440
#define CONFIG_FOGWAR
Definition: client.h:189
static void init_config()
Initialize client settings with built-in defaults.
Definition: init.c:140
char * motd
Definition: client.h:578
static void reset_vars_common()
Initialize or reset client variables.
Definition: init.c:96
guint16 exp_table_max
Definition: client.c:64
#define CONFIG_SPLITWIN
Definition: client.h:198
Race_Class_Info * classes
Definition: commands.c:98
#define CONFIG_MAPSCALE
Definition: client.h:191
#define CONFIG_MAPWIDTH
Definition: client.h:202
int cs_version
Definition: client.h:120
gint16 maxgrace
Maximum spell points.
Definition: client.h:266