-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
813 lines (713 loc) · 30.5 KB
/
Main.cs
File metadata and controls
813 lines (713 loc) · 30.5 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using BepInEx.Logging;
using HarmonyLib;
using Polytopia.Data;
using PolytopiaBackendBase.Game;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Tilemaps;
namespace Polychievements;
public static class Main
{
private static Dictionary<string, bool> unlockedDict;
private static int AchViewMode = 0;
public static List<Achievement> Achievements = new List<Achievement>()
{
/////////////////////////
/// EASY
/////////////////////////
new Achievement(){
idx = "agriculture",
name = "ach.agriculture", // Implemented
description = "ach.agriculture.desc" // Build a farm
},
new Achievement(){
idx = "veterans",
name = "ach.veterans", // Implemented
description = "ach.veterans.desc" // Own 3 veterans
},
new Achievement(){
idx = "blood",
name = "ach.blood", // Implemented
description = "ach.blood.desc" // Eradicate a tribe.
},
new Achievement(){
idx = "unseen",
name = "ach.unseen", // Implemented
description = "ach.unseen.desc" // Have three unrevealed cloaks in enemy territory.
},
new Achievement(){
idx = "win",
name = "ach.win", // Implemented
description = "ach.win.desc" // Win a game.
},
new Achievement(){
idx = "cartography",
name = "Cartography", // UNTESTED
description = "Reveal all tiles in a Large map or bigger."
},
new Achievement(){
idx = "cow",
name = "City of Wonders", // Implemented
description = "Build 7 Monuments in a single city."
},
new Achievement(){
idx = "dopilus",
name = "All roads lead to Dopilus",
hiddenDesc = true,
description = "Playing as Imperius, have half of the worlds' cities connected to your capital."
},
new Achievement()
{
idx = "colony",
name = "Colonizer", // UNTESTED
description = "Have at least 1 city from all other tribes on a Large map or bigger."
},
////////////////////////////////////////
/// MEDIUM
////////////////////////////////////////
new Achievement(){
idx = "yoink",
name = "Yoink!", // Implemented
description = "Mindbend a giant.",
category = 1
},
new Achievement(){
idx = "green",
name = "Green City",
description = "Have 5 parks in a city you own.",
category = 1
},
new Achievement(){
idx = "hypno",
name = "Hypnotist", // UNTESTED
hiddenDesc = true,
description = "Convert a mindbender with a mindbender.",
category = 1
},
new Achievement(){
idx = "serious",
name = "Serious Dedication",
description = "Research Aquatism and Navigation on a Drylands map by turn 10.",
category = 1
},
new Achievement(){
idx = "crazy",
name = "Crazy good!",
description = "Win a Crazy difficulty game.",
category = 1
},
new Achievement(){
idx = "diplomat",
name = "The Diplomat",
description = "Have an embassy in at least 4 tribes' capitals on at least Normal difficulty.",
category = 1
},
new Achievement(){
idx = "terrorknight",
name = "Knight of Terror",
description = "Own a knight that has killed more than 10 units.",
category = 1
},
new Achievement(){
idx = "lifeashore",
name = "Life Ashore",
description = "Win a Waterworld or Archipelago map without researching Aquatism or Navigation on Normal or harder difficulty.",
category = 1
},
/////////////////////////
/// HARD
/////////////////////////
new Achievement(){
idx = "howdidwe",
name = "How did we get here?",
description = "Have a Poisoned, Frozen, Speedy, Veteran unit.",
category = 2
},
new Achievement(){
idx = "superteam",
name = "Super Team", // UNTESTED
description = "Own a Giant, a Crab, a Dragon, a Gaami and a Centipede.",
category = 2
},
new Achievement(){
idx = "houdini",
name = "Houdini",
description = "Kill a unit sieging your city by spawning a Super Unit in the city, with no available tiles for the attacker to move.",
hiddenDesc = true,
category = 2
},
new Achievement(){
idx = "vengirwater",
name = "Vengir Waterworld",
description = "Win a Crazy difficulty Waterworld game with Vengir, playing against 15 bots.",
category = 2
},
new Achievement(){
idx = "welcomeneigh",
name = "A welcomed neighbour",
description = "Eradicate a tribe before turn 5.",
category = 2
},
new Achievement(){
idx = "atlantis",
name = "Atlantis",
hiddenDesc = true,
description = "Have an island city reach level 10.",
category = 2
},
new Achievement(){
idx = "economist",
name = "Economist",
description = "Have 50 income before reaching turn 20 with at least 1 crazy AI opponent.",
category = 2
},
new Achievement(){
idx = "fakediplomat",
name = "The \"Diplomat\"",
description = "Gain 50 stars in turn from infiltrating enemy cities.",
category = 2
}
};
#region Utils
/// <summary>
/// Registers an achievement safely at runtime. Returns success.
/// </summary>
/// <param name="achievement"></param>
public static bool AddAchievementRunTime(Achievement achievement)
{
if (GetAchievement(achievement.idx) != null)
{
modLogger.LogFatal("Error adding achievement at runtime! Index already exists, change description or name of achievement " + achievement.name);
return false;
}
if (unlockedDict.TryGetValue(achievement.idx, out bool result)) achievement.unlocked = result;
else achievement.unlocked = false;
Achievements.Add(achievement);
return true;
}
public static string GetAchievementIdx(string title)
{
foreach (var ach in Achievements)
{
if (ach.name == title)
{
return ach.idx;
}
}
return string.Empty;
}
public static Achievement GetAchievement(string idx)
{
foreach (var ach in Achievements)
{
if (ach.idx == idx)
{
return ach;
}
}
return null;
}
public static int GetAchievementLocation(string idx)
{
for (int i = 0; i < Achievements.Count; i++)
{
if (Achievements[i].idx == idx)
{
return i;
}
}
return -1;
}
public static List<T> ToSystemList<T>(Il2CppSystem.Collections.Generic.List<T> il2cppList)
{
var sysList = new List<T>(il2cppList.Count);
for (int i = 0; i < il2cppList.Count; i++)
{
sysList.Add(il2cppList[i]);
}
return sysList;
}
public static bool ListContainsList<T>(List<T> origin, List<T> values)
{
if(origin == null || values == null) return false;
var dict = new Dictionary<T, bool>();
foreach(var value in values)
dict.TryAdd(value, false);
foreach(var element in origin)
{
if(dict.ContainsKey(element)) dict[element] = true;
}
foreach(var kvp in dict) if (!kvp.Value) return false;
return true;
}
#endregion
public static ManualLogSource modLogger;
public static void Load(ManualLogSource logger)
{
Harmony.CreateAndPatchAll(typeof(Main));
modLogger = logger;
logger.LogMessage("Polychievements.dll loaded");
unlockedDict = PrefsHelper.LoadDict();
foreach (var ach in Achievements)
{
ach.unlocked = unlockedDict[ach.idx];
}
}
#region UI
public static int GetColor(Achievement achievement)
{
var gld = GameManager.GameState.GameLogicData;
if (achievement.unlocked)
{
return gld.GetTribeColor(PolytopiaBackendBase.Common.TribeType.Kickoo, PolytopiaBackendBase.Common.SkinType.Default);
}
else return gld.GetTribeColor(PolytopiaBackendBase.Common.TribeType.Xinxi, PolytopiaBackendBase.Common.SkinType.Default);
}
// Generously stolen from klipi
internal static void AddUiButtonToArray(UIRoundButton prefabButton, HudScreen hudScreen, UIButtonBase.ButtonAction action, UIRoundButton[] buttonArray, string? description = null)
{
UIRoundButton button = UnityEngine.GameObject.Instantiate(prefabButton, prefabButton.transform);
button.transform.parent = hudScreen.buttonBar.transform;
button.OnClicked += action;
List<UIRoundButton> list = buttonArray.ToList();
list.Insert(list.Count - 2, button);
list.ToArray();
if (description != null)
{
Transform child = button.gameObject.transform.Find("DescriptionText");
if (child != null)
{
TMPLocalizer localizer = child.gameObject.GetComponent<TMPLocalizer>();
localizer.Text = description;
}
else
{
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(HudButtonBar), nameof(HudButtonBar.Init))]
internal static void HudButtonBar_Init(HudButtonBar __instance, HudScreen hudScreen)
{
if (true && !(GameManager.Instance.isLevelLoaded && GameManager.GameState.Settings.BaseGameMode == GameMode.Custom))
{
AddUiButtonToArray(__instance.menuButton, __instance.hudScreen, (UIButtonBase.ButtonAction)MenuButtonOnClicked, __instance.buttonArray, "Achievements");
//__instance.nextTurnButton.gameObject.SetActive(false);
//__instance.techTreeButton.gameObject.SetActive(false);
//__instance.statsButton.gameObject.SetActive(false);
__instance.Show();
__instance.Update();
// __instance.buttonBar.statsButton.BlockButton = true;
void MenuButtonOnClicked(int id, BaseEventData eventdata)
{
hudScreen.replayInterface.SetData(GameManager.GameState);
hudScreen.replayInterface.timeline.gameObject.SetActive(false);
hudScreen.replayInterface.ShowViewModePopup();
}
}
}
internal static void CreatePlayerButton(SelectViewmodePopup viewmodePopup, GameState gameState, string header, string spriteName, int type, int color, ref float num)
{
UIRoundButton playerButton = GameObject.Instantiate<UIRoundButton>(viewmodePopup.buttonPrefab, viewmodePopup.gridLayout.transform);
playerButton.id = (int)type;
playerButton.rectTransform.sizeDelta = new Vector2(56f, 56f);
playerButton.Outline.gameObject.SetActive(false);
playerButton.BG.color = ColorUtil.SetAlphaOnColor(ColorUtil.ColorFromInt(color), 1f);
playerButton.text = header[0].ToString().ToUpper() + header.Substring(1);
playerButton.SetIconColor(Color.white);
playerButton.ButtonEnabled = true;
playerButton.OnClicked = (UIButtonBase.ButtonAction)OnClimateButtonClicked;
void OnClimateButtonClicked(int id, BaseEventData eventData)
{
BasicPopup popup = PopupManager.GetBasicPopup();
var achi = Achievements[id];
popup.Header = Localization.Get(achi.name);
string unlockedis = achi.unlocked ? Localization.Get("ach.ui.unlocked") : Localization.Get("ach.ui.notunlocked");
if (!(achi.hiddenDesc && !achi.unlocked)) popup.Description = Localization.Get(achi.description) + "\n\n" + unlockedis;
else popup.Description = "???\n\n" + unlockedis;
List<PopupBase.PopupButtonData> popupButtons = new()
{
new("buttons.ok")
};
if (achi.unlocked)
{
popupButtons.Insert(0, new PopupBase.PopupButtonData("buttons.ungrant", PopupBase.PopupButtonData.States.None, (UIButtonBase.ButtonAction)ungrant, -1, false, customColorStates: ColorConstants.redButtonColorStates));
}
void ungrant(int id, BaseEventData eventData)
{
BasicPopup pop1 = PopupManager.GetBasicPopup();
pop1.Header = Localization.Get("ach.ui.usure");
pop1.Description = Localization.Get("ach.ui.usure2");
List<PopupBase.PopupButtonData> popupButtons1 = new()
{
new("buttons.nevermindachi"),
new PopupBase.PopupButtonData("buttons.absolutely", PopupBase.PopupButtonData.States.None, (UIButtonBase.ButtonAction)absolutely, -1, true, customColorStates: ColorConstants.redButtonColorStates)
};
void absolutely(int id, BaseEventData baseEventData)
{
Achievements[Main.GetAchievementLocation(achi.idx)].unlocked = false;
unlockedDict[achi.idx] = false;
PrefsHelper.SaveDict(unlockedDict);
popup.Hide();
viewmodePopup.Hide();
}
pop1.Show();
pop1.buttonData = popupButtons1.ToArray();
}
popup.buttonData = popupButtons.ToArray();
popup.Show();
}
playerButton.SetSprite(PolyMod.Registry.GetSprite(spriteName));
viewmodePopup.buttons.Add(playerButton);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(SelectViewmodePopup), nameof(SelectViewmodePopup.SetData))]
internal static bool SelectViewmodePopup_SetData(SelectViewmodePopup __instance, GameState gameState)
{
if (true)
{
__instance.ClearButtons();
__instance.buttons = new Il2CppSystem.Collections.Generic.List<UIRoundButton>();
float num = 0f;
foreach (var ach in Achievements)
{
if (ach.category == AchViewMode)
{
CreatePlayerButton(__instance, gameState, Localization.Get(ach.name), "achievement", GetAchievementLocation(ach.idx), Main.GetColor(ach), ref num);
}
}
__instance.gridLayout.spacing = new Vector2(__instance.gridLayout.spacing.x, num + 30f);
__instance.gridLayout.padding.bottom = Mathf.RoundToInt(num + 30f);
__instance.gridBottomSpacer.minHeight = num + 50f;
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(SelectViewmodePopup), nameof(SelectViewmodePopup.OnPlayerButtonClicked))]
private static bool SelectViewmodePopup_OnPlayerButtonClicked(SelectViewmodePopup __instance, int id, BaseEventData eventData)
{
__instance.SetSelectedButton(id);
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ReplayInterface), nameof(ReplayInterface.ShowViewModePopup))]
private static bool ReplayInterface_ShowViewModePopup(ReplayInterface __instance)
{
if (true)
{
if (__instance.selectViewmodePopup != null && __instance.selectViewmodePopup.IsShowing())
{
return false;
}
__instance.selectViewmodePopup = PopupManager.GetSelectViewmodePopup();
if (AchViewMode == 0)
{
__instance.selectViewmodePopup.Header = Localization.Get("ach.ui.easy");
}
else if (AchViewMode == 1)
{
__instance.selectViewmodePopup.Header = Localization.Get("ach.ui.medium"); ;
}
else __instance.selectViewmodePopup.Header = Localization.Get("ach.ui.hard"); ;
__instance.selectViewmodePopup.SetData(GameManager.GameState);
__instance.selectViewmodePopup.buttonData = new PopupBase.PopupButtonData[]
{
new PopupBase.PopupButtonData("buttons.easyach", PopupBase.PopupButtonData.States.None, (UIButtonBase.ButtonAction)easy, -1, false, null),
new PopupBase.PopupButtonData("buttons.mediumach", PopupBase.PopupButtonData.States.None, (UIButtonBase.ButtonAction)medium, -1, false, null),
new PopupBase.PopupButtonData("buttons.hardach", PopupBase.PopupButtonData.States.None, (UIButtonBase.ButtonAction)hard, -1, false, null),
new PopupBase.PopupButtonData("buttons.exit", PopupBase.PopupButtonData.States.None, (UIButtonBase.ButtonAction)exit, -1, true, null)
};
void exit(int id, BaseEventData eventData)
{
__instance.CloseViewModePopup();
}
void easy(int id, BaseEventData eventData)
{
Main.AchViewMode = 0;
__instance.selectViewmodePopup.SetData(GameManager.GameState);
__instance.selectViewmodePopup.Header = Localization.Get("ach.ui.easy");
__instance.Update();
}
void medium(int id, BaseEventData eventData)
{
Main.AchViewMode = 1;
__instance.selectViewmodePopup.SetData(GameManager.GameState);
__instance.selectViewmodePopup.Header = Localization.Get("ach.ui.medium");
__instance.Update();
}
void hard(int id, BaseEventData eventData)
{
Main.AchViewMode = 2;
__instance.selectViewmodePopup.SetData(GameManager.GameState);
__instance.selectViewmodePopup.Header = Localization.Get("ach.ui.hard");
__instance.Update();
}
__instance.selectViewmodePopup.Show(__instance.viewmodeSelectButton.rectTransform.position);
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ReplayInterface), nameof(ReplayInterface.UpdateButton))]
internal static bool ReplayInterface_UpdateButton(ReplayInterface __instance)
{
if (true)
{
__instance.viewmodeSelectButton.rectTransform.sizeDelta = new Vector2(75f, 75f);
__instance.viewmodeSelectButton.iconSpriteHandle.SetCompletion((SpriteHandleCallback)TribeSpriteHandle);
GameLogicData gameLogicData = GameManager.GameState.GameLogicData;
void TribeSpriteHandle(SpriteHandle spriteHandleCallback)
{
__instance.viewmodeSelectButton.SetFaceIcon(spriteHandleCallback.sprite);
}
__instance.viewmodeSelectButton.Outline.gameObject.SetActive(false);
}
return false;
}
#endregion
#region Common Triggers
[HarmonyPostfix]
[HarmonyPatch(typeof(CaptureCityAction), nameof(CaptureCityAction.ExecuteDefault))]
public static void CaptureCityAction_ExecuteDefault(CaptureCityAction __instance, GameState gameState)
{
gameState.TryGetPlayer(__instance.PlayerId, out PlayerState playerState);
if(!GetAchievement("colony").unlocked && LargeMapOrBigger(gameState) && playerState.Id == GameManager.LocalPlayer.Id)
{
List<int> YourCities = new();
foreach(TileData city in playerState.GetCityTiles(gameState))
if(city.improvement != null && city.improvement.type == ImprovementData.Type.City) YourCities.Add((int)city.improvement.founder);
List<int> list = Enumerable.Range(1, gameState.Settings.OpponentCount+1).ToList();
if(ListContainsList(YourCities, list)) GrantAchievement(GetAchievement("colony"));
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(BuildAction), nameof(BuildAction.ExecuteDefault))]
public static void BuildAction_ExecuteDefault(BuildAction __instance, GameState gameState)
{
ImprovementAcquired(gameState, __instance.Coordinates, __instance.PlayerId, __instance.Type, "build");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(PromoteAction), nameof(PromoteAction.ExecuteDefault))]
public static void PromoteAction_ExecuteDefault(PromoteAction __instance, GameState state)
{
VeteranAcquired(state, __instance.Coordinates, __instance.PlayerId, "promote");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ActionUtils), nameof(ActionUtils.TrainUnit))]
public static void ActionUtils_TrainUnit(GameState gameState, PlayerState playerState, TileData tile, UnitData unitData)
{
if(gameState != null && gameState.Map != null) UnitAcquired(gameState, tile.coordinates, playerState.Id, "trainunit");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ActionUtils), nameof(ActionUtils.TrainUnitOnOccupiedSpace))]
public static void ActionUtils_TrainUnitOnOccupiedSpace(GameState gameState, byte playerId, UnitData.Type unitType, TileData tile)
{
UnitAcquired(gameState, tile.coordinates, playerId, "trainunitoccupied");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ConvertAction), nameof(ConvertAction.ExecuteDefault))]
public static void ConvertAction_ExecuteDefault(ConvertAction __instance, GameState gameState)
{
TileData tile = gameState.Map.GetTile(__instance.Target);
if (TileHasVeteran(tile) == 1) VeteranAcquired(gameState, tile.coordinates, __instance.PlayerId, "convert");
if(gameState.Map.GetTile(__instance.Origin).unit != null && gameState.Map.GetTile(__instance.Origin).unit.type == UnitData.Type.MindBender)
UnitAcquired(gameState, tile.coordinates, __instance.PlayerId, "hypnotistconvert");
else UnitAcquired(gameState, tile.coordinates, __instance.PlayerId, "convert");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ExamineRuinsAction), nameof(ExamineRuinsAction.ExecuteDefault))]
public static void ExamineRuinsAction_E(ExamineRuinsAction __instance, GameState gameState)
{
TileData tile = gameState.Map.GetTile(__instance.Coordinates);
if (TileHasVeteran(tile) == 1) VeteranAcquired(gameState, tile.coordinates, __instance.PlayerId, "ruin");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(WipePlayerAction), nameof(WipePlayerAction.ExecuteDefault))]
public static void WipePlayerAction_ExecuteDefault(WipePlayerAction __instance, GameState state)
{
if (__instance.PlayerId == GameManager.LocalPlayer.Id) GrantAchievement(GetAchievement("blood"));
}
[HarmonyPostfix]
[HarmonyPatch(typeof(HideAction), nameof(HideAction.Execute))]
public static void HideAction_Execute(HideAction __instance, GameState state)
{
if (state.Map.GetTile(__instance.Coordinates).unit != null)
{
UnitHid(state, state.Map.GetTile(__instance.Coordinates).unit, __instance.PlayerId, "hide");
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(MoveAction), nameof(MoveAction.ExecuteDefault))]
public static void MoveAction_ExecuteDefault(MoveAction __instance, GameState gameState)
{
var success = gameState.TryGetUnit(__instance.UnitId, out UnitState unit);
if (success)
{
if (unit.HasAbility(UnitAbility.Type.Hide)) UnitHid(gameState, unit, __instance.PlayerId, "hide");
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameOverAction), nameof(GameOverAction.Execute))]
public static void GameOverAction_Execute(GameOverAction __instance, GameState state)
{
if (__instance.WinningPlayerId == GameManager.LocalPlayer.Id)
{
Main.GrantAchievement(GetAchievement("win"));
/*if (state.Settings.Difficulty == BotDifficulty.Crazy)
{
Main.GrantAchievement(GetAchievement("")); // crazy win index here
if (state.Settings.mapPreset == MapPreset.WaterWorld && state.Settings.OpponentCount >= 15)
{
Main.GrantAchievement(GetAchievement("")); // VengirWaterWorld here? Also implement Vengir check
}
}*/
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(TileData), nameof(TileData.SetExplored))]
public static void TileData_SetExplored(TileData __instance, byte playerId, bool explored)
{
if (explored && !GetAchievement("cartography").unlocked && Main.LargeMapOrBigger(GameManager.GameState) && playerId == GameManager.LocalPlayer.Id)
{
bool flag = true;
foreach(TileData tile in GameManager.GameState.Map.Tiles)
{
if (tile.GetExplored(playerId)) continue;
flag = false;
break;
}
if(flag) GrantAchievement(GetAchievement("cartography"));
}
}
#endregion
#region Common Events
public static void UnitAcquired(GameState gameState, WorldCoordinates coordinates, byte player, string reason)
{
modLogger.LogMessage("1");
TileData tile = gameState.Map.GetTile(coordinates);
if(tile == null || tile.unit == null) return;
if(GameManager.LocalPlayer == null || player != GameManager.LocalPlayer.Id) return;
modLogger.LogMessage("2");
UnitState unit = tile.unit;
modLogger.LogMessage("3");
if(reason == "hypnotistconvert" && unit.type == UnitData.Type.Giant) GrantAchievement(GetAchievement("yoink"));
if(reason == "hypnotistconvert" && unit.type == UnitData.Type.MindBender) GrantAchievement(GetAchievement("hypno"));
modLogger.LogMessage("4");
if(!GetAchievement("superteam").unlocked && IsSuperUnit(unit.type))
{
modLogger.LogMessage("5");
List<UnitData.Type> units = new();
foreach(TileData tile1 in gameState.Map.Tiles)
{
if(tile1 != null && tile1.unit != null && tile1.unit.owner == player) units.Add(tile1.unit.type);
}
if(ListContainsList(units, SUPER_UNITS)) GrantAchievement(GetAchievement("superteam"));
}
}
public static void UnitHid(GameState gameState, UnitState unit, byte player, string reason)
{
TileData tile = gameState.Map.GetTile(unit.coordinates);
if (reason == "hide" && player == GameManager.LocalPlayer.Id)
{
if (!GetAchievement("unseen").unlocked)
{
int counter = 0;
foreach (var tile1 in gameState.Map.Tiles)
{
if(tile1.unit != null && (tile1.unit.type == UnitData.Type.Cloak || tile1.unit.type == UnitData.Type.MermaidCloak) && tile1.unit.HasEffect(UnitEffect.Invisible) && tile1.rulingCityCoordinates != WorldCoordinates.NULL_COORDINATES && tile1.owner != player)
{
counter++;
}
}
if (counter > 2) GrantAchievement(GetAchievement("unseen"));
}
}
}
public static void VeteranAcquired(GameState gameState, WorldCoordinates coordinates, byte player, string reason)
{
TileData tile = gameState.Map.GetTile(coordinates);
List<TileData> maptiles = new();
foreach (var tile1 in gameState.Map.Tiles)
{
maptiles.Add(tile1);
}
if (player == GameManager.LocalPlayer.Id)
{
if (!GetAchievement("veterans").unlocked && CounterThroughTiles(maptiles, TileHasVeteran) > 2)
{
GrantAchievement(GetAchievement("veterans"));
}
}
}
public static void ImprovementAcquired(GameState gameState, WorldCoordinates coordinates, byte player, ImprovementData.Type type, string reason)
{
TileData tile = gameState.Map.GetTile(coordinates);
TileData citytile = gameState.Map.GetTile(tile.rulingCityCoordinates);
if (player == GameManager.LocalPlayer.Id)
{
if (reason == "build")
{
if (type == ImprovementData.Type.Farm) GrantAchievement(GetAchievement("agriculture"));
if (type.IsMonument() && CounterThroughTiles(ToSystemList(ActionUtils.GetCityArea(gameState, citytile)), TileHasMonument) > 6)
{
GrantAchievement(GetAchievement("cow"));
}
}
}
}
#endregion
#region Event Utils
private static readonly List<UnitData.Type> SUPER_UNITS = new(){UnitData.Type.Giant, UnitData.Type.Gaami, UnitData.Type.FireDragon, UnitData.Type.Centipede, UnitData.Type.Crab};
public static bool IsSuperUnit(UnitData.Type type)
{
return SUPER_UNITS.Contains(type);
}
public static int CounterThroughTiles(List<TileData> tiles, Func<TileData, int> filter)
{
int sum = 0;
foreach (var tile in tiles)
{
sum += filter(tile);
}
return sum;
}
public static int CounterThroughTiles(List<WorldCoordinates> tiles, Func<TileData, int> filter)
{
List<TileData> tileList = new();
foreach (var tile in tiles)
{
tileList.Add(GameManager.GameState.Map.GetTile(tile));
}
return CounterThroughTiles(tileList, filter);
}
public static int TileHasMonument(TileData tile)
{
if (tile.improvement != null && tile.improvement.IsMonument()) return 1;
return 0;
}
public static int TileHasVeteran(TileData tile)
{
if (tile.unit != null && tile.unit.promotionLevel >= 1) return 1;
return 0;
}
public static bool LargeMapOrBigger(GameState gameState)
{
if(gameState == null || gameState.Map == null) return false;
return gameState.Map.Width >= 18;
}
#endregion
#region AchGranting
public static void AchievementPopup(Achievement achievement)
{
NotificationManager.Notify(Localization.Get(achievement.description), Localization.Get(achievement.name), PolyMod.Registry.GetSprite("achievement"));
AudioManager.PlaySFX(SFXTypes.Achievement, PolytopiaBackendBase.Common.SkinType.Default, 1f, 1f, 1f);
}
public static void GrantAchievement(Achievement achievement)
{
if (achievement.unlocked) return;
AchievementPopup(achievement);
achievement.unlocked = true;
unlockedDict[achievement.idx] = true;
PrefsHelper.SaveDict(unlockedDict);
}
#endregion
}