File: HitChecks.c

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (209 lines) | stat: -rw-r--r-- 5,735 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
/**
	HitCheck.c
	Effect for hit checking.
	Facilitates any hit check of a projectile. The Projectile hits anything
	which is either alive or returns for IsProjectileTarget(object projectile,
	object shooter) true. If the projectile hits something, it calls
	HitObject(object target) in the projectile.

	@author Newton, Boni
*/

global func FxHitCheckStart(object target, proplist effect, int temp, object by_obj, bool never_shooter)
{
	if (temp)
		return;
	effect.x = target->GetX();
	effect.y = target->GetY();
	if (!by_obj || GetType(by_obj) != C4V_C4Object)
		by_obj = target;
	if (by_obj->Contained())
		by_obj = by_obj->Contained();
	effect.shooter = by_obj;
	effect.live = false;
	effect.never_shooter = never_shooter;
	
	// C4D_Object has a hitcheck too -> change to vehicle to supress that.
	if (target->GetCategory() & C4D_Object)
		target->SetCategory((target->GetCategory() - C4D_Object) | C4D_Vehicle);
	return;
}

global func FxHitCheckStop(object target, proplist effect, int reason, bool temp)
{
	if (temp)
		return;
	
	target->SetCategory(target->GetID()->GetCategory());
	return;
}

global func FxHitCheckDoCheck(object target, proplist effect)
{
	var obj;
	// rather search in front of the projectile, since a hit might delete the effect,
	// and clonks can effectively hide in front of walls.
	var oldx = target->GetX();
	var oldy = target->GetY();
	var newx = target->GetX() + target->GetXDir() / 10;
	var newy = target->GetY() + target->GetYDir() / 10;
	var dist = Distance(oldx, oldy, newx, newy);
	
	var shooter = effect.shooter;
	var live = effect.live;
	
	if (live)
		shooter = target;
	
	if (dist <= Max(1, Max(Abs(target->GetXDir()), Abs(target->GetYDir()))) * 2)
	{
		// We search for objects along the line on which we moved since the last check
		// and sort by distance (closer first).
		for (obj in FindObjects(Find_OnLine(oldx, oldy, newx, newy),
								Find_NoContainer(),
								Find_Layer(target->GetObjectLayer()),
								Find_PathFree(target),
								Sort_Distance(oldx, oldy)))
		{
			// Excludes
			if (!obj) continue; // hit callback of one object might have removed other objects
			if(obj == target) continue;
			if(obj == shooter) continue;

			// Unlike in hazard, there is no NOFF rule (yet)
			// CheckEnemy
			//if(!CheckEnemy(obj,target)) continue;

			// IsProjectileTarget will be hit (defaults to true for OCF_Alive).
			if (obj->~IsProjectileTarget(target, shooter))
			{
				target->~HitObject(obj);
				if (!target)
					return;
			}
		}
	}
	return;
}

global func FxHitCheckEffect(string newname)
{
	if (newname == "HitCheck")
		return -2;
	return;
}

global func FxHitCheckAdd(object target, proplist effect, string neweffectname, int newtimer, by_obj, never_shooter)
{
	effect.x = target->GetX();
	effect.y = target->GetY();
	if (!by_obj)
		by_obj = target;
	if (by_obj->Contained())
		by_obj = by_obj->Contained();
	effect.shooter = by_obj;
	effect.live = false;
	effect.never_shooter = never_shooter;
	return;
}

global func FxHitCheckTimer(object target, proplist effect, int time)
{
	EffectCall(target, effect, "DoCheck");
	// It could be that it hit something and removed itself. thus check if target is still there.
	// The effect will be deleted right after this.
	if (!target)
		return -1;
	
	effect.x = target->GetX();
	effect.y = target->GetY();
	var live = effect.live;
	var never_shooter = effect.never_shooter;
	var shooter = effect.shooter;

	// The projectile will be only switched to "live", meaning that it can hit the
	// shooter himself when the shot exited the shape of the shooter one time.
	if (!never_shooter)
	{
		if (!live)
		{
			var ready = true;
			// We search for all objects with the id of our shooter.
			if (shooter)
			{
				if (FindObject(Find_AtPoint(target->GetX(), target->GetY()), Find_InArray([shooter])))
				{
					// we may not switch to "live" yet.
					ready = false;
				}
			}
			// Otherwise, the shot will be live.
			if (ready)
				effect.live = true;
		}
	}
	return;
}

global func IsProjectileTarget(object projectile, object shooter)
{
	return GetOCF() & OCF_Alive;
}

/*
	Checks whether an object is ready to take damage from this object, calling QueryCatchBlow.
*/
global func WeaponCanHit(object target)
{
	if (target->~QueryCatchBlow(this)) return false;
	if (!target || !this) return false;
	return true;
}

/*
	Deals damage to an object, draining either energy for living things or dealing damage otherwise.
	CatchBlow is called on the target if it's alive.
*/
global func WeaponDamage(object target, int damage, int damage_type, bool exact_damage)
{
	if (!this || !target) return;
	
	damage_type = damage_type ?? FX_Call_EngObjHit;
	var true_damage = damage;
	if (exact_damage) true_damage = damage / 1000;
	
	if (target->GetAlive())
	{
		target->DoEnergy(-damage, exact_damage, damage_type, GetController());
		if (!target) return;

		target->~CatchBlow(-true_damage, this);
	}
	else
	{
		target->DoDamage(true_damage, damage_type, GetController());
	}
}

/*
	Tumbles an object based on this object's speed and mass.
	strength = 100 means using 100% of the own mass for tumbling the other object.
*/
global func WeaponTumble(object target, int strength)
{
	if (!this || !target) return;
	
	strength = strength ?? 100;
	if (strength <= 0) return;
	
	if (target->GetAlive())
	{
		target->SetAction("Tumble");
		// Constrained by conservation of linear momentum, unrealism != 1 for unrealistic behaviour.
		var unrealism = 3;
		var mass = strength * GetMass() / 100;
		var obj_mass = target->GetMass();
		target->SetXDir((target->GetXDir() * obj_mass + GetXDir() * mass * unrealism) / (mass + obj_mass));		
		target->SetYDir((target->GetYDir() * obj_mass + GetYDir() * mass * unrealism) / (mass + obj_mass));
	}
}