1 // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2 // For more information, see LICENCE in the main folder
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"
11 #include "atcommand.h" // msg_txt()
12 #include "battle.h" // struct battle_config
14 #include "npc.h" // npc_event_do()
17 #include "achievement.h"
20 int chat_triggerevent(struct chat_data *cd); // forward declaration
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)
27 nullpo_retr(NULL, bl);
29 cd = (struct chat_data *) aMalloc(sizeof(struct chat_data));
31 safestrncpy(cd->title, title, sizeof(cd->title));
32 safestrncpy(cd->pass, pass, sizeof(cd->pass));
35 cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
36 cd->trigger = trigger;
40 memset(cd->usersd, 0, sizeof(cd->usersd));
42 safestrncpy(cd->npc_event, ev, sizeof(cd->npc_event));
44 cd->bl.id = map_get_new_object_id();
48 cd->bl.type = BL_CHAT;
49 cd->bl.next = cd->bl.prev = NULL;
51 if( cd->bl.id == 0 ) {
58 if( bl->type != BL_NPC )
59 cd->kick_list = idb_alloc(DB_OPT_BASE);
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
73 int chat_createpcchat(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
80 return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex]
82 if( sd->state.vending || sd->state.buyingstore ) // not chat, when you already have a store open
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.
90 if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOCHAT) ) {
91 clif_displaymessage (sd->fd, msg_txt(sd,665));
95 pc_stop_walking(sd,1);
97 cd = chat_createchat(&sd->bl, title, pass, limit, pub, 0, "", 0, 1, MAX_LEVEL);
102 pc_setchatid(sd,cd->bl.id);
104 clif_createchat(sd,0);
107 if (status_isdead(&sd->bl))
108 achievement_update_objective(sd, AG_CHAT_DYING, 1, 1);
110 achievement_update_objective(sd, AG_CHAT_CREATE, 1, 1);
112 clif_createchat(sd,1);
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
124 int chat_joinchat(struct map_session_data* sd, int chatid, const char* pass)
126 struct chat_data* cd;
130 cd = (struct chat_data*)map_id2bl(chatid);
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);
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);
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);
146 clif_joinchatfail(sd,6);
151 if( sd->status.zeny < cd->zeny ) {
152 clif_joinchatfail(sd,4);
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.
161 pc_stop_walking(sd,1);
162 cd->usersd[cd->users] = sd;
165 pc_setchatid(sd,cd->bl.id);
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
171 if (cd->owner->type == BL_PC)
172 achievement_update_objective(map_id2sd(cd->owner->id), AG_CHAT_COUNT, 1, cd->users);
174 chat_triggerevent(cd); //Event
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
185 int chat_leavechat(struct map_session_data* sd, bool kicked)
187 struct chat_data* cd;
193 cd = (struct chat_data*)map_id2bl(sd->chatID);
200 ARR_FIND( 0, cd->users, i, cd->usersd[i] == sd );
201 if ( i == cd->users ) { // Not found in the chatroom?
206 clif_leavechat(cd, sd, kicked);
212 for( i = leavechar; i < cd->users; i++ )
213 cd->usersd[i] = cd->usersd[i+1];
215 if( cd->users == 0 && cd->owner->type == BL_PC ) { // Delete empty chatroom
216 struct skill_unit* unit;
217 struct skill_unit_group* group;
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);
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;
229 ext_skill_unit_onplace(unit, &sd->bl, group->tick);
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);
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;
244 if(map_addblock( &cd->bl ))
249 clif_dispchat(cd,0); // refresh chatroom
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
260 int chat_changechatowner(struct map_session_data* sd, const char* nextownername)
262 struct chat_data* cd;
263 struct map_session_data* tmpsd;
268 cd = (struct chat_data*)map_id2bl(sd->chatID);
270 if( cd == NULL || (struct block_list*) sd != cd->owner )
273 ARR_FIND( 1, cd->users, i, strncmp(cd->usersd[i]->status.name, nextownername, NAME_LENGTH) == 0 );
275 return -1; // name not found
278 clif_clearchat(cd,0);
281 cd->owner = (struct block_list*) cd->usersd[i];
282 clif_changechatowner(cd,cd->usersd[i]);
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;
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;
294 if(map_addblock( &cd->bl ))
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
312 int chat_changechatstatus(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
314 struct chat_data* cd;
318 cd = (struct chat_data*)map_id2bl(sd->chatID);
320 if( cd == NULL || (struct block_list *)sd != cd->owner )
323 safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE);
324 safestrncpy(cd->pass, pass, CHATROOM_PASS_SIZE);
325 cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
328 clif_changechatstatus(cd);
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
340 int chat_npckickchat(struct chat_data* cd, const char* kickusername)
345 ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
348 chat_leavechat(cd->usersd[i],1);
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
358 int chat_kickchat(struct map_session_data* sd, const char* kickusername)
360 struct chat_data* cd;
365 cd = (struct chat_data *)map_id2bl(sd->chatID);
367 if( cd == NULL || (struct block_list *)sd != cd->owner )
370 ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
374 if (pc_has_permission(cd->usersd[i], PC_PERM_NO_CHAT_KICK))
375 return 0; //gm kick protection [Valaris]
377 idb_put(cd->kick_list,cd->usersd[i]->status.char_id,(void*)1);
379 chat_leavechat(cd->usersd[i],1);
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
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)
399 struct chat_data* cd;
404 ShowError("chat_createnpcchat: npc '%s' already has a chatroom, cannot create new one!\n", nd->exname);
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);
413 cd = chat_createchat(&nd->bl, title, "", limit, pub, trigger, ev, zeny, minLvl, maxLvl);
416 nd->chat_id = cd->bl.id;
424 * Removes a chat room for a NPC.
425 * @param nd : NPC requesting
427 int chat_deletenpcchat(struct npc_data* nd)
429 struct chat_data *cd;
433 cd = (struct chat_data*)map_id2bl(nd->chat_id);
439 clif_clearchat(cd, 0);
440 map_deliddb(&cd->bl);
441 map_delblock(&cd->bl);
442 map_freeblock(&cd->bl);
449 * Trigger NPC event when entering the chat room.
450 * @param cd : chat room to trigger event
453 int chat_triggerevent(struct chat_data *cd)
457 if( cd->users >= cd->trigger && cd->npc_event[0] )
458 npc_event_do(cd->npc_event);
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
468 int chat_enableevent(struct chat_data* cd)
473 chat_triggerevent(cd);
479 * Disables the event of the chat room.
480 * @param cd : chat room to trigger event
482 int chat_disableevent(struct chat_data* cd)
492 * Kicks all the users from the chat room.
493 * @param cd : chat room to trigger event
495 int chat_npckickall(struct chat_data* cd)
499 while( cd->users > 0 )
500 chat_leavechat(cd->usersd[cd->users-1],0);