summaryrefslogtreecommitdiffstats
path: root/src/jake2/client/M.java
blob: dd6e853ea8b4d207260afdb010c1c1ce83643e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * M.java
 * Copyright (C) 2003
 * 
 * $Id: M.java,v 1.9 2006-01-21 21:53:32 salomo Exp $
 */
/*
 Copyright (C) 1997-2001 Id Software, Inc.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 */
package jake2.client;

import jake2.Defines;
import jake2.Globals;
import jake2.game.*;
import jake2.server.SV;
import jake2.util.Lib;
import jake2.util.Math3D;

/**
 * M
 */
public final class M {

    public static void M_CheckGround(edict_t ent) {
        float[] point = { 0, 0, 0 };
        trace_t trace;

        if ((ent.flags & (Defines.FL_SWIM | Defines.FL_FLY)) != 0)
            return;

        if (ent.velocity[2] > 100) {
            ent.groundentity = null;
            return;
        }

        // if the hull point one-quarter unit down is solid the entity is on
        // ground
        point[0] = ent.s.origin[0];
        point[1] = ent.s.origin[1];
        point[2] = ent.s.origin[2] - 0.25f;

        trace = GameBase.gi.trace(ent.s.origin, ent.mins, ent.maxs, point, ent,
                Defines.MASK_MONSTERSOLID);

        // check steepness
        if (trace.plane.normal[2] < 0.7 && !trace.startsolid) {
            ent.groundentity = null;
            return;
        }

        // ent.groundentity = trace.ent;
        // ent.groundentity_linkcount = trace.ent.linkcount;
        // if (!trace.startsolid && !trace.allsolid)
        //   VectorCopy (trace.endpos, ent.s.origin);
        if (!trace.startsolid && !trace.allsolid) {
            Math3D.VectorCopy(trace.endpos, ent.s.origin);
            ent.groundentity = trace.ent;
            ent.groundentity_linkcount = trace.ent.linkcount;
            ent.velocity[2] = 0;
        }
    }
    
    /**
     * Returns false if any part of the bottom of the entity is off an edge that
     * is not a staircase.
     */

    public static boolean M_CheckBottom(edict_t ent) {
        float[] mins = { 0, 0, 0 };
        float[] maxs = { 0, 0, 0 };
        float[] start = { 0, 0, 0 };
        float[] stop = { 0, 0, 0 };

        trace_t trace;
        int x, y;
        float mid, bottom;

        Math3D.VectorAdd(ent.s.origin, ent.mins, mins);
        Math3D.VectorAdd(ent.s.origin, ent.maxs, maxs);

        //	   if all of the points under the corners are solid world, don't bother
        //	   with the tougher checks
        //	   the corners must be within 16 of the midpoint
        start[2] = mins[2] - 1;
        for (x = 0; x <= 1; x++)
            for (y = 0; y <= 1; y++) {
                start[0] = x != 0 ? maxs[0] : mins[0];
                start[1] = y != 0 ? maxs[1] : mins[1];
                if (GameBase.gi.pointcontents.pointcontents(start) != Defines.CONTENTS_SOLID) {
                    GameBase.c_no++;
                    //
                    //	   check it for real...
                    //
                    start[2] = mins[2];

                    //	   the midpoint must be within 16 of the bottom
                    start[0] = stop[0] = (mins[0] + maxs[0]) * 0.5f;
                    start[1] = stop[1] = (mins[1] + maxs[1]) * 0.5f;
                    stop[2] = start[2] - 2 * GameBase.STEPSIZE;
                    trace = GameBase.gi.trace(start, Globals.vec3_origin,
                            Globals.vec3_origin, stop, ent,
                            Defines.MASK_MONSTERSOLID);

                    if (trace.fraction == 1.0)
                        return false;
                    mid = bottom = trace.endpos[2];

                    //	   the corners must be within 16 of the midpoint
                    for (x = 0; x <= 1; x++)
                        for (y = 0; y <= 1; y++) {
                            start[0] = stop[0] = x != 0 ? maxs[0] : mins[0];
                            start[1] = stop[1] = y != 0 ? maxs[1] : mins[1];

                            trace = GameBase.gi.trace(start,
                                    Globals.vec3_origin, Globals.vec3_origin,
                                    stop, ent, Defines.MASK_MONSTERSOLID);

                            if (trace.fraction != 1.0
                                    && trace.endpos[2] > bottom)
                                bottom = trace.endpos[2];
                            if (trace.fraction == 1.0
                                    || mid - trace.endpos[2] > GameBase.STEPSIZE)
                                return false;
                        }

                    GameBase.c_yes++;
                    return true;
                }
            }

        GameBase.c_yes++;
        return true; // we got out easy
    }

