Add material arbitrage tests (#13315)

This commit is contained in:
Leon Friedrich
2023-01-15 21:57:59 +13:00
committed by GitHub
parent 9f3256730d
commit cf509c1e38
28 changed files with 415 additions and 34 deletions

View File

@@ -131,10 +131,8 @@ public sealed partial class LatheMenu : DefaultWindow
else
sb.Append('\n');
var adjustedAmount = amount;
if (prototype.ApplyMaterialDiscount)
adjustedAmount = (int) (adjustedAmount * component.MaterialUseMultiplier);
var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, component.MaterialUseMultiplier);
sb.Append(adjustedAmount);
sb.Append(' ');
sb.Append(Loc.GetString(proto.Name));

View File

@@ -31,15 +31,19 @@ public sealed class CargoTest
{
var mapId = testMap.MapId;
foreach (var proto in protoManager.EnumeratePrototypes<CargoProductPrototype>())
Assert.Multiple(() =>
{
var ent = entManager.SpawnEntity(proto.Product, new MapCoordinates(Vector2.Zero, mapId));
var price = pricing.GetPrice(ent);
foreach (var proto in protoManager.EnumeratePrototypes<CargoProductPrototype>())
{
var ent = entManager.SpawnEntity(proto.Product, new MapCoordinates(Vector2.Zero, mapId));
var price = pricing.GetPrice(ent);
Assert.That(price, Is.LessThan(proto.PointCost), $"Found arbitrage on {proto.ID} cargo product! Cost is {proto.PointCost} but sell is {price}!");
entManager.DeleteEntity(ent);
}
Assert.That(price, Is.LessThan(proto.PointCost), $"Found arbitrage on {proto.ID} cargo product! Cost is {proto.PointCost} but sell is {price}!");
entManager.DeleteEntity(ent);
}
});
mapManager.DeleteMap(mapId);
});

View File

