ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_party.C
Revision: 1.6
Committed: Thu Sep 14 22:34:03 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +1 -2 lines
Log Message:
indent

File Contents

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