File: player.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 (279 lines) | stat: -rw-r--r-- 7,731 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
const char *rcsid_common_player_c =
    "$Id: player.c,v 1.19 2006/02/23 20:18:28 akirschbaum 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
*/

 
#include <client.h>
#include <external.h>
#include <script.h>

/* This file handles various player related functions.  This includes
 * both things that operate on the player item, cpl structure, or
 * various commands that the player issues.
 *
 *  This file does most of the handling of commands from the client to
 *  server (see commands.c for server->client)
 *
 *  does most of the work for sending messages to the server
 *   Again, most of these appear self explanatory.  Most send a bunch of
 *   commands like apply, examine, fire, run, etc.  This looks like it
 *   was done by Mark to remove the old keypress stupidity I used. 
 */

/* This translates the numeric direction id's into the actual direction
 * commands.  This lets us send the actual command (ie, 'north'), which
 * makes handling on the server side easier.
 */

const char *const directions[9] = {"stay", "north", "northeast", "east", "southeast",
		"south","southwest", "west", "northwest"};


/*
 *  Initialiazes player item, information is received from server
 */
void new_player (long tag, char *name, long weight, long face)
{
    Spell *spell, *spnext;

    cpl.ob->tag    = tag;
    cpl.ob->nrof   = 1;
    copy_name (cpl.ob->d_name, name);
    cpl.ob->weight = (float) weight / 1000;
    cpl.ob->face   = face;

    if (cpl.spelldata) {
	for (spell = cpl.spelldata; spell; spell = spnext) {
	    spnext = spell->next;
	    free(spell);
	}
	cpl.spelldata = NULL;
    }
	
}

void look_at(int x, int y)
{
    cs_print_string(csocket.fd, "lookat %d %d", x, y);
}

void client_send_apply (int tag)
{
    cs_print_string(csocket.fd, "apply %d", tag);
}

void client_send_examine (int tag)
{
    cs_print_string(csocket.fd, "examine %d", tag);

}

/* Requests nrof objects of tag get moved to loc. */
void client_send_move (int loc, int tag, int nrof)
{
    cs_print_string(csocket.fd, "move %d %d %d", loc, tag, nrof);
}



void move_player(int dir) {
    /* Should we perhaps use the real repeat count here? */
    send_command(directions[dir], -1, SC_NORMAL);
}


/* Fire & Run code.  The server handles repeating of these actions, so
 * we only need to send a run or fire command for a particular direction
 * once - we use the drun and dfire to keep track if we need to send
 * the full command.
 */
static int drun=-1, dfire=-1;

/* Fires in a specified direction.  Note that direction 0 is a valid
 * case - the fire is centered on the player.
 */

void stop_fire()
{
    if (cpl.input_state != Playing) return;
    dfire |= 0x100;
}

void clear_fire_run()
{
    if ((dfire!=-1) && (dfire & 0x100)) {
	send_command("fire_stop", -1, SC_FIRERUN);
	dfire=-1;
    }
    if ((drun!=-1) && (drun & 0x100)) {
	send_command("run_stop", -1, SC_FIRERUN);
	drun=-1;
    }
}

void clear_fire()
{
    if (dfire!=-1) {
	send_command("fire_stop", -1, SC_FIRERUN);
	dfire=-1;
    }
}

void clear_run()
{
    if (drun!=-1) {
	send_command("run_stop", -1, SC_FIRERUN);
	drun=-1;
    }
}


void fire_dir(int dir) {
    char buf[MAX_BUF];

    if (cpl.input_state != Playing) return;
    if (dir!= dfire) {
	sprintf(buf,"fire %d", dir);
	if (send_command(buf, cpl.count, SC_NORMAL)) {
	    dfire=dir;
	    cpl.count=0;
	}
    } else {
	dfire &= 0xff;	/* Mark it so that we need a stop_fire */
    }
}

void stop_run()
{
    send_command("run_stop", -1, SC_FIRERUN);
    drun |= 0x100;
}

void run_dir(int dir) {
    char buf[MAX_BUF];

    if (dir!=drun) {
	sprintf(buf,"run %d", dir);
	if (send_command(buf, -1, SC_NORMAL))
	    drun=dir;
    } else {
	drun &= 0xff;
    }
}


/* This should be used for all 'command' processing.  Other functions should
 * call this so that proper windowing will be done.
 * command is the text command, repeat is a count value, or -1 if none
 * is desired and we don't want to reset the current count.
 * must_send means we must send this command no matter what (ie, it is
 * an administrative type of command like fire_stop, and failure to send
 * it will cause definate problems
 * return 1 if command was sent, 0 if not sent.
 */

int send_command(const char *command, int repeat, int must_send) {
    static char last_command[MAX_BUF]="";

    script_monitor(command,repeat,must_send);
    if (cpl.input_state==Reply_One) {
	LOG(LOG_ERROR,"common::send_command","Wont send command '%s' - since in reply mode!",
		command);
	cpl.count=0;
	return 0;
    }

    /* Does the server understand 'ncom'? If so, special code */
    if (csocket.cs_version >= 1021) {
	int commdiff=csocket.command_sent - csocket.command_received;

	if (commdiff<0) commdiff +=256;

	/* if too many unanswered commands, not a must send, and command is
	 * the same, drop it
	 */
	if (commdiff>use_config[CONFIG_CWINDOW] && !must_send && !strcmp(command, last_command)) {
	    if (repeat!=-1) cpl.count=0;
	    return 0;
#if 0 /* Obnoxious warning message we don't need */
	    fprintf(stderr,"Wont send command %s - window oversized %d %d\n",
		    command, csocket.command_sent, csocket.command_received);
#endif
	}
	else {
	    SockList sl;
	    char buf[MAX_BUF];

	    /* Don't want to copy in administrative commands */
	    if (!must_send) strcpy(last_command, command);
	    csocket.command_sent++;
	    csocket.command_sent &= 0xff;   /* max out at 255 */

	    SockList_Init(&sl, buf);
	    SockList_AddString(&sl, "ncom ");
	    SockList_AddShort(&sl, csocket.command_sent);
	    SockList_AddInt(&sl, repeat);
	    SockList_AddString(&sl, command);
	    SockList_Send(&sl, csocket.fd);
	}
    } else {
	cs_print_string(csocket.fd, "command %d %s", repeat,command);
    }
    if (repeat!=-1) cpl.count=0;
    return 1;
}

void CompleteCmd(unsigned char *data, int len)
{
    if (len !=6) {
	LOG(LOG_ERROR,"common::CompleteCmd","Invalid length %d - ignoring", len);
	return;
    }
    csocket.command_received = GetShort_String(data);
    csocket.command_time = GetInt_String(data+2);
    script_sync(csocket.command_sent - csocket.command_received);
}



/* This does special processing on the 'take' command.  If the
 * player has a container open, we want to specifiy what object
 * to move from that since we've sorted it.  command is
 * the command as tped, cpnext is any optional params.
 */
void command_take (const char *command, const char *cpnext)
{
    /* If the player has specified optional data, or the player
     * does not have a container open, just issue the command
     * as normal
     */
    if (cpnext || cpl.container == NULL)
	send_command(command, cpl.count, 0);
    else {
	if (cpl.container->inv == NULL) 
	    draw_info("There is nothing in the container to move", NDI_BLACK);
	else
	    cs_print_string(csocket.fd,"move %d %d %d", cpl.ob->tag, 
		cpl.container->inv->tag, cpl.count);
    }
}