RunUO/Scripts/Items/Houses/GenStaticHouses.cs

107 lines
4.2 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using Server;
using Server.Items;
using Server.Multis;
using Server.Commands;
namespace Server.Custom.StaticHousing
{
public class GenStaticHouses
{
public static void Initialize()
{
CommandSystem.Register("GenStaticHouses", AccessLevel.Administrator, new CommandEventHandler(GenStaticHouses_OnCommand));
}
[Usage("GenStaticHouses")]
[Description("Generates Static House signs from a configuration file.")]
public static void GenStaticHouses_OnCommand(CommandEventArgs e)
{
string filePath = Path.Combine(Core.BaseDirectory, "Data", "Config", "statichouses.cfg");
if (!File.Exists(filePath))
{
e.Mobile.SendMessage(33, "Error: Data/Config/statichouses.cfg not found!");
return;
}
int count = 0;
using (StreamReader ip = new StreamReader(filePath))
{
string line;
while ((line = ip.ReadLine()) != null)
{
line = line.Trim();
if (line.Length == 0 || line.StartsWith("#")) continue;
string[] split = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
// We need at least 14 arguments now (10 base + 4 for the first area)
if (split.Length >= 14)
{
try
{
Map map = Map.Parse(split[0]);
Point3D signLoc = new Point3D(int.Parse(split[1]), int.Parse(split[2]), int.Parse(split[3]));
int minZ = int.Parse(split[4]);
int maxZ = int.Parse(split[5]);
int price = int.Parse(split[6]);
int locks = int.Parse(split[7]);
int secures = int.Parse(split[8]);
int itemID = split[9].StartsWith("0x", StringComparison.OrdinalIgnoreCase)
? Convert.ToInt32(split[9], 16)
: int.Parse(split[9]);
// Loop to capture ALL blocks appended to the end of the line!
List<Rectangle2D> areas = new List<Rectangle2D>();
for (int i = 10; i < split.Length; i += 4)
{
if (i + 3 < split.Length)
{
Point2D start = new Point2D(int.Parse(split[i]), int.Parse(split[i + 1]));
Point2D end = new Point2D(start.X + int.Parse(split[i + 2]), start.Y + int.Parse(split[i + 3]));
areas.Add(new Rectangle2D(start, end));
}
}
if (map != null && !CheckForExistingHouse(map, signLoc) && areas.Count > 0)
{
StaticHouseSign sign = new StaticHouseSign(itemID, areas.ToArray(), minZ, maxZ, price, locks, secures);
sign.MoveToWorld(signLoc, map);
count++;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing statichouses.cfg line: {line}\n{ex.Message}");
}
}
}
}
e.Mobile.SendMessage(68, $"Generation complete. {count} new Static House signs were spawned.");
}
private static bool CheckForExistingHouse(Map map, Point3D loc)
{
bool exists = false;
IPooledEnumerable eable = map.GetItemsInRange(loc, 0);
foreach (Item item in eable)
{
if (item is StaticHouseSign || item is HouseSign)
{
exists = true;
break;
}
}
eable.Free();
return exists;
}
}
}