-
Notifications
You must be signed in to change notification settings - Fork 0
/
signs.c
248 lines (222 loc) · 6.15 KB
/
signs.c
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
#include <stdio.h>
#include <string.h>
#include "bitstuff.h"
#include "block.h"
#include "colour.h"
#include "client.h"
#include "level.h"
#include "player.h"
#include "position.h"
#include "mcc.h"
#define LINES_PER_MESSAGE 4
struct message_t
{
char name[16];
char line[LINES_PER_MESSAGE][64];
int radius;
struct position_t pos;
};
struct message_data_t
{
int16_t messages;
struct message_t *edit;
struct message_t message[];
};
static struct message_t *message_get_by_name(const char *name, struct message_data_t *arg, struct level_hook_data_t *ld, bool create)
{
int i;
/* Find existing message */
for (i = 0; i < arg->messages; i++)
{
if (strncasecmp(arg->message[i].name, name, sizeof arg->message[i].name - 1) == 0) return &arg->message[i];
}
if (!create) return NULL;
/* Find empty slot for new message */
for (i = 0; i < arg->messages; i++)
{
if (*arg->message[i].name == '\0')
{
memset(&arg->message[i], 0, sizeof arg->message[i]);
snprintf(arg->message[i].name, sizeof arg->message[i].name, "%s", name);
return &arg->message[i];
}
}
/* Create new slot */
ld->size = sizeof (struct message_data_t) + sizeof (struct message_t) * (arg->messages + 1);
arg = realloc(ld->data, ld->size);
if (arg == NULL) return NULL;
ld->data = arg;
memset(&arg->message[i], 0, sizeof arg->message[i]);
snprintf(arg->message[i].name, sizeof arg->message[i].name, "%s", name);
arg->messages++;
return &arg->message[i];
}
static bool message_handle_chat(struct level_t *l, struct client_t *c, char *data, struct message_data_t *arg, struct level_hook_data_t *ld)
{
if (!level_user_can_build(l, c->player)) return false;
if (strncasecmp(data, "msg edit ", 9) == 0)
{
struct message_t *s = message_get_by_name(data + 9, arg, ld, true);
if (s == NULL) goto err;
arg = ld->data;
arg->edit = s;
char buf[128];
snprintf(buf, sizeof buf, TAG_YELLOW "Editing message %s", s->name);
client_notify(c, buf);
}
else if (strncasecmp(data, "msg delete ", 11) == 0)
{
struct message_t *s = message_get_by_name(data + 11, arg, ld, false);
if (s == NULL) goto err;
memset(s, 0, sizeof *s);
client_notify(c, TAG_YELLOW "message deleted");
if (arg->edit == s) arg->edit = NULL;
l->changed = true;
}
else if (strncasecmp(data, "msg rename ", 11) == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
snprintf(s->name, sizeof s->name, data + 11);
client_notify(c, TAG_YELLOW "message renamed");
l->changed = true;
}
else if (strcasecmp(data, "msg place") == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
s->pos = c->player->pos;
client_notify(c, TAG_YELLOW "message position set");
l->changed = true;
}
else if (strncasecmp(data, "msg radius ", 11) == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
s->radius = strtol(data + 11, NULL, 10);
char buf[64];
snprintf(buf, sizeof buf, TAG_YELLOW "message radius set to %d", s->radius);
client_notify(c, buf);
l->changed = true;
}
else if (strncasecmp(data, "msg l1 ", 7) == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
snprintf(s->line[0], sizeof s->line[0], "%s", data + 7);
client_notify(c, TAG_YELLOW "message line 1 set");
l->changed = true;
}
else if (strncasecmp(data, "msg l2 ", 7) == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
snprintf(s->line[1], sizeof s->line[1], "%s", data + 7);
client_notify(c, TAG_YELLOW "message line 2 set");
l->changed = true;
}
else if (strncasecmp(data, "msg l3 ", 7) == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
snprintf(s->line[2], sizeof s->line[2], "%s", data + 7);
client_notify(c, TAG_YELLOW "message line 3 set");
l->changed = true;
}
else if (strncasecmp(data, "msg l4 ", 7) == 0)
{
struct message_t *s = arg->edit;
if (s == NULL) goto err;
snprintf(s->line[3], sizeof s->line[3], "%s", data + 7);
client_notify(c, TAG_YELLOW "message line 4 set");
l->changed = true;
}
else if (strcasecmp(data, "msg list") == 0)
{
unsigned i;
for (i = 0; i < arg->messages; i++)
{
struct message_t *s = &arg->message[i];
if (*s->name != '\0')
{
char buf[128];
snprintf(buf, sizeof buf, TAG_YELLOW "message %s at %.1fx%.1fx%.1f", s->name, s->pos.x / 32.0f, s->pos.y / 32.0f, s->pos.z / 32.0f);
client_notify(c, buf);
}
}
}
else
{
return false;
}
return true;
err:
client_notify(c, TAG_YELLOW "No message selected for editing");
return true;
}
static void message_handle_move(struct level_t *l, struct client_t *c, int index, struct message_data_t *arg)
{
/* Changing levels, don't handle messages */
if (c->player->level != c->player->new_level) return;
int i, j;
for (i = 0; i < arg->messages; i++)
{
struct message_t *s = &arg->message[i];
if (position_match(&c->player->pos, &s->pos, s->radius))
{
if (HasBit(c->player->flags, 6)) return;
for (j = 0; j < LINES_PER_MESSAGE; j++)
{
if (strlen(s->line[j]) > 0)
{
client_notify(c, s->line[j]);
}
}
SetBit(c->player->flags, 6);
return;
}
}
if (HasBit(c->player->flags, 6))
{
ClrBit(c->player->flags, 6);
}
}
static bool message_level_hook(int event, struct level_t *l, struct client_t *c, void *data, struct level_hook_data_t *arg)
{
switch (event)
{
case EVENT_CHAT: return message_handle_chat(l, c, data, arg->data, arg);
case EVENT_MOVE: message_handle_move(l, c, *(int *)data, arg->data); break;
case EVENT_INIT:
{
if (arg->size == 0)
{
LOG("Allocating new message data on %s\n", l->name);
}
else
{
struct message_data_t *sd = arg->data;
if (arg->size == sizeof (struct message_data_t) + sizeof (struct message_t) * sd->messages)
{
LOG("Found data for %d messages on %s\n", sd->messages, l->name);
/* Ensure message edit isn't set after loading */
sd->edit = NULL;
break;
}
LOG("Found invalid message data on %s, erasing\n", l->name);
free(arg->data);
}
arg->size = sizeof (struct message_data_t);
arg->data = calloc(1, arg->size);
}
}
return false;
}
void module_init(void **data)
{
register_level_hook_func("messages", &message_level_hook);
}
void module_deinit(void *data)
{
deregister_level_hook_func("messages");
}