Add zone saving and loading

This commit is contained in:
Lauri Räsänen 2025-03-17 01:47:55 +02:00
parent 057c60a76e
commit 9e62c5c9cc

112
timer.nut
View file

@ -20,6 +20,11 @@ MASK_ATTACK_TRACE <-
return NetProps.GetPropString(ply, "m_szNetname"); return NetProps.GetPropString(ply, "m_szNetname");
} }
::GetZoneFileName <- function()
{
return "zones_" + GetMapName();
}
class Zone class Zone
{ {
// aabb // aabb
@ -94,6 +99,38 @@ class Zone
Log(format("min: [%f, %f, %f]", this.min.x, this.min.y, this.min.z)); 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)); 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 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() function Tick()
{ {
this.tick_count++; 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 // Hooks
// -------------------------------- // --------------------------------