File: init.c

package info (click to toggle)
crossfire-client 1.9.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,952 kB
  • ctags: 2,747
  • sloc: ansic: 29,140; sh: 5,795; makefile: 226; perl: 48
file content (291 lines) | stat: -rw-r--r-- 9,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const char *rcsid_common_init_c =
    "$Id: init.c,v 1.31 2006/05/15 05:57:43 mwedel Exp $";
/*
    Crossfire client, a client program for the crossfire program.

    Copyright (C) 2001 Mark Wedel & Crossfire Development Team

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    The author can be reached via e-mail to crossfire-devel@real-time.com
*/

/* This handles the initialization of the client.  This includes making
 * the I_IMAGE and I_ARCH commands.
 */

#include <client.h>
#include "p_cmd.h" /* init_commands() */

/* XXX Does the x11 client *use* these? */

/* Makes the load/save code trivial - basically, the
 * entries here match the same numbers as the CONFIG_ values defined
 * in common/client.h - this means the load and save just does
 * something like a fprintf(outifle, "%s: %d", config_names[i],
 *			    want_config[i]);
 */
const char *const config_names[CONFIG_NUMS] = {
NULL, "download_all_images", "echo_bindings",
"fasttcpsend", "command_window", "cacheimages", "fog_of_war", "iconscale",
"mapscale", "popups", "displaymode", "showicon", "tooltips", "sound", "splitinfo",
"split", "show_grid", "lighting", "trim_info_window",
"map_width", "map_height", "foodbeep", "darkness", "port",
"grad_color_bars", "resistances", "smoothing", "nosplash",
"auto_apply_container", "mapscroll", "sign_popups", "message_timestamping"
};

sint16 want_config[CONFIG_NUMS], use_config[CONFIG_NUMS];

#define TEST_FREE_AND_CLEAR(xyz) {if (xyz) { free(xyz); xyz=NULL; } }

void VersionCmd(char *data, int len)
{
    char *cp;

    csocket.cs_version = atoi(data);
    /* set sc_version in case it is an old server supplying only one version */
    csocket.sc_version = csocket.cs_version;
    if (csocket.cs_version != VERSION_CS) {
        LOG(LOG_WARNING,"common::VersionCmd","Differing C->S version numbers (%d,%d)",
            VERSION_CS,csocket.cs_version);
/*	exit(1);*/
    }
    cp = strchr(data,' ');
    if (!cp) return;
    csocket.sc_version = atoi(cp);
    if (csocket.sc_version != VERSION_SC) {
    LOG(LOG_WARNING,"common::VersionCmd","Differing S->C version numbers (%d,%d)",
        VERSION_SC,csocket.sc_version);
    }
    cp = strchr(cp+1, ' ');
    if (cp)
	LOG(LOG_INFO,"common::VersionCmd","Playing on server type %s", cp);
}

void SendVersion(ClientSocket csock)
{
    cs_print_string(csock.fd,
		    "version %d %d %s", VERSION_CS, VERSION_SC, VERSION_INFO);
}


void SendAddMe(ClientSocket csock)
{
    cs_print_string(csock.fd, "addme");
}


void SendSetFaceMode(ClientSocket csock,int mode)
{
    cs_print_string(csock.fd, "setfacemode %d", mode);
}