@@ -1,14 +1,11 @@
using System.Threading.Tasks;
using Content.Server.Construction.Components;
using Content.Server.Stack;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Construction.Steps;
using Content.Shared.Prototypes;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using System.Threading.Tasks;
namespace Content.IntegrationTests.Tests.Construction
{
@@ -90,6 +87,37 @@ namespace Content.IntegrationTests.Tests.Construction
await pairTracker.CleanReturnAsync();
}
[Test]
public async Task DeconstructionIsValid()
{
await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings { NoClient = true });
var server = pairTracker.Pair.Server;
var entMan = server.ResolveDependency<IEntityManager>();
var protoMan = server.ResolveDependency<IPrototypeManager>();
var compFact = server.ResolveDependency<IComponentFactory>();
var name = compFact.GetComponentName(typeof(ConstructionComponent));
Assert.Multiple(() =>
{
foreach (var proto in protoMan.EnumeratePrototypes<EntityPrototype>())
{
if (proto.Abstract || !proto.Components.TryGetValue(name, out var reg))
continue;
var comp = (ConstructionComponent) reg.Component;
var target = comp.DeconstructionNode;
if (target == null)
continue;
var graph = protoMan.Index<ConstructionGraphPrototype>(comp.Graph);
Assert.That(graph.Nodes.ContainsKey(target), $"Invalid deconstruction node \"{target}\" on graph \"{graph.ID}\" for construction entity \"{proto.ID}\"!");
}
});
await pairTracker.CleanReturnAsync();
}
[Test]
public async Task TestStartReachesValidTarget()
{

View File

@@ -0,0 +1,301 @@
using Content.Server.Cargo.Systems;
using Content.Server.Construction.Completions;
using Content.Server.Construction.Components;
using Content.Server.Destructible;
using Content.Server.Destructible.Thresholds.Behaviors;
using Content.Server.Stack;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Construction.Steps;
using Content.Shared.Lathe;
using Content.Shared.Research.Prototypes;
using Content.Shared.Stacks;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Content.IntegrationTests.Tests;
/// <summary>
/// This test checks that any destructible or constructible entities do not drop more resources than are required to
/// create them.
/// </summary>
[TestFixture]
public sealed class MaterialArbitrageTest
{
[Test]
public async Task NoMaterialArbitrage()
{
// TODO check lathe resource prices?
// I CBF doing that atm because I know that will probably fail for most lathe recipies.
await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings() {NoClient = true});
var server = pairTracker.Pair.Server;
var testMap = await PoolManager.CreateTestMap(pairTracker);
await server.WaitIdleAsync();
var entManager = server.ResolveDependency<IEntityManager>();
var sysManager = server.ResolveDependency<IEntitySystemManager>();
var mapManager = server.ResolveDependency<IMapManager>();
Assert.That(mapManager.IsMapInitialized(testMap.MapId));
var protoManager = server.ResolveDependency<IPrototypeManager>();
var pricing = sysManager.GetEntitySystem<PricingSystem>();
var stackSys = sysManager.GetEntitySystem<StackSystem>();
var compFact = server.ResolveDependency<IComponentFactory>();
var constructionName = compFact.GetComponentName(typeof(ConstructionComponent));
var destructibleName = compFact.GetComponentName(typeof(DestructibleComponent));
var stackName = compFact.GetComponentName(typeof(StackComponent));
// construct inverted lathe recipe dictionary
Dictionary<string, LatheRecipePrototype> latheRecipes = new();
foreach (var proto in protoManager.EnumeratePrototypes<LatheRecipePrototype>())
{
latheRecipes.Add(proto.Result, proto);
}
// Lets assume the possible lathe for resource multipliers:
var multiplier = MathF.Pow(LatheComponent.DefaultPartRatingMaterialUseMultiplier, MachinePartComponent.MaxRating - 1);
// create construction dictionary
Dictionary<string, ConstructionComponent> constructionRecipes = new();
foreach (var proto in protoManager.EnumeratePrototypes<EntityPrototype>())
{
if (proto.NoSpawn || proto.Abstract)
continue;
if (!proto.Components.TryGetValue(constructionName, out var destructible))
continue;
var comp = (ConstructionComponent) destructible.Component;
constructionRecipes.Add(proto.ID, comp);
}
// Get ingredients required to construct an entity
Dictionary<string, Dictionary<string, int>> constructionMaterials = new();
foreach (var (id, comp) in constructionRecipes)
{
var materials = new Dictionary<string, int>();
var graph = protoManager.Index<ConstructionGraphPrototype>(comp.Graph);
if (!graph.TryPath(graph.Start, comp.Node, out var path) || path.Length == 0)
continue;
var cur = graph.Nodes[graph.Start];
foreach (var node in path)
{
var edge = cur.GetEdge(node.Name);
cur = node;
foreach (var step in edge.Steps)
{
if (step is MaterialConstructionGraphStep materialStep)
materials[materialStep.MaterialPrototypeId] = materialStep.Amount + materials.GetValueOrDefault(materialStep.MaterialPrototypeId);
}
}
constructionMaterials.Add(id, materials);
}
Dictionary<string, double> priceCache = new();
Dictionary<string, (Dictionary<string, int> Ents, Dictionary<string, int> Mats)> spawnedOnDestroy = new();
// Here we get the set of entities/materials spawned when destroying an entity.
foreach (var proto in protoManager.EnumeratePrototypes<EntityPrototype>())
{
if (proto.NoSpawn || proto.Abstract)
continue;
if (!proto.Components.TryGetValue(destructibleName, out var destructible))
continue;
var comp = (DestructibleComponent) destructible.Component;
var spawnedEnts = new Dictionary<string, int>();
var spawnedMats = new Dictionary<string, int>();
// This test just blindly assumes that ALL spawn entity behaviors get triggered. In reality, some entities
// might only trigger a subset. If that starts being a problem, this test either needs fixing or needs to
// get an ignored prototypes list.
foreach (var threshold in comp.Thresholds)
{
foreach (var behaviour in threshold.Behaviors)
{
if (behaviour is not SpawnEntitiesBehavior spawn)
continue;
foreach (var (key, value) in spawn.Spawn)
{
spawnedEnts[key] = spawnedEnts.GetValueOrDefault(key) + value.Max;
var spawnProto = protoManager.Index<EntityPrototype>(key);
if (!spawnProto.Components.TryGetValue(stackName, out var reg))
continue;
var stack = (StackComponent) reg.Component;
spawnedMats[stack.StackTypeId] = value.Max + spawnedMats.GetValueOrDefault(stack.StackTypeId);
}
}
}
if (spawnedEnts.Count > 0)
spawnedOnDestroy.Add(proto.ID, (spawnedEnts, spawnedMats));
}
// This is the main loop where we actually check for destruction arbitrage
Assert.Multiple(async () =>
{
foreach (var (id, (spawnedEnts, spawnedMats)) in spawnedOnDestroy)
{
// Check cargo sell price
// several constructible entities have no sell price
// also this test only really matters if the entity is also purchaseable.... eh..
var spawnedPrice = await GetSpawnedPrice(spawnedEnts);
var price = await GetPrice(id);
if (spawnedPrice > 0 && price > 0)
Assert.LessOrEqual(spawnedPrice, price, $"{id} increases in price after being destroyed");
// Check lathe production
if (latheRecipes.TryGetValue(id, out var recipe))
{
foreach (var (matId, amount) in recipe.RequiredMaterials)
{
var actualAmount = SharedLatheSystem.AdjustMaterial(amount, recipe.ApplyMaterialDiscount, multiplier);
if (spawnedMats.TryGetValue(matId, out var numSpawned))
Assert.LessOrEqual(numSpawned, actualAmount, $"destroying a {id} spawns more {matId} than required to produce via an (upgraded) lathe.");
}
}
// Check construction.
if (constructionMaterials.TryGetValue(id, out var constructionMats))
{
foreach (var (matId, amount) in constructionMats)
{
if (spawnedMats.TryGetValue(matId, out var numSpawned))
Assert.LessOrEqual(numSpawned, amount, $"destroying a {id} spawns more {matId} than required to construct it.");
}
}
}
});
// Finally, lets also check for deconstruction arbitrage.
// Get ingredients returned when deconstructing an entity
Dictionary<string, Dictionary<string, int>> deconstructionMaterials = new();
foreach (var (id, comp) in constructionRecipes)
{
if (comp.DeconstructionNode == null)
continue;
var materials = new Dictionary<string, int>();
var graph = protoManager.Index<ConstructionGraphPrototype>(comp.Graph);
if (!graph.TryPath(comp.Node, comp.DeconstructionNode, out var path) || path.Length == 0)
continue;
var cur = graph.Nodes[comp.Node];
foreach (var node in path)
{
var edge = cur.GetEdge(node.Name);
cur = node;
foreach (var completion in edge.Completed)
{
if (completion is not SpawnPrototype spawnCompletion)
continue;
var spawnProto = protoManager.Index<EntityPrototype>(spawnCompletion.Prototype);
if (!spawnProto.Components.TryGetValue(stackName, out var reg))
continue;
var stack = (StackComponent) reg.Component;
materials[stack.StackTypeId] = spawnCompletion.Amount + materials.GetValueOrDefault(stack.StackTypeId);
}
}
deconstructionMaterials.Add(id, materials);
}
// This is functionally the same loop as before, but now testinng deconstruction rather than destruction.
// This is pretty braindead. In principle construction graphs can have loops and whatnot.
Assert.Multiple(async () =>
{
foreach (var (id, deconstructedMats) in deconstructionMaterials)
{
// Check cargo sell price
var deconstructedPrice = await GetDeconstructedPrice(deconstructedMats);
var price = await GetPrice(id);
if (deconstructedPrice > 0 && price > 0)
Assert.LessOrEqual(deconstructedPrice, price, $"{id} increases in price after being deconstructed");
// Check lathe production
if (latheRecipes.TryGetValue(id, out var recipe))
{
foreach (var (matId, amount) in recipe.RequiredMaterials)
{
var actualAmount = SharedLatheSystem.AdjustMaterial(amount, recipe.ApplyMaterialDiscount, multiplier);
if (deconstructedMats.TryGetValue(matId, out var numSpawned))
Assert.LessOrEqual(numSpawned, actualAmount, $"deconstructing {id} spawns more {matId} than required to produce via an (upgraded) lathe.");
}
}
// Check construction.
if (constructionMaterials.TryGetValue(id, out var constructionMats))
{
foreach (var (matId, amount) in constructionMats)
{
if (deconstructedMats.TryGetValue(matId, out var numSpawned))
Assert.LessOrEqual(numSpawned, amount, $"deconstructing a {id} spawns more {matId} than required to construct it.");
}
}
}
});
await server.WaitPost(() => mapManager.DeleteMap(testMap.MapId));
await pairTracker.CleanReturnAsync();
async Task<double> GetSpawnedPrice(Dictionary<string, int> ents)
{
double price = 0;
foreach (var (id, num) in ents)
{
price += num * await GetPrice(id);
}
return price;
}
async Task<double> GetPrice(string id)
{
if (!priceCache.TryGetValue(id, out var price))
{
await server.WaitPost(() =>
{
var ent = entManager.SpawnEntity(id, testMap.GridCoords);
stackSys.SetCount(ent, 1);
priceCache[id] = price = pricing.GetPrice(ent);
entManager.DeleteEntity(ent);
});
}
return price;
}
async Task<double> GetDeconstructedPrice(Dictionary<string, int> mats)
{
double price = 0;
foreach (var (id, num) in mats)
{
var matProto = protoManager.Index<StackPrototype>(id).Spawn;
price += num * await GetPrice(matProto);
}
return price;
}
}
}

