File: Script.c

package info (click to toggle)
openclonk 8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 169,520 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (146 lines) | stat: -rw-r--r-- 3,578 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
/*--
	Relaunch container
	Author: Maikel

	This container holds the clonk after relaunches.
	* The time the clonk is held can be specified by calling GetRelaunchRule()->SetRespawnDelay(int time).
	* After that time the clonk is released and OnClonkLeftRelaunch(object clonk) is called in the scenario script.
	* Optionally the clonk can choose a weapon if GetRelaunchWeaponList in the scenario script returns a valid id-array.
--*/

local menu;
local has_selected;
local crew;

local hold_crew = false;
local relaunch_time = 36 * 10;

// Sets the time, in seconds, the clonk is held in the container.
public func SetRelaunchTime(int to_time, bool to_hold)
{
	relaunch_time = to_time * 36;
	hold_crew = to_hold;
	return;
}

// Returns the time, in seconds the clonk is held.
public func GetRelaunchTime() { return relaunch_time / 36; }

// Retrieve weapon list from scenario.
private func WeaponList() { return GameCall("RelaunchWeaponList"); }

public func StartRelaunch(object clonk)
{
	if (!clonk)
		return;
	// Only 1 clonk can be inside.
	if (crew)
		return;
	// Save clonk for later use.
	crew = clonk;
	clonk->Enter(this);
	AddEffect("IntTimeLimit", this, 100, 36, this);
	ScheduleCall(this, "OpenWeaponMenu", 36, 0, clonk);
	GameCall("OnClonkEnteredRelaunch", clonk, clonk->GetOwner());
	return true;
}

private func OpenWeaponMenu(object clonk)
{
	if (!clonk)
		return;	
	if (!menu)
	{
		var weapons = WeaponList();
		if (weapons)
		{
			menu = CreateObject(MenuStyle_Default, nil, nil, clonk->GetOwner());
			menu->SetPermanent();
			menu->SetTitle(Format("$MsgWeapon$", relaunch_time / 36));
			clonk->SetMenu(menu, true);
			
			if (GetType(GetRelaunchRule().last_used_player_weapons) != C4V_Array)
				GetRelaunchRule().last_used_player_weapons = [];
			for (var weapon in weapons)
			{
				if (GetRelaunchRule().last_used_player_weapons[clonk->GetOwner()] != weapon)
				{
					menu->AddItem(weapon, weapon->GetName(), nil, this, "OnWeaponSelected", weapon);
				}
			}
				
			menu->Open();
		}
	}
}

public func FxIntTimeLimitTimer(object target, effect fx, int fxtime)
{
	var clonk = crew;
	if (!clonk)
	{
		RemoveObject();
		return FX_Execute_Kill;
	}
	if (fxtime >= relaunch_time)
	{
		if (!has_selected && WeaponList())
			GiveWeapon(RandomElement(WeaponList()));
		RelaunchClonk();
		return FX_Execute_Kill;
	}
	if (menu)
		menu->SetTitle(Format("$MsgWeapon$", (relaunch_time - fxtime) / 36));
	else
		PlayerMessage(clonk->GetOwner(), Format("$MsgRelaunch$", (relaunch_time - fxtime) / 36));
	return FX_OK;
}

public func OnWeaponSelected(id weapon)
{
	if (!crew)
		return;
	GiveWeapon(weapon);
	if (GetRelaunchRule()->GetLastWeaponUse())
		GetRelaunchRule().last_used_player_weapons[crew->GetOwner()] = weapon;
	has_selected = true;
	// Close menu manually, to prevent selecting more weapons.
	if (menu)
		menu->Close();

	if (!hold_crew)
		RelaunchClonk();
	return true;
}

private func RelaunchClonk()
{
	var clonk = crew;
	// When relaunching from disabled state (i.e base respawn), reset view to clonk.
	if (!clonk->GetCrewEnabled())
	{
		clonk->SetCrewEnabled(true);
		SetCursor(clonk->GetOwner(), clonk);
		SetPlrView(clonk->GetOwner(), clonk);
	}
	clonk->Exit();
	GameCall("OnClonkLeftRelaunch", clonk, clonk->GetOwner());
	if (menu)
		menu->Close();
	PlayerMessage(clonk->GetOwner(), "");
	RemoveObject();
	return;
}

private func GiveWeapon(id weapon_id)
{
	var newobj = CreateObjectAbove(weapon_id);
	newobj->~OnRelaunchCreation(crew);
	crew->Collect(newobj);
	return true;
}


/*-- Saving --*/

public func SaveScenarioObject() { return false; }