Airship + Unittest (#496)

* wings

* update cargo shuttle

* update goblin sound

* add new unit test

* Update CP14EntityTest.cs

* Update CP14RitualTest.cs

* add buy and sell hints

* paper reading

* hardwork

* clean task

* body forkfiltered

* Update crystal.yml

* more fork flitered

* Update basic.yml

* Update wild.yml

* Update CP14EntityTest.cs

* realise pusharing

* pusharing alchemical vials

* coin disappear works

* Fix
This commit is contained in:
Ed
2024-10-19 01:09:15 +03:00
committed by GitHub
parent f14177bf86
commit 78e94b1623
46 changed files with 817 additions and 321 deletions

View File

@@ -149,11 +149,6 @@ public sealed partial class CP14CurrencySystem : CP14SharedCurrencySystem
args.Verbs.Add(platinumVerb);
}
public HashSet<EntityUid> GenerateMoney(EntProtoId currencyType, int target, EntityCoordinates coordinates)
{
return GenerateMoney(currencyType, target, coordinates, out _);
}
public HashSet<EntityUid> GenerateMoney(EntProtoId currencyType, int target, EntityCoordinates coordinates, out int remainder)
{
remainder = target;
@@ -189,18 +184,18 @@ public sealed partial class CP14CurrencySystem : CP14SharedCurrencySystem
spawns.Add(ent);
remainder -= singleCurrency;
if (TryComp<StackComponent>(ent, out var stack))
if (TryComp<StackComponent>(ent, out var stack) && _proto.TryIndex<StackPrototype>(stack.StackTypeId, out var indexedStack))
{
AdjustStack(ent, stack, singleCurrency, ref remainder);
AdjustStack(ent, stack, indexedStack, singleCurrency, ref remainder);
}
return false;
}
private void AdjustStack(EntityUid ent, StackComponent stack, float singleCurrency, ref int remainder)
private void AdjustStack(EntityUid ent, StackComponent stack, StackPrototype stackProto, float singleCurrency, ref int remainder)
{
var singleStackCurrency = singleCurrency / stack.Count;
var stackLeftSpace = stack.MaxCountOverride - stack.Count;
var stackLeftSpace = stackProto.MaxCount - stack.Count;
if (stackLeftSpace is not null)
{

View File

@@ -25,22 +25,25 @@ public sealed partial class CP14CargoSystem
if (_timing.CurTime < ship.NextTravelTime || ship.NextTravelTime == TimeSpan.Zero)
continue;
if (Transform(ship.Shuttle).MapUid == Transform(ship.TradePostMap).MapUid)
if (ship.Shuttle is null || ship.TradePostMap is null)
continue;
if (Transform(ship.Shuttle.Value).MapUid == Transform(ship.TradePostMap.Value).MapUid)
{
// if landed on trade post
ship.NextTravelTime = _timing.CurTime + ship.StationWaitTime;
SendShuttleToStation((uid, ship));
SendShuttleToStation(ship.Shuttle.Value);
}
else
{
// if landed on station
ship.NextTravelTime = _timing.CurTime + ship.TradePostWaitTime;
SendShuttleToTradepost((uid, ship));
SendShuttleToTradepost(ship.Shuttle.Value, ship.TradePostMap.Value);
}
}
}
private void SendShuttleToStation(Entity<CP14StationTravelingStoreShipTargetComponent> station, float startupTime = 0f)
private void SendShuttleToStation(EntityUid shuttle, float startupTime = 0f)
{
var targetPoints = new List<EntityUid>();
var targetEnumerator = EntityQueryEnumerator<CP14TravelingStoreShipFTLTargetComponent, TransformComponent>(); //TODO - different method position location
@@ -54,16 +57,16 @@ public sealed partial class CP14CargoSystem
var target = _random.Pick(targetPoints);
var targetXform = Transform(target);
var shuttleComp = Comp<ShuttleComponent>(station.Comp.Shuttle);
var shuttleComp = Comp<ShuttleComponent>(shuttle);
_shuttles.FTLToCoordinates(station.Comp.Shuttle, shuttleComp, targetXform.Coordinates, targetXform.LocalRotation, hyperspaceTime: 5f, startupTime: startupTime);
_shuttles.FTLToCoordinates(shuttle, shuttleComp, targetXform.Coordinates, targetXform.LocalRotation, hyperspaceTime: 5f, startupTime: startupTime);
}
private void SendShuttleToTradepost(Entity<CP14StationTravelingStoreShipTargetComponent> station)
private void SendShuttleToTradepost(EntityUid shuttle, EntityUid tradePostMap)
{
var shuttleComp = Comp<ShuttleComponent>(station.Comp.Shuttle);
var shuttleComp = Comp<ShuttleComponent>(shuttle);
_shuttles.FTLToCoordinates(station.Comp.Shuttle, shuttleComp, new EntityCoordinates(station.Comp.TradePostMap, Vector2.Zero), Angle.Zero, hyperspaceTime: 5f);
_shuttles.FTLToCoordinates(shuttle, shuttleComp, new EntityCoordinates(tradePostMap, Vector2.Zero), Angle.Zero, hyperspaceTime: 5f);
}
private void OnFTLCompleted(Entity<CP14TravelingStoreShipComponent> ent, ref FTLCompletedEvent args)
@@ -71,16 +74,23 @@ public sealed partial class CP14CargoSystem
if (!TryComp<CP14StationTravelingStoreShipTargetComponent>(ent.Comp.Station, out var station))
return;
if (Transform(ent).MapUid == Transform(station.TradePostMap).MapUid) //Landed on tradepost
if (station.TradePostMap is not null && Transform(ent).MapUid == Transform(station.TradePostMap.Value).MapUid) //Landed on tradepost
{
station.OnStation = false;
SellingThings((ent.Comp.Station, station));
var b = station.Balance;
SellingThings((ent.Comp.Station, station)); // +balance
TopUpBalance((ent.Comp.Station, station)); //+balance
BuyToQueue((ent.Comp.Station, station)); //-balance +buyQueue
TrySpawnBuyedThings((ent.Comp.Station, station));
UpdateStorePositions((ent.Comp.Station, station));
}
else //Landed on station
{
station.OnStation = true;
CashOut((ent.Comp.Station, station));
}
UpdateAllStores();
}

