-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagnets.qc
150 lines (120 loc) · 3.11 KB
/
magnets.qc
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
// How do they work?
float LINEAR = 2;
float NO_MONSTERS = 4;
void() magnet_touch =
{
if (self.estate != STATE_ACTIVE)
return;
if (!(CheckValidTouch() || !(other.flags & FL_MONSTER) || !(other.movetype == MOVETYPE_BOUNCE) || !(other.movetype == MOVETYPE_FLYMISSILE) || !(other.movetype == MOVETYPE_FLY) ))
return;
if (other.flags & FL_MONSTER && self.spawnflags & NO_MONSTERS)
{
return;
}
local vector disp;
local float dist, inv_dist_squared, force;
disp = other.origin - self.singularity;
if (self.mangle) {
makevectors(self.mangle);
// Create an invisible plane that the mangle is normal to.
// Instead of using distance from the point entity in 3D space, use distance from the plane.
disp[0] *= v_forward[0];
disp[1] *= v_forward[1];
disp[2] *= v_forward[2];
}
dist = vlen(disp);
// Can add a constant (with the wait key) to remove singularities.
// Should be used for attractors, as well as planar magnets since the force is at full strength across the plane.
if (self.spawnflags & LINEAR)
{
inv_dist_squared = 1 / ((dist) + (self.wait * self.wait));
}
else
{
inv_dist_squared = 1 / ((dist * dist) + (self.wait * self.wait));
}
force = self.impulse * self.impulse;
force *= inv_dist_squared;
// squaring removes the negative, so give it back if we gave a negative impulse
if (self.impulse < 0)
{
force *= -1;
}
if (other.movetype == MOVETYPE_FLYMISSILE || other.movetype == MOVETYPE_FLY)
{
vector oldvel = other.velocity;
other.angles = vectoangles(other.velocity);
}
if (self.mangle)
{
makevectors(self.mangle);
other.velocity += normalize(v_forward) * force;
}
else
{
other.velocity += normalize(disp) * force;
}
if (other.flags & FL_MONSTER)
{
other.flags &~= FL_ONGROUND;
other.velocity += normalize(disp) * force;
}
if (other.movetype == MOVETYPE_FLYMISSILE || other.movetype == MOVETYPE_FLY)
{
other.velocity = normalize(other.velocity);
other.velocity *= vlen(oldvel);
}
// Black holes, for example
if ((self.killradius > 0) && !self.mangle && (vlen(other.origin - self.singularity) < self.killradius))
{
if (CanDamage(other, self))
{
T_Damage(other, self, self, 1000);
}
if (other.classname != "player")
{
remove(other);
}
}
}
void() magnet_use =
{
if (self.estate == STATE_INACTIVE)
self.estate = STATE_ACTIVE;
else
self.estate = STATE_INACTIVE;
}
void() magnet_think =
{
local entity m;
m = find(world, targetname, self.target);
self.singularity = m.origin;
}
void() trigger_magnet =
{
if (SUB_Inhibit ()) // new spawnflags for all entities -- iw
return;
InitTrigger();
if (!self.impulse)
{
self.impulse = 500;
}
if (!self.wait)
{
self.wait = 0;
}
// If there's no target, then there's no reference point for the magnet's force
// Even planar magnets need a target to define the location of the plane
if (!self.target)
{
objerror("trigger_magnet has no target");
}
self.touch = magnet_touch;
if (self.spawnflags & START_OFF)
self.estate = STATE_INACTIVE;
else
self.estate = STATE_ACTIVE;
self.use = magnet_use;
self.nextthink = time + 0.01;
self.think = magnet_think;
}