code:
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment

This file is part of Shadow Warrior version 1.2

Shadow Warrior 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------

#ifndef PLAYER_H

#define PLAYER_H

#define PLAYER_HORIZ_MAX 299    // !JIM! was 199 and 5
#define PLAYER_HORIZ_MIN -99    // Had to make plax sky pan up/down like in Duke
// But this is MUCH better!

#define MIN_SWIM_DEPTH 15

// Player view height
#define PLAYER_HEIGHT Z(58)
#define PLAYER_CRAWL_HEIGHT Z(36)
#define PLAYER_SWIM_HEIGHT Z(26)
#define PLAYER_DIVE_HEIGHT Z(26)
#define PLAYER_DIE_DOWN_HEIGHT Z(4)
#define PLAYER_DIE_UP_HEIGHT Z(8)

// step heights - effects floor_dist's
#define PLAYER_STEP_HEIGHT Z(30)
//#define PLAYER_STEP_HEIGHT Z(34)
//#define PLAYER_STEP_HEIGHT Z(38)

#define PLAYER_CRAWL_STEP_HEIGHT Z(8)
#define PLAYER_SWIM_STEP_HEIGHT Z(8)
#define PLAYER_DIVE_STEP_HEIGHT Z(8)

//#define PLAYER_JUMP_STEP_HEIGHT Z(16)
//#define PLAYER_FALL_STEP_HEIGHT Z(16)

//#define PLAYER_JUMP_STEP_HEIGHT Z(34)
//#define PLAYER_FALL_STEP_HEIGHT Z(24)

#define PLAYER_JUMP_STEP_HEIGHT Z(48)
#define PLAYER_FALL_STEP_HEIGHT Z(24)

// FLOOR_DIST variables are the difference in the Players view and the sector floor.
// Must be at LEAST this distance or you cannot move onto sector.
#define PLAYER_RUN_FLOOR_DIST (PLAYER_HEIGHT - PLAYER_STEP_HEIGHT)
#define PLAYER_CRAWL_FLOOR_DIST (PLAYER_CRAWL_HEIGHT - PLAYER_CRAWL_STEP_HEIGHT)
#define PLAYER_WADE_FLOOR_DIST (PLAYER_HEIGHT - PLAYER_STEP_HEIGHT)
#define PLAYER_JUMP_FLOOR_DIST (PLAYER_HEIGHT - PLAYER_JUMP_STEP_HEIGHT)
#define PLAYER_FALL_FLOOR_DIST (PLAYER_HEIGHT - PLAYER_FALL_STEP_HEIGHT)
#define PLAYER_SWIM_FLOOR_DIST (PLAYER_SWIM_HEIGHT - PLAYER_SWIM_STEP_HEIGHT)
#define PLAYER_DIVE_FLOOR_DIST (PLAYER_DIVE_HEIGHT - PLAYER_DIVE_STEP_HEIGHT)

// FLOOR_DIST variables are the difference in the Players view and the sector floor.
// Must be at LEAST this distance or you cannot move onto sector.
#define PLAYER_RUN_CEILING_DIST Z(10)
#define PLAYER_SWIM_CEILING_DIST (Z(12))
#define PLAYER_DIVE_CEILING_DIST (Z(22))
#define PLAYER_CRAWL_CEILING_DIST (Z(12))
#define PLAYER_JUMP_CEILING_DIST Z(4)
#define PLAYER_FALL_CEILING_DIST Z(4)
#define PLAYER_WADE_CEILING_DIST Z(4)

//
// DIVE
//

#define PLAYER_DIVE_MAX_SPEED       (1700)
#define PLAYER_DIVE_INC             (600)
#define PLAYER_DIVE_BOB_AMT (Z(8))

#define PLAYER_DIVE_TIME (12*120)       // time before damage is taken
#define PLAYER_DIVE_DAMAGE_AMOUNT (-1)  // amount of damage accessed
#define PLAYER_DIVE_DAMAGE_TIME (50) // time between damage accessment

//
// FLY
//

#define PLAYER_FLY_MAX_SPEED       (2560)
#define PLAYER_FLY_INC             (1000)
#define PLAYER_FLY_BOB_AMT (Z(12))

// Height from which Player will actually call DoPlayerBeginFall()
//#define PLAYER_FALL_HEIGHT Z(16)
#define PLAYER_FALL_HEIGHT Z(28)
#define PLAYER_FALL_DAMAGE_AMOUNT (10)

//
// DEATH
//

// dead head height - used in DeathFall
#define PLAYER_DEATH_HEIGHT (Z(16))
#define PLAYER_DEAD_HEAD_FLOORZ_OFFSET (Z(7))

//#define PLAYER_NINJA_XREPEAT (56)
//#define PLAYER_NINJA_YREPEAT (56)
#define PLAYER_NINJA_XREPEAT (47)
#define PLAYER_NINJA_YREPEAT (33)

int SetVisHigh(void);
int SetVisNorm(void);
void DoWeapon(void);
void HeadBobStateControl(void);
int DoPickTarget(SPRITEp sp, uint32_t max_delta_ang, SWBOOL skip_targets);
void DoPlayer(void);
void domovethings(void);
void InitAllPlayers(void);
void InitMultiPlayerInfo(void);
void MoveScrollMode2D(PLAYERp pp);
void DoPlayerDivePalette(PLAYERp pp);
void DoPlayerNightVisionPalette(PLAYERp pp);
void DoPlayerStopDiveNoWarp(PLAYERp pp);
void DoPlayerResetMovement(PLAYERp pp);
void DoPlayerZrange(PLAYERp pp);
void DoPlayerSpriteThrow(PLAYERp pp);
int DoPlayerWadeSuperJump(PLAYERp pp);
void DoPlayerWarpTeleporter(PLAYERp pp);
void UpdatePlayerSprite(PLAYERp pp);
void PlaySOsound(short sectnum,short sound_num);
void DoSpawnTeleporterEffectPlace(SPRITEp sp);
void FindMainSector(SECTOR_OBJECTp sop);

#endif

repo_name: orochi/nblood

license: mit


code:
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment

This file is part of Shadow Warrior version 1.2

Shadow Warrior 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------

//#define MAIN
#define QUIET
#include "build.h"

#include "keys.h"
#include "names2.h"
#include "panel.h"
#include "game.h"
//#include "quake.h"
//#include "jsector.h"

//#include "mytypes.h"
//#include "control.h"
//#include "function.h"
#include "network.h"
//#include "symutil.h"

SWBOOL PredictionOn = TRUE;
SWBOOL Prediction = FALSE;
PLAYER PredictPlayer;
USER PredictUser;
PLAYERp ppp = &PredictPlayer;

typedef struct
{
    int x,y,z;
    fix16_t q16horiz, q16ang;
    short filler;
} PREDICT, *PREDICTp;

PREDICT Predict[MOVEFIFOSIZ];
int predictmovefifoplc;

void DoPlayerSectorUpdatePreMove(PLAYERp);
void DoPlayerSectorUpdatePostMove(PLAYERp);

#define PREDICT_DEBUG 0

#if PREDICT_DEBUG
void (*pred_last_func)(PLAYERp) = NULL;
#endif

void
InitPrediction(PLAYERp pp)
{
    if (!PredictionOn)
        return;

#if PREDICT_DEBUG
    pred_last_func = pp->DoPlayerAction;
#endif

    // make a copy of player struct and sprite
    *ppp = *pp;
    PredictUser = *User[pp->PlayerSprite];
}

#if PREDICT_DEBUG
PredictDebug(PLAYERp ppp)
{
    static FILE *fout = NULL;
    static char pred_sym_name[80];

    if (SymCountCode == 0)
        LoadSymTable("swcode.sym", &SymTableCode, &SymCountCode);

    if (SymCountCode <= 0)
        return;

    if (!fout)
    {
        if ((fout = fopen("dbgpred.txt", "wb")) == NULL)
            return;
    }

    if (ppp->DoPlayerAction != pred_last_func)
    {
        extern uint32_t MoveThingsCount;
        SYM_TABLEp st_ptr;
        uint32_t unrelocated_offset;
        uint32_t offset_from_symbol;

        unrelocated_offset = SymCodePtrToOffset((void *)ppp->DoPlayerAction);
        st_ptr = SearchSymTableByOffset(SymTableCode, SymCountCode, unrelocated_offset, &offset_from_symbol);
        ASSERT(st_ptr);
        strcpy(pred_sym_name, st_ptr->Name);

        fprintf(fout, "%s, %d\n", pred_sym_name, MoveThingsCount);
    }
}
#endif