View File

@@ -6,7 +6,7 @@ namespace Content.Server._CP14.TravelingStoreShip;
public sealed partial class CP14CargoSystem
{
public void InitializeStore()
public void InitializeUI()
{
SubscribeLocalEvent<CP14CargoStoreComponent, BeforeActivatableUIOpenEvent>(OnBeforeUIOpen);
}
@@ -27,15 +27,17 @@ public sealed partial class CP14CargoSystem
private void OnBeforeUIOpen(Entity<CP14CargoStoreComponent> ent, ref BeforeActivatableUIOpenEvent args)
{
//TODO: If you open a store on a mapping, and initStore() it, the entity will throw an error when you try to save the grid\map.
if (ent.Comp.Station is null)
TryInitStore(ent);
UpdateUIProducts(ent);
}
//TODO: redo
private void UpdateAllStores()
{
//TODO: redo
var query = EntityQueryEnumerator<CP14CargoStoreComponent>();
while (query.MoveNext(out var uid, out var store))
{
@@ -53,32 +55,30 @@ public sealed partial class CP14CargoSystem
foreach (var proto in ent.Comp.Station.Value.Comp.CurrentBuyPositions)
{
if (!_proto.TryIndex(proto.Key, out var indexedProto))
continue;
var name = Loc.GetString(indexedProto.Name);
var name = Loc.GetString(proto.Key.Name);
var desc = new StringBuilder();
desc.Append(Loc.GetString(indexedProto.Desc) + "\n");
foreach (var service in indexedProto.Services)
desc.Append(Loc.GetString(proto.Key.Desc) + "\n");
foreach (var service in proto.Key.Services)
{
desc.Append(service.GetDescription(_proto, EntityManager));
}
prodBuy.Add(new CP14StoreUiProductEntry(proto.Key.Id, indexedProto.Icon, name, desc.ToString(), proto.Value));
desc.Append("\n" + Loc.GetString("cp14-store-buy-hint", ("name", Loc.GetString(proto.Key.Name)), ("code", "#" + proto.Key.Code)));
prodBuy.Add(new CP14StoreUiProductEntry(proto.Key.ID, proto.Key.Icon, name, desc.ToString(), proto.Value));
}
foreach (var proto in ent.Comp.Station.Value.Comp.CurrentSellPositions)
{
if (!_proto.TryIndex(proto.Key, out var indexedProto))
continue;
var name = Loc.GetString(indexedProto.Name);
var name = Loc.GetString(proto.Key.Name);
var desc = new StringBuilder();
desc.Append(Loc.GetString(indexedProto.Desc) + "\n");
desc.Append(indexedProto.Service.GetDescription(_proto, EntityManager));
desc.Append(Loc.GetString(proto.Key.Desc) + "\n");
desc.Append(proto.Key.Service.GetDescription(_proto, EntityManager) + "\n");
desc.Append("\n" + Loc.GetString("cp14-store-sell-hint", ("name", Loc.GetString(proto.Key.Name))));
prodSell.Add(new CP14StoreUiProductEntry(proto.Key.Id, indexedProto.Icon, name, desc.ToString(), proto.Value));
prodSell.Add(new CP14StoreUiProductEntry(proto.Key.ID, proto.Key.Icon, name, desc.ToString(), proto.Value));
}
var stationComp = ent.Comp.Station.Value.Comp;

View File

@@ -5,6 +5,10 @@ using Content.Server.Station.Systems;
using Content.Shared._CP14.Currency;
using Content.Shared._CP14.TravelingStoreShip;
using Content.Shared._CP14.TravelingStoreShip.Prototype;
using Content.Shared.Maps;
using Content.Shared.Paper;
using Content.Shared.Physics;
using Content.Shared.Storage;
using Content.Shared.Storage.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
@@ -28,6 +32,7 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly CP14CurrencySystem _currency = default!;
[Dependency] private readonly SharedStorageSystem _storage = default!;
[Dependency] private readonly TurfSystem _turf = default!;
private EntityQuery<TransformComponent> _xformQuery;
@@ -38,7 +43,8 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
public override void Initialize()
{
base.Initialize();
InitializeStore();
InitializeUI();
InitializeShuttle();
_xformQuery = GetEntityQuery<TransformComponent>();
@@ -76,7 +82,7 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
var shuttle = shuttleUids[0];
station.Comp.Shuttle = shuttle;
station.Comp.TradePostMap = _mapManager.GetMapEntityId(tradepostMap);
var travelingStoreShipComp = EnsureComp<CP14TravelingStoreShipComponent>(station.Comp.Shuttle);
var travelingStoreShipComp = EnsureComp<CP14TravelingStoreShipComponent>(station.Comp.Shuttle.Value);
travelingStoreShipComp.Station = station;
station.Comp.NextTravelTime = _timing.CurTime + TimeSpan.FromSeconds(10f);
@@ -104,6 +110,9 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
}
}
/// <summary>
/// Sell all the items we can, and replenish the internal balance
/// </summary>
private void SellingThings(Entity<CP14StationTravelingStoreShipTargetComponent> station)
{
var shuttle = station.Comp.Shuttle;
@@ -119,7 +128,7 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
var sentEntities = new HashSet<EntityUid>();
_lookup.GetEntitiesInRange(uid, 1, sentEntities, LookupFlags.Dynamic | LookupFlags.Sundries);
_lookup.GetEntitiesInRange(uid, 0.5f, sentEntities, LookupFlags.Dynamic | LookupFlags.Sundries);
foreach (var ent in sentEntities)
{
@@ -130,56 +139,154 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
}
}
var cash = 0;
foreach (var sellPos in station.Comp.CurrentSellPositions)
{
if (!_proto.TryIndex(sellPos.Key, out var indexedPos))
continue;
while (indexedPos.Service.TrySell(EntityManager, toSell))
while (sellPos.Key.Service.TrySell(EntityManager, toSell))
{
cash += sellPos.Value;
station.Comp.Balance += sellPos.Value;
}
}
}
/// <summary>
/// Take all the money from the tradebox, and credit it to the internal balance
/// </summary>
private void TopUpBalance(Entity<CP14StationTravelingStoreShipTargetComponent> station)
{
var tradebox = GetTradeBox(station);
if (tradebox is null)
return;
if (!TryComp<StorageComponent>(tradebox, out var tradeStorage))
return;
//Get all currency in tradebox
int cash = 0;
foreach (var stored in tradeStorage.Container.ContainedEntities)
{
var price = _currency.GetTotalCurrency(stored);
if (price > 0)
{
cash += price;
QueueDel(stored);
}
}
var moneyBox = GetMoneyBox(station);
station.Comp.Balance += cash;
}
private void BuyToQueue(Entity<CP14StationTravelingStoreShipTargetComponent> station)
{
var tradebox = GetTradeBox(station);
if (tradebox is null)
return;
if (!TryComp<StorageComponent>(tradebox, out var tradeStorage))
return;
//Reading all papers in tradebox
List<KeyValuePair<CP14StoreBuyPositionPrototype, int>> requests = new();
foreach (var stored in tradeStorage.Container.ContainedEntities)
{
if (!TryComp<PaperComponent>(stored, out var paper))
continue;
var splittedText = paper.Content.Split("#");
foreach (var fragment in splittedText)
{
foreach (var buyPosition in station.Comp.CurrentBuyPositions)
{
if (fragment.StartsWith(buyPosition.Key.Code))
requests.Add(buyPosition);
}
}
QueueDel(stored);
}
//Trying spend tradebox money to buy requested things
foreach (var request in requests)
{
if (station.Comp.Balance < request.Value)
continue;
station.Comp.Balance -= request.Value;
station.Comp.BuyedQueue.Enqueue(request);
}
}
//Dequeue buyed items, and spawn they on shuttle
private void TrySpawnBuyedThings(Entity<CP14StationTravelingStoreShipTargetComponent> station)
{
var shuttle = station.Comp.Shuttle;
var query = EntityQueryEnumerator<CP14BuyingPalettComponent, TransformComponent>();
while (query.MoveNext(out var uid, out _, out var palletXform))
{
if (station.Comp.BuyedQueue.Count <= 0)
break;
if (palletXform.ParentUid != shuttle || !palletXform.Anchored)
continue;
var tileRef = palletXform.Coordinates.GetTileRef();
if (tileRef is null)
continue;
if (_turf.IsTileBlocked(tileRef.Value, CollisionGroup.ItemMask))
continue;
var buyedThing = station.Comp.BuyedQueue.Dequeue();
Spawn(buyedThing.Key.ID, palletXform.Coordinates);
}
}
/// <summary>
/// Transform all the accumulated balance into physical money, which we will give to the players.
/// </summary>
private void CashOut(Entity<CP14StationTravelingStoreShipTargetComponent> station)
{
var moneyBox = GetTradeBox(station);
if (moneyBox is not null)
{
var coord = Transform(moneyBox.Value).Coordinates;
if (cash > 0)
if (station.Comp.Balance > 0)
{
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.PP.Key, cash, coord, out var remainder);
cash = remainder;
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.PP.Key, station.Comp.Balance, coord, out var remainder);
station.Comp.Balance = remainder;
foreach (var coin in coins)
{
_storage.Insert(moneyBox.Value, coin, out _);
}
}
if (cash > 0)
if (station.Comp.Balance > 0)
{
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.GP.Key, cash, coord, out var remainder);
cash = remainder;
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.GP.Key, station.Comp.Balance, coord, out var remainder);
station.Comp.Balance = remainder;
foreach (var coin in coins)
{
_storage.Insert(moneyBox.Value, coin, out _);
}
}
if (cash > 0)
if (station.Comp.Balance > 0)
{
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.SP.Key, cash, coord, out var remainder);
cash = remainder;
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.SP.Key, station.Comp.Balance, coord, out var remainder);
station.Comp.Balance = remainder;
foreach (var coin in coins)
{
_storage.Insert(moneyBox.Value, coin, out _);
}
}
if (cash > 0)
if (station.Comp.Balance > 0)
{
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.CP.Key, cash, coord);
var coins = _currency.GenerateMoney(CP14SharedCurrencySystem.CP.Key, station.Comp.Balance, coord, out var remainder);
station.Comp.Balance = remainder;
foreach (var coin in coins)
{
_storage.Insert(moneyBox.Value, coin, out _);
@@ -188,7 +295,7 @@ public sealed partial class CP14CargoSystem : CP14SharedCargoSystem
}
}
private EntityUid? GetMoneyBox(Entity<CP14StationTravelingStoreShipTargetComponent> station)
private EntityUid? GetTradeBox(Entity<CP14StationTravelingStoreShipTargetComponent> station)
{
var query = EntityQueryEnumerator<CP14CargoMoneyBoxComponent, TransformComponent>();