-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolyPlusPatcher.cs
221 lines (209 loc) · 11.4 KB
/
PolyPlusPatcher.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using HarmonyLib;
using Polytopia.Data;
using UnityEngine;
namespace PolyPlus {
public class PolyPlusPatcher
{
public static void Load()
{
CreateEnumCaches();
Harmony.CreateAndPatchAll(typeof(PolyPlusPatcher));
}
internal static void CreateEnumCaches()
{
Console.Write("Created mapping for ImprovementAbility.Type with id waterembark and index " + PolyMod.ModLoader.autoidx);
EnumCache<ImprovementAbility.Type>.AddMapping("halved", (ImprovementAbility.Type)PolyMod.ModLoader.autoidx);
EnumCache<ImprovementAbility.Type>.AddMapping("halved", (ImprovementAbility.Type)PolyMod.ModLoader.autoidx);
PolyMod.ModLoader.autoidx++;
Console.Write("Created mapping for PlayerAbility.Type with id waterembark and index " + PolyMod.ModLoader.autoidx);
EnumCache<PlayerAbility.Type>.AddMapping("waterembark", (PlayerAbility.Type)PolyMod.ModLoader.autoidx);
EnumCache<PlayerAbility.Type>.AddMapping("waterembark", (PlayerAbility.Type)PolyMod.ModLoader.autoidx);
PolyMod.ModLoader.autoidx++;
Console.Write("Created mapping for PlayerAbility.Type with id polyplusstatic and index " + PolyMod.ModLoader.autoidx);
EnumCache<UnitAbility.Type>.AddMapping("polyplusstatic", (UnitAbility.Type)PolyMod.ModLoader.autoidx);
EnumCache<UnitAbility.Type>.AddMapping("polyplusstatic", (UnitAbility.Type)PolyMod.ModLoader.autoidx);
PolyMod.ModLoader.autoidx++;
int tribeBonusAutoidx = (int)Enum.GetValues(typeof(TribeData.BonusEnum)).Cast<TribeData.BonusEnum>().Last();
tribeBonusAutoidx++;
Console.Write("Created mapping for TribeData.BonusEnum with id citypark and index " + tribeBonusAutoidx);
EnumCache<TribeData.BonusEnum>.AddMapping("citypark", (TribeData.BonusEnum)tribeBonusAutoidx);
EnumCache<TribeData.BonusEnum>.AddMapping("citypark", (TribeData.BonusEnum)tribeBonusAutoidx);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UnitData), nameof(UnitData.getPromotionLimit))]
private static void UnitData_getPromotionLimit(ref int __result, UnitData __instance, PlayerState player, GameState gameState)
{
if(__instance.unitAbilities.Contains(EnumCache<UnitAbility.Type>.GetType("polyplusstatic")))
{
__result = 0;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UnitPopup), nameof(UnitPopup.UnitData), MethodType.Setter)]
private static void UnitPopup_UnitData_Set(UnitPopup __instance)
{
Vector2 anchoredPosition = __instance.iconContainer.anchoredPosition;
string improvementName = string.Empty;
if (__instance.Unit != null && GameManager.GameState != null && GameManager.GameState.Map != null)
{
TileData tile = GameManager.GameState.Map.GetTile(__instance.Unit.UnitState.home);
if (tile != null && tile.HasImprovement(ImprovementData.Type.City))
{
improvementName = tile.improvement.name;
}
}
string unitDescription = string.IsNullOrEmpty(improvementName) ? string.Empty : string.Format("{0}\n", Localization.Get("world.unit.info.from", new Il2CppSystem.Object[] { improvementName }));
string unitProgressText;
int killCount = (int)(__instance.Unit ? __instance.Unit.UnitState.xp : 0);
if (UIManager.Instance.CurrentScreen != UIConstants.Screens.TechTree && __instance.unit != null && __instance.unit.unitData.HasAbility(EnumCache<UnitAbility.Type>.GetType("polyplusstatic")))
{
unitProgressText = Localization.Get("polyplus.unit.veteran.static.progress", new Il2CppSystem.Object[]
{
killCount.ToString(),
});
__instance.Description = string.Format("{0}{1}", unitDescription, unitProgressText);
anchoredPosition.x = 48f;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(MapGenerator), nameof(MapGenerator.SetTileAsCapital))]
private static void MapGenerator_SetTileAsCapital(GameState gameState, PlayerState playerState, TileData tile)
{
TribeData tribeData;
if (tile != null && gameState.GameLogicData.TryGetData(playerState.tribe, out tribeData))
{
if (tribeData.bonus == EnumCache<TribeData.BonusEnum>.GetType("citypark"))
{
tile.improvement.production = 2;
tile.improvement.baseScore += 250;
tile.improvement.AddReward(CityReward.Park);
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(PathFinder), nameof(PathFinder.IsTileAccessible))]
private static void PathFinder_IsTileAccessible(ref bool __result, TileData tile, TileData origin, PathFinderSettings settings)
{
if(PlayerExtensions.HasAbility(settings.playerState, EnumCache<PlayerAbility.Type>.GetType("waterembark"), settings.gameState) && tile.IsWater && !origin.IsWater && settings.unit != null){
if((tile.terrain == Polytopia.Data.TerrainData.Type.Water && settings.allowedTerrain.Contains(Polytopia.Data.TerrainData.Type.Water)) || (tile.terrain == Polytopia.Data.TerrainData.Type.Ocean && settings.allowedTerrain.Contains(Polytopia.Data.TerrainData.Type.Ocean)))
{
__result = true;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(PathFinder), nameof(PathFinder.GetMoveOptions))]
private static void PathFinder_GetMoveOptions(ref Il2CppSystem.Collections.Generic.List<WorldCoordinates> __result, GameState gameState, WorldCoordinates start, int maxCost, UnitState unit)
{
PlayerState playerState;
TileData startTile = GameManager.GameState.Map.GetTile(start);
Il2CppSystem.Collections.Generic.List<WorldCoordinates> options = __result;
List<WorldCoordinates> toRemove = new List<WorldCoordinates>();
if(gameState.TryGetPlayer(unit.owner, out playerState))
{
if(PlayerExtensions.HasAbility(playerState, EnumCache<PlayerAbility.Type>.GetType("waterembark"), gameState))
{
foreach (WorldCoordinates destination in options)
{
if(!startTile.IsWater)
{
Il2CppSystem.Collections.Generic.List<WorldCoordinates> path = PathFinder.GetPath(gameState, start, destination, maxCost, unit);
path.Reverse();
bool hadWater = false;
foreach (WorldCoordinates pathTile in path)
{
TileData tile = gameState.Map.GetTile(pathTile);
if(hadWater)
{
toRemove.Add(tile.coordinates);
}
if(tile.terrain == Polytopia.Data.TerrainData.Type.Water || tile.terrain == Polytopia.Data.TerrainData.Type.Ocean)
{
if(tile.improvement != null)
{
if(gameState.GameLogicData.TryGetData(tile.improvement.type, out ImprovementData imrovementData))
{
if(!imrovementData.HasAbility(ImprovementAbility.Type.Bridge))
{
hadWater = true;
}
}
}
else
{
hadWater = true;
}
}
}
}
}
}
}
foreach (var item in toRemove)
{
options.Remove(item);
}
__result = options;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(MoveAction), nameof(MoveAction.ExecuteDefault))]
private static void MoveAction_ExecuteDefault(MoveAction __instance, GameState gameState)
{
UnitState unitState;
PlayerState playerState;
UnitData unitData;
if (gameState.TryGetUnit(__instance.UnitId, out unitState) && gameState.TryGetPlayer(__instance.PlayerId, out playerState) && gameState.GameLogicData.TryGetData(unitState.type, out unitData))
{
WorldCoordinates worldCoordinates = __instance.Path[0];
TileData tile2 = gameState.Map.GetTile(worldCoordinates);
tile2.SetUnit(unitState);
unitState.coordinates = worldCoordinates;
bool hasNoBridge = true;
if(tile2.improvement != null)
{
if(gameState.GameLogicData.TryGetData(tile2.improvement.type, out ImprovementData imrovementData))
{
if(imrovementData.HasAbility(ImprovementAbility.Type.Bridge))
{
hasNoBridge = false;
}
}
}
if(hasNoBridge && !unitData.IsAquatic() && !unitState.HasAbility(UnitAbility.Type.Fly, gameState) && tile2.IsWater && PlayerExtensions.HasAbility(playerState, EnumCache<PlayerAbility.Type>.GetType("waterembark"), gameState))
{
gameState.ActionStack.Add(new EmbarkAction(__instance.PlayerId, worldCoordinates));
}
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ExamineRuinsAction), nameof(ExamineRuinsAction.ExecuteDefault))]
private static bool ExecuteDefault(ExamineRuinsAction __instance, GameState gameState)
{
if(__instance.Reward == RuinsReward.City)
{
RuinsReward[] excludedValues = new RuinsReward[]{ RuinsReward.None, RuinsReward.City, RuinsReward.SuperUnit, RuinsReward.Battleship, RuinsReward.Seamonster};
Array values = Enum.GetValues(typeof(RuinsReward));
var filteredValues = values.Cast<RuinsReward>().Where(v => !excludedValues.Contains(v)).ToArray();
System.Random random = new System.Random();
__instance.Reward = filteredValues[random.Next(filteredValues.Length)];
}
return true;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ActionUtils), nameof(ActionUtils.CalculateImprovementLevel))]
private static void ActionUtils_CalculateImprovementLevel(ref int __result, GameState gameState, TileData tile)
{
if(tile.improvement != null)
{
if (gameState.GameLogicData.TryGetData(tile.improvement.type, out ImprovementData improvementData))
{
if(improvementData.HasAbility(EnumCache<ImprovementAbility.Type>.GetType("halved")))
{
__result = __result / 2;
return;
}
}
}
}
}
}