dba8a72a8f021e0720abaa9c8c563c94e0c7be8a
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / vehicle / spiderbot_weapons.qc
1 #include "spiderbot_weapons.qh"
2
3 #ifdef IMPLEMENTATION
4
5 #ifdef SVQC
6
7 void spiderbot_rocket_artillery(entity this)
8 {
9     this.nextthink = time;
10     UpdateCSQCProjectile(this);
11 }
12
13 void spiderbot_rocket_unguided(entity this)
14 {
15     vector newdir, olddir;
16
17     this.nextthink  = time;
18
19     olddir = normalize(this.velocity);
20     newdir = normalize(this.pos1 - this.origin) + randomvec() * autocvar_g_vehicle_spiderbot_rocket_noise;
21     this.velocity = normalize(olddir + newdir * autocvar_g_vehicle_spiderbot_rocket_turnrate) * autocvar_g_vehicle_spiderbot_rocket_speed;
22
23     UpdateCSQCProjectile(this);
24
25     if (IS_DEAD(this.owner) || this.cnt < time || vdist(this.pos1 - this.origin, <, 16))
26         this.use(this, NULL, NULL);
27 }
28
29 void spiderbot_rocket_guided(entity this)
30 {
31     vector newdir, olddir;
32
33     this.nextthink  = time;
34
35     if(!this.realowner.vehicle)
36         setthink(this, spiderbot_rocket_unguided);
37
38     crosshair_trace(this.realowner);
39     olddir = normalize(this.velocity);
40     newdir = normalize(trace_endpos - this.origin) + randomvec() * autocvar_g_vehicle_spiderbot_rocket_noise;
41     this.velocity = normalize(olddir + newdir * autocvar_g_vehicle_spiderbot_rocket_turnrate) * autocvar_g_vehicle_spiderbot_rocket_speed;
42
43     UpdateCSQCProjectile(this);
44
45     if (IS_DEAD(this.owner) || this.cnt < time)
46         this.use(this, NULL, NULL);
47 }
48
49 void spiderbot_guide_release(entity this)
50 {
51     entity rkt;
52     rkt = findchainentity(realowner, this.owner);
53     if(!rkt)
54         return;
55
56     crosshair_trace(this.owner);
57     while(rkt)
58     {
59         if(getthink(rkt) == spiderbot_rocket_guided)
60         {
61             rkt.pos1 = trace_endpos;
62             setthink(rkt, spiderbot_rocket_unguided);
63         }
64         rkt = rkt.chain;
65     }
66 }
67
68 float spiberbot_calcartillery_flighttime;
69 vector spiberbot_calcartillery(vector org, vector tgt, float ht)
70 {
71     float grav, sdist, zdist, vs, vz, jumpheight;
72     vector sdir;
73
74     grav  = autocvar_sv_gravity;
75     zdist = tgt_z - org_z;
76     sdist = vlen(tgt - org - zdist * '0 0 1');
77     sdir  = normalize(tgt - org - zdist * '0 0 1');
78
79     // how high do we need to go?
80     jumpheight = fabs(ht);
81     if(zdist > 0)
82         jumpheight = jumpheight + zdist;
83
84     // push so high...
85     vz = sqrt(2 * grav * jumpheight); // NOTE: sqrt(positive)!
86
87     // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
88     if(ht < 0)
89         if(zdist < 0)
90             vz = -vz;
91
92     vector solution;
93     solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
94     // ALWAYS solvable because jumpheight >= zdist
95     if(!solution_z)
96         solution_y = solution_x; // just in case it is not solvable due to roundoff errors, assume two equal solutions at their center (this is mainly for the usual case with ht == 0)
97     if(zdist == 0)
98         solution_x = solution_y; // solution_x is 0 in this case, so don't use it, but rather use solution_y (which will be sqrt(0.5 * jumpheight / grav), actually)
99
100     if(zdist < 0)
101     {
102         // down-jump
103         if(ht < 0)
104         {
105             // almost straight line type
106             // jump apex is before the jump
107             // we must take the larger one
108             spiberbot_calcartillery_flighttime = solution_y;
109         }
110         else
111         {
112             // regular jump
113             // jump apex is during the jump
114             // we must take the larger one too
115             spiberbot_calcartillery_flighttime = solution_y;
116         }
117     }
118     else
119     {
120         // up-jump
121         if(ht < 0)
122         {
123             // almost straight line type
124             // jump apex is after the jump
125             // we must take the smaller one
126             spiberbot_calcartillery_flighttime = solution_x;
127         }
128         else
129         {
130             // regular jump
131             // jump apex is during the jump
132             // we must take the larger one
133             spiberbot_calcartillery_flighttime = solution_y;
134         }
135     }
136     vs = sdist / spiberbot_calcartillery_flighttime;
137
138     // finally calculate the velocity
139     return sdir * vs + '0 0 1' * vz;
140 }
141
142 void spiderbot_rocket_do(entity this)
143 {
144     vector v;
145     entity rocket = NULL;
146
147     if (this.wait != -10)
148     {
149         if (PHYS_INPUT_BUTTON_ATCK2(this.owner) && this.vehicle_weapon2mode == SBRM_GUIDE)
150         {
151             if (this.wait == 1)
152             if (this.tur_head.frame == 9 || this.tur_head.frame == 1)
153             {
154                 if(this.gun2.cnt < time && this.tur_head.frame == 9)
155                     this.tur_head.frame = 1;
156
157                 return;
158             }
159             this.wait = 1;
160         }
161         else
162         {
163             if(this.wait)
164                 spiderbot_guide_release(this);
165
166             this.wait = 0;
167         }
168     }
169
170     if(this.gun2.cnt > time)
171         return;
172
173     if (this.tur_head.frame >= 9)
174     {
175         this.tur_head.frame = 1;
176         this.wait = 0;
177     }
178
179     if(this.wait != -10)
180     if(!PHYS_INPUT_BUTTON_ATCK2(this.owner))
181         return;
182
183     if(forbidWeaponUse(this.owner))
184         return;
185
186     v = gettaginfo(this.tur_head,gettagindex(this.tur_head,"tag_fire"));
187
188     switch(this.vehicle_weapon2mode)
189     {
190         case SBRM_VOLLY:
191             rocket = vehicles_projectile(this, EFFECT_SPIDERBOT_ROCKETLAUNCH.eent_eff_name, SND_ROCKET_FIRE,
192                                    v, normalize(randomvec() * autocvar_g_vehicle_spiderbot_rocket_spread + v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
193                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
194                                    DEATH_VH_SPID_ROCKET.m_id, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, false, true, this.owner);
195             crosshair_trace(this.owner);
196             float _dist = (random() * autocvar_g_vehicle_spiderbot_rocket_radius) + vlen(v - trace_endpos);
197             _dist -= (random() * autocvar_g_vehicle_spiderbot_rocket_radius) ;
198             rocket.nextthink  = time + (_dist / autocvar_g_vehicle_spiderbot_rocket_speed);
199             setthink(rocket, vehicles_projectile_explode);
200
201             if(PHYS_INPUT_BUTTON_ATCK2(this.owner) && this.tur_head.frame == 1)
202                 this.wait = -10;
203             break;
204         case SBRM_GUIDE:
205             rocket = vehicles_projectile(this, EFFECT_SPIDERBOT_ROCKETLAUNCH.eent_eff_name, SND_ROCKET_FIRE,
206                                    v, normalize(v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
207                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
208                                    DEATH_VH_SPID_ROCKET.m_id, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, false, false, this.owner);
209             crosshair_trace(this.owner);
210             rocket.pos1    = trace_endpos;
211             rocket.nextthink  = time;
212             setthink(rocket, spiderbot_rocket_guided);
213
214
215         break;
216         case SBRM_ARTILLERY:
217             rocket = vehicles_projectile(this, EFFECT_SPIDERBOT_ROCKETLAUNCH.eent_eff_name, SND_ROCKET_FIRE,
218                                    v, normalize(v_forward) * autocvar_g_vehicle_spiderbot_rocket_speed,
219                                    autocvar_g_vehicle_spiderbot_rocket_damage, autocvar_g_vehicle_spiderbot_rocket_radius, autocvar_g_vehicle_spiderbot_rocket_force, 1,
220                                    DEATH_VH_SPID_ROCKET.m_id, PROJECTILE_SPIDERROCKET, autocvar_g_vehicle_spiderbot_rocket_health, false, true, this.owner);
221
222             crosshair_trace(this.owner);
223
224             rocket.pos1    = trace_endpos + randomvec() * (0.75 * autocvar_g_vehicle_spiderbot_rocket_radius);
225             rocket.pos1_z          = trace_endpos_z;
226
227             traceline(v, v + '0 0 1' * MAX_SHOT_DISTANCE, MOVE_WORLDONLY, this);
228             float h1 = 0.75 * vlen(v - trace_endpos);
229
230             //v = trace_endpos;
231             traceline(v , rocket.pos1 + '0 0 1' * MAX_SHOT_DISTANCE, MOVE_WORLDONLY, this);
232             float h2 = 0.75 * vlen(rocket.pos1 - v);
233
234             rocket.velocity  = spiberbot_calcartillery(v, rocket.pos1, ((h1 < h2) ? h1 : h2));
235             rocket.movetype  = MOVETYPE_TOSS;
236             rocket.gravity   = 1;
237             //setthink(rocket, spiderbot_rocket_artillery);
238         break;
239     }
240     rocket.classname  = "spiderbot_rocket";
241
242     rocket.cnt = time + autocvar_g_vehicle_spiderbot_rocket_lifetime;
243
244     this.tur_head.frame += 1;
245     if (this.tur_head.frame == 9)
246         this.attack_finished_single[0] = autocvar_g_vehicle_spiderbot_rocket_reload;
247     else
248         this.attack_finished_single[0] = ((this.vehicle_weapon2mode ==  SBRM_VOLLY) ? autocvar_g_vehicle_spiderbot_rocket_refire2 : autocvar_g_vehicle_spiderbot_rocket_refire);
249
250     this.gun2.cnt = time + this.attack_finished_single[0];
251 }
252
253 #endif
254
255 #endif