div0's new grappling hook physics (with cfgs to en-/disable them)
[divverent/nexuiz.git] / data / qcsrc / server / gamec / g_hook.c
1 /*============================================
2
3       Wazat's Nexuiz Grappling Hook
4
5         Contact: Wazat1@gmail.com
6
7
8 Installation instructions:
9 --------------------------
10
11 1. Place hook.c in your gamec source directory with the other source files.
12
13 2. Add this line to the bottom of progs.src:
14
15 gamec/hook.c
16
17 3. Open defs.h and add these lines to the very bottom:
18
19 // Wazat's grappling hook
20 .entity         hook;
21 void GrapplingHookFrame();
22 void RemoveGrapplingHook(entity pl);
23 void SetGrappleHookBindings();
24 // hook impulses
25 float GRAPHOOK_FIRE             = 20;
26 float GRAPHOOK_RELEASE          = 21;
27 // (note: you can change the hook impulse #'s to whatever you please)
28
29 4. Open client.c and add this to the top of PutClientInServer():
30
31         RemoveGrapplingHook(self); // Wazat's Grappling Hook
32
33 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
34
35         // Wazat's grappling hook
36         SetGrappleHookBindings();
37
38 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
39
40         GrapplingHookFrame();
41
42 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
43 bind ctrl "+hook"
44
45 And you should be done!
46
47
48 ============================================*/
49
50 .float rope_length;
51 .float button6_pressed_before;
52
53 void RemoveGrapplingHook(entity pl)
54 {
55         if(pl.hook == world)
56                 return;
57         remove(pl.hook);
58         pl.hook = world;
59         if(pl.movetype == MOVETYPE_FLY)
60                 pl.movetype = MOVETYPE_WALK;
61
62         pl.hook_time = time + 0.0;
63 }
64
65 void GrapplingHookThink()
66 {
67         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
68         vector dir, org, end;
69         if(self.owner.health <= 0 || self.owner.hook != self)   // how did that happen?
70         {                                                                                                               // well, better fix it anyway
71                 remove(self);
72                 return;
73         }
74
75         self.nextthink = time + 0.1;
76
77         makevectors(self.owner.v_angle);
78         org = self.owner.origin + self.owner.view_ofs + v_forward * 15 - v_right * 5 + v_up * -12;
79
80         if(self.rope_length < 0)
81                 self.rope_length = vlen(org - self.origin);
82
83         if(self.state == 1)
84         {
85                 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
86                 // speed the rope is pulled with
87
88                 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
89                 // force the rope will use if it is stretched
90
91                 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
92                 // force the rope will use if it is stretched
93
94                 minlength = cvar("g_balance_grapplehook_length_min");//100;
95                 // minimal rope length
96                 // if the rope goes below this length, it isn't pulled any more
97
98                 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
99                 // if the rope is stretched by more than this amount, more rope is
100                 // given to you again
101
102                 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
103                 // while hanging on the rope, this friction component will help you a
104                 // bit to control the rope
105
106                 dir = self.origin - org;
107                 dist = vlen(dir);
108                 dir = normalize(dir);
109
110                 if(cvar("g_grappling_hook_tarzan"))
111                 {
112                         // first pull the rope...
113                         newlength = self.rope_length - pullspeed * 0.1;
114                         newlength = max(newlength, minlength);
115                         if(newlength < dist - ropestretch) // overstretched?
116                         {
117                                 newlength = dist - ropestretch;
118                                 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
119                                         self.owner.velocity = self.owner.velocity + 0.1 * dir * rubberforce_overstretch;
120                         }
121                         self.rope_length = newlength;
122
123                         // then pull the player
124                         spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
125                         self.owner.velocity = self.owner.velocity * (1 - 0.1 * ropeairfriction);
126                         self.owner.velocity = self.owner.velocity + 0.1 * dir * spd * rubberforce;
127                 }
128                 else
129                 {
130                         end = self.origin - dir*50;
131                         dist = vlen(end - org);
132                         if(dist < 200)
133                                 spd = dist * (pullspeed / 200);
134                         else
135                                 spd = pullspeed;
136                         if(spd < 50)
137                                 spd = 0;
138                         self.owner.velocity = dir*spd;
139                         self.owner.movetype = MOVETYPE_FLY;
140                 }
141
142                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
143
144                 org = org + dir*50; // get the beam out of the player's eyes
145         }
146
147         WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
148         WriteByte (MSG_BROADCAST, TE_BEAM);
149         WriteEntity (MSG_BROADCAST, self);
150         WriteCoord (MSG_BROADCAST, self.origin_x);
151         WriteCoord (MSG_BROADCAST, self.origin_y);
152         WriteCoord (MSG_BROADCAST, self.origin_z);
153         WriteCoord (MSG_BROADCAST, org_x);
154         WriteCoord (MSG_BROADCAST, org_y);
155         WriteCoord (MSG_BROADCAST, org_z);
156 }
157
158 void GrapplingHookTouch (void)
159 {
160         if (other == self.owner)
161                 return;
162         else if (pointcontents (self.origin) == CONTENT_SKY)
163         {
164                 RemoveGrapplingHook(self.owner);
165                 return;
166         }
167
168         sound (self, CHAN_BODY, "weapons/hook_impact.ogg", 1, ATTN_NORM);
169
170         self.state = 1;
171         self.think = GrapplingHookThink;
172         self.nextthink = time + 0.1;
173         self.touch = SUB_Null;
174         self.velocity = '0 0 0';
175         self.movetype = MOVETYPE_NONE;
176         self.rope_length = -1;
177 }
178
179 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
180 {
181         if(self.health > 0)
182         {
183                 self.health = self.health - damage;
184                 if (self.health <= 0)
185                 {
186                         if(attacker != self.owner)
187                         {
188                                 self.owner.pusher = attacker;
189                                 self.owner.pushltime = time + cvar("g_maxpushtime");
190                         }
191                         RemoveGrapplingHook(self.owner);
192                 }
193         }
194 }
195
196 void FireGrapplingHook (void)
197 {
198         local entity missile;
199         local vector org;
200
201         makevectors(self.v_angle);
202
203         sound (self, CHAN_WEAPON, "weapons/hook_fire.ogg", 1, ATTN_NORM);
204         org = self.origin + self.view_ofs + v_forward * 15 - v_right * 5 + v_up * -12;
205         te_customflash(org, 160, 0.2, '1 0 0');
206
207         missile = spawn ();
208         missile.owner = self;
209         self.hook = missile;
210         missile.classname = "grapplinghook";
211
212         missile.movetype = MOVETYPE_FLY;
213         missile.solid = SOLID_BBOX;
214
215         setmodel (missile, "models/ebomb.mdl"); // replace by something CENTERED!
216         setsize (missile, '-3 -3 -3', '3 3 3');
217         setorigin (missile, org);
218
219         missile.state = 0; // not latched onto anything
220
221         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
222         missile.angles = vectoangles (missile.velocity);
223         //missile.glow_color = 250; // 244, 250
224         //missile.glow_size = 120;
225         missile.touch = GrapplingHookTouch;
226         missile.think = GrapplingHookThink;
227         missile.nextthink = time + 0.1;
228
229         missile.effects = EF_FULLBRIGHT | EF_ADDITIVE | EF_LOWPRECISION;
230
231         missile.health = cvar("g_balance_grapplehook_health");//120
232         missile.event_damage = GrapplingHook_Damage;
233         missile.takedamage = DAMAGE_AIM;
234         missile.damageforcescale = 0;
235 }
236
237 void GrapplingHookFrame()
238 {
239         // this function has been modified for Nexuiz
240         if (self.button6 && cvar("g_grappling_hook"))
241         {
242                 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
243                         FireGrapplingHook();
244         }
245         else
246         {
247                 if (self.hook)
248                         RemoveGrapplingHook(self);
249         }
250         self.button6_pressed_before = self.button6;
251         /*
252         // if I have no hook or it's not pulling yet, make sure I'm not flying!
253         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
254         {
255                 self.movetype = MOVETYPE_WALK;
256         }
257         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && cvar("g_grappling_hook"))
258         {
259                 // fire hook
260                 FireGrapplingHook();
261                 return;
262         }
263         else if(self.hookimpulse == GRAPHOOK_RELEASE)
264         {
265                 // remove hook, reset movement type
266                 RemoveGrapplingHook(self);
267                 return;
268         }
269         */
270         /*else // make sure the player's movetype is correct
271         {
272                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
273                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
274                 {
275                         self.movetype = MOVETYPE_WALK;
276                 }
277         }*/
278         // note: The hook entity does the actual pulling
279 }
280
281 void SetGrappleHookBindings()
282 {
283         // this function has been modified for Nexuiz
284         /*
285         stuffcmd(self, strcat("alias +hook \"impulse ", ftos(GRAPHOOK_FIRE),    "\"\n"));
286         stuffcmd(self, strcat("alias -hook \"impulse ", ftos(GRAPHOOK_RELEASE), "\"\n"));
287         */
288 }