void
DoPrediction(PLAYERp ppp)
{
    USERp u;
    SPRITE spr;
    int bakrandomseed;

    // routine called from MoveLoop

    if (!PredictionOn)
        return;

    ppp->input = Player[myconnectindex].inputfifo[predictmovefifoplc & (MOVEFIFOSIZ-1)];

    // get rid of input bits so it doesn't go into other code branches that would
    // get it out of sync
    RESET(ppp->input.bits,
          BIT(SK_SHOOT)|BIT(SK_OPERATE)|BIT(SK_INV_LEFT)|BIT(SK_INV_RIGHT)|
          BIT(SK_INV_USE)|BIT(SK_HIDE_WEAPON)|
          BIT(SK_AUTO_AIM)|
          BIT(SK_CENTER_VIEW)|
          SK_WEAPON_MASK|
          SK_INV_HOTKEY_MASK
          );

    SET(ppp->KeyPressFlags,
        BIT(SK_SHOOT)|BIT(SK_OPERATE)|BIT(SK_INV_LEFT)|BIT(SK_INV_RIGHT)|
        BIT(SK_INV_USE)|BIT(SK_HIDE_WEAPON)|
        BIT(SK_AUTO_AIM)|
        BIT(SK_CENTER_VIEW)|
        SK_WEAPON_MASK|
        SK_INV_HOTKEY_MASK
        );

    // back up things so they won't get stepped on
    bakrandomseed = randomseed;
    spr = sprite[Player[myconnectindex].PlayerSprite];
    sprite[Player[myconnectindex].PlayerSprite].cstat = 0;

    u = User[ppp->PlayerSprite];
    User[ppp->PlayerSprite] = &PredictUser;

    ppp->oq16ang = ppp->q16ang;
    ppp->oposx = ppp->posx;
    ppp->oposy = ppp->posy;
    ppp->oposz = ppp->posz;
    ppp->oq16horiz = ppp->q16horiz;

#if PREDICT_DEBUG
    PredictDebug(ppp);
#endif

    // go through the player MOVEMENT code only
    Prediction = TRUE;
    DoPlayerSectorUpdatePreMove(ppp);
    (*ppp->DoPlayerAction)(ppp);
    DoPlayerSectorUpdatePostMove(ppp);
    Prediction = FALSE;

    // restore things
    User[ppp->PlayerSprite] = u;
    sprite[Player[myconnectindex].PlayerSprite] = spr;
    randomseed = bakrandomseed;

    Predict[predictmovefifoplc & (MOVEFIFOSIZ-1)].q16ang = ppp->q16ang;
    Predict[predictmovefifoplc & (MOVEFIFOSIZ-1)].x = ppp->posx;
    Predict[predictmovefifoplc & (MOVEFIFOSIZ-1)].y = ppp->posy;
    Predict[predictmovefifoplc & (MOVEFIFOSIZ-1)].z = ppp->posz;
    Predict[predictmovefifoplc & (MOVEFIFOSIZ-1)].q16horiz = ppp->q16horiz;
    predictmovefifoplc++;
}

void
CorrectPrediction(int actualfifoplc)
{
    PREDICTp predict = &Predict[actualfifoplc & (MOVEFIFOSIZ-1)];

    if (!PredictionOn)
        return;

    if (!CommEnabled)
        return;

    // see if player position is predicted position
    if (predict->q16ang == Player[myconnectindex].q16ang &&
        predict->x == Player[myconnectindex].posx &&
        predict->y == Player[myconnectindex].posy &&
        predict->z == Player[myconnectindex].posz &&
        predict->q16horiz == Player[myconnectindex].q16horiz
        )
    {
        return;
    }

//    //DSPRINTF(ds,"PREDICT ERROR: %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld", fix16_to_int(predict->q16ang),  fix16_to_int(Player[myconnectindex].q16ang), predict->x,    Player[myconnectindex].posx, predict->y,    Player[myconnectindex].posy, predict->z,    Player[myconnectindex].posz,  fix16_to_int(predict->q16horiz),fix16_to_int(Player[myconnectindex].q16horiz));
//    MONO_PRINT(ds);

    InitPrediction(&Player[myconnectindex]);
    // puts the predicted pos back to actual pos
    predictmovefifoplc = movefifoplc;

    while (predictmovefifoplc < Player[myconnectindex].movefifoend)
    {
        DoPrediction(ppp);
    }
}


code:
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment

This file is part of Shadow Warrior version 1.2

Shadow Warrior 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------
#include "build.h"
#include "common.h"

#include "names2.h"
#include "game.h"
#include "tags.h"
#include "common_game.h"
#include "break.h"
#include "quake.h"
#include "sprite.h"

#define QUAKE_Match(sp) (SP_TAG2(sp))
#define QUAKE_Zamt(sp) (SP_TAG3(sp))
#define QUAKE_Radius(sp) (SP_TAG4(sp))
#define QUAKE_Duration(sp) (SP_TAG5(sp))
#define QUAKE_WaitSecs(sp) (SP_TAG6(sp))
#define QUAKE_AngAmt(sp) (SP_TAG7(sp))
#define QUAKE_PosAmt(sp) (SP_TAG8(sp))
#define QUAKE_RandomTest(sp) (SP_TAG9(sp))
#define QUAKE_WaitTics(sp) (SP_TAG13(sp))

#define QUAKE_TestDontTaper(sp) (TEST_BOOL1(sp))
#define QUAKE_KillAfterQuake(sp) (TEST_BOOL2(sp))
// only for timed quakes
#define QUAKE_WaitForTrigger(sp) (TEST_BOOL3(sp))

short CopyQuakeSpotToOn(SPRITEp sp)
{
    short New;
    SPRITEp np;

    New = COVERinsertsprite(sp->sectnum, STAT_QUAKE_SPOT);
    np = &sprite[New];

    memcpy(np, sp, sizeof(SPRITE));

    np->sectnum = sp->sectnum;
    np->statnum = sp->statnum;

    np->cstat = 0;
    np->extra = 0;
    np->owner = -1;

    change_sprite_stat(New, STAT_QUAKE_ON);

    QUAKE_Duration(np) *= 120;

    return New;
}

void DoQuakeMatch(short match)
{
    short i, nexti;
    SPRITEp sp;

    TRAVERSE_SPRITE_STAT(headspritestat[STAT_QUAKE_SPOT], i, nexti)
    {
        sp = &sprite[i];

        if (QUAKE_Match(sp) == match)
        {
            if ((int16_t)QUAKE_WaitTics(sp) > 0)
            {
                // its not waiting any more
                RESET_BOOL3(sp);
            }
            else
            {
                CopyQuakeSpotToOn(sp);
                if (QUAKE_KillAfterQuake(sp))
                {
                    KillSprite(i);
                    continue;
                }
            }
        }

    }
}

void ProcessQuakeOn(void)
{
    short i, nexti;
    SPRITEp sp;

    TRAVERSE_SPRITE_STAT(headspritestat[STAT_QUAKE_ON], i, nexti)
    {
        sp = &sprite[i];

        // get rid of quake when timer runs out
        QUAKE_Duration(sp) -= synctics*4;
        if (QUAKE_Duration(sp) < 0)
        {
            KillSprite(i);
            continue;
        }
    }
}

void ProcessQuakeSpot(void)
{
    short i, nexti;
    SPRITEp sp;
    int rand_test;

    // check timed quakes and random quakes
    TRAVERSE_SPRITE_STAT(headspritestat[STAT_QUAKE_SPOT], i, nexti)
    {
        sp = &sprite[i];

        // not a timed quake
        if (!QUAKE_WaitSecs(sp))
            continue;

        // don't process unless triggered
        if (QUAKE_WaitForTrigger(sp))
            continue;

        // spawn a quake if time is up
        //QUAKE_WaitTics(sp) -= 4*synctics;
        SET_SP_TAG13(sp, (QUAKE_WaitTics(sp)-4*synctics));
        if ((int16_t)QUAKE_WaitTics(sp) < 0)
        {
            // reset timer - add in Duration of quake
            //QUAKE_WaitTics(sp) = ((QUAKE_WaitSecs(sp)*10L) + QUAKE_Duration(sp)) * 120L;
            SET_SP_TAG13(sp, (((QUAKE_WaitSecs(sp)*10L) + QUAKE_Duration(sp)) * 120L));

            // spawn a quake if condition is met
            rand_test = QUAKE_RandomTest(sp);
            // wrong - all quakes need to happen at the same time on all computerssg
            //if (!rand_test || (rand_test && STD_RANDOM_RANGE(128) < rand_test))
            if (!rand_test || (rand_test && RANDOM_RANGE(128) < rand_test))
            {
                CopyQuakeSpotToOn(sp);
                // kill quake spot if needed
                if (QUAKE_KillAfterQuake(sp))
                {
                    DeleteNoSoundOwner(i);
                    KillSprite(i);
                    continue;
                }
            }
        }
    }
}

