2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2008-01-13 11:43:43 +00:00
|
|
|
#include <string.h>
|
2007-12-15 10:24:49 +00:00
|
|
|
#include <engine/e_config.h>
|
2008-01-13 11:43:43 +00:00
|
|
|
#include <game/g_mapitems.h>
|
2007-12-15 10:24:49 +00:00
|
|
|
#include "gs_common.h"
|
2007-10-05 00:05:21 +00:00
|
|
|
|
|
|
|
gameobject::gameobject()
|
|
|
|
: entity(OBJTYPE_GAME)
|
|
|
|
{
|
|
|
|
// select gametype
|
2007-12-11 23:10:07 +00:00
|
|
|
if(strcmp(config.sv_gametype, "ctf") == 0)
|
2007-10-05 00:05:21 +00:00
|
|
|
{
|
|
|
|
gametype = GAMETYPE_CTF;
|
|
|
|
dbg_msg("game", "-- Capture The Flag --");
|
|
|
|
}
|
2007-12-11 23:10:07 +00:00
|
|
|
else if(strcmp(config.sv_gametype, "tdm") == 0)
|
2007-10-05 00:05:21 +00:00
|
|
|
{
|
|
|
|
gametype = GAMETYPE_TDM;
|
|
|
|
dbg_msg("game", "-- Team Death Match --");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gametype = GAMETYPE_DM;
|
|
|
|
dbg_msg("game", "-- Death Match --");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2007-12-11 23:10:07 +00:00
|
|
|
do_warmup(config.sv_warmup);
|
2007-10-05 00:05:21 +00:00
|
|
|
game_over_tick = -1;
|
|
|
|
sudden_death = 0;
|
|
|
|
round_start_tick = server_tick();
|
|
|
|
round_count = 0;
|
2007-11-04 00:19:41 +00:00
|
|
|
is_teamplay = false;
|
2007-12-09 09:47:05 +00:00
|
|
|
teamscore[0] = 0;
|
|
|
|
teamscore[1] = 0;
|
2007-10-05 00:05:21 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 11:43:43 +00:00
|
|
|
// UGLY!!!!
|
|
|
|
extern vec2 spawn_points[3][64];
|
|
|
|
extern int num_spawn_points[3];
|
|
|
|
|
|
|
|
bool gameobject::on_entity(int index, vec2 pos)
|
|
|
|
{
|
|
|
|
int type = -1;
|
|
|
|
int subtype = 0;
|
|
|
|
|
|
|
|
if(index == ENTITY_SPAWN)
|
|
|
|
spawn_points[0][num_spawn_points[0]++] = pos;
|
|
|
|
else if(index == ENTITY_SPAWN_RED)
|
|
|
|
spawn_points[1][num_spawn_points[1]++] = pos;
|
|
|
|
else if(index == ENTITY_SPAWN_BLUE)
|
|
|
|
spawn_points[2][num_spawn_points[2]++] = pos;
|
|
|
|
else if(index == ENTITY_ARMOR_1)
|
|
|
|
type = POWERUP_ARMOR;
|
|
|
|
else if(index == ENTITY_HEALTH_1)
|
|
|
|
type = POWERUP_HEALTH;
|
|
|
|
else if(index == ENTITY_WEAPON_SHOTGUN)
|
|
|
|
{
|
|
|
|
type = POWERUP_WEAPON;
|
|
|
|
subtype = WEAPON_SHOTGUN;
|
|
|
|
}
|
|
|
|
else if(index == ENTITY_WEAPON_ROCKET)
|
|
|
|
{
|
|
|
|
type = POWERUP_WEAPON;
|
|
|
|
subtype = WEAPON_ROCKET;
|
|
|
|
}
|
|
|
|
else if(index == ENTITY_POWERUP_NINJA)
|
|
|
|
{
|
|
|
|
type = POWERUP_NINJA;
|
|
|
|
subtype = WEAPON_NINJA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(type != -1)
|
|
|
|
{
|
|
|
|
powerup *ppower = new powerup(type, subtype);
|
|
|
|
ppower->pos = pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-05 00:05:21 +00:00
|
|
|
void gameobject::endround()
|
|
|
|
{
|
2007-10-06 17:36:24 +00:00
|
|
|
if(warmup) // game can't end when we are running warmup
|
|
|
|
return;
|
|
|
|
|
2007-10-05 00:05:21 +00:00
|
|
|
world->paused = true;
|
|
|
|
game_over_tick = server_tick();
|
|
|
|
sudden_death = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameobject::resetgame()
|
|
|
|
{
|
|
|
|
world->reset_requested = true;
|
|
|
|
}
|
|
|
|
|
2008-01-12 12:27:55 +00:00
|
|
|
static bool is_separator(char c) { return c == ';' || c == ' ' || c == ',' || c == '\t'; }
|
2007-10-05 00:05:21 +00:00
|
|
|
|
|
|
|
void gameobject::startround()
|
|
|
|
{
|
|
|
|
resetgame();
|
|
|
|
|
|
|
|
round_start_tick = server_tick();
|
|
|
|
sudden_death = 0;
|
|
|
|
game_over_tick = -1;
|
|
|
|
world->paused = false;
|
|
|
|
teamscore[0] = 0;
|
|
|
|
teamscore[1] = 0;
|
|
|
|
round_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameobject::cyclemap()
|
|
|
|
{
|
|
|
|
if(!strlen(config.sv_maprotation))
|
|
|
|
return;
|
|
|
|
// handle maprotation
|
|
|
|
char buf[512];
|
|
|
|
const char *s = strstr(config.sv_maprotation, config.sv_map);
|
|
|
|
if(s == 0)
|
|
|
|
s = config.sv_maprotation; // restart rotation
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s += strlen(config.sv_map); // skip this map
|
|
|
|
while(is_separator(s[0]))
|
|
|
|
s++;
|
|
|
|
if(s[0] == 0)
|
|
|
|
s = config.sv_maprotation; // restart rotation
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for(; i < 512; i++)
|
|
|
|
{
|
|
|
|
buf[i] = s[i];
|
|
|
|
if(is_separator(s[i]) || s[i] == 0)
|
|
|
|
{
|
|
|
|
buf[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0; // skip spaces
|
|
|
|
while(is_separator(buf[i]))
|
|
|
|
i++;
|
|
|
|
|
|
|
|
dbg_msg("game", "rotating map to %s", &buf[i]);
|
|
|
|
strcpy(config.sv_map, &buf[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameobject::post_reset()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
|
|
|
{
|
|
|
|
if(players[i].client_id != -1)
|
|
|
|
players[i].respawn();
|
|
|
|
}
|
|
|
|
}
|
2007-11-18 14:24:34 +00:00
|
|
|
|
|
|
|
void gameobject::on_player_info_change(class player *p)
|
|
|
|
{
|
2007-11-26 22:26:33 +00:00
|
|
|
const int team_colors[2] = {65387, 10223467};
|
2007-11-18 14:24:34 +00:00
|
|
|
if(is_teamplay)
|
|
|
|
{
|
|
|
|
if(p->team >= 0 || p->team <= 1)
|
|
|
|
{
|
|
|
|
p->use_custom_color = 1;
|
|
|
|
p->color_body = team_colors[p->team];
|
|
|
|
p->color_feet = team_colors[p->team];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-18 23:29:34 +00:00
|
|
|
int gameobject::on_player_death(class player *victim, class player *killer, int weapon)
|
2007-10-05 00:05:21 +00:00
|
|
|
{
|
|
|
|
// do scoreing
|
|
|
|
if(!killer)
|
2007-11-18 23:29:34 +00:00
|
|
|
return 0;
|
2007-10-05 00:05:21 +00:00
|
|
|
if(killer == victim)
|
2007-12-11 21:19:52 +00:00
|
|
|
victim->score--; // suicide
|
2007-10-05 00:05:21 +00:00
|
|
|
else
|
2007-12-11 21:19:52 +00:00
|
|
|
{
|
|
|
|
if(is_teamplay && victim->team == killer->team)
|
|
|
|
killer->score--; // teamkill
|
|
|
|
else
|
|
|
|
killer->score++; // normal kill
|
|
|
|
}
|
2007-11-18 23:29:34 +00:00
|
|
|
return 0;
|
2007-10-05 00:05:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 17:36:24 +00:00
|
|
|
void gameobject::do_warmup(int seconds)
|
|
|
|
{
|
|
|
|
warmup = seconds*SERVER_TICK_SPEED;
|
|
|
|
}
|
|
|
|
|
2007-12-10 20:53:37 +00:00
|
|
|
bool gameobject::is_friendly_fire(int cid1, int cid2)
|
|
|
|
{
|
|
|
|
if(cid1 == cid2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(is_teamplay)
|
|
|
|
{
|
|
|
|
if(players[cid1].team == players[cid2].team)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-05 00:05:21 +00:00
|
|
|
|
|
|
|
void gameobject::tick()
|
|
|
|
{
|
2007-10-06 17:36:24 +00:00
|
|
|
// do warmup
|
|
|
|
if(warmup)
|
|
|
|
{
|
|
|
|
warmup--;
|
|
|
|
if(!warmup)
|
2007-12-10 21:05:57 +00:00
|
|
|
startround();
|
2007-10-06 17:36:24 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 00:05:21 +00:00
|
|
|
if(game_over_tick != -1)
|
|
|
|
{
|
|
|
|
// game over.. wait for restart
|
|
|
|
if(server_tick() > game_over_tick+server_tickspeed()*10)
|
|
|
|
{
|
|
|
|
cyclemap();
|
|
|
|
startround();
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 19:51:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
// update browse info
|
|
|
|
int prog = -1;
|
2007-12-11 23:10:07 +00:00
|
|
|
if(config.sv_timelimit > 0)
|
|
|
|
prog = max(prog, (server_tick()-round_start_tick) * 100 / (config.sv_timelimit*server_tickspeed()*60));
|
2007-11-27 19:51:48 +00:00
|
|
|
|
2007-12-11 23:10:07 +00:00
|
|
|
if(config.sv_scorelimit)
|
2007-11-27 19:51:48 +00:00
|
|
|
{
|
|
|
|
if(is_teamplay)
|
|
|
|
{
|
2007-12-11 23:10:07 +00:00
|
|
|
prog = max(prog, (teamscore[0]*100)/config.sv_scorelimit);
|
|
|
|
prog = max(prog, (teamscore[1]*100)/config.sv_scorelimit);
|
2007-11-27 19:51:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
|
|
|
{
|
|
|
|
if(players[i].client_id != -1)
|
2007-12-11 23:10:07 +00:00
|
|
|
prog = max(prog, (players[i].score*100)/config.sv_scorelimit);
|
2007-11-27 19:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(warmup)
|
|
|
|
prog = -1;
|
2007-12-18 23:21:57 +00:00
|
|
|
|
2007-11-27 19:51:48 +00:00
|
|
|
server_setbrowseinfo(gametype, prog);
|
2007-10-05 00:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gameobject::snap(int snapping_client)
|
|
|
|
{
|
|
|
|
obj_game *game = (obj_game *)snap_new_item(OBJTYPE_GAME, 0, sizeof(obj_game));
|
|
|
|
game->paused = world->paused;
|
|
|
|
game->game_over = game_over_tick==-1?0:1;
|
|
|
|
game->sudden_death = sudden_death;
|
|
|
|
|
2007-12-11 23:10:07 +00:00
|
|
|
game->score_limit = config.sv_scorelimit;
|
|
|
|
game->time_limit = config.sv_timelimit;
|
2007-10-05 00:05:21 +00:00
|
|
|
game->round_start_tick = round_start_tick;
|
|
|
|
game->gametype = gametype;
|
|
|
|
|
2007-10-06 17:36:24 +00:00
|
|
|
game->warmup = warmup;
|
|
|
|
|
2007-10-05 00:05:21 +00:00
|
|
|
game->teamscore[0] = teamscore[0];
|
|
|
|
game->teamscore[1] = teamscore[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
int gameobject::getteam(int notthisid)
|
|
|
|
{
|
|
|
|
int numplayers[2] = {0,0};
|
|
|
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
|
|
|
{
|
|
|
|
if(players[i].client_id != -1 && players[i].client_id != notthisid)
|
|
|
|
{
|
2007-12-16 09:56:26 +00:00
|
|
|
if(players[i].team == 0 || players[i].team == 1)
|
|
|
|
numplayers[players[i].team]++;
|
2007-10-05 00:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numplayers[0] > numplayers[1] ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:47:49 +00:00
|
|
|
void gameobject::do_team_wincheck()
|
|
|
|
{
|
|
|
|
if(game_over_tick == -1 && !warmup)
|
|
|
|
{
|
|
|
|
// check score win condition
|
2007-12-11 23:10:07 +00:00
|
|
|
if((config.sv_scorelimit > 0 && (teamscore[0] >= config.sv_scorelimit || teamscore[1] >= config.sv_scorelimit)) ||
|
|
|
|
(config.sv_timelimit > 0 && (server_tick()-round_start_tick) >= config.sv_timelimit*server_tickspeed()*60))
|
2007-11-26 20:47:49 +00:00
|
|
|
{
|
|
|
|
if(teamscore[0] != teamscore[1])
|
|
|
|
endround();
|
|
|
|
else
|
|
|
|
sudden_death = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-18 23:21:57 +00:00
|
|
|
int gameobject::clampteam(int team)
|
|
|
|
{
|
|
|
|
if(team < 0) // spectator
|
|
|
|
return -1;
|
|
|
|
if(is_teamplay)
|
|
|
|
return team&1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-05 00:05:21 +00:00
|
|
|
gameobject *gameobj = 0;
|