File: Script.c

package info (click to toggle)
openclonk 8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 169,500 kB
  • sloc: cpp: 180,478; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 139; sh: 101; javascript: 34
file content (396 lines) | stat: -rw-r--r-- 10,691 bytes parent folder | download | duplicates (5)
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
/**
	ControllerInventoryBar

	Displays inventory slots and extra information.

	@authors Zapper, Clonkonaut
*/

/*
	inventory_slot contains an array of proplists with the following attributes:
		ID: submenu ID. Unique in combination with the target == this
		obj: last object that was shown here
		hand: bool, whether select with a hand
		quick: bool, whether this is the quick switch slot
*/

// HUD margin and size in tenths of em.
static const GUI_Controller_InventoryBar_IconMarginScreenTop = 5;
static const GUI_Controller_InventoryBar_IconSize = 20;
static const GUI_Controller_InventoryBar_IconMargin = 5;

local inventory_slots;
local inventory_gui_menu;
local inventory_gui_id;

/* GUI creation */

// For custom HUD graphics overload the following function as deemed fit.

func AssembleInventoryButton(int max_slots, int slot_number, proplist slot_info)
{
	// The gui already exists, only update it with a new submenu
	var pos = CalculateButtonPosition(slot_number, max_slots);

	return
	{
		Target = this,
		slot_number =
		{
			Priority = 3, // Make sure the slot number is drawn above the icon.
			Style = GUI_TextTop,
			Text = Format("%2d", slot_info.slot + 1)
		},
		quick_switch = // Shows quick switch control key if this is the quick switch slot
		{
			Priority = 3,
			Style = GUI_NoCrop | GUI_TextHCenter | GUI_TextBottom,
			Left = "-50%",
			Right = "150%",
			Top = Format(" %s%s", "20%", ToEmString(-2)),
			Bottom = "20%",
			Text = { Std = "", Quick = Format("<c dddd00>[%s]</c>", GetPlayerControlAssignment(GetOwner(), CON_QuickSwitch, true)), Selected = "" }
		},
		Style = GUI_NoCrop,
		ID = slot_info.ID,
		Symbol = {Std = Icon_Menu_Circle, Quick = Icon_Menu_Circle, Selected = Icon_Menu_CircleHighlight},
		Left = pos.Left, Top = pos.Top, Right = pos.Right, Bottom = pos.Bottom,
		count =
		{
			ID = 1000 + slot_info.ID,
			Style = GUI_TextRight | GUI_TextBottom,
			Text = nil,
			Priority = 2
		},
		// Prepare (invisible) extra-slot display circle.
		extra_slot =
		{
			Top = ToEmString(GUI_Controller_InventoryBar_IconSize),
			Bottom = ToEmString(GUI_Controller_InventoryBar_IconSize + GUI_Controller_InventoryBar_IconSize/2),
			Style = GUI_TextLeft,
			Text = nil,
			symbol =// used to display an infinity sign if necessary (Icon_Number)
			{
				Right = ToEmString(GUI_Controller_InventoryBar_IconSize/2),
				GraphicsName = "Inf",
			},
			circle =// shows the item in the extra slot
			{
				Left = ToEmString(GUI_Controller_InventoryBar_IconSize/2),
				Symbol = nil,
				symbol = {}
			}
		},
		overlay = // Custom inventory overlays can be shown here.
		{
			ID = 2000 + slot_info.ID
		}
	};
}

/* Creation / Destruction */

private func Construction()
{
	inventory_slots = [];

	inventory_gui_menu =
	{
		Target = this,
		Player = NO_OWNER, // will be shown once a gui update occurs
		Style = GUI_Multiple | GUI_IgnoreMouse | GUI_NoCrop
	};
	inventory_gui_id = GuiOpen(inventory_gui_menu);

	return _inherited(...);
}

private func Destruction()
{
	GuiClose(inventory_gui_id);

	_inherited(...);
}

/* Callbacks */

public func OnCrewDisabled(object clonk)
{
	ScheduleUpdateInventory();

	return _inherited(clonk, ...);
}

public func OnCrewEnabled(object clonk)
{
	ScheduleUpdateInventory();

	return _inherited(clonk, ...);
}