void QuakeViewChange(PLAYERp pp, int *z_diff, int *x_diff, int *y_diff, short *ang_diff)
{
    short i, nexti;
    SPRITEp sp;
    SPRITEp save_sp = NULL;
    int dist,save_dist = 999999;
    int dist_diff, scale_value;
    int ang_amt;
    int radius;
    int pos_amt;

    *z_diff = 0;
    *x_diff = 0;
    *y_diff = 0;
    *ang_diff = 0;

    if (GamePaused)
        return;

    // find the closest quake - should be a strength value
    TRAVERSE_SPRITE_STAT(headspritestat[STAT_QUAKE_ON], i, nexti)
    {
        sp = &sprite[i];

        dist = FindDistance3D(pp->posx - sp->x, pp->posy - sp->y, pp->posz - sp->z);

        // shake whole level
        if (QUAKE_TestDontTaper(sp))
        {
            save_dist = dist;
            save_sp = sp;
            break;
        }

        if (dist < save_dist)
        {
            save_dist = dist;
            save_sp = sp;
        }
    }

    if (!save_sp)
        return;
    else
        sp = save_sp;

    radius = QUAKE_Radius(sp) * 8L;
    if (save_dist > radius)
        return;

    *z_diff = Z(STD_RANDOM_RANGE(QUAKE_Zamt(sp)) - (QUAKE_Zamt(sp)/2));

    ang_amt = QUAKE_AngAmt(sp) * 4L;
    *ang_diff = STD_RANDOM_RANGE(ang_amt) - (ang_amt/2);

    pos_amt = QUAKE_PosAmt(sp) * 4L;
    *x_diff = STD_RANDOM_RANGE(pos_amt) - (pos_amt/2);
    *y_diff = STD_RANDOM_RANGE(pos_amt) - (pos_amt/2);

    if (!QUAKE_TestDontTaper(sp))
    {
        // scale values from epicenter
        dist_diff = radius - save_dist;
        scale_value = divscale16(dist_diff, radius);

        *z_diff = mulscale16(*z_diff, scale_value);
        *ang_diff = mulscale16(*ang_diff, scale_value);
        *x_diff = mulscale16(*x_diff, scale_value);
        *y_diff = mulscale16(*y_diff, scale_value);
    }
}

int SpawnQuake(short sectnum, int x, int y, int z,
               short tics, short amt, int radius)
{
    short SpriteNum;
    SPRITEp sp;

    SpriteNum = COVERinsertsprite(sectnum, STAT_QUAKE_ON);
    sp = &sprite[SpriteNum];

    ASSERT(SpriteNum >= 0);

    sp->x = x;
    sp->y = y;
    sp->z = z;
    sp->cstat = 0;
    sp->owner = -1;
    sp->extra = 0;

    QUAKE_Match(sp) = -1;
    QUAKE_Zamt(sp) = amt;
    QUAKE_Radius(sp) = radius/8;
    QUAKE_Duration(sp) = tics;
    QUAKE_AngAmt(sp) = 8;
    QUAKE_PosAmt(sp) = 0;

    PlaySound(DIGI_ERUPTION, &sp->x, &sp->y, &sp->z, v3df_follow|v3df_dontpan);
    Set3DSoundOwner(SpriteNum);

    return SpriteNum;
}

SWBOOL
SetQuake(PLAYERp pp, short tics, short amt)
{
    SpawnQuake(pp->cursectnum, pp->posx, pp->posy, pp->posz,  tics, amt, 30000);
    return FALSE;
}

int
SetExpQuake(int16_t Weapon)
{
    SPRITEp sp = &sprite[Weapon];

    SpawnQuake(sp->sectnum, sp->x, sp->y, sp->z,  40, 4, 20000); // !JIM! was 8, 40000
    return 0;
}

int
SetGunQuake(int16_t SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];

    SpawnQuake(sp->sectnum, sp->x, sp->y, sp->z,  40, 8, 40000);

    return 0;
}

int
SetPlayerQuake(PLAYERp pp)
{
    SpawnQuake(pp->cursectnum, pp->posx, pp->posy, pp->posz,  40, 8, 40000);

    return 0;
}

int
SetNuclearQuake(int16_t Weapon)
{
    SPRITEp sp = &sprite[Weapon];

    SpawnQuake(sp->sectnum, sp->x, sp->y, sp->z, 400, 8, 64000);
    return 0;
}

int
SetSumoQuake(int16_t SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];

    SpawnQuake(sp->sectnum, sp->x, sp->y, sp->z,  120, 4, 20000);
    return 0;
}

int
SetSumoFartQuake(int16_t SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];

    SpawnQuake(sp->sectnum, sp->x, sp->y, sp->z,  60, 4, 4000);
    return 0;
}

#include "saveable.h"

static saveable_code saveable_quake_code[] =
{
    SAVE_CODE(CopyQuakeSpotToOn),
    SAVE_CODE(DoQuakeMatch),
    SAVE_CODE(ProcessQuakeOn),
    SAVE_CODE(ProcessQuakeSpot),
    SAVE_CODE(QuakeViewChange),
    SAVE_CODE(SpawnQuake),
    SAVE_CODE(SetQuake),
    SAVE_CODE(SetExpQuake),
    SAVE_CODE(SetGunQuake),
    SAVE_CODE(SetPlayerQuake),
    SAVE_CODE(SetNuclearQuake),
    SAVE_CODE(SetSumoQuake),
    SAVE_CODE(SetSumoFartQuake),
};

saveable_module saveable_quake =
{
    // code
    saveable_quake_code,
    SIZ(saveable_quake_code),

    // data
    NULL,0
};


code:
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment

This file is part of Shadow Warrior version 1.2

Shadow Warrior 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------

void DoQuakeMatch(short match);
void ProcessQuakeOn(void);
void ProcessQuakeSpot(void);
void QuakeViewChange(PLAYERp pp, int *z_diff, int *x_diff, int *y_diff, short *ang_diff);
void DoQuake(PLAYERp pp);
SWBOOL SetQuake(PLAYERp pp, short tics, short amt);
int SetExpQuake(int16_t Weapon);
int SetGunQuake(int16_t SpriteNum);
int SetPlayerQuake(PLAYERp mpp);
int SetNuclearQuake(int16_t Weapon);
int SetSumoQuake(int16_t SpriteNum);
int SetSumoFartQuake(int16_t SpriteNum);


code:
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment

This file is part of Shadow Warrior version 1.2

Shadow Warrior 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------

#ifndef RESERVE_H
#define RESERVE_H

// This header is used for reserving tile space for programatic purposes
// MAXTILES is currently at 6144 in size - anything under this is ok

#define MAXMIRRORS          8
// This is just some, high, blank tile number not used
// by real graphics to put the MAXMIRRORS mirrors in
#define MIRRORLABEL         6000

#define TILT_TILE           6016

// save screen and tilt tile stuff
#define SAVE_SCREEN_TILE    6017
#define SAVE_SCREEN_XSIZE 160L
#define SAVE_SCREEN_YSIZE 100L
#endif


code:
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment

This file is part of Shadow Warrior version 1.2

Shadow Warrior 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------
#include "build.h"

#include "keys.h"
#include "names2.h"
#include "panel.h"
#include "game.h"
#include "tags.h"
#include "ai.h"
#include "pal.h"
#include "actor.h"
#include "sprite.h"
#include "track.h"

ANIMATOR InitRipperHang;
ANIMATOR DoActorMoveJump;
ANIMATOR DoRipperMoveJump;
ANIMATOR DoRipperHangJF;
ANIMATOR DoRipperQuickJump;

DECISION RipperBattle[] =
{
    {748, InitActorMoveCloser},
    {750, InitActorAlertNoise},
//    {900, InitRipperHang},
    {755, InitActorAttackNoise},
    {1024, InitActorAttack}
};

DECISION RipperOffense[] =
{
    {700, InitActorMoveCloser},
    {710, InitActorAlertNoise},
    {1024, InitActorAttack}
};

DECISION RipperBroadcast[] =
{
    {3, InitActorAlertNoise},
    {6, InitActorAmbientNoise},
    {1024, InitActorDecide}
};

DECISION RipperSurprised[] =
{
    {30, InitRipperHang},
    {701, InitActorMoveCloser},
    {1024, InitActorDecide}
};

DECISION RipperEvasive[] =
{
    {6, InitRipperHang},
    {1024, NULL}
};

DECISION RipperLostTarget[] =
{
    {980, InitActorFindPlayer},
    {1024, InitActorWanderAround}
};

DECISION RipperCloseRange[] =
{
    {900,   InitActorAttack             },
    {1024,  InitActorReposition         }
};

PERSONALITY RipperPersonality =
{
    RipperBattle,
    RipperOffense,
    RipperBroadcast,
    RipperSurprised,
    RipperEvasive,
    RipperLostTarget,
    RipperCloseRange,
    RipperCloseRange
};

ATTRIBUTE RipperAttrib =
{
    {200, 220, 240, 280},               // Speeds
    {5, 0, -2, -4},                     // Tic Adjusts
    3,                                  // MaxWeapons;
    {
        DIGI_RIPPERAMBIENT, DIGI_RIPPERALERT, DIGI_RIPPERATTACK,
        DIGI_RIPPERPAIN, DIGI_RIPPERSCREAM, DIGI_RIPPERHEARTOUT,
        0,0,0,0
    }
};

//////////////////////
//
// RIPPER RUN
//
//////////////////////

