-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathturret.qc
296 lines (245 loc) · 7.46 KB
/
turret.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
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
290
291
292
293
294
295
296
float TURRET_STATE_INACTIVE = 0;
float TURRET_STATE_ACTIVATING = 1;
float TURRET_STATE_ACTIVE = 2;
float TURRET_STATE_DEACTIVATING = 3;
float TURRET_STATE_IDLE = 4;
/*
===============================================================================
difficulty tuning functions
===============================================================================
*/
float() TurretMissleSpeed =
{
return 600;
};
float() TurretRefireTime =
{
return 0.2;
};
float() TurretTurnSpeed =
{
return 50 + startskill * 20;
};
float TURRET_MAX_PITCH = 6;
float TURRET_MIN_PITCH = -6;
/*
===============================================================================
shooting
these functions are all performed by the helper entity
===============================================================================
*/
void(vector org, vector vec) LaunchLaser;
void() turret_shoot =
{
local vector ang;
// local vector v_forward_flat;
self.effects = self.effects | EF_MUZZLEFLASH;
sound (self, CHAN_VOICE, "enforcer/enfire.wav", 1, ATTN_NORM);
//calculate pitch based on the enemy's position, but clamp it to a narrow range
ang = vectoangles(self.enemy.origin - self.origin);
if (ang_x > 180)
ang_x = ang_x - 360;
if (ang_x > TURRET_MAX_PITCH)
ang_x = TURRET_MAX_PITCH;
if (ang_x < TURRET_MIN_PITCH)
ang_x = TURRET_MIN_PITCH;
self.angles_x = 0 - ang_x; //invert it because for some reason +pitch is downwards for makevectors() but upwards for vectoangles()
makevectors (self.angles);
LaunchLaser (self.origin + '0 0 1' * self.angles_x, v_forward); //shift the origin up/down a little so the lasers appear to come out of the red band
newmis.owner = self.owner;
newmis.effects = newmis.effects - EF_DIMLIGHT;
};
//returns true if existing enemy is still valid or we found a new valid enemy
float() turret_findtarget =
{
local entity e;
if (self.enemy != world)
e = self.enemy;
else //get a new enemy
{
e = checkclient ();
if (!e)
return FALSE; // current check entity isn't in PVS
}
if (e.items & IT_INVISIBILITY)
return FALSE;
if (range (e) == RANGE_FAR)
return FALSE;
if (!visible (e))
return FALSE;
if (e.takedamage == DAMAGE_NO)
return FALSE;
//
// got one
//
self.enemy = e;
if (self.enemy.classname != "player")
{
self.enemy = self.enemy.enemy;
if (self.enemy.classname != "player")
{
self.enemy = world;
return FALSE;
}
}
return TRUE;
};
void() turret_helper_think =
{
self.nextthink = time + TurretRefireTime();
if ( self.owner.state != TURRET_STATE_ACTIVE)
return;
//find enemy
if (!turret_findtarget())
return;
//turn towards enemy
self.ideal_yaw = vectoyaw(self.enemy.origin - self.origin);
self.yaw_speed = TurretTurnSpeed() * TurretRefireTime();
ChangeYaw();
//shoot if enemy is in sight (even if facing wrong way)
turret_shoot();
};
/*
===============================================================================
movement
===============================================================================
*/
void() turret_hit_bottom =
{
sound (self, CHAN_VOICE, self.noise1, 1, ATTN_NORM);
self.state = TURRET_STATE_INACTIVE;
};
void() turret_go_down =
{
sound (self, CHAN_VOICE, self.noise2, 1, ATTN_NORM);
self.state = TURRET_STATE_DEACTIVATING;
SUB_CalcMove (self.pos1, self.speed, turret_hit_bottom);
};
void() turret_hit_top =
{
sound (self, CHAN_VOICE, self.noise1, 1, ATTN_NORM);
self.state = TURRET_STATE_ACTIVE;
};
void() turret_go_up =
{
sound (self, CHAN_VOICE, self.noise2, 1, ATTN_NORM);
self.state = TURRET_STATE_ACTIVATING;
SUB_CalcMove (self.pos2, self.speed, turret_hit_top);
};
void() turret_use =
{
if (self.state == TURRET_STATE_INACTIVE || self.state == TURRET_STATE_DEACTIVATING)
{
self.frame = 0;
turret_go_up();
self.enemy = activator;
}
else if (self.state == TURRET_STATE_ACTIVATING || self.state == TURRET_STATE_ACTIVE)
{
self.frame = 1;
//if deactivated after being previously active, use movedir2 to determine position
self.pos1 = self.origin + self.movedir2;
turret_go_down();
}
};
/*
===============================================================================
spawn function
===============================================================================
*/
/*QUAKED func_turret (0 .5 .8) ? START_OFF
A rotating laser shooter that aims at the player in any horizontal direction. Has a capped rotation speed based on skill setting. When triggered, toggles between active and inactive states
START_OFF spawns in the inactive state
place in the level in the active/attacking position for proper lighting.
"movedir" the offset from active position to the initial START_OFF position
"movedir2" the offset from the active position to the deactivated position (after being previously active)
"height" the position that laser originates, measured up from the very bottom of the model
"angle" determines the initial turret direction (horizontal directions only)
"speed" speed when moving to a new position
"sounds" sounds to use when moving to a new position
0) no sound
1) stone
2) base
3) stone chain
4) screechy metal
*/
void() func_turret =
{
if (!SUB_InitEntity()) return;
local entity helper;
local vector org;
if (self.sounds == 0)
{
precache_sound ("misc/null.wav");
precache_sound ("misc/null.wav");
self.noise1 = "misc/null.wav";
self.noise2 = "misc/null.wav";
}
if (self.sounds == 1)
{
precache_sound ("doors/drclos4.wav");
precache_sound ("doors/doormv1.wav");
self.noise1 = "doors/drclos4.wav";
self.noise2 = "doors/doormv1.wav";
}
if (self.sounds == 2)
{
precache_sound ("doors/hydro1.wav");
precache_sound ("doors/hydro2.wav");
self.noise2 = "doors/hydro1.wav";
self.noise1 = "doors/hydro2.wav";
}
if (self.sounds == 3)
{
precache_sound ("doors/stndr1.wav");
precache_sound ("doors/stndr2.wav");
self.noise2 = "doors/stndr1.wav";
self.noise1 = "doors/stndr2.wav";
}
if (self.sounds == 4)
{
precache_sound ("doors/ddoor1.wav");
precache_sound ("doors/ddoor2.wav");
self.noise1 = "doors/ddoor2.wav";
self.noise2 = "doors/ddoor1.wav";
}
precache_model ("progs/laser.mdl");
precache_sound ("enforcer/enfire.wav");
precache_sound ("enforcer/enfstop.wav");
self.solid = SOLID_BSP;
self.movetype = MOVETYPE_PUSH;
setorigin (self, self.origin);
setmodel (self, self.model);
self.use = turret_use;
if (!self.obituary) self.obituary = "was blasted by a turret";
if (!self.speed)
self.speed = 100;
self.pos1 = self.origin + self.movedir; //inactive position
self.pos2 = self.origin; //active position
//spawn a second entity to handle alpha changes, since MOVETYPE_PUSH doesn't support think functions
helper = spawn();
helper.owner = self;
helper.nextthink = 0.1;
helper.think = turret_helper_think;
helper.classname = "turret";
helper.angles = self.angles;
org_x = (self.absmin_x + self.absmax_x) / 2;
org_y = (self.absmin_y + self.absmax_y) / 2;
org_z = self.absmin_z + self.height;
setorigin (helper, org);
//set actual angles to 0 now that helper has its angles
self.angles = '0 0 0';
//move to initial position
if (self.spawnflags & START_OFF)
{
self.state = TURRET_STATE_INACTIVE;
setorigin (self, self.pos1);
self.frame = 1;
}
else
{
self.state = TURRET_STATE_ACTIVE;
setorigin (self, self.pos2);
self.frame = 0;
}
};