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 (289 lines) | stat: -rw-r--r-- 5,712 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
/**
	Grapple Bow
	A crossbow which is enabled to fire grappling hooks, also has a winching system.
	
	@author Randrian
*/

// Display the hook on the inventory HUD
#include Library_HasExtraSlot
// Do mind that IsContainer is set to false, so the hook can't be taken out via the interaction menu (very important)
// See below in Callbacks for IsContainer

#include Library_Flammable

local is_aiming;
local animation_set;
local hook;
local hook_attach;

/*-- Engine Callbacks --*/

func Initialize()
{
	// The aiming animation is done by adjusting the animation position to fit the angle.
	animation_set = {
		AimMode        = AIM_Position,
		AnimationAim   = "CrossbowAimArms",
		AnimationShoot = nil,
		ShootTime      = 20,
		TurnType       = 1,
		WalkSpeed      = 84,
		WalkBack       = 56,
		AimSpeed       = 20,
	};
	OnRopeBreak();
}

func Hit()
{
	Sound("Hits::GeneralHit?");
}

func Destruction()
{
	if (hook)
	{
		var rope = hook->GetRope();
		if (rope)
			rope->BreakRope();
	}
}

func Departure()
{
	if (hook)
	{
		var rope = hook->GetRope();
		if (rope)
			rope->BreakRope();
	}
}

func Incineration()
{
	// Grapple bow becomes unusable on incineration.
	if (hook)
	{
		var rope = hook->GetRope();
		if (rope) rope->BreakRope();
		if (hook) hook->RemoveObject();
	}
	SetClrModulation(0xff606060);
	return _inherited(...);
}

func Extinguishing()
{
	// If extinguished on the same frame it got incinerated, make it usable again
	if (GetCon() >= 100)
	{
		EnsureHook();
		SetClrModulation();
	}
	return _inherited(...);
}

/*-- Callbacks --*/

public func GetAnimationSet() { return animation_set; }

// Callback from the clonk when loading is finished
public func FinishedLoading(object clonk)
{
	clonk->~StartAim(this);
	return true;
}

// Callback from the clonk, when he actually has stopped aiming.
public func FinishedAiming(object clonk, int angle)
{
	// Only shoot if the bow did not burn in the meantime.
	if (GetCon() < 100)
		return false;
	
	// Shoot the hook and detach the mesh from the bow.
	EnsureHook();
	hook->Exit();
	hook->Launch(angle, 100, clonk, this);
	hook_attach = nil;
	DetachMesh(hook_attach);
	Sound("Objects::Weapons::Bow::Shoot?");

	// Open the hand to let the string go and play the fire animation.
	PlayAnimation("Fire", 6, Anim_Linear(0, 0, GetAnimationLength("Fire"), animation_set["ShootTime"], ANIM_Hold));
	clonk->StartShoot(this);
	return true;
}

public func OnRopeBreak()
{
	if (hook_attach)
		DetachMesh(hook_attach);

	EnsureHook();
	hook->Enter(this);
	hook_attach = AttachMesh(hook, "bolt", "main");
	PlayAnimation("Load", 5, Anim_Const(GetAnimationLength("Load")));
}

func IsContainer() { return false; } // See above for explanation

/*-- Usage --*/

public func HoldingEnabled() { return true; }

public func RejectUse(object clonk)
{
	// Burned?
	if (GetCon() < 100)
		return true;
	// Able to cut the hook? Then never reject the use.
	if (hook->Contained() != this)
		return false;
	return !clonk->HasHandAction();
}

public func ControlUseStart(object clonk, int x, int y)
{
	// Cut rope, or otherwise remove helper object.
	EnsureHook();
	if (hook->Contained() != this)
	{
		var rope = hook->GetRope();
		if (rope)
		{
			rope->DrawIn();
			return true;
		}
		else
		{
			hook->Enter(this);
		}
	}

	// Start aiming.
	is_aiming = true;
	ControlUseHolding(clonk, x, y);
	FinishedLoading(clonk);
	return true;
}

public func ControlUseHolding(object clonk, int x, int y)
{
	// Update the aiming angle on mouse movement.
	var angle = Angle(0, 0, x, y);
	angle = Normalize(angle, -180);
	angle = BoundBy(angle, -160, 160);
	clonk->SetAimPosition(angle);
	return true;
}

public func ControlUseCancel(object clonk, int x, int y)
{
	clonk->CancelAiming();
	return true;
}

// Stopping says the clonk to stop with aiming (he will go on untill he has finished loading and aiming at the given angle).
public func ControlUseStop(object clonk, int x, int y)
{
	clonk->StopAim();
	return true;
}

public func Reset(object clonk)
{
	is_aiming = 0;
	clonk->StopAnimation(clonk->GetRootAnimation(11));
	StopAnimation(GetRootAnimation(6));
}

/*-- Bow Mechanics --*/

public func SetHook(object new_hook)
{
	hook = new_hook;
}

func EnsureHook()
{
	// Create hook if it went missing.
	if (!hook) 
		hook = CreateObject(GrappleHook);
	return hook;
}

public func DrawRopeIn()
{
	if (hook)
	{
		var rope = hook->GetRope();
		if (rope)
			rope->DrawIn();
	}
}

// If shot (e.g. by a cannon) the rope is drawn in.
public func LaunchProjectile()
{
	if (hook)
	{
		var rope = hook->GetRope();
		if (rope)
			rope->DrawIn();
	}
	_inherited(...);
}

func RejectCollect(id whatever, object hopefully_hook)
{
	// The grapple bow will only its own hook, very picky thing
	if (hopefully_hook != hook) return true;
	return false;
}

/*-- Production --*/

public func IsInventorProduct() { return true; }

/*-- Display --*/

public func GetCarryMode(object clonk, bool idle)
{
	if (idle) return CARRY_Back;

	if (clonk->~IsJumping())
		return CARRY_Hand;

	return CARRY_Grappler;
}

public func GetCarryTransform(object clonk, bool idle, bool nohand)
{
	if (idle || nohand)
		return Trans_Translate(0,3000);
}

public func GetCarrySpecial(object clonk)
{
	if (is_aiming)
		return "pos_hand2";
}

public func GetCarryBone2(object clonk) { return "main2"; }

func Definition(proplist def)
{
	def.PictureTransformation = Trans_Mul(Trans_Translate(-2500, 1000),Trans_Scale(1800),Trans_Rotate(-60,1,-1,1), Trans_Rotate(180, 0, 1, 0));
}

/*-- Properties --*/

local Name = "$Name$";
local Description = "$Description$";
local Collectible = 1;
local Components = {Wood = 2, Metal = 1/*, Rope = 1*/};
local BlastIncinerate = 30;
local MaterialIncinerate = true;
local BurnDownTime = 140;