#define RIPPER_RUN_RATE 16

ANIMATOR DoRipperMove, NullRipper, DoActorDebris;

STATE s_RipperRun[5][4] =
{
    {
        {RIPPER_RUN_R0 + 0, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[0][1]},
        {RIPPER_RUN_R0 + 1, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[0][2]},
        {RIPPER_RUN_R0 + 2, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[0][3]},
        {RIPPER_RUN_R0 + 3, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[0][0]},
    },
    {
        {RIPPER_RUN_R1 + 0, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[1][1]},
        {RIPPER_RUN_R1 + 1, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[1][2]},
        {RIPPER_RUN_R1 + 2, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[1][3]},
        {RIPPER_RUN_R1 + 3, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[1][0]},
    },
    {
        {RIPPER_RUN_R2 + 0, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[2][1]},
        {RIPPER_RUN_R2 + 1, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[2][2]},
        {RIPPER_RUN_R2 + 2, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[2][3]},
        {RIPPER_RUN_R2 + 3, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[2][0]},
    },
    {
        {RIPPER_RUN_R3 + 0, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[3][1]},
        {RIPPER_RUN_R3 + 1, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[3][2]},
        {RIPPER_RUN_R3 + 2, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[3][3]},
        {RIPPER_RUN_R3 + 3, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[3][0]},
    },
    {
        {RIPPER_RUN_R4 + 0, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[4][1]},
        {RIPPER_RUN_R4 + 1, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[4][2]},
        {RIPPER_RUN_R4 + 2, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[4][3]},
        {RIPPER_RUN_R4 + 3, RIPPER_RUN_RATE | SF_TIC_ADJUST, DoRipperMove, &s_RipperRun[4][0]},
    }
};

STATEp sg_RipperRun[] =
{
    &s_RipperRun[0][0],
    &s_RipperRun[1][0],
    &s_RipperRun[2][0],
    &s_RipperRun[3][0],
    &s_RipperRun[4][0]
};

//////////////////////
//
// RIPPER STAND
//
//////////////////////

#define RIPPER_STAND_RATE 12

STATE s_RipperStand[5][1] =
{
    {
        {RIPPER_STAND_R0 + 0, RIPPER_STAND_RATE, DoRipperMove, &s_RipperStand[0][0]},
    },
    {
        {RIPPER_STAND_R1 + 0, RIPPER_STAND_RATE, DoRipperMove, &s_RipperStand[1][0]},
    },
    {
        {RIPPER_STAND_R2 + 0, RIPPER_STAND_RATE, DoRipperMove, &s_RipperStand[2][0]},
    },
    {
        {RIPPER_STAND_R3 + 0, RIPPER_STAND_RATE, DoRipperMove, &s_RipperStand[3][0]},
    },
    {
        {RIPPER_STAND_R4 + 0, RIPPER_STAND_RATE, DoRipperMove, &s_RipperStand[4][0]},
    },
};

STATEp sg_RipperStand[] =
{
    s_RipperStand[0],
    s_RipperStand[1],
    s_RipperStand[2],
    s_RipperStand[3],
    s_RipperStand[4]
};

//////////////////////
//
// RIPPER SWIPE
//
//////////////////////

#define RIPPER_SWIPE_RATE 8
ANIMATOR InitActorDecide;
ANIMATOR InitRipperSlash;

STATE s_RipperSwipe[5][8] =
{
    {
        {RIPPER_SWIPE_R0 + 0, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[0][1]},
        {RIPPER_SWIPE_R0 + 1, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[0][2]},
        {RIPPER_SWIPE_R0 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[0][3]},
        {RIPPER_SWIPE_R0 + 2, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[0][4]},
        {RIPPER_SWIPE_R0 + 3, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[0][5]},
        {RIPPER_SWIPE_R0 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[0][6]},
        {RIPPER_SWIPE_R0 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSwipe[0][7]},
        {RIPPER_SWIPE_R0 + 3, RIPPER_SWIPE_RATE, DoRipperMove, &s_RipperSwipe[0][7]},
    },
    {
        {RIPPER_SWIPE_R1 + 0, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[1][1]},
        {RIPPER_SWIPE_R1 + 1, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[1][2]},
        {RIPPER_SWIPE_R1 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[1][3]},
        {RIPPER_SWIPE_R1 + 2, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[1][4]},
        {RIPPER_SWIPE_R1 + 3, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[1][5]},
        {RIPPER_SWIPE_R1 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[1][6]},
        {RIPPER_SWIPE_R1 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSwipe[1][7]},
        {RIPPER_SWIPE_R1 + 3, RIPPER_SWIPE_RATE, DoRipperMove, &s_RipperSwipe[1][7]},
    },
    {
        {RIPPER_SWIPE_R2 + 0, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[2][1]},
        {RIPPER_SWIPE_R2 + 1, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[2][2]},
        {RIPPER_SWIPE_R2 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[2][3]},
        {RIPPER_SWIPE_R2 + 2, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[2][4]},
        {RIPPER_SWIPE_R2 + 3, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[2][5]},
        {RIPPER_SWIPE_R2 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[2][6]},
        {RIPPER_SWIPE_R2 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSwipe[2][7]},
        {RIPPER_SWIPE_R2 + 3, RIPPER_SWIPE_RATE, DoRipperMove, &s_RipperSwipe[2][7]},
    },
    {
        {RIPPER_SWIPE_R3 + 0, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[3][1]},
        {RIPPER_SWIPE_R3 + 1, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[3][2]},
        {RIPPER_SWIPE_R3 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[3][3]},
        {RIPPER_SWIPE_R3 + 2, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[3][4]},
        {RIPPER_SWIPE_R3 + 3, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[3][5]},
        {RIPPER_SWIPE_R3 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[3][6]},
        {RIPPER_SWIPE_R3 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSwipe[3][7]},
        {RIPPER_SWIPE_R3 + 3, RIPPER_SWIPE_RATE, DoRipperMove, &s_RipperSwipe[3][7]},
    },
    {
        {RIPPER_SWIPE_R4 + 0, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[4][1]},
        {RIPPER_SWIPE_R4 + 1, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[4][2]},
        {RIPPER_SWIPE_R4 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[4][3]},
        {RIPPER_SWIPE_R4 + 2, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[4][4]},
        {RIPPER_SWIPE_R4 + 3, RIPPER_SWIPE_RATE, NullRipper, &s_RipperSwipe[4][5]},
        {RIPPER_SWIPE_R4 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_RipperSwipe[4][6]},
        {RIPPER_SWIPE_R4 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSwipe[4][7]},
        {RIPPER_SWIPE_R4 + 3, RIPPER_SWIPE_RATE, DoRipperMove, &s_RipperSwipe[4][7]},
    }
};

STATEp sg_RipperSwipe[] =
{
    &s_RipperSwipe[0][0],
    &s_RipperSwipe[1][0],
    &s_RipperSwipe[2][0],
    &s_RipperSwipe[3][0],
    &s_RipperSwipe[4][0]
};

//////////////////////
//
// RIPPER SPEW
//
//////////////////////

#define RIPPER_SPEW_RATE 8
ANIMATOR InitActorDecide;
ANIMATOR InitCoolgFire;

STATE s_RipperSpew[5][7] =
{
    {
        {RIPPER_SWIPE_R0 + 0, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[0][1]},
        {RIPPER_SWIPE_R0 + 1, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[0][2]},
        {RIPPER_SWIPE_R0 + 1, 0 | SF_QUICK_CALL, InitCoolgFire, &s_RipperSpew[0][3]},
        {RIPPER_SWIPE_R0 + 2, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[0][4]},
        {RIPPER_SWIPE_R0 + 3, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[0][5]},
        {RIPPER_SWIPE_R0 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSpew[0][6]},
        {RIPPER_SWIPE_R0 + 3, RIPPER_SPEW_RATE, DoRipperMove, &s_RipperSpew[0][6]},
    },
    {
        {RIPPER_SWIPE_R1 + 0, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[1][1]},
        {RIPPER_SWIPE_R1 + 1, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[1][2]},
        {RIPPER_SWIPE_R1 + 1, 0 | SF_QUICK_CALL, InitCoolgFire, &s_RipperSpew[1][3]},
        {RIPPER_SWIPE_R1 + 2, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[1][4]},
        {RIPPER_SWIPE_R1 + 3, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[1][5]},
        {RIPPER_SWIPE_R1 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSpew[1][6]},
        {RIPPER_SWIPE_R1 + 3, RIPPER_SPEW_RATE, DoRipperMove, &s_RipperSpew[1][6]},
    },
    {
        {RIPPER_SWIPE_R2 + 0, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[2][1]},
        {RIPPER_SWIPE_R2 + 1, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[2][2]},
        {RIPPER_SWIPE_R2 + 1, 0 | SF_QUICK_CALL, InitCoolgFire, &s_RipperSpew[2][3]},
        {RIPPER_SWIPE_R2 + 2, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[2][4]},
        {RIPPER_SWIPE_R2 + 3, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[2][5]},
        {RIPPER_SWIPE_R2 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSpew[2][6]},
        {RIPPER_SWIPE_R2 + 3, RIPPER_SPEW_RATE, DoRipperMove, &s_RipperSpew[2][6]},
    },
    {
        {RIPPER_SWIPE_R3 + 0, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[3][1]},
        {RIPPER_SWIPE_R3 + 1, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[3][2]},
        {RIPPER_SWIPE_R3 + 1, 0 | SF_QUICK_CALL, InitCoolgFire, &s_RipperSpew[3][3]},
        {RIPPER_SWIPE_R3 + 2, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[3][4]},
        {RIPPER_SWIPE_R3 + 3, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[3][5]},
        {RIPPER_SWIPE_R3 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSpew[3][6]},
        {RIPPER_SWIPE_R3 + 3, RIPPER_SPEW_RATE, DoRipperMove, &s_RipperSpew[3][6]},
    },
    {
        {RIPPER_SWIPE_R4 + 0, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[4][1]},
        {RIPPER_SWIPE_R4 + 1, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[4][2]},
        {RIPPER_SWIPE_R4 + 1, 0 | SF_QUICK_CALL, InitCoolgFire, &s_RipperSpew[4][3]},
        {RIPPER_SWIPE_R4 + 2, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[4][4]},
        {RIPPER_SWIPE_R4 + 3, RIPPER_SPEW_RATE, NullRipper, &s_RipperSpew[4][5]},
        {RIPPER_SWIPE_R4 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_RipperSpew[4][6]},
        {RIPPER_SWIPE_R4 + 3, RIPPER_SPEW_RATE, DoRipperMove, &s_RipperSpew[4][6]},
    }
};