View File

@@ -12,5 +12,12 @@ namespace Content.Server.Construction.Components
[ViewVariables(VVAccess.ReadWrite)]
[DataField("rating")]
public int Rating { get; private set; } = 1;
/// <summary>
/// This number is used in tests to ensure that you can't use high quality machines for arbitrage. In
/// principle there is nothing wrong with using higher quality parts, but you have to be careful to not
/// allow them to be put into a lathe or something like that.
/// </summary>
public const int MaxRating = 4;
}
}

View File

@@ -83,7 +83,9 @@ namespace Content.Shared.Lathe
/// The value that is used to calculate the modifier <see cref="MaterialUseMultiplier"/>
/// </summary>
[DataField("partRatingMaterialUseMultiplier")]
public float PartRatingMaterialUseMultiplier = 0.75f;
public float PartRatingMaterialUseMultiplier = DefaultPartRatingMaterialUseMultiplier;
public const float DefaultPartRatingMaterialUseMultiplier = 0.75f;
#endregion
}

View File

@@ -4,6 +4,7 @@ using JetBrains.Annotations;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using System.Net.Mail;
namespace Content.Shared.Lathe;
@@ -50,9 +51,7 @@ public abstract class SharedLatheSystem : EntitySystem
foreach (var (material, needed) in recipe.RequiredMaterials)
{
var adjustedAmount = recipe.ApplyMaterialDiscount
? (int) (needed * component.MaterialUseMultiplier)
: needed;
var adjustedAmount = AdjustMaterial(needed, recipe.ApplyMaterialDiscount, component.MaterialUseMultiplier);
if (_materialStorage.GetMaterialAmount(component.Owner, material) < adjustedAmount * amount)
return false;
@@ -60,6 +59,9 @@ public abstract class SharedLatheSystem : EntitySystem
return true;
}
public static int AdjustMaterial(int original, bool reduce, float multiplier)
=> reduce ? (int) MathF.Ceiling(original * multiplier) : original;
protected abstract bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component);
}

