File: Script.c

package info (click to toggle)
openclonk 8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 169,484 kB
  • sloc: cpp: 180,478; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 139; sh: 101
file content (112 lines) | stat: -rw-r--r-- 2,660 bytes parent folder | download | duplicates (6)
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
/*-- 
	Sell Gems
	Author: Sven2
	
	Sell n gems at flagpole.
--*/


#include Library_Goal

local gems_to_sell;

protected func Initialize()
{
	gems_to_sell = 20; // default
	return inherited(...);
}

func SetTargetAmount(int new_amount)
{
	gems_to_sell = new_amount;
	return true;
}

public func GetTargetAmount()
{
	return gems_to_sell;
}

func OnGemSold()
{
	// A gem was sold. Subtract.
	gems_to_sell = Max(gems_to_sell - 1, 0);
	return true;
}

/* Scenario saving */

func SaveScenarioObject(props)
{
	if (!inherited(props, ...)) return false;
	if (gems_to_sell) props->AddCall("Goal", this, "SetTargetAmount", gems_to_sell);
	return true;
}


/*-- Goal interface --*/

// The goal is fulfilled if no more gems need to be sold
public func IsFulfilled()
{
	return gems_to_sell <= 0;
}

public func GetDescription(int plr)
{
	var message;
	if (IsFulfilled())
		message = "$MsgGoalFulfilled$";	
	else
	{
		var gems_solid = GetMaterialCount(Material("Ruby")) * 10 / 11 / GetMaterialVal("Blast2ObjectRatio", "Material", Material("Ruby"));
		gems_solid += GetMaterialCount(Material("Amethyst")) * 10 / 11 / GetMaterialVal("Blast2ObjectRatio", "Material", Material("Amethyst"));
		var gems_pieces = ObjectCount(Find_Or(Find_ID(Ruby), Find_ID(Amethyst)));
		message = Format("$MsgGoalUnfulfilled$", gems_to_sell, gems_solid, gems_pieces);
	}

	return message;
}

// Shows or hides a message window with information.
public func Activate(int plr)
{
	// If goal message open -> hide it.
	if (GetEffect("GoalMessage", this))
	{
		CustomMessage("", nil, plr, nil, nil, nil, nil, nil, MSG_HCenter);
		RemoveEffect("GoalMessage", this);
		return;
	}
	// Otherwise open a new message.
	AddEffect("GoalMessage", this, 100, 0, this);
	var message;
	if (IsFulfilled())
		message = "@$MsgGoalFulfilled$";	
	else
	{
		var gems_solid = GetMaterialCount(Material("Ruby")) * 10 / 11 / GetMaterialVal("Blast2ObjectRatio", "Material", Material("Ruby"));
		gems_solid += GetMaterialCount(Material("Amethyst")) * 10 / 11 / GetMaterialVal("Blast2ObjectRatio", "Material", Material("Amethyst"));
		var gems_pieces = ObjectCount(Find_Or(Find_ID(Ruby), Find_ID(Amethyst)));
		message = Format("@$MsgGoalUnfulfilled$", gems_to_sell, gems_solid, gems_pieces);
	}

	CustomMessage(message, nil, plr, 0, 16 + 64, 0xffffff, GUI_MenuDeco, this, MSG_HCenter);
	return;
}

protected func FxGoalMessageStart() {}

public func GetShortDescription(int plr)
{
	// Show acquired wealth compared to goal.
	var clr = RGB(255, 0, 0);
	if (gems_to_sell<=0)
		clr = RGB(0, 255, 0);
	var msg = Format("<c %x>%d</c>{{%i}}", clr, gems_to_sell, Ruby);
	return msg;
}

/*-- Proplist --*/

local Name = "$Name$";