STATEp sg_RipperSpew[] =
{
    &s_RipperSpew[0][0],
    &s_RipperSpew[1][0],
    &s_RipperSpew[2][0],
    &s_RipperSpew[3][0],
    &s_RipperSpew[4][0]
};

//////////////////////
//
// RIPPER HEART - show players heart
//
//////////////////////

#define RIPPER_HEART_RATE 14
ANIMATOR DoRipperStandHeart;

STATE s_RipperHeart[5][4] =
{
    {
        {RIPPER_HEART_R0 + 0, RIPPER_HEART_RATE, DoRipperStandHeart, &s_RipperHeart[0][0]},
    },
    {
        {RIPPER_HEART_R1 + 0, RIPPER_HEART_RATE, DoRipperStandHeart, &s_RipperHeart[1][0]},
    },
    {
        {RIPPER_HEART_R2 + 0, RIPPER_HEART_RATE, DoRipperStandHeart, &s_RipperHeart[2][0]},
    },
    {
        {RIPPER_HEART_R3 + 0, RIPPER_HEART_RATE, DoRipperStandHeart, &s_RipperHeart[3][0]},
    },
    {
        {RIPPER_HEART_R4 + 0, RIPPER_HEART_RATE, DoRipperStandHeart, &s_RipperHeart[4][0]},
    }
};

STATEp sg_RipperHeart[] =
{
    &s_RipperHeart[0][0],
    &s_RipperHeart[1][0],
    &s_RipperHeart[2][0],
    &s_RipperHeart[3][0],
    &s_RipperHeart[4][0]
};

//////////////////////
//
// RIPPER HANG
//
//////////////////////

#define RIPPER_HANG_RATE 14
ANIMATOR DoRipperHang;

STATE s_RipperHang[5][4] =
{
    {
        {RIPPER_HANG_R0 + 0, RIPPER_HANG_RATE, DoRipperHang, &s_RipperHang[0][0]},
    },
    {
        {RIPPER_HANG_R1 + 0, RIPPER_HANG_RATE, DoRipperHang, &s_RipperHang[1][0]},
    },
    {
        {RIPPER_HANG_R2 + 0, RIPPER_HANG_RATE, DoRipperHang, &s_RipperHang[2][0]},
    },
    {
        {RIPPER_HANG_R3 + 0, RIPPER_HANG_RATE, DoRipperHang, &s_RipperHang[3][0]},
    },
    {
        {RIPPER_HANG_R4 + 0, RIPPER_HANG_RATE, DoRipperHang, &s_RipperHang[4][0]},
    }
};

STATEp sg_RipperHang[] =
{
    &s_RipperHang[0][0],
    &s_RipperHang[1][0],
    &s_RipperHang[2][0],
    &s_RipperHang[3][0],
    &s_RipperHang[4][0]
};

//////////////////////
//
// RIPPER PAIN
//
//////////////////////

#define RIPPER_PAIN_RATE 38
ANIMATOR DoRipperPain;

STATE s_RipperPain[5][1] =
{
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_PAIN_RATE, DoRipperPain, &s_RipperPain[0][0]},
    },
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_PAIN_RATE, DoRipperPain, &s_RipperPain[1][0]},
    },
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_PAIN_RATE, DoRipperPain, &s_RipperPain[2][0]},
    },
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_PAIN_RATE, DoRipperPain, &s_RipperPain[3][0]},
    },
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_PAIN_RATE, DoRipperPain, &s_RipperPain[4][0]},
    }
};

STATEp sg_RipperPain[] =
{
    &s_RipperPain[0][0],
    &s_RipperPain[1][0],
    &s_RipperPain[2][0],
    &s_RipperPain[3][0],
    &s_RipperPain[4][0]
};

//////////////////////
//
// RIPPER JUMP
//
//////////////////////

#define RIPPER_JUMP_RATE 25

STATE s_RipperJump[5][6] =
{
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_JUMP_RATE, NullRipper, &s_RipperJump[0][1]},
        {RIPPER_JUMP_R0 + 1, RIPPER_JUMP_RATE, DoRipperMoveJump, &s_RipperJump[0][1]},
    },
    {
        {RIPPER_JUMP_R1 + 0, RIPPER_JUMP_RATE, NullRipper, &s_RipperJump[1][1]},
        {RIPPER_JUMP_R1 + 1, RIPPER_JUMP_RATE, DoRipperMoveJump, &s_RipperJump[1][1]},
    },
    {
        {RIPPER_JUMP_R2 + 0, RIPPER_JUMP_RATE, NullRipper, &s_RipperJump[2][1]},
        {RIPPER_JUMP_R2 + 1, RIPPER_JUMP_RATE, DoRipperMoveJump, &s_RipperJump[2][1]},
    },
    {
        {RIPPER_JUMP_R3 + 0, RIPPER_JUMP_RATE, NullRipper, &s_RipperJump[3][1]},
        {RIPPER_JUMP_R3 + 1, RIPPER_JUMP_RATE, DoRipperMoveJump, &s_RipperJump[3][1]},
    },
    {
        {RIPPER_JUMP_R4 + 0, RIPPER_JUMP_RATE, NullRipper, &s_RipperJump[4][1]},
        {RIPPER_JUMP_R4 + 1, RIPPER_JUMP_RATE, DoRipperMoveJump, &s_RipperJump[4][1]},
    }
};

STATEp sg_RipperJump[] =
{
    &s_RipperJump[0][0],
    &s_RipperJump[1][0],
    &s_RipperJump[2][0],
    &s_RipperJump[3][0],
    &s_RipperJump[4][0]
};

//////////////////////
//
// RIPPER FALL
//
//////////////////////

#define RIPPER_FALL_RATE 25

STATE s_RipperFall[5][6] =
{
    {
        {RIPPER_FALL_R0 + 0, RIPPER_FALL_RATE, DoRipperMoveJump, &s_RipperFall[0][0]},
    },
    {
        {RIPPER_FALL_R1 + 0, RIPPER_FALL_RATE, DoRipperMoveJump, &s_RipperFall[1][0]},
    },
    {
        {RIPPER_FALL_R2 + 0, RIPPER_FALL_RATE, DoRipperMoveJump, &s_RipperFall[2][0]},
    },
    {
        {RIPPER_FALL_R3 + 0, RIPPER_FALL_RATE, DoRipperMoveJump, &s_RipperFall[3][0]},
    },
    {
        {RIPPER_FALL_R4 + 0, RIPPER_FALL_RATE, DoRipperMoveJump, &s_RipperFall[4][0]},
    }
};

STATEp sg_RipperFall[] =
{
    &s_RipperFall[0][0],
    &s_RipperFall[1][0],
    &s_RipperFall[2][0],
    &s_RipperFall[3][0],
    &s_RipperFall[4][0]
};

//////////////////////
//
// RIPPER JUMP ATTACK
//
//////////////////////

#define RIPPER_JUMP_ATTACK_RATE 35
int DoRipperBeginJumpAttack(short SpriteNum);

