ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_party.C
Revision: 1.8
Committed: Thu Dec 14 22:45:41 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +2 -2 lines
Log Message:
- implement event watcher autoncancellation on reload
- used it everywhere
- removed lots of compatibility cruft
  - configure does no longer check for mandatory unix functionality/headers
  - confgiure now runs much faster

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 root 1.8 newparty->partyname = strdup (params);
60 root 1.4 newparty->total_exp = 0;
61     newparty->kills = 0;
62     newparty->passwd[0] = '\0';
63     newparty->next = NULL;
64 root 1.8 newparty->partyleader = strdup (op->name);
65 root 1.4 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 root 1.7
279 root 1.4 if (strcmp (params, "help") == 0)
280     {
281     new_draw_info (NDI_UNIQUE, 0, op, "To form a party type: party form <partyname>");
282     new_draw_info (NDI_UNIQUE, 0, op, "To join a party type: party join <partyname>");
283     new_draw_info (NDI_UNIQUE, 0, op, "If the party has a passwd, it will you prompt you for it.");
284     new_draw_info (NDI_UNIQUE, 0, op, "For a list of current parties type: party list");
285     new_draw_info (NDI_UNIQUE, 0, op, "To leave a party type: party leave");
286     new_draw_info (NDI_UNIQUE, 0, op, "To change a passwd for a party type: party passwd <password>");
287     new_draw_info (NDI_UNIQUE, 0, op, "There is an 8 character max");
288     new_draw_info (NDI_UNIQUE, 0, op, "To talk to party members type: party say <msg>");
289     new_draw_info (NDI_UNIQUE, 0, op, "To see who is in your party: party who");
290 elmex 1.1 #ifdef PARTY_KILL_LOG
291 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "To see what you've killed, type: party kills");
292 elmex 1.1 #endif
293 root 1.4 return 1;
294     }
295 root 1.7
296 elmex 1.1 #ifdef PARTY_KILL_LOG
297 root 1.4 if (!strncmp (params, "kills", 5))
298 elmex 1.1 {
299 root 1.4 int i, max;
300 elmex 1.1 char chr;
301     char buffer[80];
302     float exp;
303    
304 root 1.4 if (op->contr->party == NULL)
305 root 1.2 {
306 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "You are not a member of any party.");
307 root 1.2 return 1;
308     }
309 elmex 1.1 tmpparty = op->contr->party;
310 root 1.4 if (!tmpparty->kills)
311 root 1.2 {
312 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "You haven't killed anything yet.");
313 root 1.2 return 1;
314     }
315 root 1.4 max = tmpparty->kills - 1;
316     if (max > PARTY_KILL_LOG - 1)
317     max = PARTY_KILL_LOG - 1;
318     new_draw_info (NDI_UNIQUE, 0, op, "Killed | Killer| Exp");
319     new_draw_info (NDI_UNIQUE, 0, op, "----------------+----------------+--------");
320     for (i = 0; i <= max; i++)
321     {
322     exp = tmpparty->party_kills[i].exp;
323     chr = ' ';
324     if (exp > 1000000)
325     {
326     exp /= 1000000;
327     chr = 'M';
328     }
329     else if (exp > 1000)
330     {
331     exp /= 1000;
332     chr = 'k';
333     }
334     sprintf (buffer, "%16s|%16s|%6.1f%c", tmpparty->party_kills[i].dead, tmpparty->party_kills[i].killer, exp, chr);
335     new_draw_info (NDI_UNIQUE, 0, op, buffer);
336     }
337     exp = tmpparty->total_exp;
338     chr = ' ';
339     if (exp > 1000000)
340     {
341     exp /= 1000000;
342     chr = 'M';
343     }
344     else if (exp > 1000)
345     {
346     exp /= 1000;
347     chr = 'k';
348     }
349     new_draw_info (NDI_UNIQUE, 0, op, "----------------+----------------+--------");
350     sprintf (buffer, "Totals: %d kills, %.1f%c exp", tmpparty->kills, exp, chr);
351     new_draw_info (NDI_UNIQUE, 0, op, buffer);
352 elmex 1.1 return 1;
353     }
354     #endif /* PARTY_KILL_LOG */
355 root 1.7
356 root 1.4 if (strncmp (params, "say ", 4) == 0)
357 elmex 1.1 {
358 root 1.4 if (op->contr->party == NULL)
359     {
360     new_draw_info (NDI_UNIQUE, 0, op, "You are not a member of any party.");
361     return 1;
362     }
363     params += 4;
364     currentparty = op->contr->party->partyname;
365     snprintf (buf, MAX_BUF - 1, "[%s] %s says: %s", currentparty, &op->name, params);
366     send_party_message (op, buf);
367 root 1.7 new_draw_info_format (NDI_LT_GREEN | NDI_UNIQUE, 0, op, "[%s] You say: %s", currentparty, params);
368 root 1.4 return 1;
369     }
370    
371     if (strncmp (params, "form ", 5) == 0)
372     {
373     int player_count;
374     player *pl;
375    
376     params += 5;
377     if (op->contr->party)
378     oldparty = op->contr->party;
379     else
380     oldparty = NULL;
381    
382     if (firstparty)
383     {
384     for (tmpparty = firstparty; tmpparty != NULL; tmpparty = tmpparty->next)
385 elmex 1.1 {
386 root 1.4 if (!strcmp (tmpparty->partyname, params))
387     {
388     new_draw_info_format (NDI_UNIQUE, 0, op, "The party %s already exists, pick another name", params);
389     return 1;
390     }
391 elmex 1.1 }
392 root 1.4 lastparty->next = form_party (op, params);
393     lastparty = lastparty->next;
394     }
395     else
396     {
397     firstparty = form_party (op, params);
398     lastparty = firstparty;
399     }
400     /*
401     * The player might have previously been a member of a party, if so, he will be leaving
402     * it, so check if there are any other members and if not, delete the party
403     */
404     player_count = 0;
405     if (oldparty)
406     {
407     for (pl = first_player; pl->next != NULL; pl = pl->next)
408     {
409     if (pl->party == oldparty)
410     player_count++;
411 root 1.2 }
412 root 1.4 if (player_count == 0)
413     remove_party (oldparty);
414 root 1.2 }
415 root 1.4 return 0;
416     } /* form */
417    
418     if (strcmp (params, "leave") == 0)
419     {
420     if (op->contr->party == NULL)
421     {
422     new_draw_info (NDI_UNIQUE, 0, op, "You are not a member of any party.");
423     return 1;
424 root 1.2 }
425 root 1.4 currentparty = op->contr->party->partyname;
426     new_draw_info_format (NDI_UNIQUE, 0, op, "You leave party %s.", currentparty);
427     sprintf (buf, "%s leaves party %s.", &op->name, currentparty);
428     send_party_message (op, buf);
429     op->contr->party = NULL;
430     return 1;
431 elmex 1.1 }
432 root 1.7
433 root 1.4 if (strcmp (params, "who") == 0)
434     {
435     player *pl;
436 elmex 1.1
437 root 1.4 tmpparty = op->contr->party;
438     if (op->contr->party == NULL)
439     {
440     new_draw_info (NDI_UNIQUE, 0, op, "You are not a member of any party.");
441     return 1;
442     }
443     new_draw_info_format (NDI_UNIQUE, 0, op, "Members of party: %s.", op->contr->party->partyname);
444     for (pl = first_player; pl != NULL; pl = pl->next)
445     if (pl->ob->contr->party == op->contr->party)
446     {
447     if (settings.set_title == TRUE)
448     {
449     if (pl->ob->contr->own_title[0] != '\0')
450     sprintf (buf, "%3d %s the %s", pl->ob->level, &pl->ob->name, pl->ob->contr->own_title);
451     else
452     sprintf (buf, "%3d %s the %s", pl->ob->level, &pl->ob->name, pl->ob->contr->title);
453     }
454     else
455     sprintf (buf, "%3d %s the %s", pl->ob->level, &pl->ob->name, pl->ob->contr->title);
456     new_draw_info (NDI_UNIQUE, 0, op, buf);
457     }
458 elmex 1.1 return 1;
459 root 1.4 } /* leave */
460 elmex 1.1
461 root 1.4 if (strncmp (params, "passwd ", 7) == 0)
462     {
463     partylist *tmplist;
464 elmex 1.1
465 root 1.4 params += 7;
466 elmex 1.1
467 root 1.4 if (op->contr->party == NULL)
468     {
469     new_draw_info (NDI_UNIQUE, 0, op, "You are not a member of a party");
470     return 1;
471     }
472 elmex 1.1
473 root 1.4 if (strlen (params) > 8)
474     {
475     new_draw_info (NDI_UNIQUE, 0, op, "The password must not exceed 8 characters");
476     return 1;
477     }
478 elmex 1.1
479 root 1.4 tmplist = firstparty;
480     while (tmplist != NULL)
481     {
482     if (tmplist == op->contr->party)
483     {
484     strcpy (tmplist->passwd, params);
485     new_draw_info_format (NDI_UNIQUE, 0, op, "The password for party %s is %s", tmplist->partyname, tmplist->passwd);
486     snprintf (buf, MAX_BUF, "Password for party %s is now %s, changed by %s", tmplist->partyname, tmplist->passwd, &op->name);
487     send_party_message (op, buf);
488     return 0;
489     }
490     tmplist = tmplist->next;
491     }
492     return 0;
493     } /* passwd */
494 elmex 1.1
495 root 1.4 if (strcmp (params, "list") == 0)
496     {
497     partylist *tmplist;
498 elmex 1.1
499 root 1.4 tmplist = firstparty;
500 elmex 1.1
501 root 1.4 if (firstparty == NULL)
502     {
503     new_draw_info (NDI_UNIQUE, 0, op, "There are no parties active right now");
504     return 1;
505     }
506 elmex 1.1
507 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "Party name Leader");
508     new_draw_info (NDI_UNIQUE, 0, op, "---------- ------");
509 elmex 1.1
510 root 1.4 while (tmplist != NULL)
511     {
512     new_draw_info_format (NDI_UNIQUE, 0, op, "%-32s %s", tmplist->partyname, tmplist->partyleader);
513     tmplist = tmplist->next;
514     }
515     return 0;
516     } /* list */
517 elmex 1.1
518 root 1.4 if (strncmp (params, "join ", 5) == 0)
519     {
520 elmex 1.1
521 root 1.4 params += 5;
522 elmex 1.1
523 root 1.4 /* Can't join a party cause non exist */
524     if (firstparty == NULL)
525     {
526     new_draw_info_format (NDI_UNIQUE, 0, op, "Party: %s does not exist. You must form it first", params);
527 elmex 1.1 return 1;
528     }
529 root 1.4
530     /* Special case if thier is only one party */
531     if (firstparty->next == NULL)
532     {
533     if (strcmp (firstparty->partyname, params) != 0)
534     {
535     new_draw_info_format (NDI_UNIQUE, 0, op, "Party: %s does not exist. You must form it first", params);
536     return 1;
537     }
538     else
539     {
540     if (op->contr->party == firstparty)
541     {
542     new_draw_info_format (NDI_UNIQUE, 0, op, "You are already in party: %s", firstparty->partyname);
543     return 1;
544     }
545     /* found party player wants to join */
546     if (firstparty->passwd[0] == '\0')
547     {
548     op->contr->party = firstparty;
549     new_draw_info_format (NDI_UNIQUE, 0, op, "You have joined party: %s", firstparty->partyname);
550     snprintf (buf, MAX_BUF, "%s joins party %s", &op->name, firstparty->partyname);
551     send_party_message (op, buf);
552     return 0;
553     }
554     else
555     {
556     get_party_password (op, firstparty);
557     return 0;
558     }
559     }
560 elmex 1.1 }
561    
562 root 1.4 tmpparty = firstparty;
563     while (tmpparty != NULL)
564     {
565     if (strcmp (tmpparty->partyname, params) == 0)
566     {
567     if (op->contr->party == tmpparty)
568     {
569     new_draw_info_format (NDI_UNIQUE, 0, op, "You are already a member of party: %s", tmpparty->partyname);
570     return 1;
571     }
572     else
573     {
574     if (tmpparty->passwd[0] == '\0')
575     {
576     new_draw_info_format (NDI_UNIQUE, 0, op, "You have joined party: %s", tmpparty->partyname);
577     op->contr->party = tmpparty;
578     snprintf (buf, MAX_BUF, "%s joins party %s", &op->name, tmpparty->partyname);
579     send_party_message (op, buf);
580     return 0;
581     }
582     else
583     {
584     get_party_password (op, tmpparty);
585     return 0;
586     }
587     }
588     }
589     else
590     tmpparty = tmpparty->next;
591 elmex 1.1 }
592    
593 root 1.4 new_draw_info_format (NDI_UNIQUE, 0, op, "Party %s does not exist. You must form it first.", params);
594     return 1;
595     } /* join */
596    
597     new_draw_info (NDI_UNIQUE, 0, op, "To form a party type: party form <partyname>");
598     new_draw_info (NDI_UNIQUE, 0, op, "To join a party type: party join <partyname>");
599     new_draw_info (NDI_UNIQUE, 0, op, "If the party has a passwd, it will you prompt you for it.");
600     new_draw_info (NDI_UNIQUE, 0, op, "For a list of current parties type: party list");
601     new_draw_info (NDI_UNIQUE, 0, op, "To leave a party type: party leave");
602     new_draw_info (NDI_UNIQUE, 0, op, "To change a passwd for a party type: party passwd <password>");
603     new_draw_info (NDI_UNIQUE, 0, op, "There is an 8 character max");
604     new_draw_info (NDI_UNIQUE, 0, op, "To talk to party members type: party say <msg>");
605     new_draw_info (NDI_UNIQUE, 0, op, "To see who is in your party: party who");
606 elmex 1.1 #ifdef PARTY_KILL_LOG
607 root 1.4 new_draw_info (NDI_UNIQUE, 0, op, "To see what you've killed, type: party kills");
608 elmex 1.1 #endif
609     return 1;
610     }