OSDN Git Service

First version
[st-ro/stro.git] / src / map / chat.c
1 // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2 // For more information, see LICENCE in the main folder
3
4 #include "../common/cbasetypes.h"
5 #include "../common/malloc.h"
6 #include "../common/nullpo.h"
7 #include "../common/showmsg.h"
8 #include "../common/strlib.h"
9 #include "../common/mmo.h"
10 #include "map.h"
11 #include "atcommand.h" // msg_txt()
12 #include "battle.h" // struct battle_config
13 #include "clif.h"
14 #include "npc.h" // npc_event_do()
15 #include "pc.h"
16 #include "chat.h"
17 #include "achievement.h"
18
19
20 int chat_triggerevent(struct chat_data *cd); // forward declaration
21
22 /// Initializes a chatroom object (common functionality for both pc and npc chatrooms).
23 /// Returns a chatroom object on success, or NULL on failure.
24 static struct chat_data* chat_createchat(struct block_list* bl, const char* title, const char* pass, int limit, bool pub, int trigger, const char* ev, int zeny, int minLvl, int maxLvl)
25 {
26         struct chat_data* cd;
27         nullpo_retr(NULL, bl);
28
29         cd = (struct chat_data *) aMalloc(sizeof(struct chat_data));
30
31         safestrncpy(cd->title, title, sizeof(cd->title));
32         safestrncpy(cd->pass, pass, sizeof(cd->pass));
33         cd->pub = pub;
34         cd->users = 0;
35         cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
36         cd->trigger = trigger;
37         cd->zeny = zeny;
38         cd->minLvl = minLvl;
39         cd->maxLvl = maxLvl;
40         memset(cd->usersd, 0, sizeof(cd->usersd));
41         cd->owner = bl;
42         safestrncpy(cd->npc_event, ev, sizeof(cd->npc_event));
43
44         cd->bl.id   = map_get_new_object_id();
45         cd->bl.m    = bl->m;
46         cd->bl.x    = bl->x;
47         cd->bl.y    = bl->y;
48         cd->bl.type = BL_CHAT;
49         cd->bl.next = cd->bl.prev = NULL;
50
51         if( cd->bl.id == 0 ) {
52                 aFree(cd);
53                 cd = NULL;
54         }
55
56         map_addiddb(&cd->bl);
57
58         if( bl->type != BL_NPC )
59                 cd->kick_list = idb_alloc(DB_OPT_BASE);
60
61         return cd;
62 }
63
64 /**
65  * Player chat room creation.
66  * @param sd : player requesting
67  * @param title : title of chat room
68  * @param pass : password for chat room
69  * @param limit : amount allowed to enter
70  * @param pub : public or private
71  * @return 0
72  */
73 int chat_createpcchat(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
74 {
75         struct chat_data* cd;
76
77         nullpo_ret(sd);
78
79         if( sd->chatID )
80                 return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex]
81
82         if( sd->state.vending || sd->state.buyingstore ) // not chat, when you already have a store open
83                 return 0;
84
85         if( map[sd->bl.m].flag.nochat ) {
86                 clif_displaymessage(sd->fd, msg_txt(sd,281));
87                 return 0; //Can't create chatrooms on this map.
88         }
89
90         if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOCHAT) ) {
91                 clif_displaymessage (sd->fd, msg_txt(sd,665));
92                 return 0;
93         }
94
95         pc_stop_walking(sd,1);
96
97         cd = chat_createchat(&sd->bl, title, pass, limit, pub, 0, "", 0, 1, MAX_LEVEL);
98
99         if( cd ) {
100                 cd->users = 1;
101                 cd->usersd[0] = sd;
102                 pc_setchatid(sd,cd->bl.id);
103                 pc_stop_attack(sd);
104                 clif_createchat(sd,0);
105                 clif_dispchat(cd,0);
106
107                 if (status_isdead(&sd->bl))
108                         achievement_update_objective(sd, AG_CHAT_DYING, 1, 1);
109                 else
110                         achievement_update_objective(sd, AG_CHAT_CREATE, 1, 1);
111         } else
112                 clif_createchat(sd,1);
113
114         return 0;
115 }
116
117  /**
118  * Join an existing chat room.
119  * @param sd : player requesting
120  * @param chatid : ID of the chat room
121  * @param pass : password of chat room
122  * @return 0
123  */
124 int chat_joinchat(struct map_session_data* sd, int chatid, const char* pass)
125 {
126         struct chat_data* cd;
127
128         nullpo_ret(sd);
129
130         cd = (struct chat_data*)map_id2bl(chatid);
131
132         if( cd == NULL || cd->bl.type != BL_CHAT || cd->bl.m != sd->bl.m || sd->state.vending || sd->state.buyingstore || sd->chatID || ((cd->owner->type == BL_NPC) ? cd->users+1 : cd->users) >= cd->limit ) {
133                 clif_joinchatfail(sd,0);
134                 return 0;
135         }
136
137         if( !cd->pub && strncmp(pass, cd->pass, sizeof(cd->pass)) != 0 && !pc_has_permission(sd, PC_PERM_JOIN_ALL_CHAT) ) {
138                 clif_joinchatfail(sd,1);
139                 return 0;
140         }
141
142         if( sd->status.base_level < cd->minLvl || sd->status.base_level > cd->maxLvl ) {
143                 if(sd->status.base_level < cd->minLvl)
144                         clif_joinchatfail(sd,5);
145                 else
146                         clif_joinchatfail(sd,6);
147
148                 return 0;
149         }
150
151         if( sd->status.zeny < cd->zeny ) {
152                 clif_joinchatfail(sd,4);
153                 return 0;
154         }
155
156         if( cd->owner->type != BL_NPC && idb_exists(cd->kick_list,sd->status.char_id) ) {
157                 clif_joinchatfail(sd,2);//You have been kicked out of the room.
158                 return 0;
159         }
160
161         pc_stop_walking(sd,1);
162         cd->usersd[cd->users] = sd;
163         cd->users++;
164
165         pc_setchatid(sd,cd->bl.id);
166
167         clif_joinchatok(sd, cd); //To the person who newly joined the list of all
168         clif_addchat(cd, sd); //Reports To the person who already in the chat
169         clif_dispchat(cd, 0); //Reported number of changes to the people around
170
171         if (cd->owner->type == BL_PC)
172                 achievement_update_objective(map_id2sd(cd->owner->id), AG_CHAT_COUNT, 1, cd->users);
173
174         chat_triggerevent(cd); //Event
175
176         return 0;
177 }
178
179 /**
180  * Make player (sd) leave a chat room.
181  * @param sd : player requesting
182  * @param kicked : for clif notification, kicked=1 or regular leave
183  * @return 0:success, 1:failed
184  */
185 int chat_leavechat(struct map_session_data* sd, bool kicked)
186 {
187         struct chat_data* cd;
188         int i;
189         int leavechar;
190
191         nullpo_retr(1, sd);
192
193         cd = (struct chat_data*)map_id2bl(sd->chatID);
194
195         if( cd == NULL ) {
196                 pc_setchatid(sd, 0);
197                 return 1;
198         }
199
200         ARR_FIND( 0, cd->users, i, cd->usersd[i] == sd );
201         if ( i == cd->users ) { // Not found in the chatroom?
202                 pc_setchatid(sd, 0);
203                 return -1;
204         }
205
206         clif_leavechat(cd, sd, kicked);
207         pc_setchatid(sd, 0);
208         cd->users--;
209
210         leavechar = i;
211
212         for( i = leavechar; i < cd->users; i++ )
213                 cd->usersd[i] = cd->usersd[i+1];
214
215         if( cd->users == 0 && cd->owner->type == BL_PC ) { // Delete empty chatroom
216                 struct skill_unit* unit;
217                 struct skill_unit_group* group;
218
219                 clif_clearchat(cd, 0);
220                 db_destroy(cd->kick_list);
221                 map_deliddb(&cd->bl);
222                 map_delblock(&cd->bl);
223                 map_freeblock(&cd->bl);
224
225                 unit = map_find_skill_unit_oncell(&sd->bl, sd->bl.x, sd->bl.y, AL_WARP, NULL, 0);
226                 group = (unit != NULL) ? unit->group : NULL;
227
228                 if (group != NULL)
229                         ext_skill_unit_onplace(unit, &sd->bl, group->tick);
230
231                 return 1;
232         }
233
234         if( leavechar == 0 && cd->owner->type == BL_PC ) { // Set and announce new owner
235                 cd->owner = (struct block_list*) cd->usersd[0];
236                 clif_changechatowner(cd, cd->usersd[0]);
237                 clif_clearchat(cd, 0);
238
239                 //Adjust Chat location after owner has been changed.
240                 map_delblock( &cd->bl );
241                 cd->bl.x = cd->usersd[0]->bl.x;
242                 cd->bl.y = cd->usersd[0]->bl.y;
243
244                 if(map_addblock( &cd->bl ))
245                         return 1;
246
247                 clif_dispchat(cd,0);
248         } else
249                 clif_dispchat(cd,0); // refresh chatroom
250
251         return 0;
252 }
253
254 /**
255  * Change a chat room's owner.
256  * @param sd : player requesting
257  * @param nextownername : string of new owner (name should be in chatroom)
258  * @return 0:success, 1:failure
259  */
260 int chat_changechatowner(struct map_session_data* sd, const char* nextownername)
261 {
262         struct chat_data* cd;
263         struct map_session_data* tmpsd;
264         int i;
265
266         nullpo_retr(1, sd);
267
268         cd = (struct chat_data*)map_id2bl(sd->chatID);
269
270         if( cd == NULL || (struct block_list*) sd != cd->owner )
271                 return 1;
272
273         ARR_FIND( 1, cd->users, i, strncmp(cd->usersd[i]->status.name, nextownername, NAME_LENGTH) == 0 );
274         if( i == cd->users )
275                 return -1;  // name not found
276
277         // erase temporarily
278         clif_clearchat(cd,0);
279
280         // set new owner
281         cd->owner = (struct block_list*) cd->usersd[i];
282         clif_changechatowner(cd,cd->usersd[i]);
283
284         // swap the old and new owners' positions
285         tmpsd = cd->usersd[i];
286         cd->usersd[i] = cd->usersd[0];
287         cd->usersd[0] = tmpsd;
288
289         // set the new chatroom position
290         map_delblock( &cd->bl );
291         cd->bl.x = cd->owner->x;
292         cd->bl.y = cd->owner->y;
293
294         if(map_addblock( &cd->bl ))
295                 return 1;
296
297         // and display again
298         clif_dispchat(cd,0);
299
300         return 0;
301 }
302
303 /**
304  * Change a chat room's status (title, etc).
305  * @param sd : player requesting
306  * @param title : new title
307  * @param pass : new password
308  * @param limit : new limit
309  * @param pub : public or private
310  * @return 1:success, 0:failure
311  */
312 int chat_changechatstatus(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
313 {
314         struct chat_data* cd;
315
316         nullpo_retr(1, sd);
317
318         cd = (struct chat_data*)map_id2bl(sd->chatID);
319
320         if( cd == NULL || (struct block_list *)sd != cd->owner )
321                 return 1;
322
323         safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE);
324         safestrncpy(cd->pass, pass, CHATROOM_PASS_SIZE);
325         cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
326         cd->pub = pub;
327
328         clif_changechatstatus(cd);
329         clif_dispchat(cd,0);
330
331         return 0;
332 }
333
334 /**
335  * Kicks a user from the chat room.
336  * @param cd : chat to be kicked from
337  * @param kickusername : player name to be kicked
338  * @retur 1:success, 0:failure
339  */
340 int chat_npckickchat(struct chat_data* cd, const char* kickusername)
341 {
342         int i;
343         nullpo_ret(cd);
344
345         ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
346         if( i == cd->users )
347                 return -1;
348         chat_leavechat(cd->usersd[i],1);
349         return 0;
350 }
351
352 /**
353  * Kick a member from a chat room.
354  * @param sd : player requesting
355  * @param kickusername : player name to be kicked
356  * @retur 1:success, 0:failure
357  */
358 int chat_kickchat(struct map_session_data* sd, const char* kickusername)
359 {
360         struct chat_data* cd;
361         int i;
362
363         nullpo_retr(1, sd);
364
365         cd = (struct chat_data *)map_id2bl(sd->chatID);
366
367         if( cd == NULL || (struct block_list *)sd != cd->owner )
368                 return -1;
369
370         ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
371         if( i == cd->users )
372                 return -1;
373
374         if (pc_has_permission(cd->usersd[i], PC_PERM_NO_CHAT_KICK))
375                 return 0; //gm kick protection [Valaris]
376
377         idb_put(cd->kick_list,cd->usersd[i]->status.char_id,(void*)1);
378
379         chat_leavechat(cd->usersd[i],1);
380
381         return 0;
382 }
383
384 /**
385  * Creates a chat room for a NPC.
386  * @param nd : NPC requesting
387  * @param title : title of chat room
388  * @param limit : limit of users in chat room
389  * @param pub : public or private
390  * @param trigger : event trigger
391  * @param ev : event name
392  * @param zeny : zeny cost
393  * @param minLvl : minimum level to enter
394  * @param maxLvl : maximum level to enter
395  * @return 0
396  */
397 int chat_createnpcchat(struct npc_data* nd, const char* title, int limit, bool pub, int trigger, const char* ev, int zeny, int minLvl, int maxLvl)
398 {
399         struct chat_data* cd;
400
401         nullpo_ret(nd);
402
403         if( nd->chat_id ) {
404                 ShowError("chat_createnpcchat: npc '%s' already has a chatroom, cannot create new one!\n", nd->exname);
405                 return 0;
406         }
407
408         if( zeny > MAX_ZENY || maxLvl > MAX_LEVEL ) {
409                 ShowError("chat_createnpcchat: npc '%s' has a required lvl or amount of zeny over the max limit!\n", nd->exname);
410                 return 0;
411         }
412
413         cd = chat_createchat(&nd->bl, title, "", limit, pub, trigger, ev, zeny, minLvl, maxLvl);
414
415         if( cd ) {
416                 nd->chat_id = cd->bl.id;
417                 clif_dispchat(cd,0);
418         }
419
420         return 0;
421 }
422
423 /**
424  * Removes a chat room for a NPC.
425  * @param nd : NPC requesting
426  */
427 int chat_deletenpcchat(struct npc_data* nd)
428 {
429         struct chat_data *cd;
430
431         nullpo_ret(nd);
432
433         cd = (struct chat_data*)map_id2bl(nd->chat_id);
434
435         if( cd == NULL )
436                 return 0;
437
438         chat_npckickall(cd);
439         clif_clearchat(cd, 0);
440         map_deliddb(&cd->bl);
441         map_delblock(&cd->bl);
442         map_freeblock(&cd->bl);
443         nd->chat_id = 0;
444
445         return 0;
446 }
447
448  /**
449  * Trigger NPC event when entering the chat room.
450  * @param cd : chat room to trigger event
451  * @return 0
452  */
453 int chat_triggerevent(struct chat_data *cd)
454 {
455         nullpo_ret(cd);
456
457         if( cd->users >= cd->trigger && cd->npc_event[0] )
458                 npc_event_do(cd->npc_event);
459
460         return 0;
461 }
462
463  /**
464  * Enables the event of the chat room.
465  * At most, 127 users are needed to trigger the event.
466  * @param cd : chat room to trigger event
467  */
468 int chat_enableevent(struct chat_data* cd)
469 {
470         nullpo_ret(cd);
471
472         cd->trigger &= 0x7f;
473         chat_triggerevent(cd);
474
475         return 0;
476 }
477
478 /**
479  * Disables the event of the chat room.
480  * @param cd : chat room to trigger event
481  */
482 int chat_disableevent(struct chat_data* cd)
483 {
484         nullpo_ret(cd);
485
486         cd->trigger |= 0x80;
487
488         return 0;
489 }
490
491 /**
492  * Kicks all the users from the chat room.
493  * @param cd : chat room to trigger event
494  */
495 int chat_npckickall(struct chat_data* cd)
496 {
497         nullpo_ret(cd);
498
499         while( cd->users > 0 )
500                 chat_leavechat(cd->usersd[cd->users-1],0);
501
502         return 0;
503 }