ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/c_party.C
Revision: 1.42
Committed: Sat Nov 17 23:40:03 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.41: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.21 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.39 *
4 root 1.42 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.40 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.35 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.39 *
9 root 1.29 * Deliantra is free software: you can redistribute it and/or modify it under
10     * the terms of the Affero GNU General Public License as published by the
11     * Free Software Foundation, either version 3 of the License, or (at your
12     * option) any later version.
13 root 1.39 *
14 root 1.20 * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18 root 1.39 *
19 root 1.29 * You should have received a copy of the Affero GNU General Public License
20     * and the GNU General Public License along with this program. If not, see
21     * <http://www.gnu.org/licenses/>.
22 root 1.39 *
23 root 1.21 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.15 */
25 elmex 1.1
26     #include <global.h>
27 root 1.11 #include <sproto.h>
28 elmex 1.1 #include <spells.h>
29    
30 root 1.22 static partylist *firstparty;
31 elmex 1.1
32 root 1.4 partylist *
33 root 1.34 get_firstparty ()
34 elmex 1.1 {
35 root 1.4 return firstparty;
36 elmex 1.1 }
37    
38 root 1.33 static void
39 root 1.4 remove_party (partylist *target_party)
40     {
41     if (firstparty == NULL)
42     {
43     LOG (llevError, "remove_party(): I was asked to remove party %s, but no parties are defined", target_party->partyname);
44     return;
45     }
46 elmex 1.1
47 root 1.10 for_all_players (pl)
48 root 1.4 if (pl->party == target_party)
49     pl->party = NULL;
50 elmex 1.1
51 root 1.22 partylist **prevlink = &firstparty;
52 root 1.4
53 root 1.22 for (partylist *p = firstparty; p; p = p->next)
54     if (p->next == target_party)
55     {
56     prevlink = &p->next;
57     break;
58     }
59 root 1.4
60 root 1.22 *prevlink = target_party->next;
61 root 1.4
62 root 1.22 free (target_party->partyleader);
63     free (target_party->partyname);
64     sfree (target_party);
65 elmex 1.1 }
66    
67     /* Remove unused parties, this could be made to scale a lot better. */
68 root 1.4 void
69 root 1.34 obsolete_parties ()
70 root 1.4 {
71     int player_count;
72     partylist *party;
73     partylist *next = NULL;
74    
75     if (!firstparty)
76     return; /* we can't obsolete parties if there aren't any */
77 root 1.22
78 root 1.4 for (party = firstparty; party != NULL; party = next)
79     {
80     next = party->next;
81     player_count = 0;
82 root 1.10 for_all_players (pl)
83 root 1.4 if (pl->party == party)
84     player_count++;
85     if (player_count == 0)
86     remove_party (party);
87 elmex 1.1 }
88     }
89    
90 root 1.4 void
91 root 1.17 add_kill_to_party (partylist *party, const char *killer, const char *dead, long exp)
92 elmex 1.1 {
93 root 1.4 int i, pos;
94 elmex 1.1
95 root 1.4 if (party == NULL)
96     return;
97 root 1.12
98 root 1.4 if (party->kills >= PARTY_KILL_LOG)
99 elmex 1.1 {
100 root 1.4 pos = PARTY_KILL_LOG - 1;
101     for (i = 0; i < PARTY_KILL_LOG - 1; i++)
102     party->party_kills[i] = party->party_kills[i + 1];
103 elmex 1.1 }
104     else
105 root 1.4 pos = party->kills;
106 root 1.12
107 elmex 1.1 party->kills++;
108 root 1.4 party->total_exp += exp;
109     party->party_kills[pos].exp = exp;
110 root 1.5 assign (party->party_kills[pos].killer, killer);
111     assign (party->party_kills[pos].dead, dead);
112 root 1.4 party->party_kills[pos].killer[MAX_NAME] = 0;
113     party->party_kills[pos].dead[MAX_NAME] = 0;
114 elmex 1.1 }
115    
116 root 1.32 static int
117 root 1.4 confirm_party_password (object *op)
118     {
119     partylist *tmppartylist;
120    
121     for (tmppartylist = firstparty; tmppartylist != NULL; tmppartylist = tmppartylist->next)
122     {
123     if (!strcmp (op->contr->party_to_join->partyname, tmppartylist->partyname))
124     {
125     if (strcmp (op->contr->write_buf + 1, tmppartylist->passwd) == 0)
126     return 0;
127     else
128     return 1;
129 root 1.2 }
130 elmex 1.1 }
131 root 1.4 return 1;
132 elmex 1.1 }
133    
134 root 1.33 static void
135 root 1.22 send_party_message (object *op, const char *msg)
136     {
137     for_all_players (pl)
138     if (pl->ob->contr->party == op->contr->party && pl->ob != op)
139 root 1.25 pl->send_msg (NDI_UNIQUE, MSG_CHANNEL ("party"), msg);
140 root 1.22 }
141    
142     void
143 root 1.4 receive_party_password (object *op, char k)
144     {
145    
146     if (confirm_party_password (op) == 0)
147     {
148     partylist *joined_party = op->contr->party_to_join;
149     char buf[MAX_BUF];
150    
151     op->contr->party = op->contr->party_to_join;
152     op->contr->party_to_join = NULL;
153     new_draw_info_format (NDI_UNIQUE, 0, op, "You have joined party: %s\n", joined_party->partyname);
154     snprintf (buf, MAX_BUF, "%s joins party %s", &op->name, joined_party->partyname);
155     send_party_message (op, buf);
156 root 1.9 op->contr->ns->state = ST_PLAYING;
157 root 1.4 return;
158     }
159     else
160     {
161     new_draw_info (NDI_UNIQUE, 0, op, "You entered the wrong password");
162     op->contr->party_to_join = NULL;
163 root 1.9 op->contr->ns->state = ST_PLAYING;
164 root 1.4 return;
165     }
166 elmex 1.1 }
167    
168 root 1.4 int
169     command_gsay (object *op, char *params)
170 elmex 1.1 {
171 root 1.4 if (!params)
172     return 0;
173 root 1.12
174 root 1.31 command_party (op, format ("say %s", params));
175 root 1.30
176 elmex 1.1 return 0;
177     }
178    
179 root 1.4 int
180     command_party (object *op, char *params)
181 elmex 1.1 {
182 root 1.28 dynbuf_text &buf = msg_dynbuf; buf.clear ();
183    
184 root 1.22 partylist *party = op->contr->party;
185    
186     if (!params)
187     params = (char *)"";
188 elmex 1.1
189 root 1.22 if (!strcmp (params, "help"))
190 root 1.27 buf << "\n"
191     " - To form a party type: C<party form> <partyname>\n"
192 root 1.26 " - To join a party type: C<party join> <partyname>\n"
193     " - If the party has a passwd, it will you prompt you for it.\n"
194     " - For a list of current parties type: C<party list>\n"
195     " - To leave a party type: C<party leave>\n"
196     " - To change a passwd for a party type: C<party passwd> <password>\n"
197     " - There is an 8 character max\n"
198     " - To talk to party members type: C<party say> <msg>\n"
199     " - To see who is in your party: C<party who>\n"
200     " - To see what you've killed, type: C<party kills>\n";
201 root 1.22 else if (strncmp (params, "form ", 5) == 0)
202 root 1.4 {
203 root 1.22 params += 5;
204    
205     if (party)
206     buf << "You are already a member of party " << party->partyname << ". You must leave it first.";
207 root 1.4 else
208     {
209 root 1.22 for (partylist *tmpparty = firstparty; tmpparty; tmpparty = tmpparty->next)
210     {
211     if (!strcmp (tmpparty->partyname, params))
212     {
213     buf << "The party " << tmpparty->partyname << " already exists, pick another name";
214     goto reply;
215     }
216     }
217    
218     /* Forms the party struct for a party called 'params'. it is the responsibility
219 root 1.41 * of the caller to ensure that the name is unique, and that it is placed in the
220 root 1.22 * main party list correctly */
221 root 1.7
222 root 1.22 party = salloc0<partylist> ();
223     party->partyname = strdup (params);
224     party->total_exp = 0;
225     party->kills = 0;
226     party->passwd[0] = '\0';
227     party->next = NULL;
228     party->partyleader = strdup (op->name);
229 root 1.7
230 root 1.22 buf << "You have formed party: " << party->partyname << ".";
231 elmex 1.1
232 root 1.22 party->next = firstparty;
233     op->contr->party = firstparty = party;
234 root 1.4 }
235 elmex 1.1 }
236 root 1.22 else if (strcmp (params, "list") == 0)
237 elmex 1.1 {
238 root 1.22 if (!firstparty)
239     buf << "There are no parties active right now";
240     else
241 root 1.4 {
242 root 1.22 buf << "Party name Leader\n\n"
243     "---------- ------\n\n";
244    
245     for (partylist *p = firstparty; p; p = p->next)
246     buf.printf ("%-32s %s\n\n", p->partyname, p->partyleader);
247 root 1.4 }
248     }
249 root 1.22 else if (strncmp (params, "join ", 5) == 0)
250 root 1.4 {
251 root 1.22 params += 5;
252 root 1.4
253 root 1.22 /* Can't join a party cause non exist */
254     if (!firstparty)
255     buf << "Party: " << params << " does not exist. You must form it first.";
256     else if (party)
257     buf << "You are already a member of party " << party->partyname << ". You must leave it first.";
258 root 1.4 else
259 root 1.22 for (partylist *p = firstparty; p; p = p->next)
260     if (!strcmp (p->partyname, params))
261 elmex 1.1 {
262 root 1.22 if (!*p->passwd)
263 root 1.4 {
264 root 1.22 op->contr->party = p;
265    
266     buf << op->name << " joins party " << p->partyname << ".";
267     send_party_message (op, buf);
268     buf.clear ();
269    
270     buf << "You have joined party: " << p->partyname << ".";
271 root 1.4 }
272 root 1.22 else
273     get_party_password (op, p);
274    
275     goto reply;
276 elmex 1.1 }
277 root 1.22 else
278     buf << "Party " << params << " does not exist. You must form it first.";
279     }
280     else if (!party)
281     buf << "You are not a member of any party.\n\n"
282     "For help try: C<party help>";
283     else if (!*params)
284     buf << "You are a member of party " << party->partyname << ".";
285     else if (!strncmp (params, "kills", 5))
286     {
287     if (!party->kills)
288     buf << "You haven't killed anything yet.";
289 root 1.4 else
290     {
291 root 1.22 int max = min (party->kills - 1, PARTY_KILL_LOG - 1);
292    
293 root 1.26 buf << " Killed | Killer| Exp\n"
294     << " ----------------+----------------+--------\n";
295 root 1.14
296 root 1.22 for (int i = 0; i <= max; i++)
297     {
298     sint64 exp = party->party_kills[i].exp;
299     char suffix = ' ';
300     if (exp > 1000000)
301     {
302     exp /= 1000000;
303     suffix = 'M';
304     }
305     else if (exp > 1000)
306     {
307     exp /= 1000;
308     suffix = 'k';
309     }
310 root 1.14
311 root 1.26 buf.printf (" %16s|%16s|%6.1f%c\n", party->party_kills[i].dead, party->party_kills[i].killer, (double)exp, suffix);
312 root 1.22 }
313 root 1.4
314 root 1.26 buf << " ----------------+----------------+--------\n";
315 root 1.7
316 root 1.4 {
317 root 1.22 sint64 exp = party->total_exp;
318     char suffix = ' ';
319    
320     if (exp > 1000000)
321     {
322     exp /= 1000000;
323     suffix = 'M';
324     }
325     else if (exp > 1000)
326 root 1.4 {
327 root 1.22 exp /= 1000;
328     suffix = 'k';
329 root 1.4 }
330 root 1.22
331 root 1.26 buf.printf (" Totals: %d kills, %.1f%c exp", party->kills, (double)exp, suffix);
332 root 1.4 }
333 root 1.22 }
334     }
335     else if (strncmp (params, "say ", 4) == 0)
336 root 1.4 {
337 root 1.22 params += 4;
338 elmex 1.1
339 root 1.22 buf << "[" << party->partyname << "] " << op->name << " says: " << params;
340     send_party_message (op, buf);
341     buf.clear ();
342 elmex 1.1
343 root 1.22 buf << "[" << party->partyname << "] You say: " << params;
344     }
345     else if (strcmp (params, "leave") == 0)
346     {
347     buf << op->name << " leaves party " << party->partyname << ".";
348     send_party_message (op, buf);
349     buf.clear ();
350 elmex 1.1
351 root 1.22 buf << "You leave party " << party->partyname << ".";
352 elmex 1.1
353 root 1.22 op->contr->party = 0;
354     obsolete_parties ();
355     }
356     else if (strcmp (params, "who") == 0)
357 root 1.4 {
358 root 1.26 buf << "Members of party " << party->partyname << ".\n";
359 elmex 1.1
360 root 1.22 for_all_players (pl)
361     if (pl->ob->contr->party == party)
362 root 1.26 buf.printf (" - %s/%d %s\n\n", &pl->ob->name, pl->ob->level,
363 root 1.22 *pl->ob->contr->own_title ? pl->ob->contr->own_title : pl->ob->contr->title);
364     }
365     else if (strncmp (params, "passwd ", 7) == 0)
366 root 1.4 {
367 root 1.22 params += 7;
368 elmex 1.1
369 root 1.22 if (strlen (params) > 8)
370     buf << "The password must not exceed 8 characters";
371     else
372 root 1.4 {
373 root 1.22 strcpy (party->passwd, params);
374     buf << "The password for party " << party->partyname << " is set to B<" << params << "> by " << &op->name;
375 elmex 1.1 }
376 root 1.22 }
377     else
378     buf << "I did not understand your command. For help try: C<party help>";
379 elmex 1.1
380 root 1.22 reply:
381 root 1.25 op->contr->send_msg (NDI_UNIQUE | NDI_REPLY, MSG_CHANNEL ("party"), buf);
382 elmex 1.1
383     return 1;
384     }