// The server selects the island's existing scenario. No island dependency is
// added to Escape, and no world resource is changed by these settings.
modded class SCR_MissionHeader
{
 [Attribute("0", desc: "Experimental: replace this scenario's game mode with Escape. Use a plain terrain exploration scenario.")]
 bool m_bERR_EnableEscape;
 [Attribute("", desc: "Optional airport centres: X Z pairs separated by semicolons, for example 1525 5075. Up to 16 centres.")]
 string m_sERR_Airfields;
}

class ERR_TerrainTakeoverSettings
{
 static bool Coordinate(string value, out float number)
 {
  if (value.IsEmpty() || value.Length() > 16) return false;
  bool digit, dot;
  for (int i = 0; i < value.Length(); i++)
  {
   string character = value.Substring(i, 1);
   if (i == 0 && character == "-") continue;
   if (character == "." && !dot) { dot = true; continue; }
   if ("0123456789".IndexOf(character) < 0) return false;
   digit = true;
  }
  if (!digit) return false;
  number = value.ToFloat();
  return number >= -1000000 && number <= 1000000;
 }
 static bool ParseAirfields(string text, array<vector> centres)
 {
  centres.Clear();
  text.TrimInPlace();
  if (text.IsEmpty()) return true;
  if (text.Length() > 1024) return false;
  array<string> entries = {};
  text.Split(";", entries, false);
  if (entries.IsEmpty() || entries.Count() > 16) return false;
  foreach (string entry : entries)
  {
   entry.TrimInPlace();
   array<string> pair = {};
   entry.Split(" ", pair, true);
   float x, z;
   if (pair.Count() != 2 || !Coordinate(pair[0], x) || !Coordinate(pair[1], z)) { centres.Clear(); return false; }
   centres.Insert(Vector(x, 0, z));
  }
  return true;
 }
 static bool Enabled()
 {
  if (!GetGame() || SCR_Global.IsEditMode()) return false;
  SCR_MissionHeader header = SCR_MissionHeader.Cast(GetGame().GetMissionHeader());
  if (header && header.m_bERR_EnableEscape) return true;
  // Also support owner-controlled CLI launches (including local-mod servers
  // and Workbench play, which may not apply mission-header JSON overrides).
  string enabled;
  return System.GetCLIParam("errTerrainTakeover", enabled) && enabled == "1";
 }
 static string Airfields()
 {
  SCR_MissionHeader header = SCR_MissionHeader.Cast(GetGame().GetMissionHeader());
  if (header && header.m_bERR_EnableEscape) return header.m_sERR_Airfields;
  string centres;
  System.GetCLIParam("errAirfields", centres);
  return centres;
 }
}
