1 /**
2 	HUD Adapter
3 
4 	Clonk-side scripts for the HUD. This object basically redirects the
5 	engine callbacks for the clonk to the HUD. All crew members that
6 	are to be shown in the HUD have to include this object and return
7 	_inherited(...); if they overload one of the callbacks used here.
8 
9 	This adapter redirects to the per player HUD controller and also
10 	directly to the per clonk HUD selector.
11 
12 	Requires the ClonkControl.ocd to be included in the clonk too.
13 
14 	@authors Newton
15 */
16 
17 local HUDcontroller;
18 
IsHUDAdapter()19 public func IsHUDAdapter()
20 {
21 	return true;
22 }
23 
24 // Either returns the current HUD controller or creates one.
25 // But only if owner is a human otherwise returns nil.
GetHUDController()26 private func GetHUDController()
27 {
28 	var plr = GetOwner();
29 	// During runtime join, plr isn't a valid player in the joining client yet
30 	// when this function is called from OnSynchronized(). This code previously
31 	// checked player validity before returning a cached HUD object which would
32 	// cause a desync.
33 	if (HUDcontroller) return HUDcontroller;
34 	if (GetPlayerType(plr) != C4PT_User) return nil;
35 	var controllerDef = Library_HUDController->GetGUIControllerID();
36 	HUDcontroller = FindObject(Find_ID(controllerDef), Find_Owner(plr));
37 	if (!HUDcontroller)
38 		HUDcontroller = CreateObject(controllerDef, AbsX(0), AbsY(0), plr);
39 	return HUDcontroller;
40 }
41 
42 // Update HUD controller e.g. when it was reinitialized
SetHUDController(object new_controller)43 public func SetHUDController(object new_controller)
44 {
45 	HUDcontroller = new_controller;
46 	return true;
47 }
48 
49 /*-- Engine callbacks --*/
50 
51 // Bootstrap the HUD on the recruitement of a crew member.
Recruitment(int plr)52 private func Recruitment(int plr)
53 {
54 	if (GetHUDController())
55 	{
56 		HUDcontroller->~OnCrewRecruitment(this, plr, ...);
57 		HUDcontroller->~ScheduleUpdateInventory();
58 	}
59 	return _inherited(plr, ...);
60 }
61 
62 // On savegame load or after section change, ensure that there's a HUD adapter
OnSynchronized(...)63 public func OnSynchronized(...)
64 {
65 	if (GetHUDController())
66 		HUDcontroller->~ScheduleUpdateInventory();
67 	return _inherited(...);
68 }
69 
DeRecruitment(int plr)70 private func DeRecruitment(int plr)
71 {
72 	if (HUDcontroller)
73 		HUDcontroller->~OnCrewDeRecruitment(this, plr, ...);
74 	return _inherited(plr, ...);
75 }
76 
Death(int killed_by)77 private func Death(int killed_by)
78 {
79 	if (HUDcontroller)
80 		HUDcontroller->~OnCrewDeath(this, killed_by, ...);
81 	return _inherited(killed_by, ...);
82 }
83 
Destruction()84 private func Destruction()
85 {
86 	if (HUDcontroller)
87 		HUDcontroller->~OnCrewDestruction(this, ...);
88 	return _inherited(...);
89 }
90 
ControlHotkey(int hotindex)91 public func ControlHotkey(int hotindex)
92 {
93 	if (HUDcontroller)
94 		return HUDcontroller->~ControlHotkey(hotindex);
95 }
96 
OnPromotion()97 private func OnPromotion()
98 {
99 	if (HUDcontroller)
100 		HUDcontroller->~OnCrewRankChange(this);
101 	return _inherited(...);
102 }
103 
OnEnergyChange(int change,int cause,int caused_by)104 private func OnEnergyChange(int change, int cause, int caused_by)
105 {
106 	if (HUDcontroller)
107 		HUDcontroller->~OnCrewHealthChange(this, change, cause, caused_by);
108 	return _inherited(change, cause, caused_by, ...);
109 
110 }
OnBreathChange(int change)111 private func OnBreathChange(int change)
112 {
113 	if (HUDcontroller)
114 		HUDcontroller->~OnCrewBreathChange(this, change);
115 	return _inherited(...);
116 }
117 
OnMagicEnergyChange(int change)118 private func OnMagicEnergyChange(int change)
119 {
120 	if (HUDcontroller)
121 		HUDcontroller->~OnCrewMagicChange(this, change);
122 	return _inherited(...);
123 }
124 
OnNameChanged()125 private func OnNameChanged()
126 {
127 	if (HUDcontroller)
128 		HUDcontroller->~OnCrewNameChange(this);
129 	return _inherited(...);
130 }
131 
OnPhysicalChange(string physical,int change)132 private func OnPhysicalChange(string physical, int change)
133 {
134 	if (HUDcontroller)
135 	{
136 		if (!physical)
137 		{
138 			HUDcontroller->~OnCrewHealthChange(this, change);
139 			HUDcontroller->~OnCrewBreathChange(this, change);
140 		}
141 		else if (physical == "Energy") HUDcontroller->~OnCrewHealthChange(this, change);
142 		else if (physical == "Breath") HUDcontroller->~OnCrewBreathChange(this, change);
143 	}
144 	return _inherited(physical, change, ...);
145 }
146 
CrewSelection(bool unselect)147 private func CrewSelection(bool unselect)
148 {
149 	if (HUDcontroller)
150 		HUDcontroller->~OnCrewSelection(this, unselect);
151 	return _inherited(unselect, ...);
152 }
153 
OnCrewEnabled()154 private func OnCrewEnabled()
155 {
156 	if (HUDcontroller)
157 		HUDcontroller->~OnCrewEnabled(this);
158 	return _inherited(...);
159 }
160 
OnCrewDisabled()161 private func OnCrewDisabled()
162 {
163 	if (HUDcontroller)
164 		HUDcontroller->~OnCrewDisabled(this);
165 	return _inherited(...);
166 }
167 
168 // from ClonkControl.ocd
OnSlotFull(int slot)169 private func OnSlotFull(int slot)
170 {
171 	if (HUDcontroller)
172 		HUDcontroller->~OnSlotObjectChanged(slot);
173 	return _inherited(slot, ...);
174 }
175 
OnSlotEmpty(int slot)176 private func OnSlotEmpty(int slot)
177 {
178 	if (HUDcontroller)
179 		HUDcontroller->~OnSlotObjectChanged(slot);
180 	return _inherited(slot, ...);
181 }
182 
183 // handled by GUI_Controller_ActionBars
StartInteractionCheck(object clonk)184 func StartInteractionCheck(object clonk)
185 {
186 	if (HUDcontroller)
187 		return HUDcontroller->~StartInteractionCheck(clonk, ...);
188 }
189 
190 // handled by GUI_Controller_ActionBars
StopInteractionCheck()191 func StopInteractionCheck()
192 {
193 	if (HUDcontroller)
194 		return HUDcontroller->~StopInteractionCheck(...);
195 }
196 
OnInventoryHotkeyPress(int slot)197 private func OnInventoryHotkeyPress(int slot)
198 {
199 	if (HUDcontroller)
200 		HUDcontroller->~OnInventoryHotkeyPress(slot);
201 	return _inherited(slot, ...);
202 }
203 
OnInventoryHotkeyRelease(int slot)204 private func OnInventoryHotkeyRelease(int slot)
205 {
206 	if (HUDcontroller)
207 		HUDcontroller->~OnInventoryHotkeyRelease(slot);
208 	return _inherited(slot, ...);
209 }
210 
211 // when something in the inventory changed
OnInventoryChange()212 private func OnInventoryChange()
213 {
214 	if (HUDcontroller)
215 		HUDcontroller->~ScheduleUpdateInventory();
216 	return _inherited(...);
217 }
218 
Collection2()219 public func Collection2()
220 {
221 	if (HUDcontroller)
222 		HUDcontroller->~ScheduleUpdateInventory();
223 	return _inherited(...);
224 }
225 
Ejection()226 public func Ejection()
227 {
228 	if (HUDcontroller)
229 		HUDcontroller->~ScheduleUpdateInventory();
230 	return _inherited(...);
231 }
232 
ControlContents()233 public func ControlContents()
234 {
235 	if (HUDcontroller)
236 		HUDcontroller->~ScheduleUpdateInventory();
237 	return _inherited(...);
238 }
239