public func OnCrewSelection(object clonk, bool deselect)
{
	ScheduleUpdateInventory();

	return _inherited(clonk, deselect, ...);
}

// call from HUDAdapter (Clonk)
public func OnSlotObjectChanged(int slot)
{
	// refresh inventory
	ScheduleUpdateInventory();

	return _inherited(slot, ...);
}

// Updates the Inventory in 1 frame
public func ScheduleUpdateInventory()
{
	if (!GetEffect("UpdateInventory", this))
		AddEffect("UpdateInventory", this, 1, 1, this);
}

private func FxUpdateInventoryTimer()
{
	UpdateInventory();
	return FX_Execute_Kill;
}

/* Display */

private func UpdateInventory()
{
	// only display if we have a clonk and it's not disabled
	var clonk = GetCursor(GetOwner());
	if(!clonk || !clonk->GetCrewEnabled())
	{
		if (inventory_gui_menu.Player != NO_OWNER)
		{
			inventory_gui_menu.Player = NO_OWNER;
			GuiUpdate(inventory_gui_menu, inventory_gui_id);
		}

		return;
	}

	// Make sure inventory is visible
	if (inventory_gui_menu.Player != GetOwner())
	{
		inventory_gui_menu.Player = GetOwner();
		GuiUpdate(inventory_gui_menu, inventory_gui_id);
	}

	UpdateInventoryButtons(clonk);

	// update inventory-slots
	var hand_item_pos = clonk->~GetHandItemPos(0);
	var quick_switch_slot = clonk->~GetQuickSwitchSlot();

	for (var slot_info in inventory_slots)
	{
		var item = clonk->GetItem(slot_info.slot);
		// Enable objects to provide a custom overlay for the icon slot.
		// This could e.g. be used by special scenarios or third-party mods.
		var custom_overlay = nil;
		// For stacked objects, we will have multiple virtual objects in one slot.
		var stack_count = nil;
		if (item)
		{
			stack_count = item->~GetStackCount();
			custom_overlay = item->~GetInventoryIconOverlay();
		}
		var needs_selection = hand_item_pos == slot_info.slot;
		var needs_quick_switch = quick_switch_slot == slot_info.slot;
		var has_extra_slot = item && item->~HasExtraSlot();
		if ((!!item == slot_info.empty) || (item != slot_info.obj) || (needs_selection != slot_info.hand) || (needs_quick_switch != slot_info.quick) || (stack_count != slot_info.last_count) || has_extra_slot || slot_info.had_custom_overlay || custom_overlay)
		{
			// Hide or show extra-slot display?
			var extra_slot_player = NO_OWNER;
			var extra_symbol = nil;
			var extra_symbol_stack_count = nil;
			var contents = nil;
			var extra_slot_background_symbol = nil;
			if (has_extra_slot)
			{
				// Show!
				contents = item->Contents(0);
				if (contents)
				{
					extra_symbol = contents;
					// Stack count: either actual stack count or stacked object count.
					extra_symbol_stack_count = contents->~GetStackCount();
					if (extra_symbol_stack_count == nil)
					{
					    // Stack count fallback to actually stacked objects
					    extra_symbol_stack_count = item->ContentsCount(contents->GetID());
					}
				}
				extra_slot_player = GetOwner();
				extra_slot_background_symbol = Icon_Menu_Circle;
				// And attach tracker..
				var i = 0, e = nil;
				var found = false;
				while (e = GetEffect("ExtraSlotUpdater", item, i++))
				{
					if (e.CommandTarget != this) continue;
					found = true;
					break;
				}
				if (!found) AddEffect("ExtraSlotUpdater", item, 1, 30 + Random(60), this);
			}
			// What to display in the extra slot?
			var extra_text = nil, number_symbol = nil;
			if (extra_symbol && extra_symbol_stack_count)
			{
				if (contents->~IsInfiniteStackCount())
					number_symbol = Icon_Number;
				else extra_text = Format("%dx", extra_symbol_stack_count);
			}
			
			// Close a possible lingering custom overlay for that slot.
			var custom_overlay_id = 2000 + slot_info.ID;
			GuiClose(inventory_gui_id, custom_overlay_id, nil);
			
			// Compose the update!
			var update =
			{
				slot = { Symbol = item },
				extra_slot =
				{
					Player = extra_slot_player,
					Text = extra_text,
					symbol =
					{
						Symbol = number_symbol
					},
					circle =
					{
						Symbol = extra_slot_background_symbol,
						symbol = { Symbol = extra_symbol }
					}
				},
				count = 
				{
					Text = ""
				}
			};
			
			if (item)
			{
				if (stack_count > 1 && !item->~IsInfiniteStackCount())
				{
					update.count.Text = Format("%dx", stack_count);
					slot_info.last_count = stack_count;
				}
			}
			else
			{
				slot_info.last_count = nil;
			}
			
			if (custom_overlay)
			{
				update.overlay = custom_overlay;
				update.overlay.ID = custom_overlay_id;
				slot_info.had_custom_overlay = true;
			}
			else
			{
				slot_info.had_custom_overlay = false;
			}
			
			GuiUpdate(update, inventory_gui_id, slot_info.ID, this);

			var tag = "Std";
			if (needs_quick_switch) tag = "Quick";
			if (needs_selection) tag = "Selected";
			GuiUpdateTag(tag, inventory_gui_id, slot_info.ID, this);

			slot_info.hand = needs_selection;
			slot_info.quick = needs_quick_switch;
			slot_info.obj = item;
			slot_info.empty = !item;
		}
	}
}

