1 |
/* |
2 |
* static char *rcsid_c_party_c = |
3 |
* "$Id: c_party.c,v 1.24 2005/12/17 19:45:00 ryo_saeba Exp $"; |
4 |
*/ |
5 |
|
6 |
/* |
7 |
CrossFire, A Multiplayer game for X-windows |
8 |
|
9 |
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
10 |
Copyright (C) 1992 Frank Tore Johansen |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation; either version 2 of the License, or |
15 |
(at your option) any later version. |
16 |
|
17 |
This program is distributed in the hope that it will be useful, |
18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
GNU General Public License for more details. |
21 |
|
22 |
You should have received a copy of the GNU General Public License |
23 |
along with this program; if not, write to the Free Software |
24 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 |
|
26 |
The authors can be reached via e-mail at crossfire-devel@real-time.com |
27 |
*/ |
28 |
|
29 |
#include <global.h> |
30 |
#ifndef __CEXTRACT__ |
31 |
#include <sproto.h> |
32 |
#endif |
33 |
#include <spells.h> |
34 |
|
35 |
static partylist * firstparty=NULL; /* Keeps track of first party in list */ |
36 |
static partylist * lastparty=NULL; /*Keeps track of last party in list */ |
37 |
|
38 |
partylist* get_firstparty(void) |
39 |
{ |
40 |
return firstparty; |
41 |
} |
42 |
|
43 |
void remove_party(partylist *target_party); |
44 |
|
45 |
/* Forms the party struct for a party called 'params'. it is the responsibility |
46 |
* of the caller to ensure that the name is unique, and that it is placed in the |
47 |
* main party list correctly */ |
48 |
static partylist *form_party(object *op, const char *params) { |
49 |
|
50 |
partylist * newparty; |
51 |
|
52 |
newparty = (partylist *)malloc(sizeof(partylist)); |
53 |
newparty->partyname = strdup_local(params); |
54 |
newparty->total_exp=0; |
55 |
newparty->kills=0; |
56 |
newparty->passwd[0] = '\0'; |
57 |
newparty->next = NULL; |
58 |
newparty->partyleader = strdup_local(op->name); |
59 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
60 |
"You have formed party: %s",newparty->partyname); |
61 |
op->contr->party=newparty; |
62 |
return newparty; |
63 |
} |
64 |
|
65 |
void remove_party(partylist *target_party) { |
66 |
partylist *tmpparty; |
67 |
partylist *previousparty; |
68 |
partylist *nextparty; |
69 |
player *pl; |
70 |
|
71 |
if (firstparty==NULL) { |
72 |
LOG(llevError, "remove_party(): I was asked to remove party %s, but no parties are defined", |
73 |
target_party->partyname); |
74 |
return; |
75 |
} |
76 |
for (pl=first_player;pl!=NULL;pl=pl->next) |
77 |
if (pl->party==target_party) pl->party=NULL; |
78 |
|
79 |
/* special case-ism for parties at the beginning and end of the list */ |
80 |
if (target_party==firstparty) { |
81 |
firstparty=firstparty->next; |
82 |
if (target_party->partyleader) free(target_party->partyleader); |
83 |
if (target_party->partyname) free(target_party->partyname); |
84 |
free(target_party); |
85 |
return; |
86 |
} |
87 |
else if (target_party == lastparty) { |
88 |
for (tmpparty=firstparty;tmpparty->next!=NULL;tmpparty=tmpparty->next) { |
89 |
if (tmpparty->next==target_party) { |
90 |
lastparty=tmpparty; |
91 |
if (target_party->partyleader) free(target_party->partyleader); |
92 |
if (target_party->partyname) free(target_party->partyname); |
93 |
free(target_party); |
94 |
lastparty->next=NULL; |
95 |
return; |
96 |
} |
97 |
} |
98 |
} |
99 |
for (tmpparty=firstparty;tmpparty->next!=NULL;tmpparty=tmpparty->next) |
100 |
if (tmpparty->next == target_party) { |
101 |
previousparty=tmpparty; |
102 |
nextparty=tmpparty->next->next; |
103 |
/* this should be safe, because we already dealt with the lastparty case */ |
104 |
|
105 |
previousparty->next=nextparty; |
106 |
if (target_party->partyleader) free(target_party->partyleader); |
107 |
if (target_party->partyname) free(target_party->partyname); |
108 |
free(target_party); |
109 |
return; |
110 |
} |
111 |
} |
112 |
|
113 |
/* Remove unused parties, this could be made to scale a lot better. */ |
114 |
void obsolete_parties(void) { |
115 |
int player_count; |
116 |
player *pl; |
117 |
partylist *party; |
118 |
partylist* next = NULL; |
119 |
|
120 |
if (!firstparty) return; /* we can't obsolete parties if there aren't any */ |
121 |
for (party=firstparty; party!=NULL; party=next) { |
122 |
next = party->next; |
123 |
player_count=0; |
124 |
for (pl=first_player;pl!=NULL;pl=pl->next) |
125 |
if (pl->party==party) player_count++; |
126 |
if (player_count == 0) |
127 |
remove_party(party); |
128 |
} |
129 |
} |
130 |
|
131 |
#ifdef PARTY_KILL_LOG |
132 |
void add_kill_to_party(partylist *party, char *killer, char *dead, long exp) |
133 |
{ |
134 |
int i,pos; |
135 |
|
136 |
if(party==NULL) return; |
137 |
if(party->kills>=PARTY_KILL_LOG) |
138 |
{ |
139 |
pos=PARTY_KILL_LOG-1; |
140 |
for(i=0;i<PARTY_KILL_LOG-1;i++) |
141 |
memcpy(&(party->party_kills[i]),&(party->party_kills[i+1]), |
142 |
sizeof(party->party_kills[0])); |
143 |
} |
144 |
else |
145 |
pos=party->kills; |
146 |
party->kills++; |
147 |
party->total_exp+=exp; |
148 |
party->party_kills[pos].exp=exp; |
149 |
strncpy(party->party_kills[pos].killer,killer,MAX_NAME); |
150 |
strncpy(party->party_kills[pos].dead,dead,MAX_NAME); |
151 |
party->party_kills[pos].killer[MAX_NAME]=0; |
152 |
party->party_kills[pos].dead[MAX_NAME]=0; |
153 |
} |
154 |
#endif |
155 |
|
156 |
int confirm_party_password(object *op) { |
157 |
partylist *tmppartylist; |
158 |
for(tmppartylist = firstparty; tmppartylist != NULL;tmppartylist = tmppartylist->next) { |
159 |
if(!strcmp(op->contr->party_to_join->partyname, tmppartylist->partyname)) { |
160 |
if(strcmp(op->contr->write_buf+1,tmppartylist->passwd) == 0) |
161 |
return 0; |
162 |
else |
163 |
return 1; |
164 |
} |
165 |
} |
166 |
return 1; |
167 |
} |
168 |
|
169 |
void receive_party_password(object *op, char k) { |
170 |
|
171 |
if(confirm_party_password(op) == 0) { |
172 |
partylist* joined_party = op->contr->party_to_join; |
173 |
char buf[ MAX_BUF ]; |
174 |
op->contr->party = op->contr->party_to_join; |
175 |
op->contr->party_to_join = NULL; |
176 |
new_draw_info_format(NDI_UNIQUE, 0,op, |
177 |
"You have joined party: %s\n",joined_party->partyname); |
178 |
snprintf( buf, MAX_BUF, "%s joins party %s", op->name, joined_party->partyname ); |
179 |
send_party_message( op, buf ); |
180 |
op->contr->state = ST_PLAYING; |
181 |
return; |
182 |
} |
183 |
else { |
184 |
new_draw_info(NDI_UNIQUE, 0,op,"You entered the wrong password"); |
185 |
op->contr->party_to_join = NULL; |
186 |
op->contr->state = ST_PLAYING; |
187 |
return; |
188 |
} |
189 |
} |
190 |
|
191 |
void send_party_message(object *op,char *msg) |
192 |
{ |
193 |
player *pl; |
194 |
for(pl=first_player;pl!=NULL;pl=pl->next) |
195 |
if(pl->ob->contr->party==op->contr->party && pl->ob!=op) |
196 |
new_draw_info(NDI_WHITE, 0, pl->ob, msg); |
197 |
} |
198 |
|
199 |
int command_gsay(object *op, char *params) |
200 |
{ |
201 |
char party_params[MAX_BUF]; |
202 |
|
203 |
if (!params) return 0; |
204 |
strcpy(party_params, "say "); |
205 |
strcat(party_params,params); |
206 |
command_party(op,party_params); |
207 |
return 0; |
208 |
} |
209 |
|
210 |
|
211 |
int command_party (object *op, char *params) |
212 |
{ |
213 |
char buf[MAX_BUF]; |
214 |
partylist *tmpparty, *oldparty; /* For iterating over linked list */ |
215 |
char * currentparty; /* For iterating over linked list */ |
216 |
|
217 |
if(params == NULL) { |
218 |
if(op->contr->party==NULL) { |
219 |
new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party."); |
220 |
new_draw_info(NDI_UNIQUE, 0,op,"For help try: party help"); |
221 |
} |
222 |
else { |
223 |
currentparty = op->contr->party->partyname; |
224 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
225 |
"You are a member of party %s.", currentparty); |
226 |
} |
227 |
return 1; |
228 |
} |
229 |
if(strcmp(params, "help")==0) { |
230 |
new_draw_info(NDI_UNIQUE, 0,op,"To form a party type: party form <partyname>"); |
231 |
new_draw_info(NDI_UNIQUE, 0,op,"To join a party type: party join <partyname>"); |
232 |
new_draw_info(NDI_UNIQUE, 0,op,"If the party has a passwd, it will you prompt you for it."); |
233 |
new_draw_info(NDI_UNIQUE, 0,op,"For a list of current parties type: party list"); |
234 |
new_draw_info(NDI_UNIQUE, 0,op,"To leave a party type: party leave"); |
235 |
new_draw_info(NDI_UNIQUE, 0,op,"To change a passwd for a party type: party passwd <password>"); |
236 |
new_draw_info(NDI_UNIQUE, 0,op,"There is an 8 character max"); |
237 |
new_draw_info(NDI_UNIQUE, 0,op,"To talk to party members type: party say <msg>"); |
238 |
new_draw_info(NDI_UNIQUE, 0,op,"To see who is in your party: party who"); |
239 |
#ifdef PARTY_KILL_LOG |
240 |
new_draw_info(NDI_UNIQUE, 0,op,"To see what you've killed, type: party kills"); |
241 |
#endif |
242 |
return 1; |
243 |
} |
244 |
#ifdef PARTY_KILL_LOG |
245 |
if(!strncmp(params, "kills",5)) |
246 |
{ |
247 |
int i,max; |
248 |
char chr; |
249 |
char buffer[80]; |
250 |
float exp; |
251 |
|
252 |
if(op->contr->party==NULL) |
253 |
{ |
254 |
new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party."); |
255 |
return 1; |
256 |
} |
257 |
tmpparty = op->contr->party; |
258 |
if(!tmpparty->kills) |
259 |
{ |
260 |
new_draw_info(NDI_UNIQUE,0,op,"You haven't killed anything yet."); |
261 |
return 1; |
262 |
} |
263 |
max=tmpparty->kills-1; |
264 |
if(max>PARTY_KILL_LOG-1) max=PARTY_KILL_LOG-1; |
265 |
new_draw_info(NDI_UNIQUE,0,op, |
266 |
"Killed | Killer| Exp"); |
267 |
new_draw_info(NDI_UNIQUE,0,op, |
268 |
"----------------+----------------+--------"); |
269 |
for(i=0;i<=max;i++) |
270 |
{ |
271 |
exp=tmpparty->party_kills[i].exp; |
272 |
chr=' '; |
273 |
if(exp>1000000) { exp/=1000000; chr='M'; } |
274 |
else |
275 |
if(exp>1000) { exp/=1000; chr='k'; } |
276 |
sprintf(buffer,"%16s|%16s|%6.1f%c", |
277 |
tmpparty->party_kills[i].dead, |
278 |
tmpparty->party_kills[i].killer,exp,chr); |
279 |
new_draw_info(NDI_UNIQUE,0,op,buffer); |
280 |
} |
281 |
exp=tmpparty->total_exp; |
282 |
chr=' '; |
283 |
if(exp>1000000) { exp/=1000000; chr='M'; } |
284 |
else |
285 |
if(exp>1000) { exp/=1000; chr='k'; } |
286 |
new_draw_info(NDI_UNIQUE,0,op, |
287 |
"----------------+----------------+--------"); |
288 |
sprintf(buffer,"Totals: %d kills, %.1f%c exp",tmpparty->kills, |
289 |
exp,chr); |
290 |
new_draw_info(NDI_UNIQUE,0,op,buffer); |
291 |
return 1; |
292 |
} |
293 |
#endif /* PARTY_KILL_LOG */ |
294 |
if(strncmp(params, "say ", 4)==0) |
295 |
{ |
296 |
if(op->contr->party==NULL) |
297 |
{ |
298 |
new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party."); |
299 |
return 1; |
300 |
} |
301 |
params += 4; |
302 |
currentparty = op->contr->party->partyname; |
303 |
snprintf(buf,MAX_BUF-1, "[%s] %s says: %s", currentparty, op->name, params); |
304 |
send_party_message(op,buf); |
305 |
new_draw_info_format(NDI_WHITE, 0,op,"[%s] You say: %s", currentparty, params); |
306 |
return 1; |
307 |
} |
308 |
|
309 |
if(strncmp(params, "form ",5) == 0) { |
310 |
int player_count; |
311 |
player *pl; |
312 |
|
313 |
params += 5; |
314 |
if (op->contr->party) oldparty = op->contr->party; |
315 |
else oldparty = NULL; |
316 |
|
317 |
if (firstparty) { |
318 |
for (tmpparty = firstparty; tmpparty != NULL;tmpparty = tmpparty->next) { |
319 |
if (!strcmp(tmpparty->partyname, params)) { |
320 |
new_draw_info_format(NDI_UNIQUE, 0,op, |
321 |
"The party %s already exists, pick another name",params); |
322 |
return 1; |
323 |
} |
324 |
} |
325 |
lastparty->next=form_party(op, params); |
326 |
lastparty = lastparty->next; |
327 |
} |
328 |
else { |
329 |
firstparty=form_party(op, params); |
330 |
lastparty=firstparty; |
331 |
} |
332 |
/* |
333 |
* The player might have previously been a member of a party, if so, he will be leaving |
334 |
* it, so check if there are any other members and if not, delete the party |
335 |
*/ |
336 |
player_count=0; |
337 |
if (oldparty) { |
338 |
for (pl=first_player;pl->next!=NULL;pl=pl->next) { |
339 |
if (pl->party==oldparty) player_count++; |
340 |
} |
341 |
if (player_count == 0) |
342 |
remove_party(oldparty); |
343 |
} |
344 |
return 0; |
345 |
} /* form */ |
346 |
|
347 |
if(strcmp(params, "leave")==0) { |
348 |
if(op->contr->party==NULL) |
349 |
{ |
350 |
new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party."); |
351 |
return 1; |
352 |
} |
353 |
currentparty = op->contr->party->partyname; |
354 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
355 |
"You leave party %s.",currentparty); |
356 |
sprintf(buf,"%s leaves party %s.",op->name,currentparty); |
357 |
send_party_message(op,buf); |
358 |
op->contr->party=NULL; |
359 |
return 1; |
360 |
} |
361 |
if(strcmp(params, "who")==0) { |
362 |
player *pl; |
363 |
tmpparty = op->contr->party; |
364 |
if(op->contr->party==NULL) { |
365 |
new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party."); |
366 |
return 1; |
367 |
} |
368 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
369 |
"Members of party: %s.", op->contr->party->partyname); |
370 |
for(pl=first_player;pl!=NULL;pl=pl->next) |
371 |
if(pl->ob->contr->party==op->contr->party) { |
372 |
if (settings.set_title == TRUE) { |
373 |
if(pl->ob->contr->own_title[0]!='\0') |
374 |
sprintf(buf,"%3d %s the %s", |
375 |
pl->ob->level,pl->ob->name,pl->ob->contr->own_title); |
376 |
else |
377 |
sprintf(buf,"%3d %s the %s", |
378 |
pl->ob->level,pl->ob->name,pl->ob->contr->title); |
379 |
} else |
380 |
sprintf(buf,"%3d %s the %s", |
381 |
pl->ob->level,pl->ob->name,pl->ob->contr->title); |
382 |
new_draw_info(NDI_UNIQUE, 0,op,buf); |
383 |
} |
384 |
return 1; |
385 |
} /* leave */ |
386 |
|
387 |
if(strncmp(params, "passwd ", 7) == 0) { |
388 |
partylist *tmplist; |
389 |
|
390 |
params += 7; |
391 |
|
392 |
if(op->contr->party == NULL) { |
393 |
new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of a party"); |
394 |
return 1; |
395 |
} |
396 |
|
397 |
if(strlen(params) > 8) { |
398 |
new_draw_info(NDI_UNIQUE, 0,op,"The password must not exceed 8 characters"); |
399 |
return 1; |
400 |
} |
401 |
|
402 |
tmplist = firstparty; |
403 |
while(tmplist != NULL) { |
404 |
if(tmplist == op->contr->party) { |
405 |
strcpy(tmplist->passwd,params); |
406 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
407 |
"The password for party %s is %s", tmplist->partyname,tmplist->passwd); |
408 |
snprintf( buf, MAX_BUF, "Password for party %s is now %s, changed by %s", |
409 |
tmplist->partyname, tmplist->passwd, op->name ); |
410 |
send_party_message(op,buf); |
411 |
return 0; |
412 |
} |
413 |
tmplist = tmplist->next; |
414 |
} |
415 |
return 0; |
416 |
} /* passwd */ |
417 |
|
418 |
if(strcmp(params, "list") == 0) { |
419 |
partylist * tmplist; |
420 |
|
421 |
tmplist = firstparty; |
422 |
|
423 |
if(firstparty == NULL) { |
424 |
new_draw_info(NDI_UNIQUE, 0,op,"There are no parties active right now"); |
425 |
return 1; |
426 |
} |
427 |
|
428 |
new_draw_info(NDI_UNIQUE, 0,op,"Party name Leader"); |
429 |
new_draw_info(NDI_UNIQUE, 0,op,"---------- ------"); |
430 |
|
431 |
while(tmplist != NULL) { |
432 |
new_draw_info_format(NDI_UNIQUE, 0,op, |
433 |
"%-32s %s",tmplist->partyname |
434 |
,tmplist->partyleader); |
435 |
tmplist = tmplist->next; |
436 |
} |
437 |
return 0; |
438 |
} /* list */ |
439 |
|
440 |
if(strncmp(params,"join ",5) == 0) { |
441 |
|
442 |
params += 5; |
443 |
|
444 |
/* Can't join a party cause non exist */ |
445 |
if(firstparty == NULL) { |
446 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
447 |
"Party: %s does not exist. You must form it first",params); |
448 |
return 1; |
449 |
} |
450 |
|
451 |
/* Special case if thier is only one party */ |
452 |
if(firstparty->next == NULL) { |
453 |
if(strcmp(firstparty->partyname,params) != 0) { |
454 |
new_draw_info_format(NDI_UNIQUE, 0,op, |
455 |
"Party: %s does not exist. You must form it first",params); |
456 |
return 1; |
457 |
} |
458 |
else { |
459 |
if(op->contr->party == firstparty) { |
460 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
461 |
"You are already in party: %s" |
462 |
,firstparty->partyname); |
463 |
return 1; |
464 |
} |
465 |
/* found party player wants to join */ |
466 |
if(firstparty->passwd[0] == '\0') { |
467 |
op->contr->party = firstparty; |
468 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
469 |
"You have joined party: %s",firstparty->partyname); |
470 |
snprintf( buf, MAX_BUF, "%s joins party %s", op->name, firstparty->partyname ); |
471 |
send_party_message( op, buf ); |
472 |
return 0; |
473 |
} |
474 |
else { |
475 |
get_party_password(op,firstparty); |
476 |
return 0; |
477 |
} |
478 |
} |
479 |
} |
480 |
|
481 |
tmpparty = firstparty; |
482 |
while(tmpparty != NULL) { |
483 |
if(strcmp(tmpparty->partyname,params) == 0) { |
484 |
if(op->contr->party == tmpparty) { |
485 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
486 |
"You are already a member of party: %s" |
487 |
,tmpparty->partyname); |
488 |
return 1; |
489 |
} |
490 |
else { |
491 |
if(tmpparty->passwd[0] == '\0') { |
492 |
new_draw_info_format(NDI_UNIQUE, 0, op, |
493 |
"You have joined party: %s",tmpparty->partyname); |
494 |
op->contr->party = tmpparty; |
495 |
snprintf( buf, MAX_BUF, "%s joins party %s", op->name, tmpparty->partyname ); |
496 |
send_party_message( op, buf ); |
497 |
return 0; |
498 |
} |
499 |
else { |
500 |
get_party_password(op, tmpparty); |
501 |
return 0; |
502 |
} |
503 |
} |
504 |
} |
505 |
else |
506 |
tmpparty = tmpparty->next; |
507 |
} |
508 |
|
509 |
new_draw_info_format(NDI_UNIQUE, 0,op, |
510 |
"Party %s does not exist. You must form it first.",params); |
511 |
return 1; |
512 |
} /* join */ |
513 |
|
514 |
new_draw_info(NDI_UNIQUE, 0,op,"To form a party type: party form <partyname>"); |
515 |
new_draw_info(NDI_UNIQUE, 0,op,"To join a party type: party join <partyname>"); |
516 |
new_draw_info(NDI_UNIQUE, 0,op,"If the party has a passwd, it will you prompt you for it."); |
517 |
new_draw_info(NDI_UNIQUE, 0,op,"For a list of current parties type: party list"); |
518 |
new_draw_info(NDI_UNIQUE, 0,op,"To leave a party type: party leave"); |
519 |
new_draw_info(NDI_UNIQUE, 0,op,"To change a passwd for a party type: party passwd <password>"); |
520 |
new_draw_info(NDI_UNIQUE, 0,op,"There is an 8 character max"); |
521 |
new_draw_info(NDI_UNIQUE, 0,op,"To talk to party members type: party say <msg>"); |
522 |
new_draw_info(NDI_UNIQUE, 0,op,"To see who is in your party: party who"); |
523 |
#ifdef PARTY_KILL_LOG |
524 |
new_draw_info(NDI_UNIQUE, 0,op,"To see what you've killed, type: party kills"); |
525 |
#endif |
526 |
return 1; |
527 |
} |