STATE s_RipperJumpAttack[5][6] =
{
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_JUMP_ATTACK_RATE, NullRipper, &s_RipperJumpAttack[0][1]},
        {RIPPER_JUMP_R0 + 0, 0 | SF_QUICK_CALL, DoRipperBeginJumpAttack, &s_RipperJumpAttack[0][2]},
        {RIPPER_JUMP_R0 + 1, RIPPER_JUMP_ATTACK_RATE, DoRipperMoveJump, &s_RipperJumpAttack[0][2]},
    },
    {
        {RIPPER_JUMP_R1 + 0, RIPPER_JUMP_ATTACK_RATE, NullRipper, &s_RipperJumpAttack[1][1]},
        {RIPPER_JUMP_R1 + 0, 0 | SF_QUICK_CALL, DoRipperBeginJumpAttack, &s_RipperJumpAttack[1][2]},
        {RIPPER_JUMP_R1 + 1, RIPPER_JUMP_ATTACK_RATE, DoRipperMoveJump, &s_RipperJumpAttack[1][2]},
    },
    {
        {RIPPER_JUMP_R2 + 0, RIPPER_JUMP_ATTACK_RATE, NullRipper, &s_RipperJumpAttack[2][1]},
        {RIPPER_JUMP_R2 + 0, 0 | SF_QUICK_CALL, DoRipperBeginJumpAttack, &s_RipperJumpAttack[2][2]},
        {RIPPER_JUMP_R2 + 1, RIPPER_JUMP_ATTACK_RATE, DoRipperMoveJump, &s_RipperJumpAttack[2][2]},
    },
    {
        {RIPPER_JUMP_R3 + 0, RIPPER_JUMP_ATTACK_RATE, NullRipper, &s_RipperJumpAttack[3][1]},
        {RIPPER_JUMP_R3 + 0, 0 | SF_QUICK_CALL, DoRipperBeginJumpAttack, &s_RipperJumpAttack[3][2]},
        {RIPPER_JUMP_R3 + 1, RIPPER_JUMP_ATTACK_RATE, DoRipperMoveJump, &s_RipperJumpAttack[3][2]},
    },
    {
        {RIPPER_JUMP_R4 + 0, RIPPER_JUMP_ATTACK_RATE, NullRipper, &s_RipperJumpAttack[4][1]},
        {RIPPER_JUMP_R4 + 0, 0 | SF_QUICK_CALL, DoRipperBeginJumpAttack, &s_RipperJumpAttack[4][2]},
        {RIPPER_JUMP_R4 + 1, RIPPER_JUMP_ATTACK_RATE, DoRipperMoveJump, &s_RipperJumpAttack[4][2]},
    }
};

STATEp sg_RipperJumpAttack[] =
{
    &s_RipperJumpAttack[0][0],
    &s_RipperJumpAttack[1][0],
    &s_RipperJumpAttack[2][0],
    &s_RipperJumpAttack[3][0],
    &s_RipperJumpAttack[4][0]
};

//////////////////////
//
// RIPPER HANG_JUMP
//
//////////////////////

#define RIPPER_HANG_JUMP_RATE 20

STATE s_RipperHangJump[5][6] =
{
    {
        {RIPPER_JUMP_R0 + 0, RIPPER_HANG_JUMP_RATE, NullRipper, &s_RipperHangJump[0][1]},
        {RIPPER_JUMP_R0 + 1, RIPPER_HANG_JUMP_RATE, DoRipperHangJF, &s_RipperHangJump[0][1]},
    },
    {
        {RIPPER_JUMP_R1 + 0, RIPPER_HANG_JUMP_RATE, NullRipper, &s_RipperHangJump[1][1]},
        {RIPPER_JUMP_R1 + 1, RIPPER_HANG_JUMP_RATE, DoRipperHangJF, &s_RipperHangJump[1][1]},
    },
    {
        {RIPPER_JUMP_R2 + 0, RIPPER_HANG_JUMP_RATE, NullRipper, &s_RipperHangJump[2][1]},
        {RIPPER_JUMP_R2 + 1, RIPPER_HANG_JUMP_RATE, DoRipperHangJF, &s_RipperHangJump[2][1]},
    },
    {
        {RIPPER_JUMP_R3 + 0, RIPPER_HANG_JUMP_RATE, NullRipper, &s_RipperHangJump[3][1]},
        {RIPPER_JUMP_R3 + 1, RIPPER_HANG_JUMP_RATE, DoRipperHangJF, &s_RipperHangJump[3][1]},
    },
    {
        {RIPPER_JUMP_R4 + 0, RIPPER_HANG_JUMP_RATE, NullRipper, &s_RipperHangJump[4][1]},
        {RIPPER_JUMP_R4 + 1, RIPPER_HANG_JUMP_RATE, DoRipperHangJF, &s_RipperHangJump[4][1]},
    }
};

STATEp sg_RipperHangJump[] =
{
    &s_RipperHangJump[0][0],
    &s_RipperHangJump[1][0],
    &s_RipperHangJump[2][0],
    &s_RipperHangJump[3][0],
    &s_RipperHangJump[4][0]
};

//////////////////////
//
// RIPPER HANG_FALL
//
//////////////////////

#define RIPPER_FALL_RATE 25

STATE s_RipperHangFall[5][6] =
{
    {
        {RIPPER_FALL_R0 + 0, RIPPER_FALL_RATE, DoRipperHangJF, &s_RipperHangFall[0][0]},
    },
    {
        {RIPPER_FALL_R1 + 0, RIPPER_FALL_RATE, DoRipperHangJF, &s_RipperHangFall[1][0]},
    },
    {
        {RIPPER_FALL_R2 + 0, RIPPER_FALL_RATE, DoRipperHangJF, &s_RipperHangFall[2][0]},
    },
    {
        {RIPPER_FALL_R3 + 0, RIPPER_FALL_RATE, DoRipperHangJF, &s_RipperHangFall[3][0]},
    },
    {
        {RIPPER_FALL_R4 + 0, RIPPER_FALL_RATE, DoRipperHangJF, &s_RipperHangFall[4][0]},
    }
};

STATEp sg_RipperHangFall[] =
{
    &s_RipperHangFall[0][0],
    &s_RipperHangFall[1][0],
    &s_RipperHangFall[2][0],
    &s_RipperHangFall[3][0],
    &s_RipperHangFall[4][0]
};

//////////////////////
//
// RIPPER DIE
//
//////////////////////

#define RIPPER_DIE_RATE 16

STATE s_RipperDie[] =
{
    {RIPPER_DIE + 0, RIPPER_DIE_RATE, NullRipper, &s_RipperDie[1]},
    {RIPPER_DIE + 1, RIPPER_DIE_RATE, NullRipper, &s_RipperDie[2]},
    {RIPPER_DIE + 2, RIPPER_DIE_RATE, NullRipper, &s_RipperDie[3]},
    {RIPPER_DIE + 3, RIPPER_DIE_RATE, NullRipper, &s_RipperDie[4]},
    {RIPPER_DEAD, RIPPER_DIE_RATE, DoActorDebris, &s_RipperDie[4]},
};

#define RIPPER_DEAD_RATE 8

STATE s_RipperDead[] =
{
    {RIPPER_DIE + 2, RIPPER_DEAD_RATE, NullAnimator, &s_RipperDead[1]},
    {RIPPER_DIE + 3, RIPPER_DEAD_RATE, NullAnimator, &s_RipperDead[2]},
    {RIPPER_DEAD, SF_QUICK_CALL, QueueFloorBlood, &s_RipperDead[3]},
    {RIPPER_DEAD, RIPPER_DEAD_RATE, DoActorDebris, &s_RipperDead[3]},
};

STATEp sg_RipperDie[] =
{
    s_RipperDie
};

STATEp sg_RipperDead[] =
{
    s_RipperDead
};

STATE s_RipperDeathJump[] =
{
    {RIPPER_DIE + 0, RIPPER_DIE_RATE, DoActorDeathMove, &s_RipperDeathJump[0]}
};

STATE s_RipperDeathFall[] =
{
    {RIPPER_DIE + 1, RIPPER_DIE_RATE, DoActorDeathMove, &s_RipperDeathFall[0]}
};

STATEp sg_RipperDeathJump[] =
{
    s_RipperDeathJump
};

STATEp sg_RipperDeathFall[] =
{
    s_RipperDeathFall
};

/*
STATEp *Stand[MAX_WEAPONS];
STATEp *Run;
STATEp *Jump;
STATEp *Fall;
STATEp *Crawl;
STATEp *Swim;
STATEp *Fly;
STATEp *Rise;
STATEp *Sit;
STATEp *Look;
STATEp *Climb;
STATEp *Pain;
STATEp *Death1;
STATEp *Death2;
STATEp *Dead;
STATEp *DeathJump;
STATEp *DeathFall;
STATEp *CloseAttack[2];
STATEp *Attack[6];
STATEp *Special[2];
*/

