]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/turrets/turret/ewheel.qc
Merge branch 'martin-t/insta' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / turret / ewheel.qc
1 #include "ewheel.qh"
2
3 #ifdef SVQC
4
5 float autocvar_g_turrets_unit_ewheel_speed_fast;
6 float autocvar_g_turrets_unit_ewheel_speed_slow;
7 float autocvar_g_turrets_unit_ewheel_speed_slower;
8 float autocvar_g_turrets_unit_ewheel_speed_stop;
9 float autocvar_g_turrets_unit_ewheel_turnrate;
10
11 const int ewheel_anim_stop     = 0;
12 const int ewheel_anim_fwd_slow = 1;
13 const int ewheel_anim_fwd_fast = 2;
14 const int ewheel_anim_bck_slow = 3;
15 const int ewheel_anim_bck_fast = 4;
16
17 void ewheel_move_path(entity this)
18 {
19     // Are we close enough to a path node to switch to the next?
20     if(vdist(this.origin - this.pathcurrent.origin, <, 64))
21     {
22 #ifdef EWHEEL_FANCYPATH
23         if (this.pathcurrent.path_next == NULL)
24         {
25             // Path endpoint reached
26             pathlib_deletepath(this.pathcurrent.owner);
27             this.pathcurrent = NULL;
28
29             if (this.pathgoal)
30             {
31                 if (this.pathgoal.use)
32                     this.pathgoal.use(this.pathgoal, NULL, NULL);
33
34                 if (this.pathgoal.enemy)
35                 {
36                     this.pathcurrent = pathlib_astar(this, this.pathgoal.origin,this.pathgoal.enemy.origin);
37                     this.pathgoal = this.pathgoal.enemy;
38                 }
39             }
40             else
41                 this.pathgoal = NULL;
42         }
43         else
44             this.pathcurrent = this.pathcurrent.path_next;
45 #else
46         this.pathcurrent = this.pathcurrent.enemy;
47 #endif
48     }
49
50     if (this.pathcurrent)
51     {
52
53         this.moveto = this.pathcurrent.origin;
54         this.steerto = steerlib_attract2(this, this.moveto, 0.5, 500, 0.95);
55
56         movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_fast), 0.4);
57     }
58 }
59
60 void ewheel_move_enemy(entity this)
61 {
62     int newframe;
63
64     this.steerto = steerlib_arrive(this, this.enemy.origin,this.target_range_optimal);
65
66     this.moveto = this.origin + this.steerto * 128;
67
68     if (this.tur_dist_enemy > this.target_range_optimal)
69     {
70         if ( this.tur_head.spawnshieldtime < 1 )
71         {
72             newframe = ewheel_anim_fwd_fast;
73             movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_fast), 0.4);
74         }
75         else if (this.tur_head.spawnshieldtime < 2)
76         {
77
78             newframe = ewheel_anim_fwd_slow;
79             movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_slow), 0.4);
80        }
81         else
82         {
83             newframe = ewheel_anim_fwd_slow;
84             movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_slower), 0.4);
85         }
86     }
87     else if (this.tur_dist_enemy < this.target_range_optimal * 0.5)
88     {
89         newframe = ewheel_anim_bck_slow;
90         movelib_move_simple(this, v_forward * -1, (autocvar_g_turrets_unit_ewheel_speed_slow), 0.4);
91     }
92     else
93     {
94         newframe = ewheel_anim_stop;
95         movelib_brake_simple(this, (autocvar_g_turrets_unit_ewheel_speed_stop));
96     }
97
98     turrets_setframe(this, newframe, false);
99 }
100
101 void ewheel_move_idle(entity this)
102 {
103     if(this.frame != 0)
104     {
105         this.SendFlags |= TNSF_ANIM;
106         this.anim_start_time = time;
107     }
108
109     this.frame = 0;
110     if(this.velocity)
111         movelib_brake_simple(this, (autocvar_g_turrets_unit_ewheel_speed_stop));
112 }
113
114 void ewheel_findtarget(entity this)
115 {
116     entity e = find(NULL, targetname, this.target);
117     if (!e)
118     {
119         LOG_TRACE("Initital waypoint for ewheel does NOT exist, fix your map!");
120         this.target = "";
121     }
122
123     if (e.classname != "turret_checkpoint")
124         LOG_TRACE("Warning: not a turret path");
125     else
126     {
127
128 #ifdef EWHEEL_FANCYPATH
129         this.pathcurrent = pathlib_astar(this, this.origin, e.origin);
130         this.pathgoal = e;
131 #else
132         this.pathcurrent  = e;
133 #endif
134     }
135 }
136
137 spawnfunc(turret_ewheel) { if(!turret_initialize(this, TUR_EWHEEL)) delete(this); }
138
139 METHOD(EWheel, tr_think, void(EWheel thistur, entity it))
140 {
141     vector wish_angle, real_angle;
142
143     float vz = it.velocity_z;
144
145     it.angles_x = anglemods(it.angles_x);
146     it.angles_y = anglemods(it.angles_y);
147
148     fixedmakevectors(it.angles);
149
150     wish_angle = normalize(it.steerto);
151     wish_angle = vectoangles(wish_angle);
152     real_angle = wish_angle - it.angles;
153     real_angle = shortangle_vxy(real_angle, it.tur_head.angles);
154
155     it.tur_head.spawnshieldtime = fabs(real_angle_y);
156     real_angle_y  = bound(-it.tur_head.aim_speed, real_angle_y, it.tur_head.aim_speed);
157     it.angles_y = (it.angles_y + real_angle_y);
158
159     if(it.enemy)
160         ewheel_move_enemy(it);
161     else if(it.pathcurrent)
162         ewheel_move_path(it);
163     else
164         ewheel_move_idle(it);
165
166     it.velocity_z = vz;
167
168     if(it.velocity)
169         it.SendFlags |= TNSF_MOVE;
170 }
171
172 METHOD(EWheel, tr_death, void(EWheel this, entity it))
173 {
174     it.velocity = '0 0 0';
175
176 #ifdef EWHEEL_FANCYPATH
177     if (it.pathcurrent)
178         pathlib_deletepath(it.pathcurrent.owner);
179 #endif
180     it.pathcurrent = NULL;
181 }
182
183 METHOD(EWheel, tr_setup, void(EWheel this, entity it))
184 {
185     if(it.move_movetype == MOVETYPE_WALK)
186     {
187         it.velocity = '0 0 0';
188         it.enemy = NULL;
189
190         setorigin(it, it.pos1);
191
192         if (it.target != "")
193             InitializeEntity(it, ewheel_findtarget, INITPRIO_FINDTARGET);
194     }
195
196     it.iscreature                               = true;
197     it.teleportable                     = TELEPORT_NORMAL;
198     if(!it.damagedbycontents)
199         IL_PUSH(g_damagedbycontents, it);
200     it.damagedbycontents        = true;
201     set_movetype(it, MOVETYPE_WALK);
202     it.solid                                    = SOLID_SLIDEBOX;
203     it.takedamage                               = DAMAGE_AIM;
204     it.idle_aim                         = '0 0 0';
205     it.pos1                                     = it.origin;
206     it.target_select_flags      = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
207     it.target_validate_flags    = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
208     it.frame                                    = it.tur_head.frame = 1;
209     it.ammo_flags                               = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE | TFL_AMMO_RECIEVE;
210
211     // Convert from dgr / sec to dgr / tic
212     it.tur_head.aim_speed = (autocvar_g_turrets_unit_ewheel_turnrate);
213     it.tur_head.aim_speed = it.tur_head.aim_speed / (1 / it.ticrate);
214 }
215
216 #endif // SVQC
217 #ifdef CSQC
218
219 void ewheel_draw(entity this)
220 {
221     float dt;
222
223     dt = time - this.move_time;
224     this.move_time = time;
225     if(dt <= 0)
226         return;
227
228     fixedmakevectors(this.angles);
229     setorigin(this, this.origin + this.velocity * dt);
230     this.tur_head.angles += dt * this.tur_head.avelocity;
231
232     if (this.health < 127)
233     if(random() < 0.05)
234         te_spark(this.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
235 }
236
237         METHOD(EWheel, tr_setup, void(EWheel this, entity it))
238         {
239             it.gravity          = 1;
240             set_movetype(it, MOVETYPE_BOUNCE);
241             it.move_time                = time;
242             it.draw                     = ewheel_draw;
243         }
244
245 #endif // CSQC