Infrastructure and Production

Added the ability to directly install blueprints onto hardpoints in the colony tab
Added the ability to customize production of factories
This stuff is still kinda buggy
This commit is contained in:
hyperion51 2020-06-02 07:01:03 +01:00
commit 4eb83acb34
32 changed files with 2525 additions and 399 deletions

View file

@ -24,21 +24,43 @@ public class FactoryData : BehaviorData
public class Factory : IBehavior, IPersistentBehavior
{
public float ProductionQuality;
public double RetoolingTime;
public string ItemName;
public List<Guid> ItemsUnderConstruction = new List<Guid>();
public Entity Entity { get; }
public Gear Item { get; }
public GameContext Context { get; }
public BehaviorData Data => _data;
public float ToolingTime { get; private set; }
public Guid Blueprint
{
get => _blueprint;
set
{
if (value != _blueprint)
{
_blueprint = value;
RetoolingTime = ToolingTime;
_retooling = true;
OnToolingUpdate?.Invoke();
OnToolingUpdate = null;
ItemName = Context.Cache.Get<BlueprintData>(value).Name;
}
}
}
public event Action OnToolingUpdate;
private FactoryData _data;
private Guid _blueprint;
private float _remainingManHours;
private bool _retooling;
private bool _producing;
private List<ItemInstance> _reservedStock = new List<ItemInstance>();
private int _assignedPopulation;
private float _productionQuality;
private bool _retooling = false;
public Factory(GameContext context, FactoryData data, Entity entity, Gear item)
{
@ -50,148 +72,56 @@ public class Factory : IBehavior, IPersistentBehavior
public bool Update(float delta)
{
ToolingTime = Context.Evaluate(_data.ToolingTime, Item, Entity);
var blueprint = Context.Cache.Get<BlueprintData>(_blueprint);
if (_retooling || _producing)
if (blueprint == null)
return false;
if (RetoolingTime > 0)
{
_remainingManHours -= delta / 3600 * (_assignedPopulation + _data.AutomationPoints) /
pow(lerp(blueprint.QualityFloor, 1, saturate(_productionQuality)),
blueprint.ProductionExponent);
RetoolingTime -= delta;
return false;
}
if (_remainingManHours < 0)
if (_retooling)
{
if (_retooling)
_retooling = false;
else if (_producing)
_retooling = false;
OnToolingUpdate?.Invoke();
OnToolingUpdate = null;
}
if (ItemsUnderConstruction.Any())
{
var finishedConstruction = false;
foreach (var item in ItemsUnderConstruction.ToArray())
{
var blueprintItemData = Context.Cache.Get(blueprint.Item);
if (blueprintItemData is CraftedItemData)
Entity.IncompleteCargo[item] =
Entity.IncompleteCargo[item] - delta * (_assignedPopulation + _data.AutomationPoints) /
pow(lerp(blueprint.QualityFloor, 1, saturate(ProductionQuality)),
blueprint.ProductionExponent);
if(Entity.IncompleteCargo[item] < 0)
{
for (int i = 0; i < blueprint.Quantity; i++)
{
CraftedItemInstance newItem;
// Applying exponents to two random numbers and adding them produces a range of interesting probability distributions
var quality = blueprint.Quality *
pow(_productionQuality, blueprint.QualityExponent) *
(pow(Context.Random.NextFloat(), blueprint.RandomExponent) +
pow(Context.Random.NextFloat(), blueprint.RandomExponent)) / 2;
if (blueprintItemData is EquippableItemData equippableItemData)
{
var newGear = new Gear
{
Context = Context,
Data = blueprintItemData.ID,
Ingredients = _reservedStock.Select(ii=>ii.ID).ToList(),
Quality = quality,
Blueprint = _blueprint
};
newGear.Durability = Context.Evaluate(equippableItemData.Durability, newGear);
newItem = newGear;
}
else
{
newItem = new CompoundCommodity
{
Context = Context,
Data = blueprintItemData.ID,
Ingredients = _reservedStock.Select(ii=>ii.ID).ToList(),
Quality = quality,
Blueprint = _blueprint
};
}
Context.Cache.Add(newItem);
Entity.Cargo.Add(newItem.ID);
}
}
else
{
var simpleCommodityData = blueprintItemData as SimpleCommodityData;
var simpleCommodityInstance = Entity.Cargo.FirstOrDefault(i => Context.Cache.Get<ItemInstance>(i).Data == simpleCommodityData.ID);
if (simpleCommodityInstance == Guid.Empty)
{
var newSimpleCommodity = new SimpleCommodity
{
Context = Context,
Data = simpleCommodityData.ID,
Quantity = blueprint.Quantity
};
Context.Cache.Add(newSimpleCommodity);
Entity.Cargo.Add(newSimpleCommodity.ID);
}
else
{
Context.Cache.Get<SimpleCommodity>(simpleCommodityInstance).Quantity += blueprint.Quantity;
}
}
Entity.RecalculateMass();
var simpleIngredients = new List<SimpleCommodity>();
var compoundIngredients = new List<CompoundCommodity>();
var hasAllIngredients = true;
var cargoInstances = Entity.Cargo.Select(c => Context.Cache.Get<ItemInstance>(c));
foreach (var kvp in blueprint.Ingredients)
{
var itemData = Context.Cache.Get(kvp.Key);
if (itemData is SimpleCommodityData)
{
var matchingItem = cargoInstances.FirstOrDefault(ii =>
{
if (!(ii is SimpleCommodity simpleCommodity)) return false;
return simpleCommodity.Data == itemData.ID && simpleCommodity.Quantity >= kvp.Value;
}) as SimpleCommodity;
hasAllIngredients = hasAllIngredients && matchingItem != null;
if(matchingItem != null)
simpleIngredients.Add(matchingItem);
}
else
{
var matchingItems =
cargoInstances.Where(ii => (ii as CompoundCommodity)?.Data == itemData.ID).Cast<CompoundCommodity>().ToArray();
hasAllIngredients = hasAllIngredients && matchingItems.Length >= kvp.Value;
if(matchingItems.Length >= kvp.Value)
compoundIngredients.AddRange(matchingItems.Take(kvp.Value));
}
}
if (hasAllIngredients)
{
_reservedStock.AddRange(compoundIngredients);
foreach (var compoundCommodity in compoundIngredients)
{
Entity.Cargo.Remove(compoundCommodity.ID);
}
foreach (var simpleCommodity in simpleIngredients)
{
var blueprintQuantity = blueprint.Ingredients
.First(ingredient => ingredient.Key == simpleCommodity.ID).Value;
if (simpleCommodity.Quantity == blueprintQuantity)
{
_reservedStock.Add(simpleCommodity);
Entity.Cargo.Remove(simpleCommodity.ID);
}
else
{
var newSimpleCommodity = new SimpleCommodity
{
Context = Context,
Data = simpleCommodity.Data,
Quantity = blueprintQuantity
};
simpleCommodity.Quantity -= blueprintQuantity;
_reservedStock.Add(newSimpleCommodity);
}
}
Entity.RecalculateMass();
_remainingManHours = blueprint.ProductionTime;
_producing = true;
Entity.Cargo.Add(item);
Entity.IncompleteCargo.Remove(item);
ItemsUnderConstruction.Remove(item);
finishedConstruction = true;
}
}
if(finishedConstruction)
Entity.RecalculateMass();
return true;
}
return true;
// Applying exponents to two random numbers and adding them produces a range of interesting probability distributions for quality
ItemsUnderConstruction = Entity.Build(blueprint, blueprint.Quality *
pow(ProductionQuality, blueprint.QualityExponent) *
(pow(Context.Random.NextFloat(), blueprint.RandomExponent) +
pow(Context.Random.NextFloat(), blueprint.RandomExponent)) / 2, ItemName);
if(ItemsUnderConstruction.Any())
Entity.RecalculateMass(); // TODO: FIX THIS UGLY HACK, needed to update UI
return false;
}
public PersistentBehaviorData Store()
@ -199,12 +129,9 @@ public class Factory : IBehavior, IPersistentBehavior
return new FactoryPersistence
{
Blueprint = _blueprint,
RemainingManHours = _remainingManHours,
Retooling = _retooling,
Producing = _producing,
ReservedStock = _reservedStock.Select(i=>i.ID).ToArray(),
RetoolingTime = RetoolingTime,
AssignedPopulation = _assignedPopulation,
ProductionQuality = _productionQuality
ProductionQuality = ProductionQuality
};
}
@ -212,12 +139,9 @@ public class Factory : IBehavior, IPersistentBehavior
{
var factoryPersistence = data as FactoryPersistence;
_blueprint = factoryPersistence.Blueprint;
_remainingManHours = factoryPersistence.RemainingManHours;
_retooling = factoryPersistence.Retooling;
_producing = factoryPersistence.Producing;
_reservedStock = factoryPersistence.ReservedStock.Select(id => Context.Cache.Get<ItemInstance>(id)).ToList();
RetoolingTime = factoryPersistence.RetoolingTime;
_assignedPopulation = factoryPersistence.AssignedPopulation;
_productionQuality = factoryPersistence.ProductionQuality;
ProductionQuality = factoryPersistence.ProductionQuality;
}
}
@ -225,10 +149,7 @@ public class Factory : IBehavior, IPersistentBehavior
public class FactoryPersistence : PersistentBehaviorData
{
[JsonProperty("blueprint"), Key(0)] public Guid Blueprint;
[JsonProperty("remainingManHours"), Key(1)] public float RemainingManHours;
[JsonProperty("retooling"), Key(2)] public bool Retooling;
[JsonProperty("producing"), Key(3)] public bool Producing;
[JsonProperty("reservedStock"), Key(4)] public Guid[] ReservedStock;
[JsonProperty("retoolingTime"), Key(2)] public double RetoolingTime;
[JsonProperty("assignedPopulation"), Key(5)] public int AssignedPopulation;
[JsonProperty("productionQuality"), Key(6)] public float ProductionQuality;
}