View File

@@ -4,7 +4,7 @@
sprite: Objects/Consumable/Food/Baked/pizza.rsi
state: margherita
product: CrateFoodPizza
cost: 500
cost: 550
category: Food
group: market

View File

@@ -52,7 +52,7 @@
sprite: Objects/Specific/Service/vending_machine_restock.rsi
state: base
product: CrateVendingMachineRestockGamesFilled
cost: 650
cost: 750
category: Service
group: market

View File

@@ -71,6 +71,8 @@
- Trash
- type: Recyclable
- type: SpaceGarbage
- type: StaticPrice
price: 0
- type: entity
name: bowl

View File

@@ -74,6 +74,8 @@
max: 1
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 50
- type: entity
parent: FoodTinBase
@@ -129,6 +131,8 @@
max: 1
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 50
- type: entity
parent: FoodTinBase
@@ -179,6 +183,8 @@
max: 1
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 50
- type: entity
parent: FoodTinBase
@@ -241,6 +247,8 @@
- id: FoodTinMREOpen
sound:
path: /Audio/Items/can_open3.ogg
- type: StaticPrice
price: 50
- type: entity

View File

@@ -195,6 +195,7 @@
materialRequirements:
Cable: 5
- type: Construction
deconstructionTarget: null
graph: ThermomachineBoard
node: freezer
@@ -215,6 +216,7 @@
Cable: 5
- type: Construction
graph: ThermomachineBoard
deconstructionTarget: null
node: heater
- type: entity

View File

@@ -76,6 +76,8 @@
damage:
types:
Piercing: 5
- type: StaticPrice
price: 0
- type: entity
parent: ShardBase

View File

@@ -42,6 +42,8 @@
SheetGlass:
min: 5
max: 5
- type: StaticPrice
price: 50
#Security Shields
@@ -115,6 +117,8 @@
MaterialWoodPlank:
min: 5
max: 5
- type: StaticPrice
price: 150
- type: entity
name: makeshift shield

View File

@@ -116,6 +116,7 @@
- type: Construction
graph: ShotgunSawn
node: shotgundoublebarreled
deconstructionTarget: null
- type: entity
name: Enforcer
@@ -161,3 +162,4 @@
- type: Construction
graph: ShotgunSawn
node: shotgunsawn
deconstructionTarget: null

