~ubuntu-branches/debian/stretch/assaultcube-data/stretch

« back to all changes in this revision

Viewing changes to source/src/physics.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gonéri Le Bouder, Ansgar Burchardt, Gonéri Le Bouder
  • Date: 2010-04-02 23:37:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100402233755-kf74fxwlu634o6vg
Tags: 1.0.4+repack1-1
[ Ansgar Burchardt ]
* debian/control: fix typo in short description

[ Gonéri Le Bouder ]
* Upgrade to 1.0.4
* bump standards-version to 3.8.4
* Add Depends: ${misc:Depends} just to avoid a lintian warning
* Add a debian/source/format file for the same reason

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// physics.cpp: no physics books were hurt nor consulted in the construction of this code.
 
2
// All physics computations and constants were invented on the fly and simply tweaked until
 
3
// they "felt right", and have no basis in reality. Collision detection is simplistic but
 
4
// very robust (uses discrete steps at fixed fps).
 
5
 
 
6
#include "pch.h"
 
7
#include "cube.h"
 
8
 
 
9
float raycube(const vec &o, const vec &ray, vec &surface)
 
10
{
 
11
    surface = vec(0, 0, 0);
 
12
 
 
13
    if(ray.iszero()) return -1;
 
14
 
 
15
    vec v = o;
 
16
    float dist = 0, dx = 0, dy = 0, dz = 0;
 
17
 
 
18
    for(;;)
 
19
    {
 
20
        int x = int(v.x), y = int(v.y);
 
21
        if(x < 0 || y < 0 || x >= ssize || y >= ssize) return -1;
 
22
        sqr *s = S(x, y);
 
23
        float floor = s->floor, ceil = s->ceil;
 
24
        if(s->type==FHF) floor -= s->vdelta/4.0f;
 
25
        if(s->type==CHF) ceil += s->vdelta/4.0f;
 
26
        if(SOLID(s) || v.z < floor || v.z > ceil)
 
27
        {
 
28
            if((!dx && !dy) || s->wtex==DEFAULT_SKY || (!SOLID(s) && v.z > ceil && s->ctex==DEFAULT_SKY)) return -1;
 
29
            if(s->type!=CORNER)// && s->type!=FHF && s->type!=CHF)
 
30
            {
 
31
                if(dx<dy) surface.x = ray.x>0 ? -1 : 1;
 
32
                else surface.y = ray.y>0 ? -1 : 1;
 
33
                sqr *n = S(x+(int)surface.x, y+(int)surface.y);
 
34
                if(SOLID(n) || (v.z < floor && v.z < n->floor) || (v.z > ceil && v.z > n->ceil))
 
35
                {
 
36
                    surface = dx<dy ? vec(0, ray.y>0 ? -1 : 1, 0) : vec(ray.x>0 ? -1 : 1, 0, 0);
 
37
                    n = S(x+(int)surface.x, y+(int)surface.y);
 
38
                    if(SOLID(n) || (v.z < floor && v.z < n->floor) || (v.z > ceil && v.z > n->ceil))
 
39
                        surface = vec(0, 0, ray.z>0 ? -1 : 1);
 
40
                }
 
41
            }
 
42
            dist = max(dist-0.1f, 0.0f);
 
43
            break;
 
44
        }
 
45
        dx = ray.x ? (x + (ray.x > 0 ? 1 : 0) - v.x)/ray.x : 1e16f;
 
46
        dy = ray.y ? (y + (ray.y > 0 ? 1 : 0) - v.y)/ray.y : 1e16f;
 
47
        dz = ray.z ? ((ray.z > 0 ? ceil : floor) - v.z)/ray.z : 1e16f;
 
48
        if(dz < dx && dz < dy)
 
49
        {
 
50
            if(ray.z>0 && s->ctex==DEFAULT_SKY) return -1;
 
51
            if(s->type!=FHF && s->type!=CHF) surface.z = ray.z>0 ? -1 : 1;
 
52
            dist += dz;
 
53
            break;
 
54
        }
 
55
        float disttonext = 0.1f + min(dx, dy);
 
56
        v.add(vec(ray).mul(disttonext));
 
57
        dist += disttonext;
 
58
    }
 
59
    return dist;
 
60
}
 
61
 
 
62
bool raycubelos(const vec &from, const vec &to, float margin)
 
63
{
 
64
    vec dir(to);
 
65
    dir.sub(from);
 
66
    float limit = dir.magnitude();
 
67
    dir.mul(1.0f/limit);
 
68
    vec surface;
 
69
    float dist = raycube(from, dir, surface);
 
70
    return dist > max(limit - margin, 0.0f);
 
71
}
 
72
 
 
73
physent *hitplayer = NULL;
 
74
 
 
75
bool plcollide(physent *d, physent *o, float &headspace, float &hi, float &lo)          // collide with physent
 
