-
Notifications
You must be signed in to change notification settings - Fork 0
/
TradeMissionCalculator.cs
123 lines (110 loc) · 5.09 KB
/
TradeMissionCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
static class TradeMissionCalculator
{
/**
* TODO Implement a real Trading algorithm, this works for testing
*
*/
public static List<TradeMission> FindTradeMissions(GameShip gameShip, int howMany)
{
GameTown[] gameTowns = GameWorld.FindObjectsOfType<GameTown>().Where(t=> TradeShip.GameTownIsAlly(gameShip, t)).ToArray();
Array.Sort(gameTowns, new TownByProximityComparator(gameShip.position));
List<TradeMission> newTradeMissions = new List<TradeMission>();
if (gameTowns != null && gameTowns.Count() > 0)
{
List<TradeMission> tradeMissions = FindAll();
foreach (GameTown gameTown in gameTowns)
{
foreach (TownResource production in gameTown.production)
{
if (production.count > 0)
{
var allCurrentTradeMissions = new List<TradeMission>(newTradeMissions.Count + tradeMissions.Count);
allCurrentTradeMissions.AddRange(newTradeMissions);
allCurrentTradeMissions.AddRange(tradeMissions);
int count = HowManyCanWeBuy(production.name, gameTown, tradeMissions);
if (count > 0)
{
GameTown buyer = FindBuyer(production.name, gameTowns.Where(x => !x.name.Equals(gameTown.name)).ToArray(), allCurrentTradeMissions);
while (count > 0 && buyer != null)
{
newTradeMissions.Add(new TradeMission() { ResourceName = production.name, Departure = gameTown.id, Destination = buyer.id });
if (newTradeMissions.Count() >= howMany)
{
return newTradeMissions;
}
count--;
allCurrentTradeMissions = new List<TradeMission>(newTradeMissions.Count + tradeMissions.Count);
allCurrentTradeMissions.AddRange(newTradeMissions);
allCurrentTradeMissions.AddRange(tradeMissions);
buyer = FindBuyer(production.name, gameTowns.Where(x => !x.name.Equals(gameTown.name)).ToArray(), allCurrentTradeMissions);
}
}
}
}
}
}
return newTradeMissions;
}
public static TradeMission FindATradeMissionForResource(string resourceName, GameTown departure, Vector3 position)
{
GameTown[] gameTowns = GameWorld.FindObjectsOfType<GameTown>().Where(t => t.factionID != 0).ToArray();
Array.Sort(gameTowns, new TownByProximityComparator(position));
if (gameTowns != null && gameTowns.Count() > 0)
{
List<TradeMission> tradeMissions = FindAll();
GameTown buyer = FindBuyer(resourceName, gameTowns.Where(x=>!x.name.Equals(departure.name)).ToArray(), tradeMissions);
if (buyer != null)
{
return new TradeMission() { ResourceName = resourceName, Departure = departure.id, Destination = buyer.id };
}
}
return null;
}
private static GameTown FindBuyer(string resourceName, GameTown[] gameTowns, List<TradeMission> tradeMissions)
{
List<GameTown> destinations = FindTownsThatNeedResource(gameTowns, resourceName);
foreach (GameTown destination in destinations)
{
if (CanSell(resourceName, destination, tradeMissions))
{
return destination;
}
}
return null;
}
private static List<GameTown> FindTownsThatNeedResource(IEnumerable<GameTown> gameTowns, string resourceName)
{
return gameTowns
.Where(x => x.NeedsResource(resourceName))
.ToList();
}
private static int HowManyCanWeBuy(string resourceName, GameTown departure, List<TradeMission> allTradeMissionsForResource)
{
if(departure == null)
{
return 0;
}
int production = departure.GetProduction(resourceName);
production -= allTradeMissionsForResource.Where(m => !m.StockedUp() && m.Departure.Equals(departure.id)).Count();
return production;
}
private static Boolean CanSell(string resourceName, GameTown destination, List<TradeMission> allTradeMissionsForResource)
{
int demand = destination.GetDemand(resourceName);
demand -= allTradeMissionsForResource.Where(m => m.Destination.Equals(destination.id)).Count();
return demand > 0;
}
private static List<TradeMission> FindAll()
{
return FindAllTraders().SelectMany(x => x.TradeMissions).ToList();
}
public static List<TradeShip> FindAllTraders()
{
return GameWorld.FindObjectsOfType<TradeShip>().Where(x=> x.Owner != null && x.Owner.id == MyPlayer.id).ToList();
}
}