// Use this for initialization void Start() { // Initialize all resource thingies foreach (var resource in (Resource[])Enum.GetValues(typeof(Resource))) { m_resourceStockpiles[resource] = new ResourceStockpile(0, 15); // TOOO not have hardcoded max stockpile for each resource m_resourceDemands[resource] = 0; } // Setup some workers Population uneducated = new Population(); uneducated.m_amount = m_startPopulation; uneducated.m_maintenance[Resource.Food] = m_foodPerPop; uneducated.m_maintenance[Resource.Clothes] = m_foodPerPop; uneducated.m_maintenance[Resource.Drugs] = m_foodPerPop; uneducated.m_popType = PopulationType.Uneducated; m_population[PopulationType.Uneducated] = uneducated; // Debug stuff (kinda) if (m_cityIndex == 0) { M_CreateFoodCity(); } else if (m_cityIndex == 1) { M_CreateClothesCity(); } }
// Update is called once per frame void Update() { if (m_status == TraderStatus.Idle) { M_FindTrade(); } else if (m_status == TraderStatus.MovingToSupplier) { if (m_unit.m_destinationReached) { m_supplyCityObj.GetComponent <City>().m_resourceStockpiles[m_resource].M_TransferAllResourcesTo(ref m_stockpile); m_unit.M_SetDestination(m_demandCityObj.transform.position); m_status = TraderStatus.MovingToDemander; } } else if (m_status == TraderStatus.MovingToDemander) { if (m_unit.m_destinationReached) { ResourceStockpile targetStockpile = m_demandCityObj.GetComponent <City>().m_resourceStockpiles[m_resource]; // I really wish I had pointers to make this explicit... m_stockpile.M_TransferAllResourcesTo(ref targetStockpile); m_supplyCityObj.GetComponent <City>().m_resourceStockpiles[m_resource].M_TransferAllResourcesTo(ref m_stockpile); m_status = TraderStatus.Idle; } } }
public static void comparison() { GameResourceType type1 = GameResourceTest.getTestGameResourceType(id: 1); GameResourceType type2 = GameResourceTest.getTestGameResourceType(id: 2); ResourceStockpile stock1 = GetTestResourceYield(type: type1, value: 100); ResourceStockpile stock2 = GetTestResourceYield(type: type1, value: 200); ResourceStockpile stock3 = GetTestResourceYield(type: type2, value: 300); Assert.That(stock1.CompareTo(stock2), Is.EqualTo(0)); Assert.That(stock1.CompareTo(stock3), Is.LessThan(0)); }
public static void equality() { GameResourceType type1 = GameResourceTest.getTestGameResourceType(id: 1); GameResourceType type2 = GameResourceTest.getTestGameResourceType(id: 2); ResourceStockpile stock1 = GetTestResourceYield(type: type1, value: 100); ResourceStockpile stock2 = GetTestResourceYield(type: type1, value: 200); ResourceStockpile stock3 = GetTestResourceYield(type: type2, value: 300); ResourceStockpile stock4 = GetTestResourceYield(type: type1, value: 100); Assert.AreNotEqual(stock1, stock2); Assert.AreNotEqual(stock1, stock3); Assert.AreEqual(stock1, stock4); }
// Transfer resources from this stockpile to another stockpile public void M_TransferResourcesTo(ref ResourceStockpile otherStockpile, float amount) { if (amount < 0) { Debug.LogError("using ResourceStockpile.M_TransferResources wrong. Amount has to be >= 0"); } // Prevent transfering more than what's possible if (amount >= m_amount) { amount = m_amount; } float leftOver = otherStockpile.M_AddResources(amount); m_amount = leftOver; }
void M_UpdatePopulation() { // Update maintenance foreach (var kvp in m_population) { Population pop = kvp.Value; foreach (var kvp1 in pop.m_maintenance) { Resource resource = kvp1.Key; float maintenance = kvp1.Value; float totalMaintenance = pop.m_amount * maintenance * Time.deltaTime; ResourceStockpile stockpile = m_resourceStockpiles[resource]; float unfulfilledMaintenance = totalMaintenance - stockpile.M_FetchResources(totalMaintenance); // TODO improve this somehow? /* 1) add base demand. Too much supply decreases, too little supple increases * 2) fix decay down to base demand * 3) exponential growth?*/ if (unfulfilledMaintenance > 0) { m_resourceDemands[resource] += unfulfilledMaintenance * m_demandGrowthFactor; if (m_resourceDemands[resource] > m_maxDemand) { m_resourceDemands[resource] = m_maxDemand; } } else // Maintenance fulfilled { m_resourceDemands[resource] -= m_needDecay * Time.deltaTime; // TODO not just clear maintenance if (m_resourceDemands[resource] <= 0) { m_resourceDemands[resource] = 0; } } } } }
public void M_TransferAllResourcesTo(ref ResourceStockpile otherStockpile) { M_TransferResourcesTo(ref otherStockpile, m_amount); }
// Performs the process, consuming goods and producing public void Execute(ref Dictionary <Resource, ResourceStockpile> cityStockpiles) { if (m_currentState == ProcessState.BuildingLocalStockpiles) { // 1) Fill local input stockpiles if the city has the supplies we need foreach (var kvp in m_inputStockpiles) { kvp.Value.M_AddResources(kvp.Value.m_amount + cityStockpiles[kvp.Key].M_FetchResources(1)); // TODO reconsider fetching 1 at a time. Maybe more, maybe max } // 2) If ALL local stockpiles are filled, deplete them and start proce bool allStockpilesFull = true; foreach (var kvp in m_inputStockpiles) { if (kvp.Value.m_amount != kvp.Value.m_max) { allStockpilesFull = false; break; } } if (allStockpilesFull) { foreach (var kvp in m_inputStockpiles) { kvp.Value.M_Empty(); } m_currentState = ProcessState.Working; } } // 3) When complete time is exceeded, try to increase city stockpile by produced. If it isn't possible, try again next frame if (m_currentState == ProcessState.Working) { m_currentTime += Time.deltaTime; if (m_currentTime >= m_completionTime) { m_currentTime = 0; foreach (var kvp in m_resourcesProduced) { m_outputStockpiles[kvp.Key].M_AddResources(kvp.Value); } m_currentState = ProcessState.DoneWorking; } } if (m_currentState == ProcessState.DoneWorking) { bool allTransfersDone = true; foreach (var kvp in m_outputStockpiles) { // This better behave like a pointer! (TODO be sure? I'm sure I'll notice) ResourceStockpile cityStockpile = cityStockpiles[kvp.Key]; kvp.Value.M_TransferAllResourcesTo(ref cityStockpile); if (kvp.Value.m_amount > 0) { allTransfersDone = false; } } if (allTransfersDone) { m_currentState = ProcessState.BuildingLocalStockpiles; } } }
// Awake is called before Start private void Awake() { colonisedPlanets = new List <Planet>(); resPool = new ResourceStockpile(); collectors = new List <ResourceCollector>(); }
public static void math() { GameResourceType type1 = GameResourceTest.getTestGameResourceType(id: 1); GameResourceType type2 = GameResourceTest.getTestGameResourceType(id: 2); ResourceStockpile stock1 = GetTestResourceYield(type: type1, value: 100); ResourceStockpile stock2 = GetTestResourceYield(type: type1, value: 200); ResourceStockpile stock3 = GetTestResourceYield(type: type2, value: 300); //Addition Assert.That(stock1.canCombine(stock2)); ResourceStockpile sum1 = stock1 + stock2; Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(stock2.type, type1); Assert.AreEqual(stock2.value, 200); Assert.AreEqual(sum1.type, type1); Assert.AreEqual(sum1.value, 300); ResourceStockpile?sum2 = null; Assert.That(!stock1.canCombine(stock3)); Assert.Catch(() => sum2 = stock1 + stock3); Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(stock3.type, type2); Assert.AreEqual(stock3.value, 300); Assert.IsNull(sum2); ResourceStockpile sum3 = stock1 + 300; Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(sum3.type, type1); Assert.AreEqual(sum3.value, 400); //Subtraction ResourceStockpile diff1 = stock1 - stock2; Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(stock2.type, type1); Assert.AreEqual(stock2.value, 200); Assert.AreEqual(diff1.type, type1); Assert.AreEqual(diff1.value, -100); ResourceStockpile?diff2 = null; Assert.Catch(() => diff2 = stock1 - stock3); Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(stock3.type, type2); Assert.AreEqual(stock3.value, 300); Assert.IsNull(diff2); ResourceStockpile diff3 = stock1 - 300; Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(diff3.type, type1); Assert.AreEqual(diff3.value, -200); //Scaling ResourceStockpile prod1 = stock1 * 7; Assert.AreEqual(stock1.type, type1); Assert.AreEqual(stock1.value, 100); Assert.AreEqual(prod1.type, type1); Assert.AreEqual(prod1.value, 700); }
// Use this for initialization void Start() { m_unit = GetComponent <Unit>(); m_stockpile = new ResourceStockpile(0, m_maxTraderStorage); }