2008-08-14 18:46:52 +00:00
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include <engine/e_server_interface.h>
|
|
|
|
|
|
|
|
#include "player.hpp"
|
|
|
|
#include "gamecontext.hpp"
|
|
|
|
|
2008-09-24 09:03:49 +00:00
|
|
|
MACRO_ALLOC_POOL_ID_IMPL(PLAYER, MAX_CLIENTS)
|
|
|
|
|
2008-09-23 18:08:19 +00:00
|
|
|
PLAYER::PLAYER(int client_id)
|
2008-08-14 18:46:52 +00:00
|
|
|
{
|
2008-10-17 15:38:36 +00:00
|
|
|
respawn_tick = server_tick();
|
2008-09-23 18:08:19 +00:00
|
|
|
character = 0;
|
|
|
|
this->client_id = client_id;
|
2008-08-14 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 09:03:49 +00:00
|
|
|
PLAYER::~PLAYER()
|
|
|
|
{
|
|
|
|
delete character;
|
|
|
|
character = 0;
|
|
|
|
}
|
|
|
|
|
2008-08-14 18:46:52 +00:00
|
|
|
void PLAYER::tick()
|
|
|
|
{
|
|
|
|
server_setclientscore(client_id, score);
|
|
|
|
|
|
|
|
// do latency stuff
|
|
|
|
{
|
|
|
|
CLIENT_INFO info;
|
|
|
|
if(server_getclientinfo(client_id, &info))
|
|
|
|
{
|
|
|
|
latency.accum += info.latency;
|
|
|
|
latency.accum_max = max(latency.accum_max, info.latency);
|
|
|
|
latency.accum_min = min(latency.accum_min, info.latency);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(server_tick()%server_tickspeed() == 0)
|
|
|
|
{
|
|
|
|
latency.avg = latency.accum/server_tickspeed();
|
|
|
|
latency.max = latency.accum_max;
|
|
|
|
latency.min = latency.accum_min;
|
|
|
|
latency.accum = 0;
|
|
|
|
latency.accum_min = 1000;
|
|
|
|
latency.accum_max = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-09-24 14:54:51 +00:00
|
|
|
|
|
|
|
if(character)
|
|
|
|
{
|
|
|
|
if(character->alive)
|
|
|
|
{
|
|
|
|
view_pos = character->pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete character;
|
|
|
|
character = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-10-17 15:38:36 +00:00
|
|
|
else if(spawning && respawn_tick <= server_tick())
|
2008-08-14 18:46:52 +00:00
|
|
|
try_respawn();
|
|
|
|
}
|
|
|
|
|
2008-10-17 21:16:23 +00:00
|
|
|
void PLAYER::snap(int snapping_client)
|
2008-08-14 18:46:52 +00:00
|
|
|
{
|
2008-10-08 20:47:56 +00:00
|
|
|
NETOBJ_CLIENT_INFO *client_info = (NETOBJ_CLIENT_INFO *)snap_new_item(NETOBJTYPE_CLIENT_INFO, client_id, sizeof(NETOBJ_CLIENT_INFO));
|
|
|
|
str_to_ints(&client_info->name0, 6, server_clientname(client_id));
|
|
|
|
str_to_ints(&client_info->skin0, 6, skin_name);
|
|
|
|
client_info->use_custom_color = use_custom_color;
|
|
|
|
client_info->color_body = color_body;
|
|
|
|
client_info->color_feet = color_feet;
|
|
|
|
|
2008-08-14 18:46:52 +00:00
|
|
|
NETOBJ_PLAYER_INFO *info = (NETOBJ_PLAYER_INFO *)snap_new_item(NETOBJTYPE_PLAYER_INFO, client_id, sizeof(NETOBJ_PLAYER_INFO));
|
|
|
|
|
|
|
|
info->latency = latency.min;
|
|
|
|
info->latency_flux = latency.max-latency.min;
|
|
|
|
info->local = 0;
|
|
|
|
info->cid = client_id;
|
|
|
|
info->score = score;
|
|
|
|
info->team = team;
|
|
|
|
|
2008-10-17 21:16:23 +00:00
|
|
|
if(client_id == snapping_client)
|
2008-08-14 18:46:52 +00:00
|
|
|
info->local = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::on_disconnect()
|
|
|
|
{
|
|
|
|
kill_character();
|
|
|
|
|
|
|
|
//game.controller->on_player_death(&game.players[client_id], 0, -1);
|
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
str_format(buf, sizeof(buf), "%s has left the game", server_clientname(client_id));
|
|
|
|
game.send_chat(-1, GAMECONTEXT::CHAT_ALL, buf);
|
|
|
|
|
|
|
|
dbg_msg("game", "leave player='%d:%s'", client_id, server_clientname(client_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::on_predicted_input(NETOBJ_PLAYER_INPUT *new_input)
|
|
|
|
{
|
|
|
|
CHARACTER *chr = get_character();
|
|
|
|
if(chr)
|
|
|
|
chr->on_predicted_input(new_input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::on_direct_input(NETOBJ_PLAYER_INPUT *new_input)
|
|
|
|
{
|
|
|
|
CHARACTER *chr = get_character();
|
|
|
|
if(chr)
|
|
|
|
chr->on_direct_input(new_input);
|
|
|
|
|
|
|
|
if(!chr && team >= 0 && (new_input->fire&1))
|
|
|
|
{
|
|
|
|
spawning = true;
|
|
|
|
dbg_msg("", "I wanna spawn");
|
|
|
|
}
|
2008-09-09 15:50:41 +00:00
|
|
|
|
|
|
|
if(!chr && team == -1)
|
|
|
|
view_pos = vec2(new_input->target_x, new_input->target_y);
|
2008-08-14 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CHARACTER *PLAYER::get_character()
|
|
|
|
{
|
2008-09-23 18:08:19 +00:00
|
|
|
if(character && character->alive)
|
|
|
|
return character;
|
2008-08-14 18:46:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::kill_character()
|
|
|
|
{
|
2008-09-23 18:08:19 +00:00
|
|
|
//CHARACTER *chr = get_character();
|
|
|
|
if(character)
|
|
|
|
{
|
2008-10-14 12:11:42 +00:00
|
|
|
character->die(client_id, -1);
|
2008-09-23 18:08:19 +00:00
|
|
|
delete character;
|
|
|
|
character = 0;
|
|
|
|
}
|
2008-08-14 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::respawn()
|
|
|
|
{
|
2008-10-18 09:21:40 +00:00
|
|
|
if(team > -1)
|
|
|
|
spawning = true;
|
2008-08-14 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::set_team(int new_team)
|
|
|
|
{
|
|
|
|
// clamp the team
|
|
|
|
new_team = game.controller->clampteam(new_team);
|
|
|
|
if(team == new_team)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
str_format(buf, sizeof(buf), "%s joined the %s", server_clientname(client_id), game.controller->get_team_name(new_team));
|
|
|
|
game.send_chat(-1, GAMECONTEXT::CHAT_ALL, buf);
|
|
|
|
|
|
|
|
kill_character();
|
|
|
|
team = new_team;
|
|
|
|
score = 0;
|
|
|
|
dbg_msg("game", "team_join player='%d:%s' team=%d", client_id, server_clientname(client_id), team);
|
|
|
|
|
2008-09-23 18:08:19 +00:00
|
|
|
game.controller->on_player_info_change(game.players[client_id]);
|
2008-08-14 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PLAYER::try_respawn()
|
|
|
|
{
|
|
|
|
vec2 spawnpos = vec2(100.0f, -60.0f);
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
if(!game.controller->can_spawn(this, &spawnpos))
|
|
|
|
return;
|
2008-08-14 18:46:52 +00:00
|
|
|
|
|
|
|
// check if the position is occupado
|
|
|
|
ENTITY *ents[2] = {0};
|
|
|
|
int num_ents = game.world.find_entities(spawnpos, 64, ents, 2, NETOBJTYPE_CHARACTER);
|
|
|
|
|
|
|
|
if(num_ents == 0)
|
|
|
|
{
|
|
|
|
spawning = false;
|
2008-09-24 09:03:49 +00:00
|
|
|
character = new(client_id) CHARACTER();
|
2008-09-23 18:08:19 +00:00
|
|
|
character->spawn(this, spawnpos, team);
|
2008-08-14 18:46:52 +00:00
|
|
|
}
|
|
|
|
}
|