summaryrefslogtreecommitdiff
path: root/src/map/chat.cpp
blob: 1f9f43383dad9f85937e80f9be65a45168194d2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "chat.hpp"

#include <cstdlib>
#include <cstring>

#include "../common/nullpo.hpp"

#include "map.hpp"
#include "npc.hpp"
#include "pc.hpp"

#include "../poison.hpp"

static
int chat_triggerevent(struct chat_data *cd);
static
int chat_npckickall(struct chat_data *cd);

/*==========================================
 * チャットルームから抜ける
 *------------------------------------------
 */
int chat_leavechat(struct map_session_data *sd)
{
    struct chat_data *cd;
    int i, leavechar;

    nullpo_retr(1, sd);

    cd = (struct chat_data *) map_id2bl(sd->chatID);
    if (cd == NULL)
        return 1;

    for (i = 0, leavechar = -1; i < cd->users; i++)
    {
        if (cd->usersd[i] == sd)
        {
            leavechar = i;
            break;
        }
    }
    if (leavechar < 0)          // そのchatに所属していないらしい (バグ時のみ)
        return -1;

    cd->users--;
    pc_setchatid(sd, 0);

    if (cd->users == 0 && (*cd->owner)->type == BL_PC)
    {
        // 全員居なくなった&PCのチャットなので消す
        map_delobject(cd->bl.id, BL_CHAT); // freeまでしてくれる
    }
    else
    {
        for (i = leavechar; i < cd->users; i++)
            cd->usersd[i] = cd->usersd[i + 1];
        if (leavechar == 0 && (*cd->owner)->type == BL_PC)
        {
            // PCのチャットなので所有者が抜けたので位置変更
            cd->bl.x = cd->usersd[0]->bl.x;
            cd->bl.y = cd->usersd[0]->bl.y;
        }
    }

    return 0;
}

/*==========================================
 * npcチャットルーム作成
 *------------------------------------------
 */
int chat_createnpcchat(struct npc_data *nd, int limit, int pub, int trigger,
                        const char *title, int titlelen, const char *ev)
{
    struct chat_data *cd;

    nullpo_retr(1, nd);

    CREATE(cd, struct chat_data, 1);

    cd->limit = cd->trigger = limit;
    if (trigger > 0)
        cd->trigger = trigger;
    cd->pub = pub;
    cd->users = 0;
    memcpy(cd->pass, "", 8);
    if (titlelen >= sizeof(cd->title) - 1)
        titlelen = sizeof(cd->title) - 1;
    memcpy(cd->title, title, titlelen);
    cd->title[titlelen] = 0;

    cd->bl.m = nd->bl.m;
    cd->bl.x = nd->bl.x;
    cd->bl.y = nd->bl.y;
    cd->bl.type = BL_CHAT;
    cd->owner_ = (struct block_list *) nd;
    cd->owner = &cd->owner_;
    memcpy(cd->npc_event, ev, sizeof(cd->npc_event));

    cd->bl.id = map_addobject(&cd->bl);
    if (cd->bl.id == 0)
    {
        free(cd);
        return 0;
    }
    nd->chat_id = cd->bl.id;

    return 0;
}

/*==========================================
 * npcチャットルーム削除
 *------------------------------------------
 */
int chat_deletenpcchat(struct npc_data *nd)
{
    struct chat_data *cd;

    nullpo_ret(nd);
    nullpo_ret(cd = (struct chat_data *) map_id2bl(nd->chat_id));

    chat_npckickall(cd);
    map_delobject(cd->bl.id, BL_CHAT); // freeまでしてくれる
    nd->chat_id = 0;

    return 0;
}

/*==========================================
 * 規定人数以上でイベントが定義されてるなら実行
 *------------------------------------------
 */
int chat_triggerevent(struct chat_data *cd)
{
    nullpo_ret(cd);

    if (cd->users >= cd->trigger && cd->npc_event[0])
        npc_event_do(cd->npc_event);
    return 0;
}

/*==========================================
 * イベントの有効化
 *------------------------------------------
 */
int chat_enableevent(struct chat_data *cd)
{
    nullpo_ret(cd);

    cd->trigger &= 0x7f;
    chat_triggerevent(cd);
    return 0;
}

/*==========================================
 * イベントの無効化
 *------------------------------------------
 */
int chat_disableevent(struct chat_data *cd)
{
    nullpo_ret(cd);

    cd->trigger |= 0x80;
    return 0;
}

/*==========================================
 * チャットルームから全員蹴り出す
 *------------------------------------------
 */
int chat_npckickall(struct chat_data *cd)
{
    nullpo_ret(cd);

    while (cd->users > 0)
    {
        chat_leavechat(cd->usersd[cd->users - 1]);
    }
    return 0;
}