Add Feature pt1

This commit is contained in:
Silica 2022-03-07 07:08:47 -05:00
parent a184e4d735
commit 092534e331
131 changed files with 3113 additions and 1418 deletions

View file

@ -0,0 +1,344 @@
using System;
using System.Collections.Generic;
using HISP.Server;
namespace HISP.Game.Items
{
public class DroppedItems
{
public class DroppedItem
{
public DroppedItem(ItemInstance itmInstance)
{
if (itmInstance == null)
throw new NullReferenceException("How could this happen?");
Instance = itmInstance;
}
public int X;
public int Y;
public int DespawnTimer;
public ItemInstance Instance;
public int Data;
}
private static List<DroppedItem> droppedItemsList = new List<DroppedItem>();
public static int GetCountOfItem(Item.ItemInformation item)
{
DroppedItem[] droppedItems = droppedItemsList.ToArray();
int count = 0;
for(int i = 0; i < droppedItems.Length; i++)
{
if (droppedItems[i] == null) // Item removed in another thread.
continue;
if(droppedItems[i].Instance.ItemId == item.Id)
{
count++;
}
}
return count;
}
public static DroppedItem[] GetItemsAt(int x, int y)
{
DroppedItem[] droppedItems = droppedItemsList.ToArray();
List<DroppedItem> items = new List<DroppedItem>();
for(int i = 0; i < droppedItems.Length; i++)
{
if (droppedItems[i] == null) // Item removed in another thread.
continue;
if (droppedItems[i].X == x && droppedItems[i].Y == y)
{
items.Add(droppedItems[i]);
}
}
return items.ToArray();
}
public static void ReadFromDatabase()
{
DroppedItem[] items = Database.GetDroppedItems();
foreach (DroppedItem droppedItem in items)
droppedItemsList.Add(droppedItem);
}
public static void RemoveDroppedItem(DroppedItem item)
{
int randomId = item.Instance.RandomId;
Database.RemoveDroppedItem(randomId);
droppedItemsList.Remove(item);
}
public static bool IsDroppedItemExist(int randomId)
{
try
{
GetDroppedItemById(randomId);
return true;
}
catch(KeyNotFoundException)
{
return false;
}
}
public static DroppedItem GetDroppedItemById(int randomId)
{
DroppedItem[] droppedItems = droppedItemsList.ToArray();
for(int i = 0; i < droppedItems.Length; i++)
{
if (droppedItems[i] == null) // Item removed in another thread.
continue;
if (droppedItems[i].Instance.RandomId == randomId)
{
return droppedItems[i];
}
}
throw new KeyNotFoundException("Random id: " + randomId.ToString() + " not found");
}
public static void DespawnItems()
{
Database.DecrementDroppedItemDespawnTimer();
Database.RemoveDespawningItems(); // GO-GO-GO-GOGOGOGO GOTTA GO FAST!!!
for (int i = 0; i < droppedItemsList.Count; i++)
{
if (droppedItemsList[i] == null) // Item removed in another thread.
continue;
droppedItemsList[i].DespawnTimer -= 5;
if(droppedItemsList[i].DespawnTimer <= 0)
{
if (GameServer.GetUsersAt(droppedItemsList[i].X, droppedItemsList[i].Y, true, true).Length > 0) // Dont despawn items players are standing on
continue;
Logger.DebugPrint("Despawned Item at " + droppedItemsList[i].X + ", " + droppedItemsList[i].Y);
droppedItemsList.Remove(droppedItemsList[i]);
}
}
}
public static void AddItem(ItemInstance item, int x, int y, int despawnTimer=1500)
{
DroppedItem droppedItem = new DroppedItem(item);
droppedItem.X = x;
droppedItem.Y = y;
droppedItem.DespawnTimer = despawnTimer;
droppedItemsList.Add(droppedItem);
Database.AddDroppedItem(droppedItem);
}
public static void GenerateItems()
{
Logger.DebugPrint("Generating items.");
int newItems = 0;
foreach (Item.ItemInformation item in Item.Items)
{
int count = GetCountOfItem(item);
//do
//{
if (count < item.SpawnParamaters.SpawnCap)
{
count++;
int despawnTimer = 1440;
if (item.SpawnParamaters.SpawnInZone != null)
{
World.Zone spawnArea = World.GetZoneByName(item.SpawnParamaters.SpawnInZone);
while (true)
{
// Pick a random location inside the zone
int tryX = GameServer.RandomNumberGenerator.Next(spawnArea.StartX, spawnArea.EndX);
int tryY = GameServer.RandomNumberGenerator.Next(spawnArea.StartY, spawnArea.EndY);
if (World.InSpecialTile(tryX, tryY))
continue;
if (Map.CheckPassable(tryX, tryY)) // Can the player walk here?
{
int TileID = Map.GetTileId(tryX, tryY, false);
string TileType = Map.TerrainTiles[TileID - 1].Type; // Is it the right type?
if (item.SpawnParamaters.SpawnOnTileType == TileType)
{
if (GetItemsAt(tryX, tryY).Length > 25) // Max items in one tile.
continue;
ItemInstance instance = new ItemInstance(item.Id);
AddItem(instance, tryX, tryY, despawnTimer);
Logger.DebugPrint("Created Item ID: " + instance.ItemId + " in ZONE: " + spawnArea.Name + " at: X: " + tryX + " Y: " + tryY);
newItems++;
break;
}
else
{
continue;
}
}
else
{
continue;
}
}
}
else if (item.SpawnParamaters.SpawnOnSpecialTile != null)
{
while (true)
{
// Pick a random special tile
World.SpecialTile[] possileTiles = World.GetSpecialTilesByName(item.SpawnParamaters.SpawnOnSpecialTile);
World.SpecialTile spawnOn = possileTiles[GameServer.RandomNumberGenerator.Next(0, possileTiles.Length)];
if (Map.CheckPassable(spawnOn.X, spawnOn.Y))
{
if (GetItemsAt(spawnOn.X, spawnOn.Y).Length > 25) // Max items in one tile.
continue;
ItemInstance instance = new ItemInstance(item.Id);
AddItem(instance, spawnOn.X, spawnOn.Y, despawnTimer);
Logger.DebugPrint("Created Item ID: " + instance.ItemId + " at: X: " + spawnOn.X + " Y: " + spawnOn.Y);
newItems++;
break;
}
else
{
continue;
}
}
}
else if (item.SpawnParamaters.SpawnNearSpecialTile != null)
{
while (true)
{
// Pick a random special tile
World.SpecialTile[] possileTiles = World.GetSpecialTilesByName(item.SpawnParamaters.SpawnNearSpecialTile);
World.SpecialTile spawnNearTile = possileTiles[GameServer.RandomNumberGenerator.Next(0, possileTiles.Length)];
// Pick a direction to try spawn in
int direction = GameServer.RandomNumberGenerator.Next(0, 4);
int tryX = 0;
int tryY = 0;
if (direction == 0)
{
tryX = spawnNearTile.X + 1;
tryY = spawnNearTile.Y;
}
else if (direction == 1)
{
tryX = spawnNearTile.X - 1;
tryY = spawnNearTile.Y;
}
else if (direction == 3)
{
tryX = spawnNearTile.X;
tryY = spawnNearTile.Y + 1;
}
else if (direction == 4)
{
tryX = spawnNearTile.X;
tryY = spawnNearTile.Y - 1;
}
if (World.InSpecialTile(tryX, tryY))
{
World.SpecialTile tile = World.GetSpecialTile(tryX, tryY);
if (tile.Code != null)
continue;
}
if (Map.CheckPassable(tryX, tryY))
{
if (GetItemsAt(tryX, tryY).Length > 25) // Max here
continue;
ItemInstance instance = new ItemInstance(item.Id);
AddItem(instance, tryX, tryY, despawnTimer);
Logger.DebugPrint("Created Item ID: " + instance.ItemId + " at: X: " + tryX + " Y: " + tryY);
newItems++;
break;
}
else
{
continue;
}
}
}
else if (item.SpawnParamaters.SpawnOnTileType != null)
{
while (true)
{
// Pick a random location:
int tryX = GameServer.RandomNumberGenerator.Next(0, Map.Width);
int tryY = GameServer.RandomNumberGenerator.Next(0, Map.Height);
if (World.InSpecialTile(tryX, tryY))
continue;
if (Map.CheckPassable(tryX, tryY)) // Can the player walk here?
{
int TileID = Map.GetTileId(tryX, tryY, false);
string TileType = Map.TerrainTiles[TileID - 1].Type; // Is it the right type?
if (item.SpawnParamaters.SpawnOnTileType == TileType)
{
if (GetItemsAt(tryX, tryY).Length > 25) // Max here
continue;
ItemInstance instance = new ItemInstance(item.Id);
AddItem(instance, tryX, tryY, despawnTimer);
Logger.DebugPrint("Created Item ID: " + instance.ItemId + " at: X: " + tryX + " Y: " + tryY);
newItems++;
break;
}
else
{
continue;
}
}
else
{
continue;
}
}
}
}
//} while (count < item.SpawnParamaters.SpawnCap);
}
}
public static void DeleteAllItemsWithId(int itemId)
{
Database.DeleteAllDroppedItemsWithId(itemId);
foreach (DroppedItem itm in droppedItemsList.ToArray())
if (itm.Instance.ItemId == itemId)
droppedItemsList.Remove(itm);
}
public static void Init()
{
ReadFromDatabase();
GenerateItems();
}
}
}

