ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_party.C
Revision: 1.19
Committed: Mon May 28 21:28:35 2007 UTC (16 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.18: +17 -17 lines
Log Message:
update copyrights in server/*.C

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.19 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3 pippijn 1.15 *
4 root 1.19 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5     * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.15 *
8 root 1.19 * Crossfire TRT is free software; you can redistribute it and/or modify it
9     * under the terms of the GNU General Public License as published by the Free
10     * Software Foundation; either version 2 of the License, or (at your option)
11     * any later version.
12 pippijn 1.15 *
13 root 1.19 * This program is distributed in the hope that it will be useful, but
14     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16     * for more details.
17 pippijn 1.15 *
18 root 1.19 * You should have received a copy of the GNU General Public License along
19     * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
20     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21     *
22     * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 pippijn 1.15 */
24 elmex 1.1
25     #include <global.h>
26 root 1.11 #include <sproto.h>
27 elmex 1.1 #include <spells.h>
28    
29 root 1.4 static partylist *firstparty = NULL; /* Keeps track of first party in list */
30     static partylist *lastparty = NULL; /*Keeps track of last party in list */
31 elmex 1.1
32 root 1.4 partylist *
33     get_firstparty (void)
34 elmex 1.1 {
35 root 1.4 return firstparty;
36 elmex 1.1 }
37    
38 root 1.4 void remove_party (partylist *target_party);
39 elmex 1.1
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 root 1.4 static partylist *
44     form_party (object *op, const char *params)
45     {
46     partylist *newparty;
47    
48     newparty = (partylist *) malloc (sizeof (partylist));
49 root 1.8 newparty->partyname = strdup (params);
50 root 1.4 newparty->total_exp = 0;
51     newparty->kills = 0;
52     newparty->passwd[0] = '\0';
53     newparty->next = NULL;
54 root 1.8 newparty->partyleader = strdup (op->name);
55 root 1.4 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 elmex 1.1
74 root 1.10 for_all_players (pl)
75 root 1.4 if (pl->party == target_party)
76     pl->party = NULL;
77 elmex 1.1
78 root 1.4 /* 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 elmex 1.1 }
92 root 1.4 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 root 1.2 }
110     }
111 elmex 1.1 }
112 root 1.4 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 elmex 1.1 }
128    
129     /* Remove unused parties, this could be made to scale a lot better. */
130 root 1.4 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 root 1.10 for_all_players (pl)
144 root 1.4 if (pl->party == party)
145     player_count++;
146     if (player_count == 0)
147     remove_party (party);
148 elmex 1.1 }
149     }
150    
151 root 1.4 void
152 root 1.17 add_kill_to_party (partylist *party, const char *killer, const char *dead, long exp)
153 elmex 1.1 {
154 root 1.4 int i, pos;
155 elmex 1.1
156 root 1.4 if (party == NULL)
157     return;
158 root 1.12
159 root 1.4 if (party->kills >= PARTY_KILL_LOG)
160 elmex 1.1 {
161 root 1.4 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 elmex 1.1 }
165     else
166 root 1.4 pos = party->kills;
167 root 1.12
168 elmex 1.1 party->kills++;
169 root 1.4 party->total_exp += exp;
170     party->party_kills[pos].exp = exp;
171 root 1.5 assign (party->party_kills[pos].killer, killer);
172     assign (party->party_kills[pos].dead, dead);
173 root 1.4 party->party_kills[pos].killer[MAX_NAME] = 0;
174     party->party_kills[pos].dead[MAX_NAME] = 0;
175 elmex 1.1 }
176    
177 root 1.4 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 root 1.2 }
191 elmex 1.1 }
192 root 1.4 return 1;
193 elmex 1.1 }
194    
195 root 1.4 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 root 1.9 op->contr->ns->state = ST_PLAYING;
210 root 1.4 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 root 1.9 op->contr->ns->state = ST_PLAYING;
217 root 1.4 return;
218     }
219 elmex 1.1 }
220    
221 root 1.4 void
222     send_party_message (object *op, char *msg)
223 elmex 1.1 {
224 root 1.10 for_all_players (pl)
225 root 1.4 if (pl->ob->contr->party == op->contr->party && pl->ob != op)
226     new_draw_info (NDI_WHITE, 0, pl->ob, msg);
227 elmex 1.1 }
228    
229 root 1.4 int
230     command_gsay (object *op, char *params)
231 elmex 1.1 {
232     char party_params[MAX_BUF];
233    
234 root 1.4 if (!params)
235     return 0;
236 root 1.12
237 root 1.4 strcpy (party_params, "say ");
238     strcat (party_params, params);
239     command_party (op, party_params);
240 elmex 1.1 return 0;
241     }
242    
243 root 1.4 int
244     command_party (object *op, char *params)
245 elmex 1.1 {
246     char buf[MAX_BUF];
247 root 1.4 partylist *tmpparty, *oldparty; /* For iterating over linked list */
248     char *currentparty; /* For iterating over linked list */
249 elmex 1.1
250 root 1.4 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 elmex 1.1 }
257 root 1.4 else
258     {
259 elmex 1.1 currentparty = op->contr->party->partyname;
260 root 1.4 new_draw_info_format (NDI_UNIQUE, 0, op, "You are a member of party %s.", currentparty);
261 elmex 1.1 }
262 root 1.4 return 1;
263     }
264 root 1.7
265 root 1.4 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 root 1.7
280 root 1.4 if (!strncmp (params, "kills", 5))
281 elmex 1.1 {
282 root 1.4 int i, max;
283 elmex 1.1 char chr;
284     char buffer[80];
285     float exp;
286    
287 root 1.4 if (op->contr->party == NULL)
288 root 1.2 {
289 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "You are not a member of any party.");
290 root 1.2 return 1;
291     }
292 elmex 1.1 tmpparty = op->contr->party;
293 root 1.4 if (!tmpparty->kills)
294 root 1.2 {
295 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "You haven't killed anything yet.");
296 root 1.2 return 1;
297     }
298 root 1.4 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 elmex 1.1 return 1;
336     }
337 root 1.7
338 root 1.4 if (strncmp (params, "say ", 4) == 0)
339 elmex 1.1 {
340 root 1.4 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 root 1.7 new_draw_info_format (NDI_LT_GREEN | NDI_UNIQUE, 0, op, "[%s] You say: %s", currentparty, params);
350 root 1.4 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 elmex 1.1 {
367 root 1.4 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 elmex 1.1 }
373 root 1.4 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 root 1.14 for_all_players (pl)
389     if (pl->party == oldparty)
390     player_count++;
391    
392 root 1.4 if (player_count == 0)
393     remove_party (oldparty);
394 root 1.2 }
395 root 1.14
396 root 1.4 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 root 1.2 }
406 root 1.4 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 elmex 1.1 }
413 root 1.7
414 root 1.4 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 root 1.10 for_all_players (pl)
424 root 1.4 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 elmex 1.1 return 1;
438 root 1.4 } /* leave */
439 elmex 1.1
440 root 1.4 if (strncmp (params, "passwd ", 7) == 0)
441     {
442     partylist *tmplist;
443 elmex 1.1
444 root 1.4 params += 7;
445 elmex 1.1
446 root 1.4 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 elmex 1.1
452 root 1.4 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 elmex 1.1
458 root 1.4 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 elmex 1.1
474 root 1.4 if (strcmp (params, "list") == 0)
475     {
476     partylist *tmplist;
477 elmex 1.1
478 root 1.4 tmplist = firstparty;
479 elmex 1.1
480 root 1.4 if (firstparty == NULL)
481     {
482     new_draw_info (NDI_UNIQUE, 0, op, "There are no parties active right now");
483     return 1;
484     }
485 elmex 1.1
486 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "Party name Leader");
487     new_draw_info (NDI_UNIQUE, 0, op, "---------- ------");
488 elmex 1.1
489 root 1.4 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 elmex 1.1
497 root 1.4 if (strncmp (params, "join ", 5) == 0)
498     {
499 elmex 1.1
500 root 1.4 params += 5;
501 elmex 1.1
502 root 1.4 /* 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 elmex 1.1 return 1;
507     }
508 root 1.4
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 elmex 1.1 }
540    
541 root 1.4 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 elmex 1.1 }
571    
572 root 1.4 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 elmex 1.1 return 1;
587     }