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