// Sets the inventory size to the currently selected clonk
private func UpdateInventoryButtons(object clonk)
{
	var max_contents_count = clonk.MaxContentsCount;

	var old_count = GetLength(inventory_slots);

	// need to create more inventory buttons?
	while (max_contents_count > GetLength(inventory_slots))
		CreateNewInventoryButton(max_contents_count);

	// need to remove some inventory buttons?
	while (max_contents_count < GetLength(inventory_slots))
	{
		var slot_info = inventory_slots[-1];
		GuiClose(inventory_gui_id, slot_info.ID, this);
		SetLength(inventory_slots, GetLength(inventory_slots)-1);
	}

	// modifications occured? Adjust position of old slots
	if (old_count != max_contents_count)
	{
		for (var i = 0; i < Min(old_count, max_contents_count); ++i)
		{
			var slot_info = inventory_slots[i];
			var update = CalculateButtonPosition(i, max_contents_count);
			GuiUpdate(update, inventory_gui_id, slot_info.ID, this);
		}
	}
}

// Insert an inventory slot into the inventory-bar
private func CreateNewInventoryButton(int max_slots)
{
	var slot_number = GetLength(inventory_slots);
	var slot_info =
	{
		slot = slot_number,
		ID = slot_number + 1,
		hand = false,
		quick = false,
		obj = nil,
		empty = true
	};
	PushBack(inventory_slots, slot_info);

	var slot = AssembleInventoryButton(max_slots, slot_number, slot_info);

	GuiUpdate({_new_icon = slot}, inventory_gui_id);
}

// Calculates the position of a specific button and returns a proplist.
private func CalculateButtonPosition(int slot_number, int max_slots)
{
	var pos_x_offset = -((GUI_Controller_InventoryBar_IconSize + GUI_Controller_InventoryBar_IconMargin) * max_slots - GUI_Controller_InventoryBar_IconMargin) / 2;
	var pos_x = pos_x_offset + (GUI_Controller_InventoryBar_IconSize + GUI_Controller_InventoryBar_IconMargin) * slot_number;
	var pos_y = GUI_Controller_InventoryBar_IconMarginScreenTop;
	var pos =
	{
		Left = Format("50%%%s", ToEmString(pos_x)),
		Top = Format("0%%%s", ToEmString(pos_y)),
		Right = Format("50%%%s", ToEmString(pos_x + GUI_Controller_InventoryBar_IconSize)),
		Bottom = Format("0%%%s", ToEmString(pos_y + GUI_Controller_InventoryBar_IconSize))
	};
	return pos;
}

private func FxExtraSlotUpdaterTimer(object target, proplist effect)
{
	if (!this) return FX_Execute_Kill;
	if (!target) return FX_Execute_Kill;
	if (target->Contained() != GetCursor(GetOwner())) return FX_Execute_Kill;
	return FX_OK;
}

private func FxExtraSlotUpdaterUpdate(object target, proplist effect)
{
	if (this) ScheduleUpdateInventory();
}