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 (133 lines) | stat: -rw-r--r-- 3,260 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
/**
	ControllerWealth

	(Optionally) shows an icon and the current player's wealth in the top right corner.
	Use GUI_Controller->ShowWealth(); to show the wealth display to all players.
	Or call it individually on particular controllers.
	Use GUI_Controller->HideWealth(); to hide the display.

	@authors Maikel, Clonkonaut
*/

// HUD margin and size in tenths of em.
static const GUI_Controller_Wealth_IconSize = 25;
static const GUI_Controller_Wealth_IconMargin = 5;

static GUI_Controller_Wealth_shown; // shown or hidden for newly joining players?

local wealth_gui_menu;
local wealth_gui_id;

local wealth_display = false;

/* Showing / Hiding */

public func ShowWealth()
{
	// Definition call
	if (GetType(this) == C4V_Def)
	{
		GUI_Controller_Wealth_shown = true; // for players joining later
		var plr;
		for (var i=0; i<GetPlayerCount(C4PT_User); ++i)
		{
			var plr = GetPlayerByIndex(i, C4PT_User);
			var controller = FindObject(Find_ID(GUI_Controller), Find_Owner(plr));
			if (controller) controller->ShowWealth();
		}
	} else {
		if (wealth_display) return;
		wealth_gui_id = GuiOpen(wealth_gui_menu);
		wealth_display = true;
		this->~ShiftGoal();
	}
}

public func HideWealth()
{
	// Definition call
	if (GetType(this) == C4V_Def)
	{
		GUI_Controller_Wealth_shown = false; // for players joining later
		var plr;
		for (var i=0; i<GetPlayerCount(C4PT_User); ++i)
		{
			var plr = GetPlayerByIndex(i, C4PT_User);
			var controller = FindObject(Find_ID(Library_HUDController->GetGUIControllerID()), Find_Owner(plr));
			if (controller) controller->HideWealth();
		}
	} else {
		if (!wealth_display) return;
		GuiClose(wealth_gui_id);
		wealth_gui_id = nil;
		wealth_display = false;
		this->~UnshiftGoal();
	}
}

public func IsShowingWealth() { return wealth_display; }

/* Creation / Destruction */

private func Construction()
{
	wealth_display = GUI_Controller_Wealth_shown; // initial show/hide setting
	var plr = GetOwner();
	var wealth = GetWealth(plr);

	var margin = GUI_Controller_Wealth_IconMargin;
	var whole = margin + GUI_Controller_Wealth_IconSize;

	wealth_gui_menu =
	{
		Target = this,
		Player = plr,
		Style = GUI_Multiple | GUI_TextHCenter | GUI_TextBottom,
		Left = Format("100%%%s", ToEmString(-whole)),
		Right = Format("100%%%s", ToEmString(-margin)),
		Top = ToEmString(margin),
		Bottom = ToEmString(whole),
		Priority = 1,
		Symbol = Icon_Wealth,
		GraphicsName = GetGraphicsName(wealth),
		Text = Format("%d", wealth),
	};

	if (wealth_display) wealth_gui_id = GuiOpen(wealth_gui_menu);

	return _inherited(...);
}

private func Destruction()
{
	if (wealth_gui_id) GuiClose(wealth_gui_id);

	_inherited(...);
}

/* Callbacks */

public func OnWealthChanged(int plr)
{
	// Only update wealth when it is the right player.
	if (plr == GetOwner())
	{
		var wealth = GetWealth(plr);
		wealth_gui_menu.GraphicsName = GetGraphicsName(wealth);
		wealth_gui_menu.Text = Format("%d", wealth);
		GuiUpdate(wealth_gui_menu, wealth_gui_id);
	}

	return _inherited(plr, ...);
}

// Graphics changes in accordance to accumulated wealth
private func GetGraphicsName(int wealth)
{
	var num = "0";
	if (wealth >= 10) num = "1";
	if (wealth >= 30) num = "2";
	if (wealth >= 70) num = "3";
	if (wealth >= 120) num = "4";
	return num;
}