-
Notifications
You must be signed in to change notification settings - Fork 3
/
zmessage.inc
369 lines (309 loc) · 7.46 KB
/
zmessage.inc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
About: Multi-line message system
Author: ziggi
*/
#if !defined _samp_included
#error Please include a_samp before zmessage
#endif
#if defined _zmsg_included
#endinput
#endif
#define _zmsg_included
/*
Defines
*/
#if !defined ZMSG_MAX_CHAT_LENGTH
#define ZMSG_MAX_CHAT_LENGTH MAX_CHATBUBBLE_LENGTH
#endif
#if !defined ZMSG_MAX_PLAYER_CHAT_LENGTH
#define ZMSG_MAX_PLAYER_CHAT_LENGTH (MAX_CHATBUBBLE_LENGTH / 2)
#endif
#if !defined ZMSG_SEPARATORS_LIST
#define ZMSG_SEPARATORS_LIST ' '
#endif
#if !defined ZMSG_HYPHEN_END
#define ZMSG_HYPHEN_END " ¬"
#endif
#if !defined ZMSG_HYPHEN_START
#define ZMSG_HYPHEN_START "» "
#endif
/*
Vars
*/
static const
g_zmsg_HyphenEnd[] = ZMSG_HYPHEN_END,
g_zmsg_HyphenEndLength = sizeof(g_zmsg_HyphenEnd) - 1,
g_zmsg_HyphenStart[] = ZMSG_HYPHEN_START,
g_zmsg_HyphenStartLength = sizeof(g_zmsg_HyphenStart) - 1;
/*
Functions
*/
stock ZMsg_GetMessages(const message[], array[][], const lines = sizeof(array), const line_size = sizeof(array[]))
{
new length = strlen(message);
// shouldn't be shifted
if (length <= line_size) {
array[0][0] = '\0';
strcat(array[0], message, line_size);
return 1;
}
new
bool:is_packed,
last_color = -1,
start_pos,
end_pos,
line;
is_packed = message{0} != 0;
while (end_pos < length && line < lines) {
Zmsg_MakeString(array[line], line_size, message, is_packed, last_color, length, line,
start_pos, end_pos, g_zmsg_HyphenStart, g_zmsg_HyphenStartLength,
g_zmsg_HyphenEnd, g_zmsg_HyphenEndLength);
line++;
}
return line;
}
static stock Zmsg_ShiftStartPos(const message[], const pos, const bool:is_packed, const size = sizeof(message))
{
new result_pos = pos;
if (is_packed) {
while (result_pos < size) {
switch (message{result_pos}) {
case ZMSG_SEPARATORS_LIST: {
result_pos++;
}
default: {
break;
}
}
}
} else {
while (result_pos < size) {
switch (message[result_pos]) {
case ZMSG_SEPARATORS_LIST: {
result_pos++;
}
default: {
break;
}
}
}
}
return result_pos == size ? pos : result_pos;
}
static stock Zmsg_ShiftEndPos(const message[], const pos, const bool:is_packed)
{
new result_pos = pos;
if (is_packed) {
while (result_pos > 0) {
switch (message{result_pos}) {
case ZMSG_SEPARATORS_LIST: {
break;
}
default: {
result_pos--;
}
}
}
} else {
while (result_pos > 0) {
switch (message[result_pos]) {
case ZMSG_SEPARATORS_LIST: {
break;
}
default: {
result_pos--;
}
}
}
}
return result_pos == 0 ? pos : result_pos;
}
static stock Zmsg_MakeString(dest[], const size = sizeof(dest), const src[],
const bool:is_packed, &last_color, const length, const line, &spos, &epos,
const prefix[], const prefix_length, const postfix[], const postfix_length)
{
static
temp[ZMSG_MAX_CHAT_LENGTH + 1];
dest[0] = '\0';
// get pos
spos = Zmsg_ShiftStartPos(src, epos, is_packed, length);
epos = spos + size - postfix_length - (last_color != -1 ? 8 : 0);
if (line != 0) {
epos -= prefix_length;
}
if (epos >= length) {
epos = length;
} else {
new shift_epos = Zmsg_ShiftEndPos(src, epos, is_packed);
if (shift_epos > spos) {
epos = shift_epos;
}
}
// prefix and color
if (line != 0) {
// find and copy color
for (new i = spos; i != last_color; i--) {
if (src[i] == '}' && i - 7 >= 0 && src[i - 7] == '{') {
strmid(dest, src, i - 7, i + 1, size);
last_color = i - 1;
break;
}
}
// copy prefix
strcat(dest, prefix, size);
}
// source
strmid(temp, src, spos, epos);
if (is_packed) {
temp{epos - spos} = '\0';
}
strcat(dest, temp, size);
// postfix
if (epos != length) {
strcat(dest, postfix, size);
}
}
/*
SendClientMessage
*/
stock ZMsg_SendClientMessage(playerid, color, const message[])
{
new length = strlen(message);
// shouldn't be shifted
if (length <= ZMSG_MAX_CHAT_LENGTH) {
return SendClientMessage(playerid, color, message);
}
new
bool:is_packed,
last_color = -1,
result,
temp[ZMSG_MAX_CHAT_LENGTH + 1],
start_pos,
end_pos,
line;
is_packed = message{0} != 0;
while (end_pos < length) {
Zmsg_MakeString(temp, sizeof(temp), message, is_packed, last_color, length, line,
start_pos, end_pos, g_zmsg_HyphenStart, g_zmsg_HyphenStartLength,
g_zmsg_HyphenEnd, g_zmsg_HyphenEndLength);
result = SendClientMessage(playerid, color, temp);
line++;
}
return result;
}
#if defined _ALS_SendClientMessage
#undef SendClientMessage
#else
#define _ALS_SendClientMessage
#endif
#define SendClientMessage ZMsg_SendClientMessage
/*
SendClientMessageToAll
*/
stock ZMsg_SendClientMessageToAll(color, const message[])
{
new length = strlen(message);
// shouldn't be shifted
if (length <= ZMSG_MAX_CHAT_LENGTH) {
return SendClientMessageToAll(color, message);
}
new
bool:is_packed,
last_color = -1,
result,
temp[ZMSG_MAX_CHAT_LENGTH + 1],
start_pos,
end_pos,
line;
is_packed = message{0} != 0;
while (end_pos < length) {
Zmsg_MakeString(temp, sizeof(temp), message, is_packed, last_color, length, line,
start_pos, end_pos, g_zmsg_HyphenStart, g_zmsg_HyphenStartLength,
g_zmsg_HyphenEnd, g_zmsg_HyphenEndLength);
result = SendClientMessageToAll(color, temp);
line++;
}
return result;
}
#if defined _ALS_SendClientMessageToAll
#undef SendClientMessageToAll
#else
#define _ALS_SendClientMessageToAll
#endif
#define SendClientMessageToAll ZMsg_SendClientMessageToAll
/*
SendPlayerMessageToPlayer
*/
stock ZMsg_SendPlayerMessageToPlayer(playerid, senderid, const message[])
{
new length = strlen(message);
// shouldn't be shifted
if (length <= ZMSG_MAX_PLAYER_CHAT_LENGTH) {
return SendPlayerMessageToPlayer(playerid, senderid, message);
}
new
bool:is_packed,
last_color = -1,
result,
temp[ZMSG_MAX_CHAT_LENGTH + 1],
start_pos,
end_pos,
line;
is_packed = message{0} != 0;
while (end_pos < length) {
Zmsg_MakeString(temp, sizeof(temp), message, is_packed, last_color, length, line,
start_pos, end_pos, g_zmsg_HyphenStart, g_zmsg_HyphenStartLength,
g_zmsg_HyphenEnd, g_zmsg_HyphenEndLength);
if (line == 0) {
result = SendPlayerMessageToPlayer(playerid, senderid, temp);
} else {
SendClientMessage(playerid, -1, temp);
}
line++;
}
return result;
}
#if defined _ALS_SendPlayerMessageToPlayer
#undef SendPlayerMessageToPlayer
#else
#define _ALS_SendPlayerMessageToPlayer
#endif
#define SendPlayerMessageToPlayer ZMsg_SendPlayerMessageToPlayer
/*
SendPlayerMessageToAll
*/
stock ZMsg_SendPlayerMessageToAll(senderid, const message[])
{
new length = strlen(message);
// shouldn't be shifted
if (length <= ZMSG_MAX_PLAYER_CHAT_LENGTH) {
return SendPlayerMessageToAll(senderid, message);
}
new
bool:is_packed,
last_color = -1,
result,
temp[ZMSG_MAX_CHAT_LENGTH + 1],
start_pos,
end_pos,
line;
is_packed = message{0} != 0;
while (end_pos < length) {
Zmsg_MakeString(temp, sizeof(temp), message, is_packed, last_color, length, line,
start_pos, end_pos, g_zmsg_HyphenStart, g_zmsg_HyphenStartLength,
g_zmsg_HyphenEnd, g_zmsg_HyphenEndLength);
if (line == 0) {
result = SendPlayerMessageToAll(senderid, temp);
} else {
SendClientMessageToAll(-1, temp);
}
line++;
}
return result;
}
#if defined _ALS_SendPlayerMessageToAll
#undef SendPlayerMessageToAll
#else
#define _ALS_SendPlayerMessageToAll
#endif
#define SendPlayerMessageToAll ZMsg_SendPlayerMessageToAll