View File

@@ -192,3 +192,5 @@
states:
enum.Trigger.TriggerVisualState.Primed: primed
enum.Trigger.TriggerVisualState.Unprimed: complete
- type: StaticPrice
price: 25

View File

@@ -175,6 +175,8 @@
- type: Tag
tags:
- Wooden
- type: StaticPrice
price: 75
- type: entity
name: ritual chair

View File

@@ -46,7 +46,7 @@
components:
- type: Construction
graph: LightFixture
node: tubeLight
node: groundLight
- type: Sprite
sprite: Structures/Lighting/LightPosts/small_light_post.rsi
snapCardinals: true
@@ -117,3 +117,5 @@
damage:
types:
Heat: 20
- type: StaticPrice
price: 25

View File

@@ -527,6 +527,8 @@
layer:
- MachineLayer
- type: Recyclable
- type: StaticPrice
price: 60
- type: entity
parent: GasCanisterBrokenBase

View File

@@ -22,6 +22,8 @@
node: crateplastic
containers:
- entity_storage
- type: StaticPrice
price: 150
- type: entity
parent: CrateBaseWeldable
@@ -280,6 +282,8 @@
node: cratelivestock
containers:
- entity_storage
- type: StaticPrice
price: 125
- type: entity
parent: CrateGeneric
@@ -325,4 +329,6 @@
- LargeMobMask
layer:
- LargeMobLayer
- type: StaticPrice
price: 60

View File

@@ -49,7 +49,7 @@
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 0.5
price: 10
- type: entity
id: ReinforcedGirder
@@ -85,3 +85,5 @@
max: 1
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 66

View File

@@ -154,7 +154,7 @@
spawn:
PartRodMetal1:
min: 0
max: 2
max: 1
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: Climbable

View File

@@ -40,7 +40,7 @@
damageOverlay:
sprite: Structures/Windows/cracks.rsi
- type: StaticPrice
price: 20.5
price: 60
- type: RadiationBlocker
resistance: 2
@@ -81,4 +81,4 @@
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 20.5
price: 30

View File

@@ -48,8 +48,6 @@
trackAllDamage: true
damageOverlay:
sprite: Structures/Windows/cracks.rsi
- type: StaticPrice
price: 150
- type: entity
parent: ReinforcedWindow
@@ -68,7 +66,7 @@
node: tintedWindow
- type: Occluder
- type: StaticPrice
price: 0.75
price: 44
- type: entity
id: WindowReinforcedDirectional
@@ -109,4 +107,4 @@
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 0.75
price: 22

View File

@@ -51,7 +51,7 @@
damageOverlay:
sprite: Structures/Windows/cracks.rsi
- type: StaticPrice
price: 20.75
price: 132
- type: entity
id: PlasmaReinforcedWindowDirectional
@@ -99,4 +99,4 @@
- !type:DoActsBehavior
acts: [ "Destruction" ]
- type: StaticPrice
price: 20.75
price: 66

View File

@@ -169,7 +169,7 @@
graph: WindowDirectional
node: windowDirectional
- type: StaticPrice
price: 0.5
price: 5
- type: entity
id: WindowTintedDirectional
@@ -194,7 +194,7 @@
- type: Occluder
boundingBox: "-0.5,-0.5,0.5,-0.3"
- type: StaticPrice
price: 0.5
price: 5
- type: entity
id: WindowFrostedDirectional
@@ -214,4 +214,4 @@
sprite: Structures/Windows/directional.rsi
state: frosted_window
- type: StaticPrice
price: 0.5
price: 5

View File

@@ -42,6 +42,8 @@
- type: Construction
graph: PlasticFlapsGraph
node: plasticFlaps
- type: StaticPrice
price: 150
- type: entity
id: PlasticFlapsOpaque

View File

@@ -149,6 +149,7 @@
id: TargetHuman
result: TargetHuman
completetime: 5
applyMaterialDiscount: false # ingredients dropped when destroyed
materials:
Steel: 10
@@ -156,6 +157,7 @@
id: TargetClown
result: TargetClown
completetime: 5
applyMaterialDiscount: false # ingredients dropped when destroyed
materials:
Steel: 10
@@ -163,5 +165,6 @@
id: TargetSyndicate
result: TargetSyndicate
completetime: 5
applyMaterialDiscount: false # ingredients dropped when destroyed
materials:
Steel: 10