ACTOR_ACTION_SET RipperActionSet =
{
    sg_RipperStand,
    sg_RipperRun,
    sg_RipperJump,
    sg_RipperFall,
    NULL,                               // sg_RipperCrawl,
    NULL,                               // sg_RipperSwim,
    NULL,                               // sg_RipperFly,
    NULL,                               // sg_RipperRise,
    NULL,                               // sg_RipperSit,
    NULL,                               // sg_RipperLook,
    NULL,                               // climb
    sg_RipperPain,
    sg_RipperDie,
    NULL,                               // sg_RipperHariKari,
    sg_RipperDead,
    sg_RipperDeathJump,
    sg_RipperDeathFall,
    {sg_RipperSwipe,sg_RipperSpew},
    {800,1024},
    {sg_RipperJumpAttack, sg_RipperSpew},
    {400, 1024},
    {sg_RipperHeart, sg_RipperHang},
    NULL,
    NULL
};

ACTOR_ACTION_SET RipperBrownActionSet =
{
    sg_RipperStand,
    sg_RipperRun,
    sg_RipperJump,
    sg_RipperFall,
    NULL,                               // sg_RipperCrawl,
    NULL,                               // sg_RipperSwim,
    NULL,                               // sg_RipperFly,
    NULL,                               // sg_RipperRise,
    NULL,                               // sg_RipperSit,
    NULL,                               // sg_RipperLook,
    NULL,                               // climb
    sg_RipperPain,                      // pain
    sg_RipperDie,
    NULL,                               // sg_RipperHariKari,
    sg_RipperDead,
    sg_RipperDeathJump,
    sg_RipperDeathFall,
    {sg_RipperSwipe},
    {1024},
    {sg_RipperJumpAttack, sg_RipperSwipe},
    {800, 1024},
    {sg_RipperHeart, sg_RipperHang},
    NULL,
    NULL
};

int
SetupRipper(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u;
    ANIMATOR DoActorDecide;

    if (TEST(sp->cstat, CSTAT_SPRITE_RESTORE))
    {
        u = User[SpriteNum];
        ASSERT(u);
    }
    else
    {
        User[SpriteNum] = u = SpawnUser(SpriteNum, RIPPER_RUN_R0, s_RipperRun[0]);
        u->Health = HEALTH_RIPPER/2; // Baby rippers are weaker
    }

    ChangeState(SpriteNum, s_RipperRun[0]);
    u->Attrib = &RipperAttrib;
    DoActorSetSpeed(SpriteNum, FAST_SPEED);
    u->StateEnd = s_RipperDie;
    u->Rot = sg_RipperRun;
    sp->xrepeat = 64;
    sp->yrepeat = 64;

    if (sp->pal == PALETTE_BROWN_RIPPER)
    {
        EnemyDefaults(SpriteNum, &RipperBrownActionSet, &RipperPersonality);
        sp->xrepeat = 106;
        sp->yrepeat = 90;

        if (!TEST(sp->cstat, CSTAT_SPRITE_RESTORE))
            u->Health = HEALTH_MOMMA_RIPPER;

        sp->clipdist += 128 >> 2;
    }
    else
    {
        EnemyDefaults(SpriteNum, &RipperActionSet, &RipperPersonality);
    }

    SET(u->Flags, SPR_XFLIP_TOGGLE);

    return 0;
}

int
GetJumpHeight(short jump_speed, short jump_grav)
{
    int jump_iterations;
    int height;

    jump_speed = labs(jump_speed);

    jump_iterations = jump_speed / (jump_grav * ACTORMOVETICS);

    height = jump_speed * jump_iterations * ACTORMOVETICS;

    height = DIV256(height);

    return DIV2(height);
}

int
PickJumpSpeed(short SpriteNum, int pix_height)
{
    USERp u = User[SpriteNum];

    //ASSERT(pix_height < 128);

    u->jump_speed = -600;
    u->jump_grav = 8;

    while (TRUE)
    {
        if (GetJumpHeight(u->jump_speed, u->jump_grav) > pix_height + 20)
            break;

        u->jump_speed -= 100;

        ASSERT(u->jump_speed > -3000);
    }

    return u->jump_speed;
}

int
PickJumpMaxSpeed(short SpriteNum, short max_speed)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];
    int zh;

    ASSERT(max_speed < 0);

    u->jump_speed = max_speed;
    u->jump_grav = 8;

    zh = SPRITEp_TOS(sp);

    while (TRUE)
    {
        if (zh - Z(GetJumpHeight(u->jump_speed, u->jump_grav)) - Z(16) > u->hiz)
            break;

        u->jump_speed += 100;

        if (u->jump_speed > -200)
            break;
    }

    return u->jump_speed;
}

//
// HANGING - Jumping/Falling/Stationary
//

int
InitRipperHang(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];
    int dist;

    hitdata_t hitinfo = { { 0, 0, 0 }, -2, 0, -2 };

    SWBOOL Found = FALSE;
    short dang, tang;

    for (dang = 0; dang < 2048; dang += 128)
    {
        tang = NORM_ANGLE(sp->ang + dang);

        FAFhitscan(sp->x, sp->y, sp->z - SPRITEp_SIZE_Z(sp), sp->sectnum,  // Start position
                   sintable[NORM_ANGLE(tang + 512)],   // X vector of 3D ang
                   sintable[tang],             // Y vector of 3D ang
                   0,                          // Z vector of 3D ang
                   &hitinfo, CLIPMASK_MISSILE);

        //ASSERT(hitinfo.sect >= 0);
        if (hitinfo.sect < 0)
            continue;

        dist = Distance(sp->x, sp->y, hitinfo.x, hitinfo.y);

        if (hitinfo.wall < 0 || dist < 2000 || dist > 7000)
        {
            continue;
        }

        Found = TRUE;
        sp->ang = tang;
        break;
    }

    if (!Found)
    {
        InitActorDecide(SpriteNum);
        return 0;
    }

    NewStateGroup(SpriteNum, sg_RipperHangJump);
    u->StateFallOverride = sg_RipperHangFall;
    DoActorSetSpeed(SpriteNum, FAST_SPEED);

    //u->jump_speed = -800;
    PickJumpMaxSpeed(SpriteNum, -800);

    SET(u->Flags, SPR_JUMPING);
    RESET(u->Flags, SPR_FALLING);

    // set up individual actor jump gravity
    u->jump_grav = 8;

    DoJump(SpriteNum);

    return 0;
}

int
DoRipperHang(short SpriteNum)
{
    USERp u = User[SpriteNum];

    if ((u->WaitTics -= ACTORMOVETICS) > 0)
        return 0;

    NewStateGroup(SpriteNum, sg_RipperJumpAttack);
    // move to the 2nd frame - past the pause frame
    u->Tics += u->State->Tics;
    return 0;
}

int
DoRipperMoveHang(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];
    int nx, ny;

    // Move while jumping
    nx = sp->xvel * (int) sintable[NORM_ANGLE(sp->ang + 512)] >> 14;
    ny = sp->xvel * (int) sintable[sp->ang] >> 14;

    // if cannot move the sprite
    if (!move_actor(SpriteNum, nx, ny, 0L))
    {
        switch (TEST(u->ret, HIT_MASK))
        {
        case HIT_WALL:
        {
            short hit_wall;
            short w, nw;

            hit_wall = NORM_WALL(u->ret);

            NewStateGroup(SpriteNum, u->ActorActionSet->Special[1]);
            u->WaitTics = 2 + ((RANDOM_P2(4 << 8) >> 8) * 120);

            // hang flush with the wall
            w = hit_wall;
            nw = wall[w].point2;
            sp->ang = NORM_ANGLE(getangle(wall[nw].x - wall[w].x, wall[nw].y - wall[w].y) - 512);

            return 0;
        }
        }
    }

    return 0;
}

int
DoRipperHangJF(short SpriteNum)
{
    USERp u = User[SpriteNum];

    if (TEST(u->Flags, SPR_JUMPING | SPR_FALLING))
    {
        if (TEST(u->Flags, SPR_JUMPING))
            DoJump(SpriteNum);
        else
            DoFall(SpriteNum);
    }

    if (!TEST(u->Flags, SPR_JUMPING | SPR_FALLING))
    {
        if (DoRipperQuickJump(SpriteNum))
            return 0;

        InitActorDecide(SpriteNum);
    }

    DoRipperMoveHang(SpriteNum);

    return 0;

}

//
// JUMP ATTACK
//

int
DoRipperBeginJumpAttack(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];
    SPRITEp psp = User[SpriteNum]->tgt_sp;
    int CanSeePlayer(short SpriteNum);
    short tang;

    tang = getangle(psp->x - sp->x, psp->y - sp->y);

    if (move_sprite(SpriteNum, sintable[NORM_ANGLE(tang+512)] >> 7, sintable[tang] >> 7,
                    0L, u->ceiling_dist, u->floor_dist, CLIPMASK_ACTOR, ACTORMOVETICS))
        sp->ang = NORM_ANGLE((sp->ang + 1024) + (RANDOM_NEG(256, 6) >> 6));
    else
        sp->ang = NORM_ANGLE(tang + (RANDOM_NEG(256, 6) >> 6));

    DoActorSetSpeed(SpriteNum, FAST_SPEED);

    //u->jump_speed = -800;
    PickJumpMaxSpeed(SpriteNum, -400); // was -800

    SET(u->Flags, SPR_JUMPING);
    RESET(u->Flags, SPR_FALLING);

    // set up individual actor jump gravity
    u->jump_grav = 17; // was 8

    // if I didn't do this here they get stuck in the air sometimes
    DoActorZrange(SpriteNum);

    DoJump(SpriteNum);

    return 0;
}