View file

@ -0,0 +1,276 @@
using HISP.Player;
using HISP.Server;
using HISP.Game;
using System.Collections.Generic;
using HISP.Game.Inventory;
namespace HISP.Game.Items
{
public class Item
{
public const int MAX_STACK = 50;
public struct Effects
{
public string EffectsWhat;
public int EffectAmount;
}
public struct SpawnRules
{
public int SpawnCap;
public string SpawnInZone;
public string SpawnOnTileType;
public string SpawnOnSpecialTile;
public string SpawnNearSpecialTile;
}
public class ItemInformation
{
public int Id;
public string Name;
public string PluralName;
public string Description;
public int IconId;
public int SortBy;
public int SellPrice;
public string EmbedSwf;
public bool WishingWell;
public string Type;
public int[] MiscFlags;
public Effects[] Effects;
public SpawnRules SpawnParamaters;
public int GetMiscFlag(int no)
{
if (no < 0)
return 0;
if (no >= MiscFlags.Length)
return 0;
else
return MiscFlags[no];
}
}
public struct ThrowableItem
{
public int Id;
public string HitMessage;
public string ThrowMessage;
public string HitYourselfMessage;
}
private static List<ItemInformation> items = new List<ItemInformation>();
private static List<ThrowableItem> throwableItems = new List<ThrowableItem>();
public static void AddItemInfo(ItemInformation itm)
{
items.Add(itm);
}
public static void AddThrowableItem(ThrowableItem throwableItem)
{
throwableItems.Add(throwableItem);
}
public static ItemInformation[] Items
{
get
{
return items.ToArray();
}
}
public static ThrowableItem[] ThrowableItems
{
get
{
return throwableItems.ToArray();
}
}
public static int Present;
public static int MailMessage;
public static int DorothyShoes;
public static int PawneerOrder;
public static int Telescope;
public static int Pitchfork;
public static int WishingCoin;
public static int ModSplatterball;
public static int WaterBalloon;
public static int FishingPole;
public static int Earthworm;
public static int BirthdayToken;
public static int MagicBean;
public static int MagicDroplet;
public static int Ruby;
public static int StallionTradingCard;
public static int MareTradingCard;
public static int ColtTradingCard;
public static int FillyTradingCard;
public static int[] TradingCards
{
get
{
return new int[4] { StallionTradingCard, MareTradingCard, ColtTradingCard, FillyTradingCard };
}
}
public struct ItemPurchaseQueueItem
{
public int ItemId;
public int ItemCount;
}
public static ItemInformation GetRandomItem()
{
while (true)
{
Item.ItemInformation itm = Items[GameServer.RandomNumberGenerator.Next(0, Items.Length)];
if (itm.Type == "QUEST" || itm.Type == "CONCEPTUAL" || itm.Type == "TEXT")
continue;
return itm;
}
}
public static void UseItem(User user, ItemInstance item)
{
if (user.Inventory.HasItem(item.RandomId))
{
InventoryItem itm = user.Inventory.GetItemByRandomid(item.RandomId);
if (itm.ItemId == Item.DorothyShoes)
{
if (World.InIsle(user.X, user.Y))
{
World.Isle isle = World.GetIsle(user.X, user.Y);
if (isle.Name == "Prison Isle")
{
byte[] dontWorkHere = PacketBuilder.CreateChat(Messages.RanchDorothyShoesPrisonIsleMessage, PacketBuilder.CHAT_BOTTOM_RIGHT);
user.LoggedinClient.SendPacket(dontWorkHere);
return;
}
}
if (user.OwnedRanch == null) // How????
{
Logger.HackerPrint(user.Username + " Tried to use Dorothy Shoes when they did *NOT* own a ranch.");
user.Inventory.Remove(itm.ItemInstances[0]);
return;
}
byte[] noPlaceLIke127001 = PacketBuilder.CreateChat(Messages.RanchDorothyShoesMessage, PacketBuilder.CHAT_BOTTOM_RIGHT);
user.LoggedinClient.SendPacket(noPlaceLIke127001);
user.Teleport(user.OwnedRanch.X, user.OwnedRanch.Y);
}
else if (itm.ItemId == Item.Telescope)
{
byte[] birdMap = PacketBuilder.CreateBirdMap(user.X, user.Y);
user.LoggedinClient.SendPacket(birdMap);
}
else
{
Logger.ErrorPrint(user.Username + "Tried to use item with undefined action- ID: " + itm.ItemId);
}
}
}
public static ItemInformation[] GetAllWishableItems()
{
List<ItemInformation> itemInfo = new List<ItemInformation>();
foreach(ItemInformation item in Items)
{
if (item.WishingWell)
itemInfo.Add(item);
}
return itemInfo.ToArray();
}
public static bool ConsumeItem(User user, ItemInformation itmInfo)
{
bool toMuch = false;
foreach (Item.Effects effect in itmInfo.Effects)
{
switch (effect.EffectsWhat)
{
case "TIREDNESS":
if (user.Tiredness + effect.EffectAmount > 1000)
toMuch = true;
user.Tiredness += effect.EffectAmount;
break;
case "THIRST":
if (user.Thirst + effect.EffectAmount > 1000)
toMuch = true;
user.Thirst += effect.EffectAmount;
break;
case "HUNGER":
if (user.Hunger + effect.EffectAmount > 1000)
toMuch = true;
user.Hunger += effect.EffectAmount;
break;
case "MOOD":
break;
default:
Logger.ErrorPrint("Unknown effect: " + effect.EffectsWhat);
break;
}
}
return toMuch;
}
public static bool IsThrowable(int id)
{
foreach(ThrowableItem itm in ThrowableItems)
{
if(itm.Id == id)
{
return true;
}
}
return false;
}
public static ThrowableItem GetThrowableItem(int id)
{
foreach (ThrowableItem itm in ThrowableItems)
{
if (itm.Id == id)
{
return itm;
}
}
throw new KeyNotFoundException("id: " + id + " is not a throwable item.");
}
public static bool ItemIdExist(int id)
{
try
{
GetItemById(id);
return true;
}
catch(KeyNotFoundException)
{
return false;
}
}
public static void DoSpecialCases()
{
Tack.GenerateTackSets();
}
public static ItemInformation GetItemById(int id)
{
foreach(ItemInformation item in Items)
{
if(item.Id == id)
{
return item;
}
}
throw new KeyNotFoundException("Item id " + id + " Not found!");
}
}
}