76
{
 
77
    if(o->state!=CS_ALIVE || !o->cancollide) return true;
 
78
    const float r = o->radius+d->radius, dx = o->o.x-d->o.x, dy = o->o.y-d->o.y;
 
79
    const float deyeheight = d->eyeheight, oeyeheight = o->eyeheight;
 
80
    if(d->type==ENT_PLAYER && o->type==ENT_PLAYER ? dx*dx + dy*dy < r*r : fabs(dx)<r && fabs(dy)<r)
 
81
    {
 
82
        if(d->o.z-deyeheight<o->o.z-oeyeheight) { if(o->o.z-oeyeheight<hi) hi = o->o.z-oeyeheight-1; }
 
83
        else if(o->o.z+o->aboveeye>lo) lo = o->o.z+o->aboveeye+1;
 
84
 
 
85
        if(fabs(o->o.z-d->o.z)<o->aboveeye+deyeheight) { hitplayer = o; return false; }
 
86
        headspace = d->o.z-o->o.z-o->aboveeye-deyeheight;
 
87
        if(headspace<0) headspace = 10;
 
88
    }
 
89
    return true;
 
90
}
 
91
 
 
92
bool cornertest(int mip, int x, int y, int dx, int dy, int &bx, int &by, int &bs)    // recursively collide with a mipmapped corner cube
 
93
{
 
94
    sqr *w = wmip[mip];
 
95
    int mfactor = sfactor - mip;
 
96
    bool stest = SOLID(SWS(w, x+dx, y, mfactor)) && SOLID(SWS(w, x, y+dy, mfactor));
 
97
    mip++;
 
98
    x /= 2;
 
99
    y /= 2;
 
100
    if(SWS(wmip[mip], x, y, mfactor-1)->type==CORNER)
 
101
    {
 
102
        bx = x<<mip;
 
103
        by = y<<mip;
 
104
        bs = 1<<mip;
 
105
        return cornertest(mip, x, y, dx, dy, bx, by, bs);
 
106
    }
 
107
    return stest;
 
108
}
 
109
 
 
110
bool mmcollide(physent *d, float &hi, float &lo)           // collide with a mapmodel
 
111
{
 
112
    const float eyeheight = d->eyeheight;
 
113
    const float playerheight = eyeheight + d->aboveeye;
 
114
    loopv(ents)
 
115
    {
 
116
        entity &e = ents[i];
 
117
        if(e.type==CLIP)
 
118
        {
 
119
            if(fabs(e.x-d->o.x) < e.attr2 + d->radius && fabs(e.y-d->o.y) < e.attr3 + d->radius)
 
120
            {
 
121
                const float cz = float(S(e.x, e.y)->floor+e.attr1), ch = float(e.attr4);
 
122
                const float dz = d->o.z-d->eyeheight;
 
123
                if(dz < cz - 0.001) { if(cz<hi) hi = cz; }
 
124
                else if(cz+ch>lo) lo = cz+ch;
 
125
                if(hi-lo < playerheight) return false;
 
126
            }
 
127
        }
 
128
        else if(e.type==MAPMODEL)
 
129
        {
 
130
            mapmodelinfo &mmi = getmminfo(e.attr2);
 
131
            if(!&mmi || !mmi.h) continue;
 
132
            const float r = mmi.rad+d->radius;
 
133
            if(fabs(e.x-d->o.x)<r && fabs(e.y-d->o.y)<r)
 
134
            {
 
135
                const float mmz = float(S(e.x, e.y)->floor+mmi.zoff+e.attr3);
 
136
                const float dz = d->o.z-eyeheight;
 
137
                if(dz<mmz) { if(mmz<hi) hi = mmz; }
 
138
                else if(mmz+mmi.h>lo) lo = mmz+mmi.h;
 
139
                if(hi-lo < playerheight) return false;
 
140
            }
 
141
        }
 
142
    }
 
143
    return true;
 
144
}
 
145
 
 
146
bool objcollide(physent *d, const vec &objpos, float objrad, float objheight) // collide with custom/typeless objects
 
147
{
 
148
    const float r = d->radius+objrad;
 
149
    if(fabs(objpos.x-d->o.x)<r && fabs(objpos.y-d->o.y)<r)
 
150
    {
 
151
        const float maxdist = (d->eyeheight+d->aboveeye+objheight)/2.0f;
 
152
        const float dz = d->o.z+(-d->eyeheight+d->aboveeye)/2.0f;
 
153
        const float objz = objpos.z+objheight/2.0f;
 
154
        return dz-objz <= maxdist && dz-objz >= -maxdist;
 
155
    }
 
156
    return false;
 
157
}
 
158
 
 
159
// all collision happens here
 
