#W# Reworking Inn rooms.
This commit is contained in:
parent
7104f56ebf
commit
66eafd3451
15 changed files with 918 additions and 576 deletions
133
Scripts/Engines/InnRooms/InnRoomManager.cs
Normal file
133
Scripts/Engines/InnRooms/InnRoomManager.cs
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using Server;
|
||||
using Server.Items;
|
||||
using Server.Custom.InnRooms;
|
||||
|
||||
namespace Server.Custom.InnRooms
|
||||
{
|
||||
public enum InnRoomSize { Small, Medium, Large }
|
||||
|
||||
// This object holds the coordinates for a single room from your config file
|
||||
public class RoomDefinition
|
||||
{
|
||||
public InnRoomSize Size;
|
||||
public Map RoomMap;
|
||||
public Point3D Location;
|
||||
}
|
||||
|
||||
public class InnRoomManager
|
||||
{
|
||||
// The master list of all rooms loaded from the config file
|
||||
public static List<RoomDefinition> Rooms = new List<RoomDefinition>();
|
||||
|
||||
// This runs automatically when the server starts
|
||||
public static void Initialize()
|
||||
{
|
||||
LoadRooms();
|
||||
}
|
||||
|
||||
public static int GetCost(InnRoomSize size)
|
||||
{
|
||||
switch (size)
|
||||
{
|
||||
case InnRoomSize.Small: return 5000;
|
||||
case InnRoomSize.Medium: return 10000;
|
||||
case InnRoomSize.Large: return 20000;
|
||||
default: return 5000;
|
||||
}
|
||||
}
|
||||
|
||||
public static void LoadRooms()
|
||||
{
|
||||
Rooms.Clear();
|
||||
string filePath = Path.Combine(Core.BaseDirectory, "Data", "Config", "innrooms.cfg");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
Console.WriteLine("InnRoomManager: No innrooms.cfg found!");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Expected Format: Size Map X Y Z
|
||||
// Example: Small Felucca 6000 6000 0
|
||||
if (split.Length >= 5)
|
||||
{
|
||||
try
|
||||
{
|
||||
RoomDefinition def = new RoomDefinition();
|
||||
def.Size = (InnRoomSize)Enum.Parse(typeof(InnRoomSize), split[0], true);
|
||||
def.RoomMap = Map.Parse(split[1]);
|
||||
def.Location = new Point3D(int.Parse(split[2]), int.Parse(split[3]), int.Parse(split[4]));
|
||||
|
||||
Rooms.Add(def);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"InnRoomManager Error parsing line: {line}\n{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Console.WriteLine($"InnRoomManager: Loaded {Rooms.Count} total rooms from config.");
|
||||
}
|
||||
|
||||
// Checks all active rentals on the server to see which list indexes are currently taken
|
||||
public static List<int> GetOccupiedIndexes()
|
||||
{
|
||||
List<int> occupied = new List<int>();
|
||||
|
||||
foreach (Item item in World.Items.Values)
|
||||
{
|
||||
if (item is InnRoomHouse)
|
||||
{
|
||||
InnRoomHouse house = (InnRoomHouse)item;
|
||||
occupied.Add(house.RoomIndex);
|
||||
}
|
||||
}
|
||||
return occupied;
|
||||
}
|
||||
|
||||
// Counts how many rooms of a specific size are NOT currently rented
|
||||
public static int GetAvailableCount(InnRoomSize size)
|
||||
{
|
||||
List<int> occupied = GetOccupiedIndexes();
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < Rooms.Count; i++)
|
||||
{
|
||||
if (Rooms[i].Size == size && !occupied.Contains(i))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Finds the exact list index of the first available room of the requested size
|
||||
public static int FindFirstOpenSlot(InnRoomSize size)
|
||||
{
|
||||
List<int> occupied = GetOccupiedIndexes();
|
||||
|
||||
for (int i = 0; i < Rooms.Count; i++)
|
||||
{
|
||||
if (Rooms[i].Size == size && !occupied.Contains(i))
|
||||
{
|
||||
return i; // Returns the exact index in the Rooms list
|
||||
}
|
||||
}
|
||||
return -1; // -1 means sold out
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue