ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_party.C
Revision: 1.12
Committed: Sun Dec 31 10:28:36 2006 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +3 -9 lines
Log Message:
different interface design for c++/perl map handling, some random map framework

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