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  "music_vol",
40  "server_ticks",
41  "debounce",
42 };
43 
45 
46 #define FREE_AND_CLEAR(xyz) { free(xyz); xyz=NULL; }
47 
48 void VersionCmd(char *data, int len) {
49  char *cp;
50 
51  csocket.cs_version = atoi(data);
52  /* set sc_version in case it is an old server supplying only one version */
54  if (csocket.cs_version != VERSION_CS) {
55  LOG(LOG_WARNING, "common::VersionCmd", "Differing C->S version numbers (%d,%d)",
57  /* exit(1);*/
58  }
59  cp = strchr(data, ' ');
60  if (!cp) {
61  return;
62  }
63  csocket.sc_version = atoi(cp);
64  if (csocket.sc_version != VERSION_SC) {
65  LOG(LOG_WARNING, "common::VersionCmd", "Differing S->C version numbers (%d,%d)",
67  }
68  cp = strchr(cp + 1, ' ');
69  if (cp) {
70  LOG(LOG_DEBUG, "common::VersionCmd", "Playing on server type %s", cp);
71  }
72 }
73 
75  cs_print_string(csock.fd, "version %d %d %s",
77 }
78 
79 void SendAddMe(ClientSocket csock) {
80  cs_print_string(csock.fd, "addme");
81 }
82 
83 static void init_paths() {
84  // Set and create configuration and cache directories.
85  GString *app_config_dir = g_string_new(g_get_user_config_dir());
86  g_string_append(app_config_dir, "/crossfire");
87  config_dir = g_string_free(app_config_dir, FALSE);
88  g_mkdir_with_parents(config_dir, 0755);
89 
90  GString *app_cache_dir = g_string_new(g_get_user_cache_dir());
91  g_string_append(app_cache_dir, "/crossfire");
92  cache_dir = g_string_free(app_cache_dir, FALSE);
93  g_mkdir_with_parents(cache_dir, 0755);
94 }
95 
100 static void reset_vars_common() {
101  cpl.count_left = 0;
102  cpl.container = NULL;
103  memset(&cpl.stats, 0, sizeof(Stats));
104  cpl.stats.maxsp = 1; /* avoid div by 0 errors */
105  cpl.stats.maxhp = 1; /* ditto */
106  cpl.stats.maxgrace = 1; /* ditto */
107 
108  /* ditto - displayed weapon speed is weapon speed/speed */
109  cpl.stats.speed = 1;
110  cpl.input_text[0] = '\0';
111  cpl.title[0] = '\0';
112  cpl.range[0] = '\0';
113  cpl.last_command[0] = '\0';
114 
115  for (int i = 0; i < range_size; i++) {
116  cpl.ranges[i] = NULL;
117  }
118 
119  csocket.command_sent = 0;
121  csocket.command_time = 0;
122 
123  cpl.magicmap = NULL;
124  cpl.showmagic = 0;
125 
127  face_info.cache_hits = 0;
129  face_info.faceset = 0;
131  face_info.num_images = 0;
132 
133  stat_points = 0;
134  stat_min = 0;
135  stat_maximum = 0;
136 
137  mapdata_free();
139 }
140 
144 static void init_config() {
146  want_config[CONFIG_CACHE] = FALSE;
150  want_config[CONFIG_DOWNLOAD] = FALSE;
151  want_config[CONFIG_ECHO] = FALSE;
152  want_config[CONFIG_FASTTCP] = TRUE;
153  want_config[CONFIG_FOGWAR] = TRUE;
154  want_config[CONFIG_FOODBEEP] = FALSE;
162  want_config[CONFIG_POPUPS] = FALSE;
165  want_config[CONFIG_SHOWGRID] = FALSE;
166  want_config[CONFIG_SHOWICON] = FALSE;
168  want_config[CONFIG_SMOOTH] = FALSE;
169  want_config[CONFIG_SOUND] = TRUE;
170  want_config[CONFIG_SPLASH] = TRUE;
171  want_config[CONFIG_SPLITINFO] = FALSE;
172  want_config[CONFIG_SPLITWIN] = FALSE;
173  want_config[CONFIG_TIMESTAMP] = FALSE;
175  want_config[CONFIG_TRIMINFO] = FALSE;
181 
182  for (int i = 0; i < CONFIG_NUMS; i++) {
183  use_config[i] = want_config[i];
184  }
185 }
186 
192 void client_init() {
193  // Initialize experience tables.
194  exp_table = NULL;
195  exp_table_max = 0;
196 
198 
199  // Clear out variables related to image face caching.
201  face_info.want_faceset = NULL;
202 
203  for (int i = 0; i < MAX_FACE_SETS; i++) {
204  face_info.facesets[i].prefix = NULL;
205  face_info.facesets[i].fullname = NULL;
206  face_info.facesets[i].fallback = 0;
207  face_info.facesets[i].size = NULL;
208  face_info.facesets[i].extension = NULL;
209  face_info.facesets[i].comment = NULL;
210  }
211 
212  // Allocate memory for player-related objects.
213  cpl.ob = player_item();
214  cpl.below = map_item();
215 
217 
218  for (int i = 0; i < MAX_SKILL; i++) {
219  skill_names[i] = NULL;
220  last_used_skills[i] = -1;
221  }
222 
223  init_commands();
224  init_config();
225  init_paths();
226 
227  // Paths must be set before metaserver can be initialized.
228  ms_init();
229 }
230 
235  int i;
236 
237  for (i = 0; i < MAX_SKILL; i++) {
238  cpl.stats.skill_exp[i] = 0;
239  cpl.stats.skill_level[i] = 0;
240  }
241 }
242 
247 void client_reset() {
248  int i;
249 
250  /* Keep old checksum to compare it with the next server's checksum. */
252 
253  for (i = 0; i < MAX_FACE_SETS; i++) {
256  face_info.facesets[i].fallback = 0;
260  }
261 
263 
264  for (i = 0; i < MAX_SKILL; i++) {
266  }
267 
268  if (motd) {
270  }
271 
272  if (news) {
274  }
275 
276  if (rules) {
278  }
279 
280  if (races) {
282  num_races = 0;
283  used_races = 0;
284  races = NULL;
285  }
286 
287  if (classes) {
289  num_classes = 0;
290  used_classes = 0;
291  classes = NULL;
292  }
293 
294  serverloginmethod = 0;
295 }
Player_Struct::stats
Stats stats
Player stats.
Definition: client.h:350
Stat_struct::skill_level
gint16 skill_level[MAX_SKILL]
Level of known skills.
Definition: client.h:290
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:401
news
char * news
Definition: client.h:548
num_classes
int num_classes
Definition: client.h:550
LOG_WARNING
@ LOG_WARNING
Warning that something might not work.
Definition: client.h:438
map_item
item * map_item(void)
Definition: item.c:571
CONFIG_PORT
#define CONFIG_PORT
Is this useful any more?
Definition: client.h:205
CONFIG_TRIMINFO
#define CONFIG_TRIMINFO
Definition: client.h:200
Stat_struct::skill_exp
gint64 skill_exp[MAX_SKILL]
Experience points for skills.
Definition: client.h:291
metaserver.h
Player_Struct::title
char title[MAX_BUF]
Title of character.
Definition: client.h:352
CONFIG_SERVER_TICKS
#define CONFIG_SERVER_TICKS
Definition: client.h:218
ClientSocket::command_received
guint16 command_received
These are used for the newer 'windowing' method of commands - number of last command sent,...
Definition: client.h:127
races
Race_Class_Info * races
Definition: commands.c:98
used_classes
int used_classes
Definition: client.h:550
CFG_LT_PIXEL
#define CFG_LT_PIXEL
Definition: client.h:231
ClientSocket::fd
GSocketConnection * fd
Definition: client.h:124
Face_Information_struct::num_images
gint16 num_images
Definition: client.h:411
cache_dir
const char * cache_dir
Definition: client.c:53
client_reset
void client_reset()
Clear client variables between connections to different servers.
Definition: init.c:247
free_all_race_class_info
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
CONFIG_DOWNLOAD
#define CONFIG_DOWNLOAD
Definition: client.h:183
used_races
int used_races
Definition: client.h:550
init_commands
void init_commands()
Fills some internal arrays.
Definition: p_cmd.c:423
FREE_AND_CLEAR
#define FREE_AND_CLEAR(xyz)
Definition: init.c:46
ClientSocket::command_time
int command_time
Time (in ms) players commands currently take to execute.
Definition: client.h:132
face_info
Face_Information face_info
Definition: image.c:169
CONFIG_MUSIC_VOL
#define CONFIG_MUSIC_VOL
Definition: client.h:217
CONFIG_FOGWAR
#define CONFIG_FOGWAR
Definition: client.h:188
FaceSets_struct::size
char * size
Definition: client.h:399
Stat_struct::maxgrace
gint16 maxgrace
Maximum spell points.
Definition: client.h:268
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
count for commands
Definition: client.h:341
Player_Struct::input_text
char input_text[MAX_BUF]
keys typed (for long commands)
Definition: client.h:344
ClientSocket::sc_version
int sc_version
Server versions of these.
Definition: client.h:125
Face_Information_struct::bmaps_checksum
guint32 bmaps_checksum
Definition: client.h:412
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:44
FaceSets_struct::prefix
char * prefix
Definition: client.h:397
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:83
exp_table
guint64 * exp_table
Definition: client.c:65
mapdata.h
last_used_skills
int last_used_skills[MAX_SKILL+1]
maps position to skill id with trailing zero as stop mark.
Definition: client.c:55
Face_Information_struct::cache_misses
gint16 cache_misses
Definition: client.h:419
stat_min
int stat_min
Definition: client.h:551
CONFIG_FOODBEEP
#define CONFIG_FOODBEEP
Definition: client.h:203
CONFIG_CACHE
#define CONFIG_CACHE
Definition: client.h:187
CONFIG_NUMS
#define CONFIG_NUMS
This should always be the last value in the CONFIG_xxx list.
Definition: client.h:220
Player_Struct::container
item * container
open container
Definition: client.h:340
SendAddMe
void SendAddMe(ClientSocket csock)
Definition: init.c:79
Face_Information_struct::facesets
FaceSets facesets[MAX_FACE_SETS]
Definition: client.h:423
Stat_struct::speed
gint32 speed
Speed (is displayed as a float)
Definition: client.h:276
config_names
const char *const config_names[CONFIG_NUMS]
See common/init.c - number mapping used when loading/saving the values.
Definition: init.c:30
CFG_DM_PIXMAP
#define CFG_DM_PIXMAP
Definition: client.h:240
CONFIG_SPLITINFO
#define CONFIG_SPLITINFO
Definition: client.h:196
Stat_struct::maxhp
gint16 maxhp
Maximum hit points.
Definition: client.h:264
Player_Struct::last_command
char last_command[MAX_BUF]
Last command entered.
Definition: client.h:343
Player_Struct::ranges
item * ranges[range_size]
Object that is used for that.
Definition: client.h:345
CONFIG_SHOWGRID
#define CONFIG_SHOWGRID
Definition: client.h:198
CONFIG_TIMESTAMP
#define CONFIG_TIMESTAMP
Definition: client.h:214
Player_Struct::ob
item * ob
Player object.
Definition: client.h:337
mapdata_free
void mapdata_free(void)
Deallocate map data.
Definition: mapdata.c:645
Player_Struct::range
char range[MAX_BUF]
Range attack chosen.
Definition: client.h:353
Stat_struct::maxsp
gint16 maxsp
Maximum spell points.
Definition: client.h:266
CONFIG_MAPSCROLL
#define CONFIG_MAPSCROLL
Use bitmap operations for map scrolling.
Definition: client.h:211
stat_maximum
int stat_maximum
Definition: client.h:551
CONFIG_SIGNPOPUP
#define CONFIG_SIGNPOPUP
Definition: client.h:213
MAX_SKILL
#define MAX_SKILL
How many skill types server supports/client will get sent to it.
Definition: client.h:84
Face_Information_struct::want_faceset
char * want_faceset
Definition: client.h:410
LOG
void LOG(LogLevel level, const char *origin, const char *format,...)
Log messages of a certain importance to stderr.
Definition: misc.c:111
cs_print_string
int cs_print_string(GSocketConnection *fd, const char *str,...)
Send a printf-formatted packet to the socket.
Definition: newsocket.c:251
config_dir
const char * config_dir
Definition: client.c:52
csocket
ClientSocket csocket
Definition: client.c:70
ms_init
void ms_init()
Initialize metaserver client.
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:384
FaceSets_struct::extension
char * extension
Definition: client.h:400
Player_Struct::magicmap
guint8 * magicmap
Magic map data.
Definition: client.h:364
init_config
static void init_config()
Initialize client settings with built-in defaults.
Definition: init.c:144
CONFIG_SPLASH
#define CONFIG_SPLASH
Definition: client.h:209
SendVersion
void SendVersion(ClientSocket csock)
Definition: init.c:74
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()
Initialize or reset client variables.
Definition: init.c:100
ClientSocket
Basic support for socket communications, including the file descriptor, input buffer,...
Definition: client.h:123
CONFIG_RESISTS
#define CONFIG_RESISTS
Definition: client.h:207
Player_Struct::showmagic
guint8 showmagic
If 0, show the normal map, otherwise show the magic map.
Definition: client.h:365
Face_Information_struct::old_bmaps_checksum
guint32 old_bmaps_checksum
Definition: client.h:412
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:398
CONFIG_MAPWIDTH
#define CONFIG_MAPWIDTH
Definition: client.h:201
cpl
Client_Player cpl
Player object.
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()
Reset player experience data.
Definition: init.c:234
CONFIG_POPUPS
#define CONFIG_POPUPS
Definition: client.h:191
CONFIG_SMOOTH
#define CONFIG_SMOOTH
Definition: client.h:208
Player_Struct::below
item * below
Items below the player (pl.below->inv)
Definition: client.h:338
COMMAND_WINDOW
#define COMMAND_WINDOW
Do not send more than this many outstanding commands to the server this is only a default value.
Definition: client.h:76
Stat_struct
Definition: client.h:252
Face_Information_struct::faceset
guint8 faceset
Definition: client.h:409
rules
char * rules
Definition: client.h:548
motd
char * motd
Definition: client.h:549
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
Simple value to know if there is data in facesets[].
Definition: client.h:420
CONFIG_APPLY_CONTAINER
#define CONFIG_APPLY_CONTAINER
Reapply container.
Definition: client.h:210
Face_Information_struct::cache_hits
gint16 cache_hits
Just for debugging/logging purposes.
Definition: client.h:419
CONFIG_ICONSCALE
#define CONFIG_ICONSCALE
Definition: client.h:189
ClientSocket::command_sent
guint16 command_sent
Definition: client.h:127
client_init
void client_init()
Called ONCE during client startup to initialize configuration and other variables to reasonable defau...
Definition: init.c:192
CONFIG_DEBOUNCE
#define CONFIG_DEBOUNCE
Definition: client.h:219
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:44
skill_names
char * skill_names[MAX_SKILL]
Definition: client.c:50
LOG_DEBUG
@ LOG_DEBUG
Useful debugging information.
Definition: client.h:436
VersionCmd
void VersionCmd(char *data, int len)
Definition: init.c:48
client.h
FaceSets_struct::fallback
guint8 fallback
Definition: client.h:396
classes
Race_Class_Info * classes
Definition: client.h:611
CONFIG_DISPLAYMODE
#define CONFIG_DISPLAYMODE
Definition: client.h:192