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
370
371
372
373
374
375
376
377
378
379
380
381
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <base/math.hpp>
#include <engine/e_config.h>
#include <engine/e_server_interface.h>
#include <game/version.hpp>
#include <game/collision.hpp>
#include <game/layers.hpp>
#include <game/gamecore.hpp>
#include "gamecontext.hpp"
#include "gamemodes/dm.hpp"
#include "gamemodes/tdm.hpp"
#include "gamemodes/ctf.hpp"
TUNING_PARAMS tuning;
void send_tuning_params(int cid)
{
/*
msg_pack_start(NETMSGTYPE_SV_TUNE_PARAMS, MSGFLAG_VITAL);
int *params = (int *)&tuning;
for(unsigned i = 0; i < sizeof(tuning_params)/sizeof(int); i++)
msg_pack_int(params[i]);
msg_pack_end();
server_send_msg(cid);
*/
}
// Server hooks
void mods_client_direct_input(int client_id, void *input)
{
if(!game.world.paused)
game.players[client_id].on_direct_input((NETOBJ_PLAYER_INPUT *)input);
/*
if(i->fire)
{
msg_pack_start(MSG_EXTRA_PROJECTILE, 0);
msg_pack_end();
server_send_msg(client_id);
}*/
}
void mods_client_predicted_input(int client_id, void *input)
{
if(!game.world.paused)
game.players[client_id].on_predicted_input((NETOBJ_PLAYER_INPUT *)input);
/*
{
on_predicted_input()
if (memcmp(&game.players[client_id].input, input, sizeof(NETOBJ_PLAYER_INPUT)) != 0)
game.players[client_id].last_action = server_tick();
//game.players[client_id].previnput = game.players[client_id].input;
game.players[client_id].input = *(NETOBJ_PLAYER_INPUT*)input;
game.players[client_id].num_inputs++;
if(game.players[client_id].input.target_x == 0 && game.players[client_id].input.target_y == 0)
game.players[client_id].input.target_y = -1;
}*/
}
// Server hooks
void mods_tick()
{
game.tick();
}
void mods_snap(int client_id)
{
game.snap(client_id);
}
void mods_client_enter(int client_id)
{
//game.world.insert_entity(&game.players[client_id]);
game.players[client_id].respawn();
dbg_msg("game", "join player='%d:%s'", client_id, server_clientname(client_id));
char buf[512];
str_format(buf, sizeof(buf), "%s entered and joined the %s", server_clientname(client_id), game.controller->get_team_name(game.players[client_id].team));
game.send_chat(-1, GAMECONTEXT::CHAT_ALL, buf);
dbg_msg("game", "team_join player='%d:%s' team=%d", client_id, server_clientname(client_id), game.players[client_id].team);
}
void mods_connected(int client_id)
{
game.players[client_id].init(client_id);
//game.players[client_id].client_id = client_id;
// Check which team the player should be on
if(config.sv_tournament_mode)
game.players[client_id].team = -1;
else
game.players[client_id].team = game.controller->get_auto_team(client_id);
// send motd
NETMSG_SV_MOTD msg;
msg.message = config.sv_motd;
msg.pack(MSGFLAG_VITAL);
server_send_msg(client_id);
}
void mods_client_drop(int client_id)
{
game.players[client_id].on_disconnect();
}
void mods_message(int msgtype, int client_id)
{
void *rawmsg = netmsg_secure_unpack(msgtype);
if(!rawmsg)
{
dbg_msg("server", "dropped weird message '%s' (%d), failed on '%s'", netmsg_get_name(msgtype), msgtype, netmsg_failed_on());
return;
}
if(msgtype == NETMSGTYPE_CL_SAY)
{
NETMSG_CL_SAY *msg = (NETMSG_CL_SAY *)rawmsg;
int team = msg->team;
if(team)
team = game.players[client_id].team;
else
team = GAMECONTEXT::CHAT_ALL;
if(config.sv_spamprotection && game.players[client_id].last_chat+time_freq() > time_get())
{
// consider this as spam
}
else
{
game.players[client_id].last_chat = time_get();
game.send_chat(client_id, team, msg->message);
}
}
else if (msgtype == NETMSGTYPE_CL_SETTEAM)
{
NETMSG_CL_SETTEAM *msg = (NETMSG_CL_SETTEAM *)rawmsg;
// Switch team on given client and kill/respawn him
if(game.controller->can_join_team(msg->team, client_id))
game.players[client_id].set_team(msg->team);
else
{
char buf[128];
str_format(buf, sizeof(buf), "Only %d active players are allowed", config.sv_max_clients-config.sv_spectator_slots);
game.send_broadcast(buf, client_id);
}
}
else if (msgtype == NETMSGTYPE_CL_CHANGEINFO || msgtype == NETMSGTYPE_CL_STARTINFO)
{
NETMSG_CL_CHANGEINFO *msg = (NETMSG_CL_CHANGEINFO *)rawmsg;
game.players[client_id].use_custom_color = msg->use_custom_color;
game.players[client_id].color_body = msg->color_body;
game.players[client_id].color_feet = msg->color_feet;
// check for invalid chars
/*
unsigned char *p = (unsigned char *)name;
while (*p)
{
if(*p < 32)
*p = ' ';
p++;
}*/
// copy old name
char oldname[MAX_NAME_LENGTH];
str_copy(oldname, server_clientname(client_id), MAX_NAME_LENGTH);
server_setclientname(client_id, msg->name);
if(msgtype == NETMSGTYPE_CL_CHANGEINFO && strcmp(oldname, server_clientname(client_id)) != 0)
{
char chattext[256];
str_format(chattext, sizeof(chattext), "%s changed name to %s", oldname, server_clientname(client_id));
game.send_chat(-1, GAMECONTEXT::CHAT_ALL, chattext);
}
// set skin
str_copy(game.players[client_id].skin_name, msg->skin, sizeof(game.players[client_id].skin_name));
game.controller->on_player_info_change(&game.players[client_id]);
if(msgtype == NETMSGTYPE_CL_STARTINFO)
{
// a client that connected!
// send all info to this client
for(int i = 0; i < MAX_CLIENTS; i++)
{
if(game.players[i].client_id != -1)
game.send_info(i, client_id);
}
// send tuning parameters to client
send_tuning_params(client_id);
//
NETMSG_SV_READYTOENTER m;
m.pack(MSGFLAG_VITAL|MSGFLAG_FLUSH);
server_send_msg(client_id);
}
game.send_info(client_id, -1);
}
else if (msgtype == NETMSGTYPE_CL_EMOTICON)
{
NETMSG_CL_EMOTICON *msg = (NETMSG_CL_EMOTICON *)rawmsg;
game.send_emoticon(client_id, msg->emoticon);
}
else if (msgtype == NETMSGTYPE_CL_KILL)
{
//PLAYER *pplayer = get_player(client_id);
game.players[client_id].kill_character(); //(client_id, -1);
}
}
static void con_tune_param(void *result, void *user_data)
{
const char *param_name = console_arg_string(result, 0);
float new_value = console_arg_float(result, 1);
if(tuning.set(param_name, new_value))
{
dbg_msg("tuning", "%s changed to %.2f", param_name, new_value);
send_tuning_params(-1);
}
else
console_print("No such tuning parameter");
}
static void con_tune_reset(void *result, void *user_data)
{
TUNING_PARAMS p;
tuning = p;
send_tuning_params(-1);
console_print("tuning reset");
}
static void con_tune_dump(void *result, void *user_data)
{
for(int i = 0; i < tuning.num(); i++)
{
float v;
tuning.get(i, &v);
dbg_msg("tuning", "%s %.2f", tuning.names[i], v);
}
}
static void con_restart(void *result, void *user_data)
{
if(console_arg_num(result))
game.controller->do_warmup(console_arg_int(result, 0));
else
game.controller->startround();
}
static void con_broadcast(void *result, void *user_data)
{
game.send_broadcast(console_arg_string(result, 0), -1);
}
static void con_say(void *result, void *user_data)
{
game.send_chat(-1, GAMECONTEXT::CHAT_ALL, console_arg_string(result, 0));
}
static void con_set_team(void *result, void *user_data)
{
int client_id = clamp(console_arg_int(result, 0), 0, (int)MAX_CLIENTS);
int team = clamp(console_arg_int(result, 1), -1, 1);
dbg_msg("", "%d %d", client_id, team);
if(game.players[client_id].client_id != client_id)
return;
game.players[client_id].set_team(team);
}
void mods_console_init()
{
MACRO_REGISTER_COMMAND("tune", "si", con_tune_param, 0);
MACRO_REGISTER_COMMAND("tune_reset", "", con_tune_reset, 0);
MACRO_REGISTER_COMMAND("tune_dump", "", con_tune_dump, 0);
MACRO_REGISTER_COMMAND("restart", "?i", con_restart, 0);
MACRO_REGISTER_COMMAND("broadcast", "r", con_broadcast, 0);
MACRO_REGISTER_COMMAND("say", "r", con_say, 0);
MACRO_REGISTER_COMMAND("set_team", "ii", con_set_team, 0);
}
void mods_init()
{
//if(!data) /* only load once */
//data = load_data_from_memory(internal_data);
for(int i = 0; i < NUM_NETOBJTYPES; i++)
snap_set_staticsize(i, netobj_get_size(i));
layers_init();
col_init();
// reset everything here
//world = new GAMEWORLD;
//players = new PLAYER[MAX_CLIENTS];
// select gametype
if(strcmp(config.sv_gametype, "ctf") == 0)
game.controller = new GAMECONTROLLER_CTF;
else if(strcmp(config.sv_gametype, "tdm") == 0)
game.controller = new GAMECONTROLLER_TDM;
else
game.controller = new GAMECONTROLLER_DM;
// setup core world
//for(int i = 0; i < MAX_CLIENTS; i++)
// game.players[i].core.world = &game.world.core;
// create all entities from the game layer
MAPITEM_LAYER_TILEMAP *tmap = layers_game_layer();
TILE *tiles = (TILE *)map_get_data(tmap->data);
/*
num_spawn_points[0] = 0;
num_spawn_points[1] = 0;
num_spawn_points[2] = 0;
*/
for(int y = 0; y < tmap->height; y++)
{
for(int x = 0; x < tmap->width; x++)
{
int index = tiles[y*tmap->width+x].index - ENTITY_OFFSET;
vec2 pos(x*32.0f+16.0f, y*32.0f+16.0f);
game.controller->on_entity(index, pos);
}
}
//game.world.insert_entity(game.controller);
if(config.dbg_dummies)
{
for(int i = 0; i < config.dbg_dummies ; i++)
{
mods_connected(MAX_CLIENTS-i-1);
mods_client_enter(MAX_CLIENTS-i-1);
if(game.controller->is_teamplay)
game.players[MAX_CLIENTS-i-1].team = i&1;
}
}
}
void mods_shutdown()
{
delete game.controller;
game.controller = 0;
}
void mods_presnap() {}
void mods_postsnap()
{
game.events.clear();
}
extern "C" const char *mods_net_version() { return GAME_NETVERSION; }
extern "C" const char *mods_version() { return GAME_VERSION; }
|