From 9e62c5c9ccafeff6cf991eaf69db34ebef70a0a1 Mon Sep 17 00:00:00 2001 From: nullprop Date: Mon, 17 Mar 2025 01:47:55 +0200 Subject: [PATCH] Add zone saving and loading --- timer.nut | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/timer.nut b/timer.nut index 4dd9302..46485b8 100644 --- a/timer.nut +++ b/timer.nut @@ -20,6 +20,11 @@ MASK_ATTACK_TRACE <- return NetProps.GetPropString(ply, "m_szNetname"); } +::GetZoneFileName <- function() +{ + return "zones_" + GetMapName(); +} + class Zone { // aabb @@ -94,6 +99,38 @@ class Zone Log(format("min: [%f, %f, %f]", this.min.x, this.min.y, this.min.z)); Log(format("max: [%f, %f, %f]", this.max.x, this.max.y, this.max.z)); } + + // Serialize zone into space separated floats. + function ToString() + { + return format( + "%f %f %f %f %f %f", + this.min.x, this.min.y, this.min.z, + this.max.x, this.max.y, this.max.z + ); + } + + // Deserialize zone from a string of 6 floats. + function FromString(str) + { + local parts = split(str, " "); + if (parts.len() != 6) + { + return null; + } + + local min = Vector(); + min.x = parts[0].tofloat(); + min.y = parts[1].tofloat(); + min.z = parts[2].tofloat(); + + local max = Vector(); + max.x = parts[3].tofloat(); + max.y = parts[4].tofloat(); + max.z = parts[5].tofloat(); + + return Zone(min, max); + } } class ZoneBuilder @@ -281,6 +318,63 @@ class Timer } } + function SaveZones() + { + if (this.zones.len() <= 0) + { + PrintToPlayer(null, "No zones to save"); + return; + } + + local zone_str = ""; + for (local i = 0; i < this.zones.len(); i++) + { + zone_str += this.zones[i].ToString(); + if (i < this.zones.len() - 1) + { + zone_str += "\n"; + } + } + + local file_name = GetZoneFileName(); + StringToFile(file_name, zone_str); + PrintToPlayer(null, format("Saved %d zones to '%s'", this.zones.len(), file_name)); + } + + function LoadZones() + { + local file_name = GetZoneFileName(); + local zone_str = FileToString(file_name); + if (zone_str == null) + { + PrintToPlayer(null, format("Could not load zones from '%s'", file_name)); + Log("No file"); + return; + } + + local lines = split(zone_str, "\n"); + Log(format("Found %d lines in %s", lines.len(), file_name)); + + this.zones = []; + + foreach (line in lines) + { + local zone = Zone.FromString(line); + if (zone == null) + { + PrintToPlayer(null, format("Could not load zones from '%s'", file_name)); + Log(format("Malformed line: %s", line)); + this.zones = []; + return; + } + this.zones.append(zone); + } + + PrintToPlayer(null, format("Loaded %d zones from '%s'", this.zones.len(), file_name)); + + DrawZones(); + } + function Tick() { this.tick_count++; @@ -454,6 +548,24 @@ class Timer } } +// Save zones to a text file in scriptdata folder. +::SaveZones <- function() +{ + if ("cool_timer" in getroottable()) + { + ::cool_timer.SaveZones(); + } +} + +// Load zones from a text file in scriptdata folder. +::LoadZones <- function() +{ + if ("cool_timer" in getroottable()) + { + ::cool_timer.LoadZones(); + } +} + // -------------------------------- // Hooks // --------------------------------