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 (285 lines) | stat: -rw-r--r-- 6,079 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
/*--
	Javelin
	A simple but dangerous throwing weapon.
	
	@author: Ringwaul
--*/

#include Library_Stackable
#include Library_RangedWeapon
#include Library_Flammable

// Default timing values for animation set
// (Adjusted for speeed multiplier and stored in animation set by Library_RangedWeapon)
local DefaultShootTime = 16;
local DefaultShootTime2 = 8;

local animation_set;
local aiming;

/*-- Engine Callbacks --*/

func Initialize(...)
{
	animation_set = {
		AimMode         = AIM_Position, // The aiming animation is done by adjusting the animation position to fit the angle
		AnimationAim    = "SpearAimArms",
		AnimationShoot  = "SpearThrowArms",
		AnimationShoot2 = "SpearThrow2Arms",
		AnimationShoot3 = "SpearThrow3Arms",
		WalkBack        =  0,
	};
	return _inherited(...);
}

func Hit()
{
	if(GetEffect("Flight",this))
	{
		Stick();
		Sound("Objects::Weapons::Javelin::HitGround");
	}
	else
		Sound("Hits::Materials::Wood::WoodHit?");
}

func Entrance()
{
	// reset sticky-vertex
	SetVertex(2,VTX_Y,0,1);
}

/*-- Callbacks --*/

public func MaxStackCount() { return 3; }

public func GetAnimationSet() { return animation_set; }

// Callback from the clonk, when he actually has stopped aiming
public func FinishedAiming(object clonk, int angle)
{
	clonk->StartShoot(this);
	return true;
}

// Called in the half of the shoot animation (when ShootTime2 is over)
public func DuringShoot(object clonk, int angle)
{
	DoThrow(clonk, angle);
}

//slightly modified HitObject() from arrow
public func HitObject(object obj)
{
	var relx = GetXDir() - obj->GetXDir();
	var rely = GetYDir() - obj->GetYDir();
	var speed = Sqrt(relx*relx+rely*rely);
	
	var dmg = JavelinStrength() * speed * 1000 / 60;

	if (WeaponCanHit(obj))
	{
		if (obj->GetAlive())
			Sound("Hits::ProjectileHitLiving?");
		else
			Sound("Objects::Weapons::Javelin::HitGround");
		
		obj->~OnProjectileHit(this);
		WeaponDamage(obj, dmg, FX_Call_EngObjHit, true);
		WeaponTumble(obj, this->TumbleStrength());
		if (!this) return;
	}
	
	Stick();
}

/*-- Usage --*/

public func HoldingEnabled() { return true; }

public func RejectUse(object clonk)
{
	return !clonk->HasHandAction(false, false, true);
}

public func ControlUseStart(object clonk, int x, int y)
{
	aiming = 1;

	clonk->StartAim(this);

	ControlUseHolding(clonk, x, y);
	
	Sound("Objects::Weapons::Javelin::Draw");
	return 1;
}

public func ControlUseHolding(object clonk, ix, iy)
{
	var angle = Angle(0,0,ix,iy);
	angle = Normalize(angle,-180);

	clonk->SetAimPosition(angle);

	return true;
}

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

public func ControlUseStop(object clonk, ix, iy)
{
	if(aiming)
		clonk->StopAim();
	return true;
}

// Note that the javelin damage also takes the speed into account. A direct eye-to-eye hit will do roughly this damage.
public func JavelinStrength() { return 16; }
public func TumbleStrength() { return 100; }

public func Reset(clonk)
{
	aiming = 0;
}

public func DoThrow(object clonk, int angle)
{
	var javelin = TakeObject();
	
	var div = 60; // 40% is converted to the direction of the throwing angle.
	var xdir = clonk->GetXDir(1000);
	var ydir = clonk->GetYDir(1000);
	var speed = clonk.ThrowSpeed * shooting_strength + (100 - div) * Sqrt(xdir**2 + ydir**2) / 100;
	var jav_x = div * xdir / 100 + Sin(angle, speed);
	var jav_y = div * ydir / 100 - Cos(angle, speed);
	
	SetController(clonk->GetController());
	javelin->AddEffect("Flight",javelin,1,1,javelin,nil);
	javelin->AddEffect("HitCheck",javelin,1,1,nil,nil,clonk);	
		
	javelin->SetXDir(jav_x, 1000);
	javelin->SetYDir(jav_y, 1000);
	javelin->SetPosition(clonk->GetX(), clonk->GetY() - 6);
		
	Sound("Objects::Weapons::Javelin::Throw?");
	
	aiming = -1;
	clonk->UpdateAttach();
	
	return javelin;
}

func Stick()
{
	if(GetEffect("Flight",this))
	{
		SetXDir(0);
		SetYDir(0);
		SetRDir(0);

		RemoveEffect("Flight",this);
		RemoveEffect("HitCheck",this);
		
		var x=Sin(GetR(),+16);
		var y=Cos(GetR(),-16);
		var mat = GetMaterial(x,y);
		if(mat != -1)
		{
			//if(GetMaterialVal("DigFree","Material",mat))
			//{
			// stick in landscape
			SetVertex(2,VTX_Y,-18,1);
			//}
		}
		return;
	}
}

func FxFlightStart(object target, effect fx, int temp)
{
	if (temp)
		return FX_OK;
	target.Collectible = false;
	target->SetR(Angle(0,0,target->GetXDir(),target->GetYDir()));
	target->SetVertex(1, VTX_Y, 0, 1);
	return FX_OK;
}

func FxFlightTimer(object target, effect fx, int time)
{
	//Using Newton's arrow rotation. This would be much easier if we had arctan :(
	var oldx = fx.x;
	var oldy = fx.y;
	var newx = GetX();
	var newy = GetY();

	var anglediff = Normalize(Angle(oldx,oldy,newx,newy)-GetR(),-180);
	target->SetRDir(anglediff/2);
	fx.x = newx;
	fx.y = newy;
	target->SetR(Angle(0,0,target->GetXDir(),target->GetYDir()));
	
	if (time == 10)
		target->SetVertex(1, VTX_Y, 13, 1);
	return FX_OK;
}

func FxFlightStop(object target, effect fx, int reason, bool temp)
{
	if (temp)
		return FX_OK;	
	target.Collectible = true;
	target->SetVertex(1, VTX_Y, 13, 1);
	return FX_OK;
}

/*-- Production --*/

public func IsWeapon() { return true; }
public func IsArmoryProduct() { return true; }

/*-- Display --*/

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

	if (aiming > 0)
		return CARRY_Hand;

	return CARRY_Spear;
}

public func GetCarryBone() { return "Javelin"; }

public func GetCarrySpecial(clonk)
{
	if(aiming > 0) return "pos_hand2";
}

public func GetCarryTransform()
{
	if(aiming == 1) return Trans_Rotate(180, 0, 0, 1);
}

func Definition(def, ...) {
	SetProperty("PictureTransformation", Trans_Mul(Trans_Rotate(40,0,0,1),Trans_Rotate(-10,1,0,0)),def);
	return _inherited(def, ...);
}

/*-- Properties --*/

local Name = "$Name$";
local Description = "$Description$";
local Collectible = true;
local ForceFreeHands = true;
local Components = {Wood = 2, Metal = 1};
local BlastIncinerate = 30;
local MaterialIncinerate = true;
// Multiplication factor to clonk.ThrowSpeed
local shooting_strength = 21;