    /** 
     * M_ChangeYaw.
     */
    public static void M_ChangeYaw(edict_t ent) {
        float ideal;
        float current;
        float move;
        float speed;

        current = Math3D.anglemod(ent.s.angles[Defines.YAW]);
        ideal = ent.ideal_yaw;

        if (current == ideal)
            return;

        move = ideal - current;
        speed = ent.yaw_speed;
        if (ideal > current) {
            if (move >= 180)
                move = move - 360;
        } else {
            if (move <= -180)
                move = move + 360;
        }
        if (move > 0) {
            if (move > speed)
                move = speed;
        } else {
            if (move < -speed)
                move = -speed;
        }

        ent.s.angles[Defines.YAW] = Math3D.anglemod(current + move);
    }

    /**
     * M_MoveToGoal.
     */
    public static void M_MoveToGoal(edict_t ent, float dist) {
        edict_t goal = ent.goalentity;

        if (ent.groundentity == null
                && (ent.flags & (Defines.FL_FLY | Defines.FL_SWIM)) == 0)
            return;

        //	   if the next step hits the enemy, return immediately
        if (ent.enemy != null && SV.SV_CloseEnough(ent, ent.enemy, dist))
            return;

        //	   bump around...
        if ((Lib.rand() & 3) == 1
                || !SV.SV_StepDirection(ent, ent.ideal_yaw, dist)) {
            if (ent.inuse)
                SV.SV_NewChaseDir(ent, goal, dist);
        }
    }

    /** 
     * M_walkmove.
     */
    public static boolean M_walkmove(edict_t ent, float yaw, float dist) {
        float[] move = { 0, 0, 0 };

        if ((ent.groundentity == null)
                && (ent.flags & (Defines.FL_FLY | Defines.FL_SWIM)) == 0)
            return false;

        yaw = (float) (yaw * Math.PI * 2 / 360);

        move[0] = (float) Math.cos(yaw) * dist;
        move[1] = (float) Math.sin(yaw) * dist;
        move[2] = 0;

        return SV.SV_movestep(ent, move, true);
    }

    public static void M_CatagorizePosition(edict_t ent) {
        float[] point = { 0, 0, 0 };
        int cont;

        //
        //	get waterlevel
        //
        point[0] = ent.s.origin[0];
        point[1] = ent.s.origin[1];
        point[2] = ent.s.origin[2] + ent.mins[2] + 1;
        cont = GameBase.gi.pointcontents.pointcontents(point);

        if (0 == (cont & Defines.MASK_WATER)) {
            ent.waterlevel = 0;
            ent.watertype = 0;
            return;
        }

        ent.watertype = cont;
        ent.waterlevel = 1;
        point[2] += 26;
        cont = GameBase.gi.pointcontents.pointcontents(point);
        if (0 == (cont & Defines.MASK_WATER))
            return;

        ent.waterlevel = 2;
        point[2] += 22;
        cont = GameBase.gi.pointcontents.pointcontents(point);
        if (0 != (cont & Defines.MASK_WATER))
            ent.waterlevel = 3;
    }

