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