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 };
42 
44 
45 #define FREE_AND_CLEAR(xyz) { free(xyz); xyz=NULL; }
46 
47 void VersionCmd(char *data, int len) {
48  char *cp;
49 
50  csocket.cs_version = atoi(data);
51  /* set sc_version in case it is an old server supplying only one version */
53  if (csocket.cs_version != VERSION_CS) {
54  LOG(LOG_WARNING, "common::VersionCmd", "Differing C->S version numbers (%d,%d)",
56  /* exit(1);*/
57  }
58  cp = strchr(data, ' ');
59  if (!cp) {
60  return;
61  }
62  csocket.sc_version = atoi(cp);
63  if (csocket.sc_version != VERSION_SC) {
64  LOG(LOG_WARNING, "common::VersionCmd", "Differing S->C version numbers (%d,%d)",
66  }
67  cp = strchr(cp + 1, ' ');
68  if (cp) {
69  LOG(LOG_DEBUG, "common::VersionCmd", "Playing on server type %s", cp);
70  }
71 }
72 
74  cs_print_string(csock.fd, "version %d %d %s",
76 }
77 
78 void SendAddMe(ClientSocket csock) {
79  cs_print_string(csock.fd, "addme");
80 }
81 
82 static void init_paths() {
83  // Set and create configuration and cache directories.
84  GString *app_config_dir = g_string_new(g_get_user_config_dir());
85  g_string_append(app_config_dir, "/crossfire");
86  config_dir = g_string_free(app_config_dir, FALSE);
87  g_mkdir_with_parents(config_dir, 0755);
88 
89  GString *app_cache_dir = g_string_new(g_get_user_cache_dir());
90  g_string_append(app_cache_dir, "/crossfire");
91  cache_dir = g_string_free(app_cache_dir, FALSE);
92  g_mkdir_with_parents(cache_dir, 0755);
93 }
94 
99 static void reset_vars_common() {
100  cpl.count_left = 0;
101  cpl.container = NULL;
102  memset(&cpl.stats, 0, sizeof(Stats));
103  cpl.stats.maxsp = 1; /* avoid div by 0 errors */
104  cpl.stats.maxhp = 1; /* ditto */
105  cpl.stats.maxgrace = 1; /* ditto */
106 
107  /* ditto - displayed weapon speed is weapon speed/speed */
108  cpl.stats.speed = 1;
109  cpl.input_text[0] = '\0';
110  cpl.title[0] = '\0';
111  cpl.range[0] = '\0';
112  cpl.last_command[0] = '\0';
113 
114  for (int i = 0; i < range_size; i++) {
115  cpl.ranges[i] = NULL;
116  }
117 
118  csocket.command_sent = 0;
120  csocket.command_time = 0;
121 
122  cpl.magicmap = NULL;
123  cpl.showmagic = 0;
124 
126  face_info.cache_hits = 0;
128  face_info.faceset = 0;
130  face_info.num_images = 0;
131 
132  stat_points = 0;
133  stat_min = 0;
134  stat_maximum = 0;
135 
136  mapdata_free();
138 }
139 
143 static void init_config() {
145  want_config[CONFIG_CACHE] = FALSE;
149  want_config[CONFIG_DOWNLOAD] = FALSE;
150  want_config[CONFIG_ECHO] = FALSE;
151  want_config[CONFIG_FASTTCP] = TRUE;
152  want_config[CONFIG_FOGWAR] = TRUE;
153  want_config[CONFIG_FOODBEEP] = FALSE;
161  want_config[CONFIG_POPUPS] = FALSE;
164  want_config[CONFIG_SHOWGRID] = FALSE;
165  want_config[CONFIG_SHOWICON] = FALSE;
167  want_config[CONFIG_SMOOTH] = FALSE;
168  want_config[CONFIG_SOUND] = TRUE;
169  want_config[CONFIG_SPLASH] = TRUE;
170  want_config[CONFIG_SPLITINFO] = FALSE;
171  want_config[CONFIG_SPLITWIN] = FALSE;
172  want_config[CONFIG_TIMESTAMP] = FALSE;
174  want_config[CONFIG_TRIMINFO] = FALSE;
179 
180  for (int i = 0; i < CONFIG_NUMS; i++) {
181  use_config[i] = want_config[i];
182  }
183 }
184 
190 void client_init() {
191  // Initialize experience tables.
192  exp_table = NULL;
193  exp_table_max = 0;
194 
196 
197  // Clear out variables related to image face caching.
199  face_info.want_faceset = NULL;
200 
201  for (int i = 0; i < MAX_FACE_SETS; i++) {
202  face_info.facesets[i].prefix = NULL;
203  face_info.facesets[i].fullname = NULL;
204  face_info.facesets[i].fallback = 0;
205  face_info.facesets[i].size = NULL;
206  face_info.facesets[i].extension = NULL;
207  face_info.facesets[i].comment = NULL;
208  }
209 
210  // Allocate memory for player-related objects.
211  cpl.ob = player_item();
212  cpl.below = map_item();
213 
215 
216  for (int i = 0; i < MAX_SKILL; i++) {
217  skill_names[i] = NULL;
218  last_used_skills[i] = -1;
219  }
220 
221  init_commands();
222  init_config();
223  init_paths();
224 
225  // Paths must be set before metaserver can be initialized.
226  ms_init();
227 }
228 
233  int i;
234 
235  for (i = 0; i < MAX_SKILL; i++) {
236  cpl.stats.skill_exp[i] = 0;
237  cpl.stats.skill_level[i] = 0;
238  }
239 }
240 
245 void client_reset() {
246  int i;
247 
248  /* Keep old checksum to compare it with the next server's checksum. */
250 
251  for (i = 0; i < MAX_FACE_SETS; i++) {
254  face_info.facesets[i].fallback = 0;
258  }
259 
261 
262  for (i = 0; i < MAX_SKILL; i++) {
264  }
265 
266  if (motd) {
268  }
269 
270  if (news) {
272  }
273 
274  if (rules) {
276  }
277 
278  if (races) {
280  num_races = 0;
281  used_races = 0;
282  races = NULL;
283  }
284 
285  if (classes) {
287  num_classes = 0;
288  used_classes = 0;
289  classes = NULL;
290  }
291 
292  serverloginmethod = 0;
293 }
Player_Struct::stats
Stats stats
Player stats.
Definition: client.h:349
Stat_struct::skill_level
gint16 skill_level[MAX_SKILL]
Level of known skills.
Definition: client.h:289
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:400
news
char * news
Definition: client.h:547
num_classes
int num_classes
Definition: client.h:549
LOG_WARNING
@ LOG_WARNING
Warning that something might not work.
Definition: client.h:437
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:290
metaserver.h
Player_Struct::title
char title[MAX_BUF]
Title of character.
Definition: client.h:351
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:549
CFG_LT_PIXEL
#define CFG_LT_PIXEL
Definition: client.h:230
ClientSocket::fd
GSocketConnection * fd
Definition: client.h:124
Face_Information_struct::num_images
gint16 num_images
Definition: client.h:410
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:245
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:549
init_commands
void init_commands()
Fills some internal arrays.
Definition: p_cmd.c:407
FREE_AND_CLEAR
#define FREE_AND_CLEAR(xyz)
Definition: init.c:45
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:398
Stat_struct::maxgrace
gint16 maxgrace
Maximum spell points.
Definition: client.h:267
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:340
Player_Struct::input_text
char input_text[MAX_BUF]
keys typed (for long commands)
Definition: client.h:343
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:411
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:43
FaceSets_struct::prefix
char * prefix
Definition: client.h:396
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:82
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:418
stat_min
int stat_min
Definition: client.h:550
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:219
Player_Struct::container
item * container
open container
Definition: client.h:339
SendAddMe
void SendAddMe(ClientSocket csock)
Definition: init.c:78
Face_Information_struct::facesets
FaceSets facesets[MAX_FACE_SETS]
Definition: client.h:422
Stat_struct::speed
gint32 speed
Speed (is displayed as a float)
Definition: client.h:275
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:239
CONFIG_SPLITINFO
#define CONFIG_SPLITINFO
Definition: client.h:196
Stat_struct::maxhp
gint16 maxhp
Maximum hit points.
Definition: client.h:263
Player_Struct::last_command
char last_command[MAX_BUF]
Last command entered.
Definition: client.h:342
Player_Struct::ranges
item * ranges[range_size]
Object that is used for that.
Definition: client.h:344
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:336
mapdata_free
void mapdata_free(void)
Deallocate map data.
Definition: mapdata.c:642
Player_Struct::range
char range[MAX_BUF]
Range attack chosen.
Definition: client.h:352
Stat_struct::maxsp
gint16 maxsp
Maximum spell points.
Definition: client.h:265
CONFIG_MAPSCROLL
#define CONFIG_MAPSCROLL
Use bitmap operations for map scrolling.
Definition: client.h:211
stat_maximum
int stat_maximum
Definition: client.h:550
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:409
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:253
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:383
FaceSets_struct::extension
char * extension
Definition: client.h:399
Player_Struct::magicmap
guint8 * magicmap
Magic map data.
Definition: client.h:363
init_config
static void init_config()
Initialize client settings with built-in defaults.
Definition: init.c:143
CONFIG_SPLASH
#define CONFIG_SPLASH
Definition: client.h:209
SendVersion
void SendVersion(ClientSocket csock)
Definition: init.c:73
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:99
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:364
Face_Information_struct::old_bmaps_checksum
guint32 old_bmaps_checksum
Definition: client.h:411
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:397
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:232
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:337
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:251
Face_Information_struct::faceset
guint8 faceset
Definition: client.h:408
rules
char * rules
Definition: client.h:547
motd
char * motd
Definition: client.h:548
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:419
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:418
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:190
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:43
skill_names
char * skill_names[MAX_SKILL]
Definition: client.c:50
LOG_DEBUG
@ LOG_DEBUG
Useful debugging information.
Definition: client.h:435
VersionCmd
void VersionCmd(char *data, int len)
Definition: init.c:47
client.h
FaceSets_struct::fallback
guint8 fallback
Definition: client.h:395
classes
Race_Class_Info * classes
Definition: client.h:610
CONFIG_DISPLAYMODE
#define CONFIG_DISPLAYMODE
Definition: client.h:192