-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.c
296 lines (267 loc) · 7.67 KB
/
portal.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
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
#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"
struct portal_t
{
char name[16];
char target[16];
char target_level[32];
struct position_t pos;
};
struct portal_data_t
{
int16_t portals;
struct portal_t *edit;
struct portal_t portal[];
};
static struct portal_t *portal_get_by_name(const char *name, struct portal_data_t *arg, struct level_hook_data_t *ld, bool create)
{
int i;
/* Find existing portal */
for (i = 0; i < arg->portals; i++)
{
if (strncasecmp(arg->portal[i].name, name, sizeof arg->portal[i].name - 1) == 0) return &arg->portal[i];
}
if (!create) return NULL;
/* Find empty slot for new portal */
for (i = 0; i < arg->portals; i++)
{
if (*arg->portal[i].name == '\0')
{
memset(&arg->portal[i], 0, sizeof arg->portal[i]);
snprintf(arg->portal[i].name, sizeof arg->portal[i].name, "%s", name);
return &arg->portal[i];
}
}
/* Create new slot */
ld->size = sizeof (struct portal_data_t) + sizeof (struct portal_t) * (arg->portals + 1);
arg = realloc(ld->data, ld->size);
if (arg == NULL) return NULL;
ld->data = arg;
memset(&arg->portal[i], 0, sizeof arg->portal[i]);
snprintf(arg->portal[i].name, sizeof arg->portal[i].name, "%s", name);
arg->portals++;
return &arg->portal[i];
}
static bool portal_handle_chat(struct level_t *l, struct client_t *c, char *data, struct portal_data_t *arg, struct level_hook_data_t *ld)
{
if (!level_user_can_own(l, c->player)) return false;
if (strncasecmp(data, "portal edit ", 12) == 0)
{
struct portal_t *p = portal_get_by_name(data + 12, arg, ld, true);
if (p == NULL) goto err;
arg = ld->data;
arg->edit = p;
char buf[128];
snprintf(buf, sizeof buf, TAG_YELLOW "Editing portal %s", data + 12);
client_notify(c, buf);
}
else if (strncasecmp(data, "portal delete ", 14) == 0)
{
struct portal_t *p = portal_get_by_name(data + 14, arg, ld, false);
if (p == NULL) goto err;
memset(p, 0, sizeof *p);
client_notify(c, TAG_YELLOW "Portal deleted");
if (arg->edit == p) arg->edit = NULL;
l->changed = true;
}
else if (strncasecmp(data, "portal rename ", 14) == 0)
{
struct portal_t *p = arg->edit;
if (p == NULL) goto err;
snprintf(p->name, sizeof p->name, data + 14);
client_notify(c, TAG_YELLOW "Portal renamed");
l->changed = true;
}
else if (strcasecmp(data, "portal place") == 0)
{
struct portal_t *p = arg->edit;
if (p == NULL) goto err;
p->pos = c->player->pos;
client_notify(c, TAG_YELLOW "Portal position set");
l->changed = true;
}
else if (strcasecmp(data, "portal no target") == 0)
{
struct portal_t *p = arg->edit;
if (p == NULL) goto err;
memset(p->target, 0, sizeof p->target);
client_notify(c, TAG_YELLOW "Portal target cleared");
l->changed = true;
}
else if (strncasecmp(data, "portal target ", 14) == 0)
{
struct portal_t *p = arg->edit;
if (p == NULL) goto err;
snprintf(p->target, sizeof p->target, "%s", data + 14);
client_notify(c, TAG_YELLOW "Portal target set");
l->changed = true;
}
else if (strcasecmp(data, "portal no target-level") == 0)
{
struct portal_t *p = arg->edit;
if (p == NULL) goto err;
memset(p->target_level, 0, sizeof p->target_level);
client_notify(c, TAG_YELLOW "Portal target-level cleared");
l->changed = true;
}
else if (strncasecmp(data, "portal target-level ", 20) == 0)
{
struct portal_t *p = arg->edit;
if (p == NULL) goto err;
snprintf(p->target_level, sizeof p->target_level, "%s", data + 20);
client_notify(c, TAG_YELLOW "Portal target-level set");
l->changed = true;
}
else if (strcasecmp(data, "portal list") == 0)
{
unsigned i;
for (i = 0; i < arg->portals; i++)
{
struct portal_t *p = &arg->portal[i];
if (*p->name != '\0')
{
char buf[128];
snprintf(buf, sizeof buf, TAG_YELLOW "Portal %s at %.1fx%.1fx%.1f goes to %s on %s", p->name, p->pos.x / 32.0f, p->pos.y / 32.0f, p->pos.z / 32.0f, p->target, p->target_level);
client_notify(c, buf);
}
}
}
else
{
return false;
}
return true;
err:
client_notify(c, TAG_YELLOW "No portal selected for editing");
return true;
}
static void portal_teleport(struct client_t *c, const char *target, struct portal_data_t *arg, bool instant)
{
char buf[128];
struct portal_t *p = portal_get_by_name(target, arg, NULL, false);
if (p == NULL)
{
snprintf(buf, sizeof buf, TAG_YELLOW "Portal target %s not found", target);
client_notify(c, buf);
}
else if (p->pos.x == 0 && p->pos.y == 0 && p->pos.z == 0)
{
snprintf(buf, sizeof buf, TAG_YELLOW "Portal target %s has no position", target);
client_notify(c, buf);
}
else
{
// snprintf(buf, sizeof buf, TAG_YELLOW "Teleporting to %s", target);
// client_notify(c, buf);
player_teleport(c->player, &p->pos, instant);
}
}
static void portal_handle_move(struct level_t *l, struct client_t *c, int index, struct portal_data_t *arg)
{
/* Changing levels, don't handle teleports */
if (c->player->level != c->player->new_level) return;
// char buf[64];
// snprintf(buf, sizeof buf, "position on %s: %d %d %d\n", c->player->level->name, c->player->pos.x, c->player->pos.y, c->player->pos.z);
// client_notify(c, buf);
int i;
for (i = 0; i < arg->portals; i++)
{
struct portal_t *p = &arg->portal[i];
if (position_match(&c->player->pos, &p->pos, 32))
{
if (HasBit(c->player->flags, 7)) return;
// char buf[128];
// snprintf(buf, sizeof buf, "Reached portal %s", p->name);
// client_notify(c, buf);
SetBit(c->player->flags, 7);
/* Portal is exit only */
if (*p->target_level == '\0')
{
if (*p->target == '\0') return;
portal_teleport(c, p->target, arg, true);
}
else
{
/* Switch level */
struct level_t *l2;
if (level_get_by_name(p->target_level, &l2))
{
if (player_change_level(c->player, l2))
{
if (*p->target != '\0') c->player->hook_data = p->target;
}
}
}
return;
}
}
if (HasBit(c->player->flags, 7))
{
// client_notify(c, "Moved out of portal");
ClrBit(c->player->flags, 7);
}
}
static void portal_handle_spawn(struct level_t *l, struct client_t *c, char *data, struct portal_data_t *arg)
{
SetBit(c->player->flags, 7);
if (data == NULL) return;
portal_teleport(c, data, arg, false);
}
static bool portal_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 portal_handle_chat(l, c, data, arg->data, arg);
case EVENT_MOVE: portal_handle_move(l, c, *(int *)data, arg->data); break;
case EVENT_SPAWN: portal_handle_spawn(l, c, data, arg->data); break;
// case EVENT_LOAD: portal->edit = NULL; break;
case EVENT_INIT:
{
if (arg->size == 0)
{
LOG("Allocating new portal data on %s\n", l->name);
}
else
{
struct portal_data_t *pd = arg->data;
if (arg->size == sizeof (struct portal_data_t) + sizeof (struct portal_t) * pd->portals)
{
LOG("Found data for %d portals on %s\n", pd->portals, l->name);
unsigned i;
for (i = 0; i < pd->portals; i++)
{
struct portal_t *p = &pd->portal[i];
if (*p->name != '\0') continue;
if (p->pos.x == 0 && p->pos.y == 0 && p->pos.z == 0) continue;
snprintf(p->name, sizeof p->name, "unamed");
}
/* Ensure portal edit isn't set after loading */
pd->edit = NULL;
break;
}
LOG("Found invalid portal data on %s, erasing\n", l->name);
free(arg->data);
}
arg->size = sizeof (struct portal_data_t);
arg->data = calloc(1, arg->size);
break;
}
}
return false;
}
void module_init(void **data)
{
register_level_hook_func("portal", &portal_level_hook);
}
void module_deinit(void *data)
{
deregister_level_hook_func("portal");
}