    public static void M_WorldEffects(edict_t ent) {
        int dmg;

        if (ent.health > 0) {
            if (0 == (ent.flags & Defines.FL_SWIM)) {
                if (ent.waterlevel < 3) {
                    ent.air_finished = GameBase.level.time + 12;
                } else if (ent.air_finished < GameBase.level.time) {
                    // drown!
                    if (ent.pain_debounce_time < GameBase.level.time) {
                        dmg = (int) (2f + 2f * Math.floor(GameBase.level.time
                                - ent.air_finished));
                        if (dmg > 15)
                            dmg = 15;
                        GameCombat.T_Damage(ent, GameBase.g_edicts[0],
                                GameBase.g_edicts[0], Globals.vec3_origin,
                                ent.s.origin, Globals.vec3_origin, dmg, 0,
                                Defines.DAMAGE_NO_ARMOR, Defines.MOD_WATER);
                        ent.pain_debounce_time = GameBase.level.time + 1;
                    }
                }
            } else {
                if (ent.waterlevel > 0) {
                    ent.air_finished = GameBase.level.time + 9;
                } else if (ent.air_finished < GameBase.level.time) {
                    // suffocate!
                    if (ent.pain_debounce_time < GameBase.level.time) {
                        dmg = (int) (2 + 2 * Math.floor(GameBase.level.time
                                - ent.air_finished));
                        if (dmg > 15)
                            dmg = 15;
                        GameCombat.T_Damage(ent, GameBase.g_edicts[0],
                                GameBase.g_edicts[0], Globals.vec3_origin,
                                ent.s.origin, Globals.vec3_origin, dmg, 0,
                                Defines.DAMAGE_NO_ARMOR, Defines.MOD_WATER);
                        ent.pain_debounce_time = GameBase.level.time + 1;
                    }
                }
            }
        }

        if (ent.waterlevel == 0) {
            if ((ent.flags & Defines.FL_INWATER) != 0) {
                GameBase.gi.sound(ent, Defines.CHAN_BODY, GameBase.gi
                        .soundindex("player/watr_out.wav"), 1,
                        Defines.ATTN_NORM, 0);
                ent.flags &= ~Defines.FL_INWATER;
            }
            return;
        }

        if ((ent.watertype & Defines.CONTENTS_LAVA) != 0
                && 0 == (ent.flags & Defines.FL_IMMUNE_LAVA)) {
            if (ent.damage_debounce_time < GameBase.level.time) {
                ent.damage_debounce_time = GameBase.level.time + 0.2f;
                GameCombat.T_Damage(ent, GameBase.g_edicts[0],
                        GameBase.g_edicts[0], Globals.vec3_origin,
                        ent.s.origin, Globals.vec3_origin, 10 * ent.waterlevel,
                        0, 0, Defines.MOD_LAVA);
            }
        }
        if ((ent.watertype & Defines.CONTENTS_SLIME) != 0
                && 0 == (ent.flags & Defines.FL_IMMUNE_SLIME)) {
            if (ent.damage_debounce_time < GameBase.level.time) {
                ent.damage_debounce_time = GameBase.level.time + 1;
                GameCombat.T_Damage(ent, GameBase.g_edicts[0],
                        GameBase.g_edicts[0], Globals.vec3_origin,
                        ent.s.origin, Globals.vec3_origin, 4 * ent.waterlevel,
                        0, 0, Defines.MOD_SLIME);
            }
        }

        if (0 == (ent.flags & Defines.FL_INWATER)) {
            if (0 == (ent.svflags & Defines.SVF_DEADMONSTER)) {
                if ((ent.watertype & Defines.CONTENTS_LAVA) != 0)
                    if (Globals.rnd.nextFloat() <= 0.5)
                        GameBase.gi.sound(ent, Defines.CHAN_BODY, GameBase.gi
                                .soundindex("player/lava1.wav"), 1,
                                Defines.ATTN_NORM, 0);
                    else
                        GameBase.gi.sound(ent, Defines.CHAN_BODY, GameBase.gi
                                .soundindex("player/lava2.wav"), 1,
                                Defines.ATTN_NORM, 0);
                else if ((ent.watertype & Defines.CONTENTS_SLIME) != 0)
                    GameBase.gi.sound(ent, Defines.CHAN_BODY, GameBase.gi
                            .soundindex("player/watr_in.wav"), 1,
                            Defines.ATTN_NORM, 0);
                else if ((ent.watertype & Defines.CONTENTS_WATER) != 0)
                    GameBase.gi.sound(ent, Defines.CHAN_BODY, GameBase.gi
                            .soundindex("player/watr_in.wav"), 1,
                            Defines.ATTN_NORM, 0);
            }

            ent.flags |= Defines.FL_INWATER;
            ent.damage_debounce_time = 0;
        }
    }

    public static EntThinkAdapter M_droptofloor = new EntThinkAdapter() {
        public String getID() { return "m_drop_to_floor";}
        public boolean think(edict_t ent) {
            float[] end = { 0, 0, 0 };
            trace_t trace;

            ent.s.origin[2] += 1;
            Math3D.VectorCopy(ent.s.origin, end);
            end[2] -= 256;

            trace = GameBase.gi.trace(ent.s.origin, ent.mins, ent.maxs, end,
                    ent, Defines.MASK_MONSTERSOLID);

            if (trace.fraction == 1 || trace.allsolid)
                return true;

            Math3D.VectorCopy(trace.endpos, ent.s.origin);

            GameBase.gi.linkentity(ent);
            M.M_CheckGround(ent);
            M_CatagorizePosition(ent);
            return true;
        }
    };