160
// spawn is a dirty side effect used in spawning
 
161
// drop & rise are supplied by the physics below to indicate gravity/push for current mini-timestep
 
162
static int cornersurface = 0;
 
163
 
 
164
bool collide(physent *d, bool spawn, float drop, float rise)
 
165
{
 
166
    cornersurface = 0;
 
167
    const float fx1 = d->o.x-d->radius;     // figure out integer cube rectangle this entity covers in map
 
168
    const float fy1 = d->o.y-d->radius;
 
169
    const float fx2 = d->o.x+d->radius;
 
170
    const float fy2 = d->o.y+d->radius;
 
171
    const int x1 = int(fx1);
 
172
    const int y1 = int(fy1);
 
173
    const int x2 = int(fx2);
 
174
    const int y2 = int(fy2);
 
175
    float hi = 127, lo = -128;
 
176
    const float eyeheight = d->eyeheight;
 
177
    const float playerheight = eyeheight + d->aboveeye;
 
178
 
 
179
    for(int y = y1; y<=y2; y++) for(int x = x1; x<=x2; x++)     // collide with map
 
180
    {
 
181
        if(OUTBORD(x,y)) return false;
 
182
        sqr *s = S(x,y);
 
183
        float ceil = s->ceil;
 
184
        float floor = s->floor;
 
185
        switch(s->type)
 
186
        {
 
187
            case SOLID:
 
188
                return false;
 
189
 
 
190
            case CORNER:
 
191
            {
 
192
                int bx = x, by = y, bs = 1;
 
193
                cornersurface = 1;
 
194
                if((x==x1 && y==y2 && cornertest(0, x, y, -1,  1, bx, by, bs) && fx1-bx<=fy2-by)
 
195
                || (x==x2 && y==y1 && cornertest(0, x, y,  1, -1, bx, by, bs) && fx2-bx>=fy1-by) || !(++cornersurface)
 
196
                || (x==x1 && y==y1 && cornertest(0, x, y, -1, -1, bx, by, bs) && fx1-bx+fy1-by<=bs)
 
197
                || (x==x2 && y==y2 && cornertest(0, x, y,  1,  1, bx, by, bs) && fx2-bx+fy2-by>=bs))
 
198
                    return false;
 
199
                cornersurface = 0;
 
200
                break;
 
201
            }
 
202
 
 
203
            case FHF:       // FIXME: too simplistic collision with slopes, makes it feels like tiny stairs
 
204
                floor -= (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
 
205
                break;
 
206
 
 
207
            case CHF:
 
208
                ceil += (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
 
209
 
 
210
        }
 
211
        if(ceil<hi) hi = ceil;
 
212
        if(floor>lo) lo = floor;
 
213
    }
 
214
 
 
215
    if(hi-lo < playerheight) return false;
 
216
 
 
217
    float headspace = 10;
 
218
 
 
219
    if(d->type!=ENT_CAMERA)
 
220
    {
 
221
        loopv(players)       // collide with other players
 
222
        {
 
223
            playerent *o = players[i];
 
224
            if(!o || o==d || (o==player1 && d->type==ENT_CAMERA)) continue;
 
225
            if(!plcollide(d, o, headspace, hi, lo)) return false;
 
226
        }
 
227
        if(d!=player1) if(!plcollide(d, player1, headspace, hi, lo)) return false;
 
228
    }
 
229
 
 
230
    headspace -= 0.01f;
 
231
    if(!mmcollide(d, hi, lo)) return false;    // collide with map models
 
232
 
 
233
    if(spawn)
 
234
    {
 
235
        d->o.z = lo+eyeheight;       // just drop to floor (sideeffect)
 
236
        d->onfloor = true;
 
237
    }
 
238
    else
 
239
    {
 
240
        const float spacelo = d->o.z-eyeheight-lo;
 
241
        if(spacelo<0)
 
242
        {
 
243
            if(spacelo>-0.01)
 
244
            {
 
245
                d->o.z = lo+eyeheight;   // stick on step
 
246
            }
 
247
            else if(spacelo>-1.26f && d->type!=ENT_BOUNCE) d->o.z += rise;       // rise thru stair
 
248
            else return false;
 
249
        }
 
250
        else
 
251
        {
 
252
            d->o.z -= min(min(drop, spacelo), headspace);       // gravity
 
253
        }
 
254
 
 
255
        const float spacehi = hi-(d->o.z+d->aboveeye);
 
256
        if(spacehi<0)
 
257
        {
 
258
            if(spacehi<-0.1) return false;     // hack alert!
 
259
            if(spacelo>0.1f) d->o.z = hi-d->aboveeye; // glue to ceiling if in midair
 
260
            d->vel.z = 0;                     // cancel out jumping velocity
 
261
        }
 
262
 
 
263
        const float floorclamp = d->crouching ? 0.1f : 0.01f;
 
264
        d->onfloor = d->o.z-eyeheight-lo < floorclamp;
 
265
    }
 
266
    return true;
 
267
}
 
268
 
 
269
VARP(maxroll, 0, 0, 20); // note: when changing max value, fix network transmission
 
270
VAR(recoilbackfade, 0, 100, 1000);
 
271
 
 
272
void resizephysent(physent *pl, int moveres, int curtime, float min, float max)
 
273
{
 
274
    if(pl->eyeheightvel==0.0f) return;
 
275
 
 
276
    const bool water = hdr.waterlevel>pl->o.z;
 
277
    const float speed = curtime*pl->maxspeed/(water ? 2000.0f : 1000.0f);
 
278
    float h = pl->eyeheightvel * speed / moveres;
 
279
 
 
280
        loopi(moveres)
 
281
    {
 
282
        pl->eyeheight += h;
 
283
        pl->o.z += h;
 
284
        if(!collide(pl))
 
285
        {
 
286
            pl->eyeheight -= h; // collided, revert mini-step
 
287
            pl->o.z -= h;
 
288
            break;
 
289
        }
 
290
        if(pl->eyeheight<min) // clamp to min
 
291
        {
 
292
            pl->o.z += min - pl->eyeheight;
 
293
            pl->eyeheight = min;
 
294
            pl->eyeheightvel = 0.0f;
 
295
            break;
 
296
        }
 
297
        if(pl->eyeheight>max)
 
298
        {
 
299
            pl->o.z -= pl->eyeheight - max;
 
300
            pl->eyeheight = max;
 
301
            pl->eyeheightvel = 0.0f;
 
302
            break;
 
303
        }
 
304
    }
 
305
}
 
306
 
 
307
// main physics routine, moves a player/monster for a curtime step
 
308
// moveres indicated the physics precision (which is lower for monsters and multiplayer prediction)
 
309
// local is false for multiplayer prediction
 
310
 
 
311
void clamproll(physent *pl)
 
312
{
 
313
    if(pl->roll > maxroll) pl->roll = maxroll;
 
314
    else if(pl->roll < -maxroll) pl->roll = -maxroll;
 
315
}
 
316
 
 
317
void moveplayer(physent *pl, int moveres, bool local, int curtime)
 
318
{
 
319
    bool water = false;
 
320
    const bool editfly = pl->state==CS_EDITING;
 
321
    const bool specfly = pl->type==ENT_PLAYER && ((playerent *)pl)->spectatemode==SM_FLY;
 
322
 
 
323
    vec d;      // vector of direction we ideally want to move in
 
324
 
 
325
    float drop = 0, rise = 0;
 
326
 
 
327
    if(pl->type==ENT_BOUNCE)
 
328
    {
 
329
        bounceent* bounce = (bounceent *) pl;
 
330
        water = hdr.waterlevel>pl->o.z;
 
331
 
 
332
        const float speed = curtime*pl->maxspeed/(water ? 2000.0f : 1000.0f);
 
333
        const float friction = water ? 20.0f : (pl->onfloor || editfly || specfly ? 6.0f : 30.0f);
 
334
        const float fpsfric = max(friction*20.0f/curtime, 1.0f);
 
335
 
 
336
        if(pl->onfloor) // apply friction
 
337
        {
 
338
                pl->vel.mul(fpsfric-1);
 
339
                pl->vel.div(fpsfric);
 
340
        }
 
341
        else // apply gravity
 
342
        {
 
343
            const float CUBES_PER_METER = 4; // assumes 4 cubes make up 1 meter
 
344
            const float BOUNCE_MASS = 0.5f; // sane default mass of 0.5 kg
 
345
            const float GRAVITY = BOUNCE_MASS*9.81f/CUBES_PER_METER/1000.0f;
 
346
            bounce->vel.z -= GRAVITY*curtime;
 
347
        }
 
348
 
 
349
            d = bounce->vel;
 
350
            d.mul(speed);
 
351
        if(water) d.div(6.0f); // incorrect
 
352
 
 
353
        // rotate
 
354
        float rotspeed = bounce->rotspeed*d.magnitude();
 
355
        pl->pitch = fmod(pl->pitch+rotspeed, 360.0f);
 
356
        pl->yaw = fmod(pl->yaw+rotspeed, 360.0f);
 
357
    }
 
358
    else // fake physics for player ents to create _the_ cube movement (tm)
 
359
    {
 
360
        const int timeinair = pl->timeinair;
 
361
        int move = pl->onladder && !pl->onfloor && pl->move == -1 ? 0 : pl->move; // movement on ladder
 
362
        water = hdr.waterlevel>pl->o.z-0.5f;
 
363
 
 
364
        const bool crouching = pl->crouching || pl->eyeheight < pl->maxeyeheight;
 
365
        const float speed = curtime/(water ? 2000.0f : 1000.0f)*pl->maxspeed*(crouching ? 0.4f : 1.0f)*(specfly ? 2.0f : 1.0f);
 
366
        const float friction = water ? 20.0f : (pl->onfloor || editfly || specfly ? 6.0f : (pl->onladder ? 1.5f : 30.0f));
 
367
        const float fpsfric = max(friction/curtime*20.0f, 1.0f);
 
368
 
 
369
        d.x = (float)(move*cosf(RAD*(pl->yaw-90)));
 
370
        d.y = (float)(move*sinf(RAD*(pl->yaw-90)));
 
371
        d.z = 0.0f;
 
372
 
 
373
        if(editfly || specfly || water)
 
374
        {
 
375
            d.x *= (float)cosf(RAD*(pl->pitch));
 
376
            d.y *= (float)cosf(RAD*(pl->pitch));
 
377
            d.z = (float)(move*sinf(RAD*(pl->pitch)));
 
378
        }
 
379
 
 
380
        d.x += (float)(pl->strafe*cosf(RAD*(pl->yaw-180)));
 
381
        d.y += (float)(pl->strafe*sinf(RAD*(pl->yaw-180)));
 
382
 
 
383
            pl->vel.mul(fpsfric-1);   // slowly apply friction and direction to velocity, gives a smooth movement
 
384
            pl->vel.add(d);
 
385
            pl->vel.div(fpsfric);
 
386
        d = pl->vel;
 
387
            d.mul(speed);
 
388
 
 
389
        if(editfly)                // just apply velocity
 
390
        {
 
391
            pl->o.add(d);
 
392
            if(pl->jumpnext)
 
393
            {
 
394
                pl->jumpnext = false;
 
395
                pl->vel.z = 2;
 
396
            }
 
397
        }
 
398
        else if(specfly)
 
399
        {
 
400
            rise = speed/moveres/1.2f;
 
401
            if(pl->jumpnext)
 
402
            {
 
403
                pl->jumpnext = false;
 
404
                pl->vel.z = 2;
 
405
            }
 
406
        }
 
407
        else                        // apply velocity with collisions
 
408
        {
 
409
            if(pl->type!=ENT_CAMERA)
 
410
            {
 
411
                if(pl->onladder)
 
412
                {
 
413
                                const float climbspeed = 1.0f;
 
414
 
 
415
                                if(pl->type==ENT_BOT) pl->vel.z = climbspeed; // bots climb upwards only
 
416
                    else if(pl->type==ENT_PLAYER)
 
417
                    {
 
418
                        if(((playerent *)pl)->k_up) pl->vel.z = climbspeed;
 
419
                        else if(((playerent *)pl)->k_down) pl->vel.z = -climbspeed;
 
420
                    }
 
421
                    pl->timeinair = 0;
 
422
                }
 
423
                else
 
424
                {
 
425
                    if(pl->onfloor || water)
 
426
                    {
 
427
                        if(pl->jumpnext)
 
428
                        {
 
429
                            pl->jumpnext = false;
 
430
                            pl->vel.z = 2.0f;                                  // physics impulse upwards
 
431
                            if(water) { pl->vel.x /= 8; pl->vel.y /= 8; }      // dampen velocity change even harder, gives correct water feel
 
432
                            else if(pl==player1 || pl->type!=ENT_PLAYER) playsoundc(S_JUMP, pl);
 
433
                        }
 
434
                        pl->timeinair = 0;
 
435
                    }
 
436
                    else
 
437
                    {
 
438
                        pl->timeinair += curtime;
 
439
                    }
 
440
                }
 
441
 
 
442
                if(timeinair > 200 && !pl->timeinair)
 
443
                {
 
444
                    int sound = timeinair > 800 ? S_HARDLAND : S_SOFTLAND;
 
445
                    if(pl->state!=CS_DEAD)
 
446
                    {
 
447
                        if(pl==player1 || pl->type!=ENT_PLAYER) playsoundc(sound, pl);
 
448
                    }
 
449
                }
 
450
            }
 
451
 
 
452
            const float gravity = 20.0f;
 
453
            float dropf = (gravity-1)+pl->timeinair/15.0f;                 // incorrect, but works fine
 
454
            if(water) { dropf = 5; pl->timeinair = 0; }            // float slowly down in water
 
455
            if(pl->onladder) { dropf = 0; pl->timeinair = 0; }
 
456
 
 
457
            drop = dropf*curtime/gravity/100/moveres;                       // at high fps, gravity kicks in too fast
 
458
            rise = speed/moveres/1.2f;                                              // extra smoothness when lifting up stairs
 
459
            if(pl->maxspeed-16.0f>0.5f) pl += 0xF0F0;
 
460
        }
 
461
    }
 
462
 
 
463
    bool collided = false;
 
464
    vec oldorigin = pl->o;
 
465
 
 
466
        if(!editfly) loopi(moveres)                                // discrete steps collision detection & sliding
 
467
    {
 
468
        const float f = 1.0f/moveres;
 
469
 
 
470
        // try move forward
 
471
        pl->o.x += f*d.x;
 
472
        pl->o.y += f*d.y;
 
473
        pl->o.z += f*d.z;
 
474
        hitplayer = NULL;
 
475
        if(collide(pl, false, drop, rise)) continue;
 
476
        else collided = true;
 
477
        if(pl->type==ENT_BOUNCE && cornersurface)
 
478
        { // try corner bounce
 
479
            float ct2f = cornersurface == 2 ? -1.0 : 1.0;
 
480
            vec oo = pl->o, xd = d;
 
481
            xd.x = d.y * ct2f;
 
482
            xd.y = d.x * ct2f;
 
483
            pl->o.x += f * (-d.x + xd.x);
 
484
            pl->o.y += f * (-d.y + xd.y);
 
485
            if(collide(pl, false, drop, rise))
 
486
            {
 
487
                d = xd;
 
488
                float sw = pl->vel.x * ct2f;
 
489
                pl->vel.x = pl->vel.y * ct2f;
 
490
                pl->vel.y = sw;
 
491
                pl->vel.mul(0.7f);
 
492
                continue;
 
493
            }
 
494
            pl->o == oo;
 
495
        }
 
496
        if(pl->type==ENT_CAMERA || (pl->type==ENT_PLAYER && pl->state==CS_DEAD && ((playerent *)pl)->spectatemode != SM_FLY))
 
497
        {
 
498
            pl->o.x -= f*d.x;
 
499
            pl->o.y -= f*d.y;
 
500
            pl->o.z -= f*d.z;
 
501
            break;
 
502
        }
 
503
        if(pl->type==ENT_PLAYER && hitplayer)
 
504
        {
 
505
            float dx = hitplayer->o.x-pl->o.x, dy = hitplayer->o.y-pl->o.y,
 
506
                  push = (dx*d.x + dy*d.y)/max(dx*dx + dy*dy, 1e-3f),
 
507
                  px = push*dx, py = push*dy;
 
508
            pl->o.x -= f*px;
 
509
            pl->o.y -= f*py;
 
510
            if(collide(pl, false, drop, rise)) continue;
 
511
            pl->o.x += f*px;
 
512
            pl->o.y += f*py;
 
513
        }
 
514
        // player stuck, try slide along y axis
 
515
        pl->o.x -= f*d.x;
 
516
        if(collide(pl, false, drop, rise))
 
517
        {
 
518
            d.x = 0;
 
519
                        if(pl->type==ENT_BOUNCE) { pl->vel.x = -pl->vel.x; pl->vel.mul(0.7f); }
 
520
            continue;
 
521
        }
 
522
        pl->o.x += f*d.x;
 
523
        // still stuck, try x axis
 
524
        pl->o.y -= f*d.y;
 
525
        if(collide(pl, false, drop, rise))
 
526
        {
 
527
            d.y = 0;
 
528
                        if(pl->type==ENT_BOUNCE) { pl->vel.y = -pl->vel.y; pl->vel.mul(0.7f); }
 
529
            continue;
 
530
        }
 
531
        pl->o.y += f*d.y;
 
532
        // try just dropping down
 
533
        pl->o.x -= f*d.x;
 
534
        pl->o.y -= f*d.y;
 
535
        if(collide(pl, false, drop, rise))
 
536
        {
 
537
            d.y = d.x = 0;
 
538
            continue;
 
539
        }
 
540
        pl->o.z -= f*d.z;
 
541
        if(pl->type==ENT_BOUNCE) { pl->vel.z = -pl->vel.z; pl->vel.mul(0.5f); }
 
542
        break;
 
543
        }
 
544
 
 
545
    pl->stuck = (oldorigin==pl->o);
 
546
    if(collided) pl->oncollision();
 
547
    else pl->onmoved(oldorigin.sub(pl->o));
 
548
 
 
549
    if(pl->type==ENT_CAMERA) return;
 
550
 
 
551
    if(pl->type!=ENT_BOUNCE && pl==player1)
 
552
    {
 
553
        // automatically apply smooth roll when strafing
 
554
        if(pl->strafe==0)
 
555
        {
 
556
            pl->roll = pl->roll/(1+(float)sqrt((float)curtime)/25);
 
557
        }
 
558
        else
 
559
        {
 
560
            pl->roll += pl->strafe*curtime/-30.0f;
 
561
            clamproll(pl);
 
562
        }
 
563
 
 
564
        // smooth pitch
 
565
        const float fric = 6.0f/curtime*20.0f;
 
566
        pl->pitch += pl->pitchvel*(curtime/1000.0f)*pl->maxspeed*(pl->crouching ? 0.75f : 1.0f);
 
567
        pl->pitchvel *= fric-3;
 
568
        pl->pitchvel /= fric;
 
569
        extern int recoiltest;
 
570
        if(recoiltest)
 
571
        {
 
572
            if(pl->pitchvel < 0.05f && pl->pitchvel > 0.001f) pl->pitchvel -= recoilbackfade/100.0f; // slide back
 
573
        }
 
574
        else if(pl->pitchvel < 0.05f && pl->pitchvel > 0.001f) pl->pitchvel -= ((playerent *)pl)->weaponsel->info.recoilbackfade/100.0f; // slide back
 
575
        if(pl->pitchvel) fixcamerarange(pl); // fix pitch if necessary
 
576
    }
 
577
 
 
578
    // play sounds on water transitions
 
579
    if(pl->type!=ENT_CAMERA)
 
580
    {
 
581
        if(!pl->inwater && water)
 
582
        {
 
583
            if(!pl->lastsplash || lastmillis-pl->lastsplash>500)
 
584
            {
 
585
                playsound(S_SPLASH2, pl);
 
586
                pl->lastsplash = lastmillis;
 
587
            }
 
588
            if(pl==player1) pl->vel.z = 0;
 
589
        }
 
590
        else if(pl->inwater && !water) playsound(S_SPLASH1, &pl->o);
 
591
        pl->inwater = water;
 
592
    }
 
593
 
 
594
    // store previous locations of all players/bots
 
595
    if(pl->type==ENT_PLAYER || pl->type==ENT_BOT)
 
596
    {
 
597
        ((playerent *)pl)->history.update(pl->o, lastmillis);
 
598
    }
 
599
 
 
600
    // apply volume-resize when crouching
 
601
    if(pl->type==ENT_PLAYER)
 
602
    {
 
603
        if(pl==player1 && !(intermission || player1->onladder || (pl->trycrouch && !player1->onfloor && player1->timeinair > 50))) updatecrouch(player1, player1->trycrouch);
 
604
        const float croucheyeheight = pl->maxeyeheight*3.0f/4.0f;
 
605
        resizephysent(pl, moveres, curtime, croucheyeheight, pl->maxeyeheight);
 
606
    }
 
607
}
 
608
 
 
609
const int PHYSFPS = 200;
 
610
const int PHYSFRAMETIME = 1000 / PHYSFPS;
 
611
int physsteps = 0, physframetime = PHYSFRAMETIME, lastphysframe = 0;
 
612
 
 
613
void physicsframe()          // optimally schedule physics frames inside the graphics frames
 
614
{
 
615
    int diff = lastmillis - lastphysframe;
 
616
    if(diff <= 0) physsteps = 0;
 
617
    else
 
618
    {
 
619
        extern int gamespeed;
 
620
        physframetime = clamp((PHYSFRAMETIME*gamespeed)/100, 1, PHYSFRAMETIME);
 
621
        physsteps = (diff + physframetime - 1)/physframetime;
 
622
        lastphysframe += physsteps * physframetime;
 
623
    }
 
624
}
 
625
 
 
626
VAR(physinterp, 0, 1, 1);
 
627
 
 
628
void interppos(physent *pl)
 
629
{
 
630
    pl->o = pl->newpos;
 
631
    pl->o.z += pl->eyeheight;
 
632
 
 
633
    int diff = lastphysframe - lastmillis;
 
634
    if(diff <= 0 || !physinterp) return;
 
635
 
 
636
    vec deltapos(pl->deltapos);
 
637
    deltapos.mul(min(diff, physframetime)/float(physframetime));
 
638
    pl->o.add(deltapos);
 
639
}
 
640
 
 
641
void moveplayer(physent *pl, int moveres, bool local)
 
642
{
 
643
    if(physsteps <= 0)
 
644
    {
 
645
        if(local) interppos(pl);
 
646
        return;
 
647
    }
 
648
 
 
649
    if(local)
 
650
    {
 
651
        pl->o = pl->newpos;
 
652
        pl->o.z += pl->eyeheight;
 
653
    }
 
654
    loopi(physsteps-1) moveplayer(pl, moveres, local, physframetime);
 
655
    if(local) pl->deltapos = pl->o;
 
656
    moveplayer(pl, moveres, local, physframetime);
 
657
    if(local)
 
658
    {
 
659
        pl->newpos = pl->o;
 
660
        pl->deltapos.sub(pl->newpos);
 
661
        pl->newpos.z -= pl->eyeheight;
 
662
        interppos(pl);
 
663
    }
 
664
}
 
665
 
 
666
void movebounceent(bounceent *p, int moveres, bool local)
 
667
{
 
668
    moveplayer(p, moveres, local);
 
669
}
 
670
 
 
671
// movement input code
 
672
 
 
673
#define dir(name,v,d,s,os) void name(bool isdown) { player1->s = isdown; player1->v = isdown ? d : (player1->os ? -(d) : 0); player1->lastmove = lastmillis; }
 
674
 
 
675
dir(backward, move,   -1, k_down,  k_up)
 
676
dir(forward,  move,    1, k_up,    k_down)
 
677
dir(left,     strafe,  1, k_left,  k_right)
 
678
dir(right,    strafe, -1, k_right, k_left)
 
679
 
 
680
void attack(bool on)
 
681
{
 
682
    if(intermission) return;
 
683
    if(editmode) editdrag(on);
 
684
    else if(player1->state==CS_DEAD)
 
685
    {
 
686
        if(!on) tryrespawn();
 
687
    }
 
688
    else player1->attacking = on;
 
689
}
 
690
 
 
691
void jumpn(bool on)
 
692
{
 
693
    if(intermission) return;
 
694
    if(player1->isspectating())
 
695
    {
 
696
        if(lastmillis - player1->respawnoffset > 1000 && on) togglespect();
 
697
    }
 
698
    else if(player1->crouching) return;
 
699
    else player1->jumpnext = on;
 
700
}
 
701
 
 
702
void updatecrouch(playerent *p, bool on)
 
703
{
 
704
    if(p->crouching == on) return;
 
705
    const float crouchspeed = 0.6f;
 
706
    p->crouching = on;
 
707
    p->eyeheightvel = on ? -crouchspeed : crouchspeed;
 
708
    if(p==player1) playsoundc(on ? S_CROUCH : S_UNCROUCH);
 
709
}
 
710
 
 
711
void crouch(bool on)
 
712
{
 
713
    if(player1->isspectating()) return;
 
714
    player1->trycrouch = on;
 
715
}
 
716
 
 
717
COMMAND(backward, ARG_DOWN);
 
718
COMMAND(forward, ARG_DOWN);
 
719
COMMAND(left, ARG_DOWN);
 
720
COMMAND(right, ARG_DOWN);
 
721
COMMANDN(jump, jumpn, ARG_DOWN);
 
722
COMMAND(attack, ARG_DOWN);
 
723
COMMAND(crouch, ARG_DOWN);
 
724
 
 
725
void fixcamerarange(physent *cam)
 
726
{
 
727
    const float MAXPITCH = 90.0f;
 
728
    if(cam->pitch>MAXPITCH) cam->pitch = MAXPITCH;
 
729
    if(cam->pitch<-MAXPITCH) cam->pitch = -MAXPITCH;
 
730
    while(cam->yaw<0.0f) cam->yaw += 360.0f;
 
731
    while(cam->yaw>=360.0f) cam->yaw -= 360.0f;
 
732
}
 
733
 
 
734
FVARP(sensitivity, 1e-3f, 3.0f, 1000.0f);
 
735
VARP(invmouse, 0, 0, 1);
 
736
 
 
737
void mousemove(int dx, int dy)
 
738
{
 
739
    if(intermission) return;
 
740
    if(player1->isspectating() && player1->spectatemode==SM_FOLLOW1ST) return;
 
741
 
 
742
    const float SENSF = 33.0f;     // try match quake sens
 
743
    camera1->yaw += (dx/SENSF)*sensitivity;
 
744
    camera1->pitch -= (dy/SENSF)*sensitivity*(invmouse ? -1 : 1);
 
745
    fixcamerarange();
 
746
    if(camera1!=player1 && player1->spectatemode!=SM_DEATHCAM)
 
747
    {
 
748
        player1->yaw = camera1->yaw;
 
749
        player1->pitch = camera1->pitch;
 
750
    }
 
751
}
 
752
 
 
753
void entinmap(physent *d)    // brute force but effective way to find a free spawn spot in the map
 
754
{
 
755
    vec orig(d->o);
 
756
    loopi(100)              // try max 100 times
 
757
    {
 
758
        float dx = (rnd(21)-10)/10.0f*i;  // increasing distance
 
759
        float dy = (rnd(21)-10)/10.0f*i;
 
760
        d->o.x += dx;
 
761
        d->o.y += dy;
 
762
        if(collide(d, true))
 
763
        {
 
764
            d->resetinterp();
 
765
            return;
 
766
        }
 
767
        d->o = orig;
 
768
    }
 
769
    // leave ent at original pos, possibly stuck
 
770
    d->resetinterp();
 
771
    conoutf("can't find entity spawn spot! (%d, %d)", d->o.x, d->o.y);
 
772
}
 
773