]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/units/unit_ewheel.qc
Merge remote branch 'origin/master' into tzork/turrets-csqc
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / tturrets / units / unit_ewheel.qc
1 #define ewheel_amin_stop 0
2 #define ewheel_amin_fwd_slow 1
3 #define ewheel_amin_fwd_fast 2
4 #define ewheel_amin_bck_slow 3
5 #define ewheel_amin_bck_fast 4
6
7 void turret_ewheel_projectile_explode()
8 {
9 #ifdef TURRET_DEBUG
10     float d;
11
12     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET_EWHEEL, world);
13     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d;
14     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
15 #else
16     RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET_EWHEEL, world);
17 #endif
18
19     remove (self);
20 }
21
22
23 void ewheel_attack()
24 {
25     entity proj;
26     float i;
27
28     for (i = 0; i < 1; ++i)
29     {
30         turret_do_updates(self);
31
32         sound (self, CHAN_WEAPON, "weapons/lasergun_fire.wav", VOL_BASE, ATTN_NORM);
33         pointparticles(particleeffectnum("laser_muzzleflash"), self.tur_shotorg, self.tur_shotdir_updated * 1000, 1);
34
35         proj                    = spawn ();
36         setorigin(proj, self.tur_shotorg);
37         proj.classname       = "ewheel bolt";
38         proj.owner           = self;
39         proj.bot_dodge       = FALSE;
40         proj.bot_dodgerating = self.shot_dmg;
41         proj.think           = turret_ewheel_projectile_explode;
42         proj.nextthink       = time + 9;
43         //proj.solid           = SOLID_TRIGGER;
44         proj.movetype        = MOVETYPE_FLYMISSILE;
45         proj.velocity        = normalize(self.tur_shotdir_updated + randomvec() * self.shot_spread) * self.shot_speed;
46         proj.touch           = turret_ewheel_projectile_explode;
47         proj.enemy           = self.enemy;
48         proj.flags           = FL_PROJECTILE | FL_NOTARGET;
49         PROJECTILE_MAKETRIGGER(proj);
50
51         CSQCProjectile(proj, TRUE, PROJECTILE_LASER, TRUE);
52
53         self.tur_head.frame += 2;
54
55         if (self.tur_head.frame > 3)
56             self.tur_head.frame = 0;
57     }
58
59 }
60
61 void ewheel_move_path()
62 {
63
64     // Are we close enougth to a path node to switch to the next?
65     if (vlen(self.origin  - self.pathcurrent.origin) < 64)
66         if (self.pathcurrent.path_next == world)
67         {
68             // Path endpoint reached
69             pathlib_deletepath(self.pathcurrent.owner);
70             self.pathcurrent = world;
71
72             if (self.pathgoal)
73             {
74                 if (self.pathgoal.use)
75                     self.pathgoal.use();
76
77                 if (self.pathgoal.enemy)
78                 {
79                     self.pathcurrent = pathlib_astar(self.pathgoal.origin,self.pathgoal.enemy.origin);
80                     self.pathgoal = self.pathgoal.enemy;
81                 }
82             }
83             else
84                 self.pathgoal = world;
85         }
86         else
87             self.pathcurrent = self.pathcurrent.path_next;
88
89
90
91     if (self.pathcurrent)
92     {
93
94         self.moveto = self.pathcurrent.origin;
95         self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
96
97         movelib_move_simple(v_forward, autocvar_g_turrets_unit_ewheel_speed_fast, 0.4);
98
99         return;
100     }
101 }
102
103 void  ewheel_move_enemy()
104 {
105
106     float newframe;
107     
108     self.steerto = steerlib_arrive(self.enemy.origin,self.target_range_optimal);
109
110     //self.steerto = steerlib_standoff(self.enemy.origin,self.target_range_optimal);
111     //self.steerto = steerlib_beamsteer(self.steerto,1024,64,68,256);
112     self.moveto  = self.origin + self.steerto * 128;
113
114     if (self.tur_dist_enemy > self.target_range_optimal)
115     {
116         if ( self.tur_head.spawnshieldtime < 1 )
117         {
118             newframe = ewheel_amin_fwd_fast;
119             movelib_move_simple(v_forward, autocvar_g_turrets_unit_ewheel_speed_fast, 0.4);
120         }
121         else if (self.tur_head.spawnshieldtime < 2)
122         {
123
124             newframe = ewheel_amin_fwd_slow;
125             movelib_move_simple(v_forward, autocvar_g_turrets_unit_ewheel_speed_slow, 0.4);
126        }
127         else
128         {
129             newframe = ewheel_amin_fwd_slow;
130             movelib_move_simple(v_forward, autocvar_g_turrets_unit_ewheel_speed_slower, 0.4);
131         }
132     }
133     else if (self.tur_dist_enemy < self.target_range_optimal * 0.5)
134     {
135         newframe = ewheel_amin_bck_slow;
136         movelib_move_simple(v_forward * -1, autocvar_g_turrets_unit_ewheel_speed_slow, 0.4);
137     }
138     else
139     {
140         newframe = ewheel_amin_stop;
141         movelib_beak_simple(autocvar_g_turrets_unit_ewheel_speed_stop);
142     }
143     
144     if(self.frame != newframe)
145     {
146         self.frame = newframe;
147         self.SendFlags |= TNSF_ANIM;
148         self.anim_start_time = time;
149     }
150 }
151
152
153 void ewheel_move_idle()
154 {
155     if(self.frame != 0)
156     {
157         self.SendFlags |= TNSF_ANIM;
158         self.anim_start_time = time;
159     }
160
161     self.frame = 0;
162     if (vlen(self.velocity))
163         movelib_beak_simple(autocvar_g_turrets_unit_ewheel_speed_stop);
164 }
165
166 void ewheel_postthink()
167 {
168     float vz;
169     vector wish_angle, real_angle;
170
171     vz = self.velocity_z;
172
173     self.angles_x = anglemods(self.angles_x);
174     self.angles_y = anglemods(self.angles_y);
175
176     fixedmakevectors(self.angles);
177
178     wish_angle = normalize(self.steerto);
179     wish_angle = vectoangles(wish_angle);
180     real_angle = wish_angle - self.angles;
181     real_angle = shortangle_vxy(real_angle, self.tur_head.angles);
182
183     self.tur_head.spawnshieldtime = fabs(real_angle_y);
184     real_angle_y  = bound(-self.tur_head.aim_speed, real_angle_y, self.tur_head.aim_speed);
185     self.angles_y = (self.angles_y + real_angle_y);
186
187     if(self.enemy)
188         ewheel_move_enemy();
189     else if(self.pathcurrent)
190         ewheel_move_path();
191     else
192         ewheel_move_idle();
193
194
195     self.velocity_z = vz;
196     
197     if(vlen(self.velocity))
198         self.SendFlags |= TNSF_MOVE;
199 }
200
201 void ewheel_respawnhook()
202 {
203     entity e;
204
205     // Respawn is called & first spawn to, to set team. need to make sure we do not move the initial spawn.
206     if(self.movetype != MOVETYPE_WALK)
207                 return;
208                 
209     self.velocity = '0 0 0';
210     self.enemy = world;
211
212     setorigin(self, self.pos1);
213
214     if (self.target != "")
215     {
216         e = find(world,targetname,self.target);
217         if (!e)
218         {
219             dprint("Initital waypoint for ewheel does NOT exsist, fix your map!\n");
220             self.target = "";
221         }
222
223         if (e.classname != "turret_checkpoint")
224             dprint("Warning: not a turrret path\n");
225         else
226         {
227             self.pathcurrent = WALKER_PATH(self.origin,e.origin);
228             self.pathgoal = e;
229         }
230     }
231 }
232
233 void ewheel_diehook()
234 {
235     self.velocity = '0 0 0';
236
237     if (self.pathcurrent)
238         pathlib_deletepath(self.pathcurrent.owner);
239
240     self.pathcurrent = world;
241 }
242
243 void turret_ewheel_dinit()
244 {
245     entity e;
246
247     if (self.netname == "")      
248         self.netname     = "eWheel Turret";
249
250     if (self.target != "")
251     {
252         e = find(world,targetname,self.target);
253         if (!e)
254         {
255             bprint("Warning! initital waypoint for ewheel does NOT exsist!\n");
256             self.target = "";
257         }
258
259         if (e.classname != "turret_checkpoint")
260             dprint("Warning: not a turrret path\n");
261         else
262             self.goalcurrent = e;
263     }
264
265     self.ammo_flags = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE | TFL_AMMO_RECIVE;
266     self.turrcaps_flags = TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_MOVE | TFL_TURRCAPS_ROAM ;
267     self.turret_respawnhook = ewheel_respawnhook;
268
269     self.turret_diehook = ewheel_diehook;
270
271     if (turret_stdproc_init("ewheel_std", "models/turrets/ewheel-base2.md3", "models/turrets/ewheel-gun1.md3", TID_EWHEEL) == 0)
272     {
273         remove(self);
274         return;
275     }
276
277     self.target_select_flags   = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
278     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
279
280     self.damage_flags          |= TFL_DMG_DEATH_NOGIBS;
281
282     self.iscreature = TRUE;
283     self.movetype   = MOVETYPE_WALK;
284     self.solid      = SOLID_SLIDEBOX;
285     self.takedamage = DAMAGE_AIM;
286
287     setsize(self, '-32 -32 0', '32 32 48');
288     self.idle_aim = '0 0 0';
289
290     self.pos1 = self.origin;
291
292     // Our fire routine
293     self.turret_firefunc  = ewheel_attack;
294     self.turret_postthink = ewheel_postthink;
295     self.tur_head.frame = 1;
296
297     // Convert from dgr / sec to dgr / tic
298     self.tur_head.aim_speed = autocvar_g_turrets_unit_ewheel_turnrate;
299     self.tur_head.aim_speed = self.tur_head.aim_speed / (1 / self.ticrate);
300
301     //setorigin(self,self.origin + '0 0 128');
302     if (self.target != "")
303     {
304         e = find(world,targetname,self.target);
305         if (!e)
306         {
307             dprint("Initital waypoint for ewheel does NOT exsist, fix your map!\n");
308             self.target = "";
309         }
310
311         if (e.classname != "turret_checkpoint")
312             dprint("Warning: not a turrret path\n");
313         else
314         {
315             self.pathcurrent = WALKER_PATH(self.origin, e.origin);
316             self.pathgoal = e;
317         }
318     }
319 }
320
321 void spawnfunc_turret_ewheel()
322 {
323     g_turrets_common_precash();
324
325     precache_model ("models/turrets/ewheel-base2.md3");
326     precache_model ("models/turrets/ewheel-gun1.md3");
327
328     self.think = turret_ewheel_dinit;
329     self.nextthink = time + 0.5;
330 }