-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkTheFields.cs
More file actions
122 lines (97 loc) · 4.67 KB
/
WorkTheFields.cs
File metadata and controls
122 lines (97 loc) · 4.67 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
using System;
using System.Linq;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.GameMenus;
using TaleWorlds.CampaignSystem.Overlay;
using TaleWorlds.Core;
using TaleWorlds.Localization;
namespace TestSPMod
{
public class WorkTheFields : CampaignBehaviorBase
{
public override void RegisterEvents()
{
CampaignEvents.OnSessionLaunchedEvent.AddNonSerializedListener(this, new Action<CampaignGameStarter>(this.OnSessionLaunched));
}
private float _workProgressHours = 0;
private void onWaitTick(MenuCallbackArgs args, CampaignTime dt)
{
this._workProgressHours += (float)dt.ToHours;
}
private void game_menu_stop_working_at_village_on_consequence(MenuCallbackArgs args)
{
CalculateProduce();
EnterSettlementAction.ApplyForParty(MobileParty.MainParty, MobileParty.MainParty.LastVisitedSettlement);
GameMenu.SwitchToMenu("village");
}
private void CalculateProduce()
{
if (this._workProgressHours >= 1)
{
var village = Hero.MainHero.PartyBelongedTo.LastVisitedSettlement.Village;
var products = village.VillageType.Productions;
if (Hero.MainHero.IsPartyLeader)
{
var strength = Hero.MainHero.PartyBelongedTo.GetTotalStrengthWithFollowers();
products = products.Select(x => (x.Item1,
((x.Item2 * (this._workProgressHours / 24)) / village.GetNumberOfTroops()) * strength)).ToList();
}
foreach (var (item, amount) in products)
{
var intAmount = (int) Math.Round(amount, MidpointRounding.ToEven);
Hero.MainHero.PartyBelongedTo.ItemRoster.AddToCounts(item,
intAmount < 1 ? 1 : intAmount);
InformationManager.DisplayMessage(new InformationMessage($"Produced: {intAmount} {item.Name}",
"event:/ui/notification/child_born"));
}
InformationManager.DisplayMessage(new InformationMessage("You Stopped Working",
"event:/ui/notification/child_born"));
this._workProgressHours = 0;
}
}
private void game_menu_work_village_on_consequence(MenuCallbackArgs args)
{
this._workProgressHours = 0;
GameMenu.SwitchToMenu("village_work_menus");
LeaveSettlementAction.ApplyForParty(MobileParty.MainParty);
}
private bool game_menu_stop_waiting_at_village_on_condition(MenuCallbackArgs args)
{
CalculateProduce();
args.optionLeaveType = GameMenuOption.LeaveType.Leave;
return true;
}
private bool game_menu_village_work_on_condition(MenuCallbackArgs args)
{
this._workProgressHours = 0f;
Village village = Settlement.CurrentSettlement.Village;
args.MenuContext.GameMenu.AllowWaitingAutomatically();
args.optionLeaveType = GameMenuOption.LeaveType.Wait;
return village.VillageState == Village.VillageStates.Normal;
}
private bool game_menu_work_here_on_condition(MenuCallbackArgs args)
{
args.MenuContext.GameMenu.AllowWaitingAutomatically();
args.optionLeaveType = GameMenuOption.LeaveType.Wait;
MBTextManager.SetTextVariable("CURRENT_SETTLEMENT", Settlement.CurrentSettlement.EncyclopediaLinkWithName, false);
return true;
}
private void OnSessionLaunched(CampaignGameStarter obj)
{
obj.AddGameMenuOption("village", "village_work", "Work", game_menu_work_here_on_condition, this.game_menu_work_village_on_consequence,false,3);
obj.AddWaitGameMenu("village_work_menus", "You are Working", null, game_menu_village_work_on_condition, null, onWaitTick, GameMenu.MenuAndOptionType.WaitMenuHideProgressAndHoursOption, GameOverlays.MenuOverlayType.SettlementWithBoth);
obj.AddGameMenuOption("village_work_menus", "work_leave", "Stop Working", game_menu_stop_waiting_at_village_on_condition, this.game_menu_stop_working_at_village_on_consequence, true);
}
public override void SyncData(IDataStore dataStore)
{
try
{
dataStore.SyncData("_workProgressHours", ref _workProgressHours);
}
catch (NullReferenceException doesntExist)
{
}
}
}
}