diff --git a/common/proto10/codec.h b/common/proto10/codec.h index 5422bcc..63b9853 100644 --- a/common/proto10/codec.h +++ b/common/proto10/codec.h @@ -553,6 +553,7 @@ int p10_decode_chat_broadcast(const uint8_t* data, size_t size, p10_chat_broadca #define P10_GM_TEAM1_SCORE "team1-score" #define P10_GM_TEAM2_SCORE "team2-score" #define P10_GM_CAPTURE_LIMIT "capture-limit" +#define P10_GM_MATCH_TIME "match-time" /* seconds left of a timed match (absent: no time limit) */ #define P10_GM_TEAM1_INTEL "team1-intel" #define P10_GM_TEAM2_INTEL "team2-intel" /* The objectives' entities (12.2.3) have fixed ids: P10_OBJECTIVE_ID_BASE + diff --git a/docs/protocol-1.0.md b/docs/protocol-1.0.md index 7af6fee..ea590fc 100644 --- a/docs/protocol-1.0.md +++ b/docs/protocol-1.0.md @@ -1864,6 +1864,7 @@ them, each within `int32`. Positions are **block cells** in map coordinates | `team1-color`, `team2-color` | `r,g,b` (0..255) | team colours | | `team1-score`, `team2-score` | decimal | team scores (CTF: captures) | | `capture-limit` | decimal | CTF captures needed to win | +| `match-time` | decimal | Seconds left of the match's time limit, in the point-scored modes (CTF, ngCTF, Babel, TC, TDM, Koth: 20 min, 15 min for TDM and Koth by default, `match_seconds` in the mode's section of config.toml); absent in the others (Arena, Zombie and PtB play rounds, Creative has no end). A second apart in the last minute, to the next 10 s before it. When it reaches 0 the match ends by the score (a tie is a draw), `/extend` adds to it. A client may use it for a countdown or the closing music | | `team1-intel`, `team2-intel` | `base`, `dropped`, `carried` | the state of the intel **belonging** to that team (team 1 defends `team1-intel`); where it is and who carries it are its `Flag` entity's (below) | | `territory-count` | decimal `N` | TC: number of territories | | `territory` | `holder,capturing,permille,rate` | TC: territory `i` (`0` ≤ `i` < `N`, decimal without leading zeros: `territory0`, `territory12`): its holder (`0` team 1, `1` team 2, `2` nobody), the team that would take it, that team's share of the progress (0..1000) and its change a second (see below). Its place is its `Checkpoint` entity, id `P10_OBJECTIVE_ID_BASE + 5 + i` | diff --git a/server/Resources/config.toml b/server/Resources/config.toml index 65c877e..194d092 100644 --- a/server/Resources/config.toml +++ b/server/Resources/config.toml @@ -255,6 +255,29 @@ spawn_near_min = 35.0 # a zombie's respawn, from a living survivor spawn_near_max = 50.0 +# The time limit of the point-scored modes: match_seconds in the mode's own +# section (30..86400). When it runs out the match ends by the score (a tie is +# a draw), and the clients hear the closing music in the last 5 seconds. A +# match never runs for ever; arena, ptb and zombie play rounds (their own +# round_seconds), creative has no end. /extend adds time. +[ctf] +match_seconds = 1200 # classic CTF + +[ngctf] +match_seconds = 1200 # next gen CTF + +[babel] +match_seconds = 1200 + +[tc] +match_seconds = 1200 # territory control + +[tdm] +match_seconds = 900 # team deathmatch + +[koth] +match_seconds = 900 # king of the hill + # Arena mode (rounds without respawns on Counter-Strike maps). All keys are # optional; a map's own [arena] table and its .txt narrow them down for that # map, and the mode's rounds are won by the map's capture_limit. diff --git a/server/Source/Server/ConfigCheck.c b/server/Source/Server/ConfigCheck.c index 67a7f8f..df64899 100644 --- a/server/Source/Server/ConfigCheck.c +++ b/server/Source/Server/ConfigCheck.c @@ -261,6 +261,19 @@ static void _net(char* out, size_t size, toml_table_t* root) _int_in(out, size, "net", t, "connection_window_seconds", 1, 3600, 10); } +static void _match(char* out, size_t size, toml_table_t* root) +{ + static const struct { const char* section; int normal; } modes[] = { + {"ctf", 1200}, {"ngctf", 1200}, {"babel", 1200}, {"tc", 1200}, {"tdm", 900}, {"koth", 900}, + }; + for (size_t k = 0; k < sizeof modes / sizeof modes[0]; k++) { + toml_table_t* t = toml_table_in(root, modes[k].section); + if (t != NULL) { + _int_in(out, size, modes[k].section, t, "match_seconds", 30, 86400, modes[k].normal); + } + } +} + static void _zombie(char* out, size_t size, toml_table_t* root) { toml_table_t* t = toml_table_in(root, "zombie"); @@ -476,6 +489,7 @@ int config_check(const char* path, char* out, size_t size) _server(out, size, root); _teams(out, size, root); _net(out, size, root); + _match(out, size, root); _zombie(out, size, root); _conduct(out, size, root); _anticheat(out, size, root); diff --git a/server/Source/Server/ConfigKeys.c b/server/Source/Server/ConfigKeys.c index bbf275b..065e550 100644 --- a/server/Source/Server/ConfigKeys.c +++ b/server/Source/Server/ConfigKeys.c @@ -180,6 +180,17 @@ static const ck_key_t ck_anticheat_keys[] = { {"enabled", CK_ALL}, {"kick_on_protocol_violation", CK_ALL}, {"log_interval_seconds", CK_ALL}, CK_END, }; +/* Gamemodes/Gamemode.c gamemode_match_seconds: the time limit of the + point-scored modes, in the mode's own section (the 0.75 profile has ctf and babel) */ +static const ck_key_t ck_match_seconds_keys[] = { + {"match_seconds", CK_ALL}, + CK_END, +}; +static const ck_key_t ck_match_seconds_keys_10[] = { + {"match_seconds", CK_10}, + CK_END, +}; + /* Gamemodes/Arena.c: rounds, whose timing the map's [arena] and its .txt narrow down (the arena is a 1.0 mode: the 0.75 profile has ctf and babel) */ static const ck_key_t ck_arena_keys[] = { @@ -315,6 +326,12 @@ static const ck_table_t ck_top_subs[] = { {.name = "classes", .prof = CK_10, .any = &ck_class_shape, .sub_known = ck_class_name}, {.name = "net", .keys = ck_net_keys}, {.name = "zombie", .prof = CK_10, .keys = ck_zombie_keys}, + {.name = "ctf", .keys = ck_match_seconds_keys}, + {.name = "babel", .keys = ck_match_seconds_keys}, + {.name = "ngctf", .prof = CK_10, .keys = ck_match_seconds_keys_10}, + {.name = "tc", .prof = CK_10, .keys = ck_match_seconds_keys_10}, + {.name = "tdm", .prof = CK_10, .keys = ck_match_seconds_keys_10}, + {.name = "koth", .prof = CK_10, .keys = ck_match_seconds_keys_10}, {.name = "arena", .prof = CK_10, .keys = ck_arena_keys}, {.name = "ptb", .prof = CK_10, .keys = ck_ptb_keys}, {.name = "conduct", .keys = ck_conduct_keys}, diff --git a/server/Source/Server/Gamemodes/Ctf.c b/server/Source/Server/Gamemodes/Ctf.c index 007eb59..4a52fa1 100644 --- a/server/Source/Server/Gamemodes/Ctf.c +++ b/server/Source/Server/Gamemodes/Ctf.c @@ -30,6 +30,9 @@ #include #include #include + +/* the match's time limit, below (_ctf_time_check): set when a capture wins */ +static uint8_t ctf_ended; #include #include #include @@ -597,6 +600,7 @@ void handleTentAndIntel(server_t* server, player_t* player) server->protocol.gamemode.intel[team] = set_intel_tent_spawn_point(server, team, MAP_META_FLAG); } if (winning) { + ctf_ended = 1; gamemode_match_won(server); } } @@ -613,9 +617,46 @@ static void _ctf_base_snapshot(server_t* server); static void _ctf_posts_reset(void); static void _ctf_posts_tick(server_t* server); +/* the match's time limit: when it runs out the team with more captures wins, + equal captures are a draw. CTF used to end only at the capture limit and + could go on for ever (SP_MATCH_SECONDS shortens it for tests) */ +#define CTF_SECONDS 1200 +static uint64_t ctf_deadline; + +int _ctf_time_left(server_t* server) +{ + (void) server; + return ctf_ended ? 0 : gamemode_seconds_until(ctf_deadline); +} + +int _ctf_extend(server_t* server, int seconds) +{ + ctf_deadline += (uint64_t) seconds * NANO_IN_SECOND; + server->match_extra_seconds += (uint32_t) seconds; + evt_log(NULL, "ctf", "action=extend seconds=%d", seconds); + return 1; +} + +static void _ctf_time_check(server_t* server) +{ + if (ctf_ended || get_nanos() < ctf_deadline) { + return; + } + ctf_ended = 1; + evt_log(NULL, "ctf", "action=end why=time score=%u-%u", (unsigned) server->protocol.gamemode.score[0], + (unsigned) server->protocol.gamemode.score[1]); + gamemode_match_won(server); +} + void _ctf_init(server_t* server) { gamemode_vars_t* gm = &server->protocol.gamemode; + uint8_t mode = server->protocol.current_gamemode; + ctf_deadline = get_nanos() + (uint64_t) gamemode_match_seconds(mode == GAME_MODE_NGCTF ? "ngctf" + : mode == GAME_MODE_BABEL ? "babel" + : "ctf", + CTF_SECONDS) * NANO_IN_SECOND; + ctf_ended = 0; memset(ctf_spots, 0, sizeof(ctf_spots)); /* a new map: a script sets them again */ gm->intel_flags = 0; gm->intel[0] = set_intel_tent_spawn_point(server, 0, MAP_META_FLAG); @@ -1423,6 +1464,7 @@ static void _ctf_lane_fort(server_t* server, int t, vector3f_t w) leader (a new one only when it left), and how far along its lane it got */ void _ctf_tick(server_t* server) { + _ctf_time_check(server); _ctf_seats_tick(server); squads_census(server); _ctf_forts(server); @@ -4078,6 +4120,8 @@ const gamemode_ops_t ctf_ops = { .wire_name = "ctf", .init = _ctf_init, .tick = _ctf_tick, +.extend = _ctf_extend, +.time_left = _ctf_time_left, .player_step = handleTentAndIntel, .player_out = _ctf_player_out, .terrain_changed = _ctf_terrain_changed, @@ -4130,6 +4174,7 @@ static void _babel_slab_audit(server_t* server) static void _babel_tick(server_t* server) { + _ctf_time_check(server); static uint64_t logged_at; uint64_t now = get_nanos(); _babel_slab_audit(server); @@ -4203,6 +4248,8 @@ const gamemode_ops_t babel_ops = { .wire_name = "ctf", .init = _babel_init, .tick = _babel_tick, +.extend = _ctf_extend, +.time_left = _ctf_time_left, .player_step = handleTentAndIntel, .player_out = _ctf_player_out, .terrain_changed = _ctf_terrain_changed, diff --git a/server/Source/Server/Gamemodes/CtfInternal.h b/server/Source/Server/Gamemodes/CtfInternal.h index 298235d..432d3bc 100644 --- a/server/Source/Server/Gamemodes/CtfInternal.h +++ b/server/Source/Server/Gamemodes/CtfInternal.h @@ -15,6 +15,8 @@ void handleTentAndIntel(server_t* server, player_t* player); void _ctf_init(server_t* server); void _ctf_tick(server_t* server); +int _ctf_time_left(server_t* server); +int _ctf_extend(server_t* server, int seconds); void _ctf_player_out(server_t* server, player_t* player); void _ctf_terrain_changed(server_t* server, int built); void _ctf_state_keys(server_t* server, gm_keys_t* out); diff --git a/server/Source/Server/Gamemodes/Gamemode.c b/server/Source/Server/Gamemodes/Gamemode.c index 59dee6c..7668c43 100644 --- a/server/Source/Server/Gamemodes/Gamemode.c +++ b/server/Source/Server/Gamemodes/Gamemode.c @@ -298,6 +298,43 @@ static void _match_summary_send(server_t* server, int wait_seconds, int voting) winner, gm->score[0], gm->score[1], length, players, parts[6], parts[7], wait_seconds); } +int gamemode_match_seconds(const char* section, int normal) +{ + const char* env = getenv("SP_MATCH_SECONDS"); + int n = env != NULL ? atoi(env) : 0; + if (n >= 1 && n <= 86400) { + return n; + } + FILE* fp = fopen(server_config_path(), "r"); + if (fp == NULL) { + return normal; + } + char err[200]; + toml_table_t* conf = toml_parse_file(fp, err, sizeof err); + fclose(fp); + if (conf == NULL) { + return normal; + } + int seconds = normal; + toml_table_t* table = toml_table_in(conf, section); + if (table != NULL) { + toml_datum_t d = toml_int_in(table, "match_seconds"); + if (d.ok && d.u.i >= 30 && d.u.i <= 86400) { + seconds = (int) d.u.i; + } else if (d.ok) { + LOG_WARNING("config.toml [%s] match_seconds = %lld is outside 30..86400: %d s applies", section, (long long) d.u.i, normal); + } + } + toml_free(conf); + return seconds; +} + +int gamemode_seconds_until(uint64_t deadline) +{ + uint64_t now = get_nanos(); + return now >= deadline ? 0 : (int) ((deadline - now + NANO_IN_SECOND - 1) / NANO_IN_SECOND); +} + void gamemode_match_won(server_t* server) { if (match_reset_at != 0) { diff --git a/server/Source/Server/Gamemodes/Gamemode.h b/server/Source/Server/Gamemodes/Gamemode.h index 31eca48..ec3f0a0 100644 --- a/server/Source/Server/Gamemodes/Gamemode.h +++ b/server/Source/Server/Gamemodes/Gamemode.h @@ -137,6 +137,10 @@ typedef struct gamemode_ops void (*tick)(server_t* server); /* /extend: add seconds to a timed match; 1 when the mode did */ int (*extend)(server_t* server, int seconds); + /* seconds left of the match's time limit (0 once it has run out or the + match is over); -1 when the mode has none. Sent to the clients as + GameModeState's match-time */ + int (*time_left)(server_t* server); /* every server tick for each READY player (intel pickup, capture, tent: a player running over the intel is on it for a fraction of a second) */ void (*player_step)(server_t* server, player_t* player); @@ -231,6 +235,13 @@ void gamemode_tick(server_t* server); /* 1 when the mode added the seconds to its clock; 0 when its match has no time limit (the command says so and changes nothing) */ int gamemode_extend(server_t* server, int seconds); +/* the time limit of a point-scored mode, in seconds: config.toml's + match_seconds in the mode's own section ([ctf], [ngctf], [babel], [tc], + [tdm], [koth]: 30..86400) or normal, and $SP_MATCH_SECONDS over both when + set (tests: a match that ends in a few seconds). A match never runs for ever */ +int gamemode_match_seconds(const char* section, int normal); +/* whole seconds, rounded up, from now to a deadline (get_nanos clock); 0 when past */ +int gamemode_seconds_until(uint64_t deadline); /* a team won the match: everyone reloads, the next map starts */ void gamemode_match_won(server_t* server); diff --git a/server/Source/Server/Gamemodes/Koth.c b/server/Source/Server/Gamemodes/Koth.c index 6c06ff3..84eaa70 100644 --- a/server/Source/Server/Gamemodes/Koth.c +++ b/server/Source/Server/Gamemodes/Koth.c @@ -232,7 +232,7 @@ static void _koth_init(server_t* server) server->protocol.gamemode.score[0] = 0; server->protocol.gamemode.score[1] = 0; uint64_t now = get_nanos(); - koth.deadline = now + (uint64_t) KOTH_SECONDS * NANO_IN_SECOND; + koth.deadline = now + (uint64_t) gamemode_match_seconds("koth", KOTH_SECONDS) * NANO_IN_SECOND; koth.next_score = now; koth.next_fort = now; koth.ended = 0; @@ -261,7 +261,7 @@ static void _koth_init(server_t* server) server->features_on |= GM_FEAT_CLASSES | GM_FEAT_CLASS_PERKS; } LOG_INFO("King of the Hill: hill at %d,%d,%d, %d s held a round, %d rounds win, %d s at most", koth.hx, - koth.hy, koth.hz, KOTH_ROUND_POINTS, KOTH_ROUNDS, KOTH_SECONDS); + koth.hy, koth.hz, KOTH_ROUND_POINTS, KOTH_ROUNDS, (int) ((koth.deadline - now) / NANO_IN_SECOND)); evt_log(NULL, "koth", "action=start at=%d,%d,%d round_points=%d rounds=%d classes=%d", koth.hx, koth.hy, koth.hz, KOTH_ROUND_POINTS, KOTH_ROUNDS, classes); } @@ -469,6 +469,12 @@ static void _koth_state_keys(server_t* server, gm_keys_t* out) } } +static int _koth_time_left(server_t* server) +{ + (void) server; + return koth.ended ? 0 : gamemode_seconds_until(koth.deadline); +} + static int _koth_extend(server_t* server, int seconds) { koth.deadline += (uint64_t) seconds * NANO_IN_SECOND; @@ -734,6 +740,7 @@ const gamemode_ops_t koth_ops = { .init = _koth_init, .tick = _koth_tick, .extend = _koth_extend, + .time_left = _koth_time_left, .state_keys = _koth_state_keys, .summary_stat = "hill", .player_stat = _koth_player_hill, diff --git a/server/Source/Server/Gamemodes/NgCtf.c b/server/Source/Server/Gamemodes/NgCtf.c index 14e548c..2ec19bd 100644 --- a/server/Source/Server/Gamemodes/NgCtf.c +++ b/server/Source/Server/Gamemodes/NgCtf.c @@ -93,6 +93,8 @@ const gamemode_ops_t ngctf_ops = { .hit_damage = classes_hit_damage, .init = _ctf_init, .tick = _ctf_tick, +.extend = _ctf_extend, +.time_left = _ctf_time_left, .player_step = handleTentAndIntel, .player_out = _ctf_player_out, .terrain_changed = _ctf_terrain_changed, diff --git a/server/Source/Server/Gamemodes/Tc.c b/server/Source/Server/Gamemodes/Tc.c index 3e8ea3c..4aac25f 100644 --- a/server/Source/Server/Gamemodes/Tc.c +++ b/server/Source/Server/Gamemodes/Tc.c @@ -44,7 +44,7 @@ #define TC_SPAWN_THREAT 96.0f /* an enemy this near a territory: spawn one back */ #define TC_REFILL_DISTANCE 3.0f #define TC_REFILL_SECONDS 20 -#define TC_SECONDS 1200 +#define TC_SECONDS 1200 /* gamemode_match_seconds */ /* a squad walks to its territory behind its leader until one of them is this near it, then each bot takes its own place in it */ #define TC_SQUAD_SPLIT (TC_CAPTURE_DISTANCE * 2.5f) @@ -229,7 +229,7 @@ static void _tc_init(server_t* server) server->protocol.gamemode.score_limit = (uint8_t) tc.count; _tc_score(server); uint64_t now = get_nanos(); - tc.deadline = now + (uint64_t) TC_SECONDS * NANO_IN_SECOND; + tc.deadline = now + (uint64_t) gamemode_match_seconds("tc", TC_SECONDS) * NANO_IN_SECOND; tc.last_step = now; /* the classes are the host's choice, off unless asked: the command line (--classes 0|1) first, then the console (/classes) */ @@ -241,7 +241,7 @@ static void _tc_init(server_t* server) server->features_on |= GM_FEAT_CLASSES | GM_FEAT_CLASS_PERKS; } LOG_INFO("Territory control: %d territories, %u-%u held, %d s at most, classes %s", tc.count, - server->protocol.gamemode.score[0], server->protocol.gamemode.score[1], TC_SECONDS, classes ? "on" : "off"); + server->protocol.gamemode.score[0], server->protocol.gamemode.score[1], (int) ((tc.deadline - now) / NANO_IN_SECOND), classes ? "on" : "off"); evt_log(NULL, "tc", "action=start territories=%d held=%u-%u axis=%c classes=%d", tc.count, server->protocol.gamemode.score[0], server->protocol.gamemode.score[1], tc.axis ? 'y' : 'x', classes); } @@ -475,6 +475,12 @@ static void _tc_state_keys(server_t* server, gm_keys_t* out) } } +static int _tc_time_left(server_t* server) +{ + (void) server; + return tc.ended ? 0 : gamemode_seconds_until(tc.deadline); +} + static int _tc_extend(server_t* server, int seconds) { tc.deadline += (uint64_t) seconds * NANO_IN_SECOND; @@ -772,6 +778,7 @@ const gamemode_ops_t tc_ops = { .init = _tc_init, .tick = _tc_tick, .extend = _tc_extend, + .time_left = _tc_time_left, .state_keys = _tc_state_keys, .summary_stat = "captures", .player_stat = _tc_player_captures, diff --git a/server/Source/Server/Gamemodes/Tdm.c b/server/Source/Server/Gamemodes/Tdm.c index 52d1e19..4165bd4 100644 --- a/server/Source/Server/Gamemodes/Tdm.c +++ b/server/Source/Server/Gamemodes/Tdm.c @@ -27,10 +27,11 @@ static void _tdm_init(server_t* server) server->protocol.gamemode.score[0] = 0; server->protocol.gamemode.score[1] = 0; tdm.start = get_nanos(); - tdm.deadline = tdm.start + (uint64_t) TDM_SECONDS * NANO_IN_SECOND; + tdm.deadline = tdm.start + (uint64_t) gamemode_match_seconds("tdm", TDM_SECONDS) * NANO_IN_SECOND; tdm.ended = 0; - LOG_INFO("Team deathmatch: %d kills, %d s", TDM_SCORE_LIMIT, TDM_SECONDS); - evt_log(NULL, "tdm", "action=start limit=%d seconds=%d", TDM_SCORE_LIMIT, TDM_SECONDS); + int seconds = (int) ((tdm.deadline - tdm.start) / NANO_IN_SECOND); + LOG_INFO("Team deathmatch: %d kills, %d s", TDM_SCORE_LIMIT, seconds); + evt_log(NULL, "tdm", "action=start limit=%d seconds=%d", TDM_SCORE_LIMIT, seconds); } static void _tdm_tick(server_t* server) @@ -62,6 +63,12 @@ static void _tdm_killed(server_t* server, player_t* killer, player_t* victim) } } +static int _tdm_time_left(server_t* server) +{ + (void) server; + return tdm.ended ? 0 : gamemode_seconds_until(tdm.deadline); +} + static int _tdm_extend(server_t* server, int seconds) { tdm.deadline += (uint64_t) seconds * NANO_IN_SECOND; @@ -83,6 +90,7 @@ const gamemode_ops_t tdm_ops = { .init = _tdm_init, .tick = _tdm_tick, .extend = _tdm_extend, + .time_left = _tdm_time_left, .bot_goal = _tdm_bot_goal, .player_killed = _tdm_killed, }; diff --git a/server/Source/Server/Proto10/GameMode.c b/server/Source/Server/Proto10/GameMode.c index 80c2800..facc8d7 100644 --- a/server/Source/Server/Proto10/GameMode.c +++ b/server/Source/Server/Proto10/GameMode.c @@ -69,6 +69,13 @@ static void _gm_compute(server_t* server, gm_keys_t* s) gm_keys_put(s, t == 0 ? P10_GM_TEAM1_SCORE : P10_GM_TEAM2_SCORE, "%u", (unsigned) gm->score[t]); } gm_keys_put(s, P10_GM_CAPTURE_LIMIT, "%u", (unsigned) gm->score_limit); + /* the time limit of a point-scored mode: exact in the last minute, to + 10 s before it, so a long match does not resend the state every second */ + const gamemode_ops_t* ops = gamemode_current(server); + int left = ops->time_left != NULL ? ops->time_left(server) : -1; + if (left >= 0) { + gm_keys_put(s, P10_GM_MATCH_TIME, "%d", left <= 60 ? left : (left + 9) / 10 * 10); + } /* the mode marks the objectives it has (Flags.c) while it writes its keys */ proto10_flags_begin(); gamemode_state_keys(server, s);