void init_client_vars()
{
    int i;

    /* I think environemental variables should be more important than
     * compiled in defaults, so these probably should be reversed. 
     */
    client_libdir=getenv("CFCLIENT_LIBDIR");
#ifdef CLIENT_LIBDIR
    if (client_libdir==NULL)
	client_libdir=CLIENT_LIBDIR;
#endif

    cpl.count_left = 0;
    cpl.container = NULL;
    memset(&cpl.stats,0, sizeof(Stats));
    cpl.stats.maxsp=1;	/* avoid div by 0 errors */
    cpl.stats.maxhp=1;	/* ditto */
    cpl.stats.maxgrace=1;	/* ditto */
    /* ditto - displayed weapon speed is weapon speed/speed */
    cpl.stats.speed=1;
    cpl.input_text[0]='\0';
    cpl.title[0] = '\0';
    cpl.range[0] = '\0';
    cpl.last_command[0] = '\0';

    for (i=0; i<range_size; i++)
	cpl.ranges[i]=NULL;

    for (i=0; i<MAX_SKILL; i++) {
	cpl.stats.skill_exp[i]=0;
	cpl.stats.skill_level[i] = 0;
	skill_names[i] = NULL;
    }

    cpl.ob = player_item();
    cpl.below = map_item();
    cpl.magicmap=NULL;
    cpl.showmagic=0;
    

    csocket.command_sent=0;
    csocket.command_received=0;
    csocket.command_time=0;

    face_info.faceset = 0;
    face_info.num_images = 0;
    face_info.bmaps_checksum = 0;
    face_info.old_bmaps_checksum = 0;
    face_info.want_faceset = NULL;
    face_info.cache_hits=0;
    face_info.cache_misses=0;
    face_info.have_faceset_info=0;
    for (i=0; i<MAX_FACE_SETS; i++) {
	face_info.facesets[i].prefix = NULL;
	face_info.facesets[i].fullname = NULL;
	face_info.facesets[i].fallback = 0;
	face_info.facesets[i].size = NULL;
	face_info.facesets[i].extension = NULL;
	face_info.facesets[i].comment = NULL;
    }
    /* Makes just as much sense to initialize the arrays
     * where they are declared, but I did this so I could 
     * keep track of everything as I was updating
     * the code.  Plus, the performance difference is virtually
     * nothing.
     */
    want_config[CONFIG_DOWNLOAD] = FALSE;
    want_config[CONFIG_ECHO] = FALSE;
    want_config[CONFIG_FASTTCP] = TRUE;
    want_config[CONFIG_CWINDOW] = COMMAND_WINDOW;
    want_config[CONFIG_CACHE] = FALSE;
    want_config[CONFIG_FOGWAR] = TRUE;
    want_config[CONFIG_ICONSCALE] = 100;
    want_config[CONFIG_MAPSCALE] = 100;
    want_config[CONFIG_POPUPS] = TRUE;
    want_config[CONFIG_DISPLAYMODE] = CFG_DM_PIXMAP;
    want_config[CONFIG_SHOWICON] = FALSE;
    want_config[CONFIG_TOOLTIPS] = TRUE;
    want_config[CONFIG_SOUND] = TRUE;
    want_config[CONFIG_SPLITINFO] = FALSE;
    want_config[CONFIG_SPLITWIN] = FALSE;
    want_config[CONFIG_SHOWGRID] = FALSE;
    want_config[CONFIG_LIGHTING] = CFG_LT_TILE;
    want_config[CONFIG_TRIMINFO] = FALSE;
    want_config[CONFIG_MAPWIDTH] = 11;
    want_config[CONFIG_MAPHEIGHT] = 11;
    want_config[CONFIG_FOODBEEP] = FALSE;
    want_config[CONFIG_DARKNESS] = TRUE;
    want_config[CONFIG_PORT] = EPORT;
    want_config[CONFIG_GRAD_COLOR] = FALSE;
    want_config[CONFIG_RESISTS] = 0;
    want_config[CONFIG_RESISTS] = 0;
    want_config[CONFIG_SMOOTH] = 0;
    want_config[CONFIG_SPLASH] = TRUE;
    want_config[CONFIG_APPLY_CONTAINER] = TRUE;	    /* Same behavior before this option was put in */
    want_config[CONFIG_MAPSCROLL] = TRUE;
    want_config[CONFIG_SIGNPOPUP] = TRUE;
    want_config[CONFIG_TIMESTAMP] = FALSE;
    for (i=0; i<CONFIG_NUMS; i++) 
	use_config[i] = want_config[i];

#ifdef WIN32
    /* If HOME is not set, let's set it to . to avoid things like (null)/.crossfire paths */
    if ( !getenv( "HOME" ) )
        {
        if ( getenv( "APPDATA" ) )
            {
            char env[ MAX_BUF ];
            _snprintf( env, MAX_BUF, "HOME=%s", getenv( "APPDATA" ) );
            LOG( LOG_INFO, "common::inic.c", "init_client_vars: HOME set to %APPDATA%.\n" );
            putenv( env );
            }
        else
            {
            LOG( LOG_INFO, "common::init.c", "init_client_vars: HOME not set, setting it to .\n" );
            putenv( "HOME=." );
            }
        }
#endif
    init_commands(); /* pcmd.c */

    /* Any reasonable seed really works */
    srandom(time(NULL));
}

/* This is basically called each time a new player logs
 * on - reset all the player data
 */
void reset_player_data()
{
    int i;

    for (i=0; i<MAX_SKILL; i++) {
	cpl.stats.skill_exp[i]=0;
	cpl.stats.skill_level[i] = 0;
    }
}

/* This is used to clear values between connections to different
 * servers.  This needs to be called after init_client_vars has
 * been called because it does not re-allocated some values.
 */

void reset_client_vars()
{
    int i;

    cpl.count_left = 0;
    cpl.container = NULL;
    memset(&cpl.stats,0, sizeof(Stats));
    cpl.stats.maxsp=1;	/* avoid div by 0 errors */
    cpl.stats.maxhp=1;	/* ditto */
    cpl.stats.maxgrace=1;	/* ditto */
    /* ditto - displayed weapon speed is weapon speed/speed */
    cpl.stats.speed=1;
    cpl.input_text[0]='\0';
    cpl.title[0] = '\0';
    cpl.range[0] = '\0';
    cpl.last_command[0] = '\0';

    for (i=0; i<range_size; i++)
	cpl.ranges[i]=NULL;

    cpl.magicmap=NULL;
    cpl.showmagic=0;

    csocket.command_sent=0;
    csocket.command_received=0;
    csocket.command_time=0;

    face_info.faceset = 0;
    face_info.num_images = 0;
    /* Preserve the old one - this can be used to see if the next
     * server has the same name -> number mapping so that we don't
     * need to rebuild all the images.
     */
    face_info.old_bmaps_checksum = face_info.bmaps_checksum;
    face_info.bmaps_checksum = 0;
    face_info.cache_hits=0;
    face_info.cache_misses=0;
    face_info.have_faceset_info=0;
    for (i=0; i<MAX_FACE_SETS; i++) {
	TEST_FREE_AND_CLEAR(face_info.facesets[i].prefix);
	TEST_FREE_AND_CLEAR(face_info.facesets[i].fullname);
	face_info.facesets[i].fallback = 0;
	TEST_FREE_AND_CLEAR(face_info.facesets[i].size);
	TEST_FREE_AND_CLEAR(face_info.facesets[i].extension);
	TEST_FREE_AND_CLEAR(face_info.facesets[i].comment);
    }
    reset_player_data();
    for (i=0; i<MAX_SKILL; i++)
	skill_names[i] = NULL;

}