138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using Server;
|
|
using Server.Items;
|
|
using Server.Custom.StaticHousing;
|
|
|
|
namespace Server.Scripts.Custom
|
|
{
|
|
public class TasksManager
|
|
{
|
|
public static void Initialize()
|
|
{
|
|
Timer.DelayCall(TimeSpan.FromMinutes(1), TimeSpan.FromHours(1), new TimerCallback(ExecuteTasks));
|
|
|
|
Console.WriteLine("TasksManager: Hourly automated tasks initialized.");
|
|
}
|
|
|
|
public static void ExecuteTasks()
|
|
{
|
|
Console.WriteLine("TasksManager: Running scheduled hourly tasks...");
|
|
|
|
if ( Settings.S_DungeonFloorTraps )
|
|
{
|
|
ClearSpawnedTraps();
|
|
}
|
|
CheckStaticHouses();
|
|
}
|
|
|
|
private static void CheckStaticHouses()
|
|
{
|
|
string filePath = Path.Combine(Core.BaseDirectory, "Data", "Config", "statichouses.cfg");
|
|
|
|
if (!File.Exists(filePath))
|
|
{
|
|
Console.WriteLine("TasksManager: 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);
|
|
|
|
// Requires 14 parameters (10 base + 4 area variables)
|
|
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]);
|
|
|
|
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($"TasksManager: Error parsing statichouses.cfg line: {line}\n{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count > 0)
|
|
Console.WriteLine($"TasksManager: Respawned {count} missing Static House signs.");
|
|
}
|
|
|
|
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 Server.Multis.HouseSign)
|
|
{
|
|
exists = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
eable.Free();
|
|
return exists;
|
|
}
|
|
|
|
private static void ClearSpawnedTraps()
|
|
{
|
|
List<Item> trapsToDelete = new List<Item>();
|
|
|
|
foreach (Item item in World.Items.Values)
|
|
{
|
|
if (item is BaseTrap)
|
|
{
|
|
trapsToDelete.Add(item);
|
|
}
|
|
}
|
|
|
|
foreach (Item trap in trapsToDelete)
|
|
{
|
|
trap.Delete();
|
|
}
|
|
|
|
Console.WriteLine($"TasksManager: Wiped {trapsToDelete.Count} floor traps. Spawners will now replace them.");
|
|
}
|
|
}
|
|
}
|