View file

@ -0,0 +1,27 @@
using HISP.Security;
using HISP.Game;
namespace HISP.Game.Items
{
public class ItemInstance
{
public int RandomId;
public int ItemId;
public int Data;
public Item.ItemInformation GetItemInfo()
{
return Item.GetItemById(ItemId);
}
public ItemInstance(int id,int randomId = -1, int data=0)
{
RandomId = RandomID.NextRandomId(randomId);
Data = data;
ItemId = id;
}
}
}

View file

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Text;
using HISP.Server;
namespace HISP.Game.Items
{
public class Tack
{
public class TackSet
{
public TackSet()
{
tackItems = new List<Item.ItemInformation>();
}
public int IconId
{
get
{
return GetSaddle().IconId;
}
}
public string SetName;
private List<Item.ItemInformation> tackItems;
public void Add(Item.ItemInformation item)
{
Logger.DebugPrint("Added "+item.Name+" To Tack Set: "+this.SetName);
tackItems.Add(item);
}
public Item.ItemInformation GetSaddle()
{
foreach(Item.ItemInformation tackItem in TackItems)
{
if(tackItem.GetMiscFlag(0) == 1) // Saddle
return tackItem;
}
throw new KeyNotFoundException("Saddle not found.");
}
public Item.ItemInformation GetSaddlePad()
{
foreach(Item.ItemInformation tackItem in TackItems)
{
if(tackItem.GetMiscFlag(0) == 2) // SaddlePad
return tackItem;
}
throw new KeyNotFoundException("SaddlePad not found.");
}
public Item.ItemInformation GetBridle()
{
foreach(Item.ItemInformation tackItem in TackItems)
{
if(tackItem.GetMiscFlag(0) == 3) // Bridle
return tackItem;
}
throw new KeyNotFoundException("GetBridle not found.");
}
public string[] GetSwfNames()
{
string[] swfs = new string[3];
swfs[0] = GetSaddle().EmbedSwf;
swfs[1] = GetSaddlePad().EmbedSwf;
swfs[2] = GetBridle().EmbedSwf;
return swfs;
}
public Item.ItemInformation[] TackItems
{
get
{
return tackItems.ToArray();
}
}
public int SortPosition()
{
int pos = 0;
foreach(Item.ItemInformation tackitem in TackItems)
{
foreach(Item.Effects effect in tackitem.Effects)
{
pos += effect.EffectAmount;
}
}
return pos;
}
}
private static List<TackSet> tackSets = new List<TackSet>();
public static TackSet[] TackSets
{
get
{
return tackSets.ToArray();
}
}
public static TackSet GetSetByName(string name)
{
foreach(TackSet set in tackSets)
{
if(set.SetName == name)
{
return set;
}
}
throw new KeyNotFoundException("No TackSet with name: "+name+" was found.");
}
public static void GenerateTackSets()
{
foreach(Item.ItemInformation itemInfo in Item.Items)
{
if(itemInfo.Type == "TACK")
{
try
{
TackSet set = GetSetByName(Util.CapitalizeFirstLetter(itemInfo.EmbedSwf));
set.Add(itemInfo);
}
catch(KeyNotFoundException)
{
TackSet tackSet = new TackSet();
tackSet.SetName = Util.CapitalizeFirstLetter(itemInfo.EmbedSwf);
tackSet.Add(itemInfo);
tackSets.Add(tackSet);
}
}
}
foreach(TackSet set in TackSets)
{
if(set.TackItems.Length < 3)
{
Logger.DebugPrint("Removing set: "+set.SetName);
tackSets.Remove(set);
}
}
}
}
}