    public static void M_SetEffects(edict_t ent) {
        ent.s.effects &= ~(Defines.EF_COLOR_SHELL | Defines.EF_POWERSCREEN);
        ent.s.renderfx &= ~(Defines.RF_SHELL_RED | Defines.RF_SHELL_GREEN | Defines.RF_SHELL_BLUE);

        if ((ent.monsterinfo.aiflags & Defines.AI_RESURRECTING) != 0) {
            ent.s.effects |= Defines.EF_COLOR_SHELL;
            ent.s.renderfx |= Defines.RF_SHELL_RED;
        }

        if (ent.health <= 0)
            return;

        if (ent.powerarmor_time > GameBase.level.time) {
            if (ent.monsterinfo.power_armor_type == Defines.POWER_ARMOR_SCREEN) {
                ent.s.effects |= Defines.EF_POWERSCREEN;
            } else if (ent.monsterinfo.power_armor_type == Defines.POWER_ARMOR_SHIELD) {
                ent.s.effects |= Defines.EF_COLOR_SHELL;
                ent.s.renderfx |= Defines.RF_SHELL_GREEN;
            }
        }
    };

    //ok
    public static void M_MoveFrame(edict_t self) {
        mmove_t move; //ptr
        int index;

        move = self.monsterinfo.currentmove;
        self.nextthink = GameBase.level.time + Defines.FRAMETIME;

        if ((self.monsterinfo.nextframe != 0)
                && (self.monsterinfo.nextframe >= move.firstframe)
                && (self.monsterinfo.nextframe <= move.lastframe)) {
            self.s.frame = self.monsterinfo.nextframe;
            self.monsterinfo.nextframe = 0;
        } else {
            if (self.s.frame == move.lastframe) {
                if (move.endfunc != null) {
                    move.endfunc.think(self);

                    // regrab move, endfunc is very likely to change it
                    move = self.monsterinfo.currentmove;

                    // check for death
                    if ((self.svflags & Defines.SVF_DEADMONSTER) != 0)
                        return;
                }
            }

            if (self.s.frame < move.firstframe || self.s.frame > move.lastframe) {
                self.monsterinfo.aiflags &= ~Defines.AI_HOLD_FRAME;
                self.s.frame = move.firstframe;
            } else {
                if (0 == (self.monsterinfo.aiflags & Defines.AI_HOLD_FRAME)) {
                    self.s.frame++;
                    if (self.s.frame > move.lastframe)
                        self.s.frame = move.firstframe;
                }
            }
        }

        index = self.s.frame - move.firstframe;
        if (move.frame[index].ai != null)
            if (0 == (self.monsterinfo.aiflags & Defines.AI_HOLD_FRAME))
                move.frame[index].ai.ai(self, move.frame[index].dist
                        * self.monsterinfo.scale);
            else
                move.frame[index].ai.ai(self, 0);

        if (move.frame[index].think != null)
            move.frame[index].think.think(self);
    }

    /** Stops the Flies. */
    public static EntThinkAdapter M_FliesOff = new EntThinkAdapter() {
        public String getID() { return "m_fliesoff";}
        public boolean think(edict_t self) {
            self.s.effects &= ~Defines.EF_FLIES;
            self.s.sound = 0;
            return true;
        }
    };

    /** Starts the Flies as setting the animation flag in the entity. */
    public static EntThinkAdapter M_FliesOn = new EntThinkAdapter() {
        public String getID() { return "m_flies_on";}
        public boolean think(edict_t self) {
            if (self.waterlevel != 0)
                return true;

            self.s.effects |= Defines.EF_FLIES;
            self.s.sound = GameBase.gi.soundindex("infantry/inflies1.wav");
            self.think = M_FliesOff;
            self.nextthink = GameBase.level.time + 60;
            return true;
        }
    };

    /** Adds some flies after a random time */
    public static EntThinkAdapter M_FlyCheck = new EntThinkAdapter() {
        public String getID() { return "m_fly_check";}
        public boolean think(edict_t self) {

            if (self.waterlevel != 0)
                return true;

            if (Globals.rnd.nextFloat() > 0.5)
                return true;

            self.think = M_FliesOn;
            self.nextthink = GameBase.level.time + 5 + 10
                    * Globals.rnd.nextFloat();
            return true;
        }
    };
}