int
DoRipperMoveJump(short SpriteNum)
{
    USERp u = User[SpriteNum];

    if (TEST(u->Flags, SPR_JUMPING | SPR_FALLING))
    {
        if (TEST(u->Flags, SPR_JUMPING))
            DoJump(SpriteNum);
        else
            DoFall(SpriteNum);
    }

    if (!TEST(u->Flags, SPR_JUMPING | SPR_FALLING))
    {
        if (DoRipperQuickJump(SpriteNum))
            return 0;

        InitActorDecide(SpriteNum);
    }

    DoRipperMoveHang(SpriteNum);
    return 0;
}

//
// STD MOVEMENT
//

int
DoRipperQuickJump(short SpriteNum)
{
    USERp u = User[SpriteNum];

    // Tests to see if ripper is on top of a player/enemy and then immediatly
    // does another jump

    if (u->lo_sp)
    {
        SPRITEp tsp = u->lo_sp;

        if (TEST(tsp->extra, SPRX_PLAYER_OR_ENEMY))
        {
            NewStateGroup(SpriteNum, sg_RipperJumpAttack);
            // move past the first state
            u->Tics = 30;
            return TRUE;
        }
    }

    return FALSE;
}

int
NullRipper(short SpriteNum)
{
    USERp u = User[SpriteNum];

    if (TEST(u->Flags,SPR_SLIDING))
        DoActorSlide(SpriteNum);

    DoActorSectorDamage(SpriteNum);

    return 0;
}

int DoRipperPain(short SpriteNum)
{
    USERp u = User[SpriteNum];

    NullRipper(SpriteNum);

    if ((u->WaitTics -= ACTORMOVETICS) <= 0)
        InitActorDecide(SpriteNum);
    return 0;
}

// CTW MODIFICATION
//int DoRipperRipHeart(SpriteNum)
int DoRipperRipHeart(short SpriteNum)
// CTW MODIFICATION END
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];

    SPRITEp tsp = u->tgt_sp;

    NewStateGroup(SpriteNum, sg_RipperHeart);
    u->WaitTics = 6 * 120;

    // player face ripper
    tsp->ang = getangle(sp->x - tsp->x, sp->y - tsp->y);
    return 0;
}

// CTW MODIFICATION
//int DoRipperStandHeart(SpriteNum)
int DoRipperStandHeart(short SpriteNum)
// CTW MODIFICATION END
{
    USERp u = User[SpriteNum];

    NullRipper(SpriteNum);

    if ((u->WaitTics -= ACTORMOVETICS) <= 0)
        NewStateGroup(SpriteNum, sg_RipperRun);
    return 0;
}

void RipperHatch(short Weapon)
{
    SPRITEp wp = &sprite[Weapon];

    short New,i;
    SPRITEp np;
    USERp nu;
#define MAX_RIPPERS 1
    short rip_ang[MAX_RIPPERS];

    rip_ang[0] = RANDOM_P2(2048);
    // rip_ang[1] = NORM_ANGLE(rip_ang[0] + 1024 + (RANDOM_P2(512) - 256));

    for (i = 0; i < MAX_RIPPERS; i++)
    {
        New = COVERinsertsprite(wp->sectnum, STAT_DEFAULT);
        np = &sprite[New];
        memset(np,0,sizeof(SPRITE));
        np->sectnum = wp->sectnum;
        np->statnum = STAT_DEFAULT;
        np->x = wp->x;
        np->y = wp->y;
        np->z = wp->z;
        np->owner = -1;
        //np->xrepeat = np->yrepeat = 36;
        np->xrepeat = np->yrepeat = 64;
        np->ang = rip_ang[i];
        np->pal = 0;
        SetupRipper(New);
        nu = User[New];

        // make immediately active
        SET(nu->Flags, SPR_ACTIVE);

        NewStateGroup(New, nu->ActorActionSet->Jump);
        nu->ActorActionFunc = DoActorMoveJump;
        DoActorSetSpeed(New, FAST_SPEED);
        PickJumpMaxSpeed(New, -600);

        SET(nu->Flags, SPR_JUMPING);
        RESET(nu->Flags, SPR_FALLING);

        nu->jump_grav = 8;

        // if I didn't do this here they get stuck in the air sometimes
        DoActorZrange(New);

        DoJump(New);
    }
}

int
DoRipperMove(short SpriteNum)
{
    USERp u = User[SpriteNum];

    if (u->scale_speed)
    {
        DoScaleSprite(SpriteNum);
    }

    if (TEST(u->Flags, SPR_JUMPING | SPR_FALLING))
    {
        if (TEST(u->Flags, SPR_JUMPING))
            DoJump(SpriteNum);
        else
            DoFall(SpriteNum);
    }

    // if on a player/enemy sprite jump quickly
    if (!TEST(u->Flags, SPR_JUMPING | SPR_FALLING))
    {
        if (DoRipperQuickJump(SpriteNum))
            return 0;

        KeepActorOnFloor(SpriteNum);
    }

    if (TEST(u->Flags, SPR_SLIDING))
        DoActorSlide(SpriteNum);

    if (u->track >= 0)
        ActorFollowTrack(SpriteNum, ACTORMOVETICS);
    else
        (*u->ActorActionFunc)(SpriteNum);

    DoActorSectorDamage(SpriteNum);
    return 0;
}

#include "saveable.h"

static saveable_code saveable_ripper_code[] =
{
    SAVE_CODE(SetupRipper),
    SAVE_CODE(GetJumpHeight),
    SAVE_CODE(PickJumpSpeed),
    SAVE_CODE(PickJumpMaxSpeed),

    SAVE_CODE(InitRipperHang),
    SAVE_CODE(DoRipperHang),
    SAVE_CODE(DoRipperMoveHang),
    SAVE_CODE(DoRipperHangJF),

    SAVE_CODE(DoRipperBeginJumpAttack),
    SAVE_CODE(DoRipperMoveJump),

    SAVE_CODE(DoRipperQuickJump),
    SAVE_CODE(NullRipper),
    SAVE_CODE(DoRipperPain),
    SAVE_CODE(DoRipperRipHeart),
    SAVE_CODE(DoRipperStandHeart),
    SAVE_CODE(RipperHatch),
    SAVE_CODE(DoRipperMove),
};

static saveable_data saveable_ripper_data[] =
{
    SAVE_DATA(RipperBattle),
    SAVE_DATA(RipperOffense),
    SAVE_DATA(RipperBroadcast),
    SAVE_DATA(RipperSurprised),
    SAVE_DATA(RipperEvasive),
    SAVE_DATA(RipperLostTarget),
    SAVE_DATA(RipperCloseRange),

    SAVE_DATA(RipperPersonality),

    SAVE_DATA(RipperAttrib),

    SAVE_DATA(s_RipperRun),
    SAVE_DATA(sg_RipperRun),
    SAVE_DATA(s_RipperStand),
    SAVE_DATA(sg_RipperStand),
    SAVE_DATA(s_RipperSwipe),
    SAVE_DATA(sg_RipperSwipe),
    SAVE_DATA(s_RipperSpew),
    SAVE_DATA(sg_RipperSpew),
    SAVE_DATA(s_RipperHeart),
    SAVE_DATA(sg_RipperHeart),
    SAVE_DATA(s_RipperHang),
    SAVE_DATA(sg_RipperHang),
    SAVE_DATA(s_RipperPain),
    SAVE_DATA(sg_RipperPain),
    SAVE_DATA(s_RipperJump),
    SAVE_DATA(sg_RipperJump),
    SAVE_DATA(s_RipperFall),
    SAVE_DATA(sg_RipperFall),
    SAVE_DATA(s_RipperJumpAttack),
    SAVE_DATA(sg_RipperJumpAttack),
    SAVE_DATA(s_RipperHangJump),
    SAVE_DATA(sg_RipperHangJump),
    SAVE_DATA(s_RipperHangFall),
    SAVE_DATA(sg_RipperHangFall),
    SAVE_DATA(s_RipperDie),
    SAVE_DATA(s_RipperDead),
    SAVE_DATA(sg_RipperDie),
    SAVE_DATA(sg_RipperDead),
    SAVE_DATA(s_RipperDeathJump),
    SAVE_DATA(s_RipperDeathFall),
    SAVE_DATA(sg_RipperDeathJump),
    SAVE_DATA(sg_RipperDeathFall),

    SAVE_DATA(RipperActionSet),
    SAVE_DATA(RipperBrownActionSet),
};

saveable_module saveable_ripper =
{
    // code
    saveable_ripper_code,
    SIZ(saveable_ripper_code),

    // data
    saveable_ripper_data,
    SIZ(saveable_ripper_data)
};