-
Notifications
You must be signed in to change notification settings - Fork 0
/
cannon.c
289 lines (245 loc) · 6.52 KB
/
cannon.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
#include <math.h>
#include "block.h"
#include "client.h"
#include "level.h"
#include "player.h"
static enum blocktype_t s_cannon;
static enum blocktype_t s_cannon_ball;
static enum blocktype_t s_active_tnt;
static enum blocktype_t s_explosion;
static struct block_t s_block;
static enum blocktype_t convert_cannon(struct level_t *level, unsigned index, const struct block_t *block)
{
return DARKGREY;
}
static void physics_cannon(struct level_t *level, unsigned index, const struct block_t *block)
{
/* Decrement reload counter */
if (block->data > 0)
{
level_addupdate(level, index, block->type, block->data - 1);
}
}
static enum blocktype_t convert_cannon_ball(struct level_t *level, unsigned index, const struct block_t *block)
{
return OBSIDIAN;
}
struct cannons
{
int16_t maxy;
struct
{
int active;
float x, y, z;
float h; /* Heading */
float vel_h;
float vel_v;
unsigned loc;
unsigned origin;
unsigned owner;
} c[MAX_CLIENTS_PER_LEVEL];
float p[MAX_CLIENTS_PER_LEVEL];
};
static void cannons_handle_tick(struct level_t *l, struct client_t *c, void *data, struct cannons *cg)
{
int i, j;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
if (!cg->c[i].active) continue;
bool hit = false;
unsigned loc = cg->c[i].loc;
for (j = 0; j < 10; j++)
{
float dx = cos(cg->c[i].h) * cg->c[i].vel_h * 0.1;
float dz = sin(cg->c[i].h) * cg->c[i].vel_h * 0.1;
cg->c[i].x += dx;
cg->c[i].z += dz;
cg->c[i].y += cg->c[i].vel_v * 0.1;
if (!level_valid_xyz(l, cg->c[i].x, cg->c[i].y > cg->maxy ? cg->maxy : cg->c[i].y, cg->c[i].z))
{
loc = -1;
cg->c[i].active = false;
break;
}
else if (cg->c[i].y <= cg->maxy)
{
unsigned newloc = level_get_index(l, cg->c[i].x, cg->c[i].y, cg->c[i].z);
if (newloc == loc) continue;
if (l->blocks[newloc].type == AIR || l->blocks[newloc].type == s_explosion ||
l->blocks[newloc].type == WATER || l->blocks[newloc].type == WATERSTILL)
{
loc = newloc;
}
else
{
hit = true;
/* Hit something */
break;
}
}
}
/* Don't place if the cannon ball is off limits. */
if (cg->c[i].y > cg->maxy) loc = -1;
/* Apply gravity */
cg->c[i].vel_v -= 1.0f / 32.0f;
/* Apply simplistic airdrag */
float d = 0.995f;
cg->c[i].vel_v *= d;
cg->c[i].vel_h *= d;
/* Moved: */
if (hit)
{
level_addupdate_with_owner(l, loc, s_active_tnt, 0x401, cg->c[i].owner);
}
if (cg->c[i].loc != -1 && cg->c[i].loc != cg->c[i].origin && cg->c[i].loc != loc)
{
s_block.type = AIR;
s_block.owner = cg->c[i].owner;
s_block.physics = 1;
level_change_block_force(l, &s_block, cg->c[i].loc);
physics_list_update(l, cg->c[i].loc, 1);
}
if (hit)
{
cg->c[i].loc = -1;
cg->c[i].active = false;
break;
}
if (cg->c[i].loc != -1 && loc != cg->c[i].origin)
{
s_block.type = s_cannon_ball;
s_block.owner = 0;
s_block.physics = 0;
delete(l, loc, &l->blocks[loc]);
level_change_block_force(l, &s_block, loc);
physics_list_update(l, loc, 0);
cg->c[i].loc = loc;
cg->c[i].origin = -1;
}
}
}
static void cannons_handle_block(struct level_t *l, struct client_t *c, struct block_event *be, struct cannons *cg)
{
if (be->bt == s_cannon && be->nt == AIR)
{
be->nt = be->bt;
unsigned index = level_get_index(l, be->x, be->y, be->z);
if (l->blocks[index].data != 0)
{
client_notify(c, TAG_YELLOW "Cannot fire, reloading...");
return;
}
/* Left click */
int i = c->player->levelid;
if (cg->c[i].active)
{
client_notify(c, TAG_YELLOW "Cannot fire, reloading...");
return;
}
/* Start at the centre of the block */
cg->c[i].x = be->x + 0.5f;
cg->c[i].y = be->y + 0.5f;
cg->c[i].z = be->z + 0.5f;
cg->c[i].loc = level_get_index(l, cg->c[i].x, cg->c[i].y, cg->c[i].z);
cg->c[i].origin = cg->c[i].loc;
cg->c[i].h = (c->player->pos.h - 64) * M_PI / 128;
float p = c->player->pos.p * M_PI / 128;
float f = cg->p[i];
cg->c[i].vel_h = f * cos(p);
cg->c[i].vel_v = -f * sin(p);
cg->c[i].active = 1;
cg->c[i].owner = c->player->globalid;
client_notify(c, TAG_YELLOW "Fire!");
level_addupdate(l, index, be->bt, 40);
}
}
static bool cannons_handle_chat(struct level_t *l, struct client_t *c, char *data, struct cannons *cg)
{
if (strcasecmp(data, "cp") == 0)
{
char buf[64];
snprintf(buf, sizeof buf, TAG_YELLOW "Power at %d", (int)(cg->p[c->player->levelid] * 10));
client_notify(c, buf);
return true;
}
else if (strncasecmp(data, "cp ", 3) == 0)
{
char *endp;
int i = strtol(data + 3, &endp, 10);
if (*endp != '\0' || i > 50 || i < 0)
{
client_notify(c, TAG_YELLOW "Power must be between 0 and 50");
}
else
{
cg->p[c->player->levelid] = i * 0.1f;
char buf[64];
snprintf(buf, sizeof buf, TAG_YELLOW "Power at %d", i);
client_notify(c, buf);
}
return true;
}
else if (strncasecmp(data, "cannon maxy ", 12) == 0)
{
char buf[64];
char *endp;
int i = strtol(data + 12, &endp, 10);
if (*endp != '\0' || i < 0 || i >= l->y)
{
snprintf(buf, sizeof buf, TAG_YELLOW "MaxY must be between 0 and %d", l->y - 1);
}
else
{
cg->maxy = i;
snprintf(buf, sizeof buf, TAG_YELLOW "MaxY set to %d", i);
}
client_notify(c, buf);
return true;
}
return false;
}
static bool cannons_level_hook(int event, struct level_t *l, struct client_t *c, void *data, struct level_hook_data_t *arg)
{
switch (event)
{
case EVENT_TICK:
cannons_handle_tick(l, c, data, arg->data);
break;
case EVENT_CHAT:
return cannons_handle_chat(l, c, data, arg->data);
case EVENT_BLOCK:
cannons_handle_block(l, c, data, arg->data);
break;
case EVENT_SPAWN:
{
struct cannons *cg = arg->data;
cg->p[c->player->levelid] = 3.0f;
break;
}
case EVENT_INIT:
if (arg->size != sizeof (struct cannons))
{
free(arg->data);
arg->size = sizeof (struct cannons);
arg->data = calloc(1, arg->size);
struct cannons *cg = arg->data;
cg->maxy = l->y - 1;
}
break;
}
return false;
}
void module_init(void **data)
{
s_cannon = register_blocktype(BLOCK_INVALID, "cannon", RANK_ADV_BUILDER, &convert_cannon, NULL, NULL, &physics_cannon, false, false, false);
s_cannon_ball = register_blocktype(BLOCK_INVALID, "cannon_ball", RANK_ADMIN, &convert_cannon_ball, NULL, NULL, NULL, false, false, false);
s_active_tnt = blocktype_get_by_name("active_tnt");
s_explosion = blocktype_get_by_name("explosion");
register_level_hook_func("cannons", &cannons_level_hook);
}
void module_deinit(void *data)
{
deregister_blocktype(s_cannon);
deregister_blocktype(s_cannon_ball);
deregister_level_hook_func("cannons");
}