/// <summary> /// Sell all of the ship's cargo at the specified town. /// </summary> int SellCargo(Town town) { int profit = 0; for (int i = cargo.Count; i > 0;) { CargoEntry ent = cargo[--i]; if (ent.owner != town) { Town.ResourceEntry res = town.resources[ent.id]; if (res.production < 0) { int available = ent.amount; int demand = -Mathf.RoundToInt(res.warehouse); // Remove this cargo entry from the ship's hold cargo.RemoveAt(i); // Move the ship's contents into the warehouse res.warehouse += available; // Now it's time to calculate how much the town will pay for these goods int amount = Mathf.Max(0, demand + (res.production * 3) / 2); amount = Mathf.Min(available, amount); // High demand if (amount > 0) { profit += amount * 7; demand -= amount; available -= amount; } // Medium demand if (available > 0 && demand > 0) { amount = Mathf.Min(available, demand); profit += amount * 5; demand -= amount; available -= amount; } // Low demand if (available > 0) { profit += available * 2; } } } } return(profit); }
/// <summary> /// Load the town's exported cargo onto the ship. /// </summary> void LoadCargo(Town town) { int available = cargoAllowance; int stock = 0; // Add up the town's exported stockpiles foreach (TradeRoute.Item item in tradeRoute.items) { if (item.town == town) { Town.ResourceEntry res = town.resources[item.id]; int amount = Mathf.RoundToInt(res.warehouse); stock += amount; } } // If we can carry something, let's load it onto the ship if (stock > 0 && available > 0) { // We want to evenly load all available goods float factor = Mathf.Min((float)available / stock, 1f); foreach (TradeRoute.Item item in tradeRoute.items) { if (item.town == town) { Town.ResourceEntry res = town.resources[item.id]; int amount = Mathf.Min(available, Mathf.RoundToInt(res.warehouse * factor)); if (amount > 0) { CargoEntry ce = new CargoEntry(); ce.id = item.id; ce.amount = amount; ce.owner = town; res.warehouse -= amount; available -= amount; cargo.Add(ce); // If we reach the limit, we want to end the loading process if (available == 0) { break; } } } } } }