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"
#include "fx_man.h"

ANIMATOR InitRipper2Hang;
ANIMATOR DoActorMoveJump;
ANIMATOR DoRipper2MoveJump;
ANIMATOR DoRipper2HangJF;
ANIMATOR DoRipper2QuickJump;
ANIMATOR InitRipper2Charge;

DECISION Ripper2Battle[] =
{
    {879, InitRipper2Charge},
    {883, InitActorAttackNoise},
    {900, InitRipper2Hang},
    {1024, InitActorAttack}
};

DECISION Ripper2Offense[] =
{
    {789, InitActorMoveCloser},
    {790, InitActorAttackNoise},
    {800, InitRipper2Hang},
    {1024, InitActorAttack}
};

DECISION Ripper2Broadcast[] =
{
    {3, InitActorAmbientNoise},
    {1024, InitActorDecide}
};

DECISION Ripper2Surprised[] =
{
    {40, InitRipper2Hang},
    {701, InitActorMoveCloser},
    {1024, InitActorDecide}
};

DECISION Ripper2Evasive[] =
{
    {10, InitActorMoveCloser},
    {1024, InitRipper2Charge}
};

DECISION Ripper2LostTarget[] =
{
    {900, InitActorFindPlayer},
    {1024, InitActorWanderAround}
};

DECISION Ripper2CloseRange[] =
{
    {1024,  InitActorAttack         }
};

PERSONALITY Ripper2Personality =
{
    Ripper2Battle,
    Ripper2Offense,
    Ripper2Broadcast,
    Ripper2Surprised,
    Ripper2Evasive,
    Ripper2LostTarget,
    Ripper2CloseRange,
    Ripper2CloseRange
};

ATTRIBUTE Ripper2Attrib =
{
    {100, 120, 300, 380},               // Speeds
    {5, 0, -2, -4},                     // Tic Adjusts
    3,                                  // MaxWeapons;
    {
        DIGI_RIPPER2AMBIENT, DIGI_RIPPER2ALERT, DIGI_RIPPER2ATTACK,
        DIGI_RIPPER2PAIN, DIGI_RIPPER2SCREAM, DIGI_RIPPER2HEARTOUT,
        0,0,0,0
    }
};

//////////////////////
//
// RIPPER2 RUN
//
//////////////////////

#define RIPPER2_RUN_RATE 16

ANIMATOR DoRipper2Move, NullRipper2, DoActorDebris;

STATE s_Ripper2Run[5][4] =
{
    {
        {RIPPER2_RUN_R0 + 0, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[0][1]},
        {RIPPER2_RUN_R0 + 1, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[0][2]},
        {RIPPER2_RUN_R0 + 2, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[0][3]},
        {RIPPER2_RUN_R0 + 3, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[0][0]},
    },
    {
        {RIPPER2_RUN_R1 + 0, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[1][1]},
        {RIPPER2_RUN_R1 + 1, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[1][2]},
        {RIPPER2_RUN_R1 + 2, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[1][3]},
        {RIPPER2_RUN_R1 + 3, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[1][0]},
    },
    {
        {RIPPER2_RUN_R2 + 0, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[2][1]},
        {RIPPER2_RUN_R2 + 1, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[2][2]},
        {RIPPER2_RUN_R2 + 2, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[2][3]},
        {RIPPER2_RUN_R2 + 3, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[2][0]},
    },
    {
        {RIPPER2_RUN_R3 + 0, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[3][1]},
        {RIPPER2_RUN_R3 + 1, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[3][2]},
        {RIPPER2_RUN_R3 + 2, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[3][3]},
        {RIPPER2_RUN_R3 + 3, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[3][0]},
    },
    {
        {RIPPER2_RUN_R4 + 0, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[4][1]},
        {RIPPER2_RUN_R4 + 1, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[4][2]},
        {RIPPER2_RUN_R4 + 2, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[4][3]},
        {RIPPER2_RUN_R4 + 3, RIPPER2_RUN_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2Run[4][0]},
    }
};

STATEp sg_Ripper2Run[] =
{
    &s_Ripper2Run[0][0],
    &s_Ripper2Run[1][0],
    &s_Ripper2Run[2][0],
    &s_Ripper2Run[3][0],
    &s_Ripper2Run[4][0]
};

//////////////////////
//
// RIPPER2 RUNFAST
//
//////////////////////

#define RIPPER2_RUNFAST_RATE 14

ANIMATOR NullRipper2, DoActorDebris;

STATE s_Ripper2RunFast[5][4] =
{
    {
        {RIPPER2_RUNFAST_R0 + 0, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[0][1]},
        {RIPPER2_RUNFAST_R0 + 1, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[0][2]},
        {RIPPER2_RUNFAST_R0 + 2, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[0][3]},
        {RIPPER2_RUNFAST_R0 + 3, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[0][0]},
    },
    {
        {RIPPER2_RUNFAST_R1 + 0, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[1][1]},
        {RIPPER2_RUNFAST_R1 + 1, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[1][2]},
        {RIPPER2_RUNFAST_R1 + 2, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[1][3]},
        {RIPPER2_RUNFAST_R1 + 3, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[1][0]},
    },
    {
        {RIPPER2_RUNFAST_R2 + 0, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[2][1]},
        {RIPPER2_RUNFAST_R2 + 1, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[2][2]},
        {RIPPER2_RUNFAST_R2 + 2, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[2][3]},
        {RIPPER2_RUNFAST_R2 + 3, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[2][0]},
    },
    {
        {RIPPER2_RUNFAST_R3 + 0, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[3][1]},
        {RIPPER2_RUNFAST_R3 + 1, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[3][2]},
        {RIPPER2_RUNFAST_R3 + 2, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[3][3]},
        {RIPPER2_RUNFAST_R3 + 3, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[3][0]},
    },
    {
        {RIPPER2_RUNFAST_R4 + 0, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[4][1]},
        {RIPPER2_RUNFAST_R4 + 1, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[4][2]},
        {RIPPER2_RUNFAST_R4 + 2, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[4][3]},
        {RIPPER2_RUNFAST_R4 + 3, RIPPER2_RUNFAST_RATE | SF_TIC_ADJUST, DoRipper2Move, &s_Ripper2RunFast[4][0]},
    }
};

STATEp sg_Ripper2RunFast[] =
{
    &s_Ripper2RunFast[0][0],
    &s_Ripper2RunFast[1][0],
    &s_Ripper2RunFast[2][0],
    &s_Ripper2RunFast[3][0],
    &s_Ripper2RunFast[4][0]
};

//////////////////////
//
// RIPPER2 STAND
//
//////////////////////

#define RIPPER2_STAND_RATE 12

STATE s_Ripper2Stand[5][1] =
{
    {
        {RIPPER2_STAND_R0 + 0, RIPPER2_STAND_RATE, DoRipper2Move, &s_Ripper2Stand[0][0]},
    },
    {
        {RIPPER2_STAND_R1 + 0, RIPPER2_STAND_RATE, DoRipper2Move, &s_Ripper2Stand[1][0]},
    },
    {
        {RIPPER2_STAND_R2 + 0, RIPPER2_STAND_RATE, DoRipper2Move, &s_Ripper2Stand[2][0]},
    },
    {
        {RIPPER2_STAND_R3 + 0, RIPPER2_STAND_RATE, DoRipper2Move, &s_Ripper2Stand[3][0]},
    },
    {
        {RIPPER2_STAND_R4 + 0, RIPPER2_STAND_RATE, DoRipper2Move, &s_Ripper2Stand[4][0]},
    },
};

STATEp sg_Ripper2Stand[] =
{
    s_Ripper2Stand[0],
    s_Ripper2Stand[1],
    s_Ripper2Stand[2],
    s_Ripper2Stand[3],
    s_Ripper2Stand[4]
};

//////////////////////
//
// RIPPER2 SWIPE
//
//////////////////////

#define RIPPER2_SWIPE_RATE 14
ANIMATOR InitActorDecide;
ANIMATOR InitRipperSlash;

STATE s_Ripper2Swipe[5][8] =
{
    {
        {RIPPER2_SWIPE_R0 + 0, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[0][1]},
        {RIPPER2_SWIPE_R0 + 1, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[0][2]},
        {RIPPER2_SWIPE_R0 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[0][3]},
        {RIPPER2_SWIPE_R0 + 2, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[0][4]},
        {RIPPER2_SWIPE_R0 + 3, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[0][5]},
        {RIPPER2_SWIPE_R0 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[0][6]},
        {RIPPER2_SWIPE_R0 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Swipe[0][7]},
        {RIPPER2_SWIPE_R0 + 3, RIPPER2_SWIPE_RATE, DoRipper2Move, &s_Ripper2Swipe[0][7]},
    },
    {
        {RIPPER2_SWIPE_R1 + 0, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[1][1]},
        {RIPPER2_SWIPE_R1 + 1, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[1][2]},
        {RIPPER2_SWIPE_R1 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[1][3]},
        {RIPPER2_SWIPE_R1 + 2, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[1][4]},
        {RIPPER2_SWIPE_R1 + 3, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[1][5]},
        {RIPPER2_SWIPE_R1 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[1][6]},
        {RIPPER2_SWIPE_R1 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Swipe[1][7]},
        {RIPPER2_SWIPE_R1 + 3, RIPPER2_SWIPE_RATE, DoRipper2Move, &s_Ripper2Swipe[1][7]},
    },
    {
        {RIPPER2_SWIPE_R2 + 0, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[2][1]},
        {RIPPER2_SWIPE_R2 + 1, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[2][2]},
        {RIPPER2_SWIPE_R2 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[2][3]},
        {RIPPER2_SWIPE_R2 + 2, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[2][4]},
        {RIPPER2_SWIPE_R2 + 3, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[2][5]},
        {RIPPER2_SWIPE_R2 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[2][6]},
        {RIPPER2_SWIPE_R2 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Swipe[2][7]},
        {RIPPER2_SWIPE_R2 + 3, RIPPER2_SWIPE_RATE, DoRipper2Move, &s_Ripper2Swipe[2][7]},
    },
    {
        {RIPPER2_SWIPE_R3 + 0, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[3][1]},
        {RIPPER2_SWIPE_R3 + 1, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[3][2]},
        {RIPPER2_SWIPE_R3 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[3][3]},
        {RIPPER2_SWIPE_R3 + 2, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[3][4]},
        {RIPPER2_SWIPE_R3 + 3, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[3][5]},
        {RIPPER2_SWIPE_R3 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[3][6]},
        {RIPPER2_SWIPE_R3 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Swipe[3][7]},
        {RIPPER2_SWIPE_R3 + 3, RIPPER2_SWIPE_RATE, DoRipper2Move, &s_Ripper2Swipe[3][7]},
    },
    {
        {RIPPER2_SWIPE_R4 + 0, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[4][1]},
        {RIPPER2_SWIPE_R4 + 1, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[4][2]},
        {RIPPER2_SWIPE_R4 + 1, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[4][3]},
        {RIPPER2_SWIPE_R4 + 2, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[4][4]},
        {RIPPER2_SWIPE_R4 + 3, RIPPER2_SWIPE_RATE, NullRipper2, &s_Ripper2Swipe[4][5]},
        {RIPPER2_SWIPE_R4 + 3, 0 | SF_QUICK_CALL, InitRipperSlash, &s_Ripper2Swipe[4][6]},
        {RIPPER2_SWIPE_R4 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Swipe[4][7]},
        {RIPPER2_SWIPE_R4 + 3, RIPPER2_SWIPE_RATE, DoRipper2Move, &s_Ripper2Swipe[4][7]},
    }
};

STATEp sg_Ripper2Swipe[] =
{
    &s_Ripper2Swipe[0][0],
    &s_Ripper2Swipe[1][0],
    &s_Ripper2Swipe[2][0],
    &s_Ripper2Swipe[3][0],
    &s_Ripper2Swipe[4][0]
};

//////////////////////
//
// RIPPER2 KONG
//
//////////////////////

#define RIPPER2_MEKONG_RATE 18
ANIMATOR InitActorDecide, ChestRipper2;

STATE s_Ripper2Kong[5][7] =
{
    {
        {RIPPER2_MEKONG_R0 + 0, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[0][1]},
        {RIPPER2_MEKONG_R0 + 0, SF_QUICK_CALL,       ChestRipper2, &s_Ripper2Kong[0][2]},
        {RIPPER2_MEKONG_R0 + 1, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[0][3]},
        {RIPPER2_MEKONG_R0 + 2, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[0][4]},
        {RIPPER2_MEKONG_R0 + 3, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[0][5]},
        {RIPPER2_MEKONG_R0 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Kong[0][6]},
        {RIPPER2_MEKONG_R0 + 0, RIPPER2_MEKONG_RATE, DoRipper2Move, &s_Ripper2Kong[0][6]},
    },
    {
        {RIPPER2_MEKONG_R1 + 0, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[1][1]},
        {RIPPER2_MEKONG_R0 + 0, SF_QUICK_CALL,       ChestRipper2, &s_Ripper2Kong[1][2]},
        {RIPPER2_MEKONG_R1 + 1, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[1][3]},
        {RIPPER2_MEKONG_R1 + 2, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[1][4]},
        {RIPPER2_MEKONG_R1 + 3, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[1][5]},
        {RIPPER2_MEKONG_R1 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Kong[1][6]},
        {RIPPER2_MEKONG_R1 + 0, RIPPER2_MEKONG_RATE, DoRipper2Move, &s_Ripper2Kong[1][6]},
    },
    {
        {RIPPER2_MEKONG_R2 + 0, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[2][1]},
        {RIPPER2_MEKONG_R0 + 0, SF_QUICK_CALL,       ChestRipper2, &s_Ripper2Kong[2][2]},
        {RIPPER2_MEKONG_R2 + 1, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[2][3]},
        {RIPPER2_MEKONG_R2 + 2, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[2][4]},
        {RIPPER2_MEKONG_R2 + 3, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[2][5]},
        {RIPPER2_MEKONG_R2 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Kong[2][6]},
        {RIPPER2_MEKONG_R2 + 0, RIPPER2_MEKONG_RATE, DoRipper2Move, &s_Ripper2Kong[2][6]},
    },
    {
        {RIPPER2_MEKONG_R3 + 0, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[3][1]},
        {RIPPER2_MEKONG_R0 + 0, SF_QUICK_CALL,       ChestRipper2, &s_Ripper2Kong[3][2]},
        {RIPPER2_MEKONG_R3 + 1, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[3][3]},
        {RIPPER2_MEKONG_R3 + 2, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[3][4]},
        {RIPPER2_MEKONG_R3 + 3, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[3][5]},
        {RIPPER2_MEKONG_R3 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Kong[3][6]},
        {RIPPER2_MEKONG_R3 + 0, RIPPER2_MEKONG_RATE, DoRipper2Move, &s_Ripper2Kong[3][6]},
    },
    {
        {RIPPER2_MEKONG_R4 + 0, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[4][1]},
        {RIPPER2_MEKONG_R0 + 0, SF_QUICK_CALL,       ChestRipper2, &s_Ripper2Kong[4][2]},
        {RIPPER2_MEKONG_R4 + 1, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[4][3]},
        {RIPPER2_MEKONG_R4 + 2, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[4][4]},
        {RIPPER2_MEKONG_R4 + 3, RIPPER2_MEKONG_RATE, NullRipper2, &s_Ripper2Kong[4][5]},
        {RIPPER2_MEKONG_R4 + 3, 0 | SF_QUICK_CALL, InitActorDecide, &s_Ripper2Kong[4][6]},
        {RIPPER2_MEKONG_R4 + 0, RIPPER2_MEKONG_RATE, DoRipper2Move, &s_Ripper2Kong[4][6]},
    }
};

STATEp sg_Ripper2Kong[] =
{
    &s_Ripper2Kong[0][0],
    &s_Ripper2Kong[1][0],
    &s_Ripper2Kong[2][0],
    &s_Ripper2Kong[3][0],
    &s_Ripper2Kong[4][0]
};

//////////////////////
//
// RIPPER2 HEART - show players heart
//
//////////////////////

#define RIPPER2_HEART_RATE 20
ANIMATOR DoRipper2StandHeart;

STATE s_Ripper2Heart[5][4] =
{
    {
        {RIPPER2_HEART_R0 + 0, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[0][1]},
        {RIPPER2_HEART_R0 + 1, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[0][0]},
    },
    {
        {RIPPER2_HEART_R1 + 0, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[1][1]},
        {RIPPER2_HEART_R1 + 1, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[1][0]},
    },
    {
        {RIPPER2_HEART_R2 + 0, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[2][1]},
        {RIPPER2_HEART_R2 + 1, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[2][0]},
    },
    {
        {RIPPER2_HEART_R3 + 0, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[3][1]},
        {RIPPER2_HEART_R3 + 1, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[3][0]},
    },
    {
        {RIPPER2_HEART_R4 + 0, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[4][1]},
        {RIPPER2_HEART_R4 + 1, RIPPER2_HEART_RATE, DoRipper2StandHeart, &s_Ripper2Heart[4][0]},
    }
};

STATEp sg_Ripper2Heart[] =
{
    &s_Ripper2Heart[0][0],
    &s_Ripper2Heart[1][0],
    &s_Ripper2Heart[2][0],
    &s_Ripper2Heart[3][0],
    &s_Ripper2Heart[4][0]
};

//////////////////////
//
// RIPPER2 HANG
//
//////////////////////

#define RIPPER2_HANG_RATE 14
ANIMATOR DoRipper2Hang;

STATE s_Ripper2Hang[5][4] =
{
    {
        {RIPPER2_HANG_R0, RIPPER2_HANG_RATE, DoRipper2Hang, &s_Ripper2Hang[0][0]},
    },
    {
        {RIPPER2_HANG_R1, RIPPER2_HANG_RATE, DoRipper2Hang, &s_Ripper2Hang[1][0]},
    },
    {
        {RIPPER2_HANG_R2, RIPPER2_HANG_RATE, DoRipper2Hang, &s_Ripper2Hang[2][0]},
    },
    {
        {RIPPER2_HANG_R3, RIPPER2_HANG_RATE, DoRipper2Hang, &s_Ripper2Hang[3][0]},
    },
    {
        {RIPPER2_HANG_R4, RIPPER2_HANG_RATE, DoRipper2Hang, &s_Ripper2Hang[4][0]},
    }
};

STATEp sg_Ripper2Hang[] =
{
    &s_Ripper2Hang[0][0],
    &s_Ripper2Hang[1][0],
    &s_Ripper2Hang[2][0],
    &s_Ripper2Hang[3][0],
    &s_Ripper2Hang[4][0]
};

//////////////////////
//
// RIPPER2 PAIN
//
//////////////////////

#define RIPPER2_PAIN_RATE 38
ANIMATOR DoRipper2Pain;

STATE s_Ripper2Pain[5][1] =
{
    {
        {4414 + 0, RIPPER2_PAIN_RATE, DoRipper2Pain, &s_Ripper2Pain[0][0]},
    },
    {
        {4414 + 0, RIPPER2_PAIN_RATE, DoRipper2Pain, &s_Ripper2Pain[1][0]},
    },
    {
        {4414 + 0, RIPPER2_PAIN_RATE, DoRipper2Pain, &s_Ripper2Pain[2][0]},
    },
    {
        {4414 + 0, RIPPER2_PAIN_RATE, DoRipper2Pain, &s_Ripper2Pain[3][0]},
    },
    {
        {4414 + 0, RIPPER2_PAIN_RATE, DoRipper2Pain, &s_Ripper2Pain[4][0]},
    }
};

STATEp sg_Ripper2Pain[] =
{
    &s_Ripper2Pain[0][0],
    &s_Ripper2Pain[1][0],
    &s_Ripper2Pain[2][0],
    &s_Ripper2Pain[3][0],
    &s_Ripper2Pain[4][0]
};

//////////////////////
//
// RIPPER2 JUMP
//
//////////////////////

#define RIPPER2_JUMP_RATE 25

STATE s_Ripper2Jump[5][6] =
{
    {
        {RIPPER2_JUMP_R0 + 0, RIPPER2_JUMP_RATE, NullRipper2, &s_Ripper2Jump[0][1]},
        {RIPPER2_JUMP_R0 + 1, RIPPER2_JUMP_RATE, DoRipper2MoveJump, &s_Ripper2Jump[0][1]},
    },
    {
        {RIPPER2_JUMP_R1 + 0, RIPPER2_JUMP_RATE, NullRipper2, &s_Ripper2Jump[1][1]},
        {RIPPER2_JUMP_R1 + 1, RIPPER2_JUMP_RATE, DoRipper2MoveJump, &s_Ripper2Jump[1][1]},
    },
    {
        {RIPPER2_JUMP_R2 + 0, RIPPER2_JUMP_RATE, NullRipper2, &s_Ripper2Jump[2][1]},
        {RIPPER2_JUMP_R2 + 1, RIPPER2_JUMP_RATE, DoRipper2MoveJump, &s_Ripper2Jump[2][1]},
    },
    {
        {RIPPER2_JUMP_R3 + 0, RIPPER2_JUMP_RATE, NullRipper2, &s_Ripper2Jump[3][1]},
        {RIPPER2_JUMP_R3 + 1, RIPPER2_JUMP_RATE, DoRipper2MoveJump, &s_Ripper2Jump[3][1]},
    },
    {
        {RIPPER2_JUMP_R4 + 0, RIPPER2_JUMP_RATE, NullRipper2, &s_Ripper2Jump[4][1]},
        {RIPPER2_JUMP_R4 + 1, RIPPER2_JUMP_RATE, DoRipper2MoveJump, &s_Ripper2Jump[4][1]},
    }
};

STATEp sg_Ripper2Jump[] =
{
    &s_Ripper2Jump[0][0],
    &s_Ripper2Jump[1][0],
    &s_Ripper2Jump[2][0],
    &s_Ripper2Jump[3][0],
    &s_Ripper2Jump[4][0]
};

//////////////////////
//
// RIPPER2 FALL
//
//////////////////////

#define RIPPER2_FALL_RATE 25

STATE s_Ripper2Fall[5][6] =
{
    {
        {RIPPER2_FALL_R0 + 0, RIPPER2_FALL_RATE, DoRipper2MoveJump, &s_Ripper2Fall[0][0]},
    },
    {
        {RIPPER2_FALL_R1 + 0, RIPPER2_FALL_RATE, DoRipper2MoveJump, &s_Ripper2Fall[1][0]},
    },
    {
        {RIPPER2_FALL_R2 + 0, RIPPER2_FALL_RATE, DoRipper2MoveJump, &s_Ripper2Fall[2][0]},
    },
    {
        {RIPPER2_FALL_R3 + 0, RIPPER2_FALL_RATE, DoRipper2MoveJump, &s_Ripper2Fall[3][0]},
    },
    {
        {RIPPER2_FALL_R4 + 0, RIPPER2_FALL_RATE, DoRipper2MoveJump, &s_Ripper2Fall[4][0]},
    }
};

STATEp sg_Ripper2Fall[] =
{
    &s_Ripper2Fall[0][0],
    &s_Ripper2Fall[1][0],
    &s_Ripper2Fall[2][0],
    &s_Ripper2Fall[3][0],
    &s_Ripper2Fall[4][0]
};

//////////////////////
//
// RIPPER2 JUMP ATTACK
//
//////////////////////

#define RIPPER2_JUMP_ATTACK_RATE 35
int DoRipper2BeginJumpAttack(short SpriteNum);

STATE s_Ripper2JumpAttack[5][6] =
{
    {
        {RIPPER2_JUMP_R0 + 0, RIPPER2_JUMP_ATTACK_RATE, NullRipper2, &s_Ripper2JumpAttack[0][1]},
        {RIPPER2_JUMP_R0 + 0, 0 | SF_QUICK_CALL, DoRipper2BeginJumpAttack, &s_Ripper2JumpAttack[0][2]},
        {RIPPER2_JUMP_R0 + 2, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[0][3]},
        {RIPPER2_JUMP_R0 + 1, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[0][3]},
    },
    {
        {RIPPER2_JUMP_R1 + 0, RIPPER2_JUMP_ATTACK_RATE, NullRipper2, &s_Ripper2JumpAttack[1][1]},
        {RIPPER2_JUMP_R1 + 0, 0 | SF_QUICK_CALL, DoRipper2BeginJumpAttack, &s_Ripper2JumpAttack[1][2]},
        {RIPPER2_JUMP_R1 + 2, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[1][3]},
        {RIPPER2_JUMP_R1 + 1, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[1][3]},
    },
    {
        {RIPPER2_JUMP_R2 + 0, RIPPER2_JUMP_ATTACK_RATE, NullRipper2, &s_Ripper2JumpAttack[2][1]},
        {RIPPER2_JUMP_R2 + 0, 0 | SF_QUICK_CALL, DoRipper2BeginJumpAttack, &s_Ripper2JumpAttack[2][2]},
        {RIPPER2_JUMP_R2 + 2, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[2][3]},
        {RIPPER2_JUMP_R1 + 1, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[2][3]},
    },
    {
        {RIPPER2_JUMP_R3 + 0, RIPPER2_JUMP_ATTACK_RATE, NullRipper2, &s_Ripper2JumpAttack[3][1]},
        {RIPPER2_JUMP_R3 + 0, 0 | SF_QUICK_CALL, DoRipper2BeginJumpAttack, &s_Ripper2JumpAttack[3][2]},
        {RIPPER2_JUMP_R3 + 2, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[3][3]},
        {RIPPER2_JUMP_R1 + 1, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[3][3]},
    },
    {
        {RIPPER2_JUMP_R4 + 0, RIPPER2_JUMP_ATTACK_RATE, NullRipper2, &s_Ripper2JumpAttack[4][1]},
        {RIPPER2_JUMP_R4 + 0, 0 | SF_QUICK_CALL, DoRipper2BeginJumpAttack, &s_Ripper2JumpAttack[4][2]},
        {RIPPER2_JUMP_R4 + 2, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[4][3]},
        {RIPPER2_JUMP_R1 + 1, RIPPER2_JUMP_ATTACK_RATE, DoRipper2MoveJump, &s_Ripper2JumpAttack[4][3]},
    }
};

STATEp sg_Ripper2JumpAttack[] =
{
    &s_Ripper2JumpAttack[0][0],
    &s_Ripper2JumpAttack[1][0],
    &s_Ripper2JumpAttack[2][0],
    &s_Ripper2JumpAttack[3][0],
    &s_Ripper2JumpAttack[4][0]
};

//////////////////////
//
// RIPPER2 HANG_JUMP
//
//////////////////////

#define RIPPER2_HANG_JUMP_RATE 20

STATE s_Ripper2HangJump[5][6] =
{
    {
        {RIPPER2_JUMP_R0 + 0, RIPPER2_HANG_JUMP_RATE, NullRipper2, &s_Ripper2HangJump[0][1]},
        {RIPPER2_JUMP_R0 + 1, RIPPER2_HANG_JUMP_RATE, DoRipper2HangJF, &s_Ripper2HangJump[0][1]},
    },
    {
        {RIPPER2_JUMP_R1 + 0, RIPPER2_HANG_JUMP_RATE, NullRipper2, &s_Ripper2HangJump[1][1]},
        {RIPPER2_JUMP_R1 + 1, RIPPER2_HANG_JUMP_RATE, DoRipper2HangJF, &s_Ripper2HangJump[1][1]},
    },
    {
        {RIPPER2_JUMP_R2 + 0, RIPPER2_HANG_JUMP_RATE, NullRipper2, &s_Ripper2HangJump[2][1]},
        {RIPPER2_JUMP_R2 + 1, RIPPER2_HANG_JUMP_RATE, DoRipper2HangJF, &s_Ripper2HangJump[2][1]},
    },
    {
        {RIPPER2_JUMP_R3 + 0, RIPPER2_HANG_JUMP_RATE, NullRipper2, &s_Ripper2HangJump[3][1]},
        {RIPPER2_JUMP_R3 + 1, RIPPER2_HANG_JUMP_RATE, DoRipper2HangJF, &s_Ripper2HangJump[3][1]},
    },
    {
        {RIPPER2_JUMP_R4 + 0, RIPPER2_HANG_JUMP_RATE, NullRipper2, &s_Ripper2HangJump[4][1]},
        {RIPPER2_JUMP_R4 + 1, RIPPER2_HANG_JUMP_RATE, DoRipper2HangJF, &s_Ripper2HangJump[4][1]},
    }
};

STATEp sg_Ripper2HangJump[] =
{
    &s_Ripper2HangJump[0][0],
    &s_Ripper2HangJump[1][0],
    &s_Ripper2HangJump[2][0],
    &s_Ripper2HangJump[3][0],
    &s_Ripper2HangJump[4][0]
};

//////////////////////
//
// RIPPER2 HANG_FALL
//
//////////////////////

#define RIPPER2_FALL_RATE 25

STATE s_Ripper2HangFall[5][6] =
{
    {
        {RIPPER2_FALL_R0 + 0, RIPPER2_FALL_RATE, DoRipper2HangJF, &s_Ripper2HangFall[0][0]},
    },
    {
        {RIPPER2_FALL_R1 + 0, RIPPER2_FALL_RATE, DoRipper2HangJF, &s_Ripper2HangFall[1][0]},
    },
    {
        {RIPPER2_FALL_R2 + 0, RIPPER2_FALL_RATE, DoRipper2HangJF, &s_Ripper2HangFall[2][0]},
    },
    {
        {RIPPER2_FALL_R3 + 0, RIPPER2_FALL_RATE, DoRipper2HangJF, &s_Ripper2HangFall[3][0]},
    },
    {
        {RIPPER2_FALL_R4 + 0, RIPPER2_FALL_RATE, DoRipper2HangJF, &s_Ripper2HangFall[4][0]},
    }
};

STATEp sg_Ripper2HangFall[] =
{
    &s_Ripper2HangFall[0][0],
    &s_Ripper2HangFall[1][0],
    &s_Ripper2HangFall[2][0],
    &s_Ripper2HangFall[3][0],
    &s_Ripper2HangFall[4][0]
};

//////////////////////
//
// RIPPER2 DIE
//
//////////////////////

#define RIPPER2_DIE_RATE 18

STATE s_Ripper2Die[] =
{
    {RIPPER2_DIE + 0, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[1]},
    {RIPPER2_DIE + 1, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[2]},
    {RIPPER2_DIE + 2, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[3]},
    {RIPPER2_DIE + 3, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[4]},
    {RIPPER2_DIE + 4, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[5]},
    {RIPPER2_DIE + 5, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[6]},
    {RIPPER2_DIE + 6, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Die[7]},
    {RIPPER2_DEAD, RIPPER2_DIE_RATE, DoActorDebris, &s_Ripper2Die[7]},
};

#define RIPPER2_DEAD_RATE 8

STATE s_Ripper2Dead[] =
{
    {RIPPER2_DIE + 0, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[1]},
    {RIPPER2_DIE + 1, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[2]},
    {RIPPER2_DIE + 2, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[3]},
    {RIPPER2_DIE + 3, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[4]},
    {RIPPER2_DIE + 4, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[5]},
    {RIPPER2_DIE + 5, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[6]},
    {RIPPER2_DIE + 6, RIPPER2_DIE_RATE, NullRipper2, &s_Ripper2Dead[7]},
    {RIPPER2_DEAD, SF_QUICK_CALL, QueueFloorBlood, &s_Ripper2Dead[8]},
    {RIPPER2_DEAD, RIPPER2_DEAD_RATE, DoActorDebris, &s_Ripper2Dead[8]},
};

STATEp sg_Ripper2Die[] =
{
    s_Ripper2Die
};

STATEp sg_Ripper2Dead[] =
{
    s_Ripper2Dead
};

STATE s_Ripper2DeathJump[] =
{
    {RIPPER2_DIE + 0, RIPPER2_DIE_RATE, DoActorDeathMove, &s_Ripper2DeathJump[0]}
};

STATE s_Ripper2DeathFall[] =
{
    {RIPPER2_DIE + 1, RIPPER2_DIE_RATE, DoActorDeathMove, &s_Ripper2DeathFall[0]}
};

STATEp sg_Ripper2DeathJump[] =
{
    s_Ripper2DeathJump
};

STATEp sg_Ripper2DeathFall[] =
{
    s_Ripper2DeathFall
};

/*
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 Ripper2ActionSet =
{
    sg_Ripper2Stand,
    sg_Ripper2Run,
    sg_Ripper2Jump,
    sg_Ripper2Fall,
    NULL,                               // sg_Ripper2Crawl,
    NULL,                               // sg_Ripper2Swim,
    NULL,                               // sg_Ripper2Fly,
    NULL,                               // sg_Ripper2Rise,
    NULL,                               // sg_Ripper2Sit,
    NULL,                               // sg_Ripper2Look,
    NULL,                               // climb
    sg_Ripper2Pain,
    sg_Ripper2Die,
    NULL,                               // sg_Ripper2HariKari,
    sg_Ripper2Dead,
    sg_Ripper2DeathJump,
    sg_Ripper2DeathFall,
    {sg_Ripper2Swipe},
    {1024},
    {sg_Ripper2JumpAttack, sg_Ripper2Kong},
    {500, 1024},
    {sg_Ripper2Heart, sg_Ripper2Hang},
    NULL,
    NULL
};

ACTOR_ACTION_SET Ripper2BrownActionSet =
{
    sg_Ripper2Stand,
    sg_Ripper2Run,
    sg_Ripper2Jump,
    sg_Ripper2Fall,
    NULL,                               // sg_Ripper2Crawl,
    NULL,                               // sg_Ripper2Swim,
    NULL,                               // sg_Ripper2Fly,
    NULL,                               // sg_Ripper2Rise,
    NULL,                               // sg_Ripper2Sit,
    NULL,                               // sg_Ripper2Look,
    NULL,                               // climb
    sg_Ripper2Pain,                      // pain
    sg_Ripper2Die,
    NULL,                               // sg_Ripper2HariKari,
    sg_Ripper2Dead,
    sg_Ripper2DeathJump,
    sg_Ripper2DeathFall,
    {sg_Ripper2Swipe},
    {1024},
    {sg_Ripper2JumpAttack, sg_Ripper2Kong},
    {400, 1024},
    {sg_Ripper2Heart, sg_Ripper2Hang},
    NULL,
    NULL
};

int
SetupRipper2(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, RIPPER2_RUN_R0, s_Ripper2Run[0]);
        u->Health = HEALTH_RIPPER2;
    }

    ChangeState(SpriteNum, s_Ripper2Run[0]);
    u->Attrib = &Ripper2Attrib;
    DoActorSetSpeed(SpriteNum, NORM_SPEED);
    u->StateEnd = s_Ripper2Die;
    u->Rot = sg_Ripper2Run;
    sp->clipdist = 512 >> 2;  // This actor is bigger, needs bigger box.
    sp->xrepeat = sp->yrepeat = 55;

    if (sp->pal == PALETTE_BROWN_RIPPER)
    {
        EnemyDefaults(SpriteNum, &Ripper2BrownActionSet, &Ripper2Personality);
        sp->xrepeat += 40;
        sp->yrepeat += 40;

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

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

    SET(u->Flags, SPR_XFLIP_TOGGLE);

    return 0;
}

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

int
InitRipper2Hang(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);

        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_Ripper2HangJump);
    u->StateFallOverride = sg_Ripper2HangFall;
    DoActorSetSpeed(SpriteNum, FAST_SPEED);

    //u->jump_speed = -800;
    PickJumpMaxSpeed(SpriteNum, -(RANDOM_RANGE(400)+100));

    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
DoRipper2Hang(short SpriteNum)
{
    USERp u = User[SpriteNum];

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

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

    return 0;
}

int
DoRipper2MoveHang(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;

            // Don't keep clinging and going ever higher!
            if (abs(sp->z - u->tgt_sp->z) > (4000<<4))
                break;

            hit_wall = NORM_WALL(u->ret);

            NewStateGroup(SpriteNum, u->ActorActionSet->Special[1]);
            if (RANDOM_P2(1024<<8)>>8 > 500)
                u->WaitTics = ((RANDOM_P2(2 << 8) >> 8) * 120);
            else
                u->WaitTics = 0; // Double jump

            // 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
DoRipper2HangJF(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 (DoRipper2QuickJump(SpriteNum))
            return 0;

        InitActorDecide(SpriteNum);
    }

    DoRipper2MoveHang(SpriteNum);

    return 0;

}

//
// JUMP ATTACK
//

int
DoRipper2BeginJumpAttack(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);

    // Always jump at player if mad.
    //if(u->speed < FAST_SPEED)
    //{
    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);
    //    sp->ang = NORM_ANGLE(tang + (RANDOM_NEG(256, 6) >> 6));
    //} else
    //    sp->ang = NORM_ANGLE(tang);

    DoActorSetSpeed(SpriteNum, FAST_SPEED);

    //u->jump_speed = -800;
    PickJumpMaxSpeed(SpriteNum, -(RANDOM_RANGE(400)+100));

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

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

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

    DoJump(SpriteNum);

    return 0;
}

int
DoRipper2MoveJump(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 (DoRipper2QuickJump(SpriteNum))
            return 0;

        InitActorDecide(SpriteNum);
    }

    DoRipper2MoveHang(SpriteNum);
    return 0;
}

//
// STD MOVEMENT
//

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

    // Tests to see if ripper2 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_Ripper2JumpAttack);
            // move past the first state
            u->Tics = 30;
            return TRUE;
        }
    }

    return FALSE;
}

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

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

    DoActorSectorDamage(SpriteNum);

    return 0;
}

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

    NullRipper2(SpriteNum);

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

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

    SPRITEp tsp = u->tgt_sp;

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

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

int DoRipper2StandHeart(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];
    static int riphearthandle;

    NullRipper2(SpriteNum);

    if (!FX_SoundValidAndActive(riphearthandle))
        riphearthandle = PlaySound(DIGI_RIPPER2HEARTOUT,&sp->x,&sp->y,&sp->z,v3df_none);

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

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

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

    rip_ang[0] = RANDOM_P2(2048);
#if MAX_RIPPER2S > 1
    rip_ang[1] = NORM_ANGLE(rip_ang[0] + 1024 + (RANDOM_P2(512) - 256));
#endif

    for (i = 0; i < MAX_RIPPER2S; 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;
        np->shade = -10;
        SetupRipper2(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
DoRipper2Move(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    USERp u = User[SpriteNum];
    int DoCheckSwarm(short SpriteNum);

    if (sp->hitag == TAG_SWARMSPOT && sp->lotag == 1)
        DoCheckSwarm(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 (DoRipper2QuickJump(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;
}

int InitRipper2Charge(short SpriteNum)
{
    DoActorSetSpeed(SpriteNum, FAST_SPEED);

    InitActorMoveCloser(SpriteNum);

    NewStateGroup(SpriteNum, sg_Ripper2RunFast);

    return 0;
}

int ChestRipper2(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];

    PlaySound(DIGI_RIPPER2CHEST,&sp->x,&sp->y,&sp->z,v3df_follow);

    return 0;
}

#include "saveable.h"

static saveable_code saveable_ripper2_code[] =
{
    SAVE_CODE(SetupRipper2),

    SAVE_CODE(InitRipper2Hang),
    SAVE_CODE(DoRipper2Hang),
    SAVE_CODE(DoRipper2MoveHang),
    SAVE_CODE(DoRipper2HangJF),

    SAVE_CODE(DoRipper2BeginJumpAttack),
    SAVE_CODE(DoRipper2MoveJump),
    SAVE_CODE(DoRipper2QuickJump),
    SAVE_CODE(NullRipper2),
    SAVE_CODE(DoRipper2Pain),
    SAVE_CODE(DoRipper2RipHeart),
    SAVE_CODE(DoRipper2StandHeart),
    SAVE_CODE(Ripper2Hatch),
    SAVE_CODE(DoRipper2Move),
    SAVE_CODE(InitRipper2Charge),
    SAVE_CODE(ChestRipper2),
};

static saveable_data saveable_ripper2_data[] =
{
    SAVE_DATA(Ripper2Battle),
    SAVE_DATA(Ripper2Offense),
    SAVE_DATA(Ripper2Broadcast),
    SAVE_DATA(Ripper2Surprised),
    SAVE_DATA(Ripper2Evasive),
    SAVE_DATA(Ripper2LostTarget),
    SAVE_DATA(Ripper2CloseRange),

    SAVE_DATA(Ripper2Personality),

    SAVE_DATA(Ripper2Attrib),

    SAVE_DATA(s_Ripper2Run),
    SAVE_DATA(sg_Ripper2Run),
    SAVE_DATA(s_Ripper2RunFast),
    SAVE_DATA(sg_Ripper2RunFast),
    SAVE_DATA(s_Ripper2Stand),
    SAVE_DATA(sg_Ripper2Stand),
    SAVE_DATA(s_Ripper2Swipe),
    SAVE_DATA(sg_Ripper2Swipe),
    SAVE_DATA(s_Ripper2Kong),
    SAVE_DATA(sg_Ripper2Kong),
    SAVE_DATA(s_Ripper2Heart),
    SAVE_DATA(sg_Ripper2Heart),
    SAVE_DATA(s_Ripper2Hang),
    SAVE_DATA(sg_Ripper2Hang),
    SAVE_DATA(s_Ripper2Pain),
    SAVE_DATA(sg_Ripper2Pain),
    SAVE_DATA(s_Ripper2Jump),
    SAVE_DATA(sg_Ripper2Jump),
    SAVE_DATA(s_Ripper2Fall),
    SAVE_DATA(sg_Ripper2Fall),
    SAVE_DATA(s_Ripper2JumpAttack),
    SAVE_DATA(sg_Ripper2JumpAttack),
    SAVE_DATA(s_Ripper2HangJump),
    SAVE_DATA(sg_Ripper2HangJump),
    SAVE_DATA(s_Ripper2HangFall),
    SAVE_DATA(sg_Ripper2HangFall),
    SAVE_DATA(s_Ripper2Die),
    SAVE_DATA(s_Ripper2Dead),
    SAVE_DATA(sg_Ripper2Die),
    SAVE_DATA(sg_Ripper2Dead),
    SAVE_DATA(s_Ripper2DeathJump),
    SAVE_DATA(s_Ripper2DeathFall),
    SAVE_DATA(sg_Ripper2DeathJump),
    SAVE_DATA(sg_Ripper2DeathFall),

    SAVE_DATA(Ripper2ActionSet),
    SAVE_DATA(Ripper2BrownActionSet),
};

saveable_module saveable_ripper2 =
{
    // code
    saveable_ripper2_code,
    SIZ(saveable_ripper2_code),

    // data
    saveable_ripper2_data,
    SIZ(saveable_ripper2_data)
};


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 "names2.h"
#include "panel.h"
#include "game.h"
#include "warp.h"

void _ErrMsg(const char *strFile, unsigned uLine, const char *format, ...);
void FAF_DrawRooms(int posx, int posy, int posz, fix16_t q16ang, fix16_t q16horiz, short cursectnum);

////////////////////////////////////////////////////////////////////
//
// FLOOR ABOVE FLOOR
//
////////////////////////////////////////////////////////////////////

#define ZMAX 400
typedef struct
{
    int32_t zval[ZMAX];
    int16_t sectnum[ZMAX];
    int16_t pic[ZMAX];
    int16_t zcount;
    int16_t slope[ZMAX];
} SAVE, *SAVEp;

SAVE save;

SWBOOL FAF_DebugView = 0;

void COVERupdatesector(int32_t x, int32_t y, int16_t* newsector)
{
    // ASSERT(*newsector>=0 && *newsector<MAXSECTORS);
    updatesector(x,y,newsector);
}

int COVERinsertsprite(short sectnum, short stat)
{
    short spnum;
    spnum = insertsprite(sectnum, stat);

    PRODUCTION_ASSERT(spnum >= 0);

    sprite[spnum].x = sprite[spnum].y = sprite[spnum].z = 0;
    sprite[spnum].cstat = 0;
    sprite[spnum].picnum = 0;
    sprite[spnum].shade = 0;
    sprite[spnum].pal = 0;
    sprite[spnum].clipdist = 0;
    sprite[spnum].xrepeat = sprite[spnum].yrepeat = 0;
    sprite[spnum].xoffset = sprite[spnum].yoffset = 0;
    sprite[spnum].ang = 0;
    sprite[spnum].owner = -1;
    sprite[spnum].xvel = sprite[spnum].yvel = sprite[spnum].zvel = 0;
    sprite[spnum].lotag = 0;
    sprite[spnum].hitag = 0;
    sprite[spnum].extra = 0;

    return spnum;
}

SWBOOL
FAF_Sector(short sectnum)
{
    short SpriteNum, Next;
    SPRITEp sp;

    TRAVERSE_SPRITE_SECT(headspritesect[sectnum], SpriteNum, Next)
    {
        sp = &sprite[SpriteNum];

        if (sp->statnum == STAT_FAF &&
            (sp->hitag >= VIEW_LEVEL1 && sp->hitag <= VIEW_LEVEL6))
        {
            return TRUE;
        }
    }

    return FALSE;
}

void SetWallWarpHitscan(short sectnum)
{
    short start_wall, wall_num;
    SPRITEp sp_warp;

    if (!WarpSectorInfo(sectnum, &sp_warp))
        return;

    if (!sp_warp)
        return;

    // move the the next wall
    wall_num = start_wall = sector[sectnum].wallptr;

    // Travel all the way around loop setting wall bits
    do
    {
        if ((uint16_t)wall[wall_num].nextwall < MAXWALLS)
            SET(wall[wall_num].cstat, CSTAT_WALL_WARP_HITSCAN);
        wall_num = wall[wall_num].point2;
    }
    while (wall_num != start_wall);
}

void ResetWallWarpHitscan(short sectnum)
{
    short start_wall, wall_num;

    // move the the next wall
    wall_num = start_wall = sector[sectnum].wallptr;

    // Travel all the way around loop setting wall bits
    do
    {
        RESET(wall[wall_num].cstat, CSTAT_WALL_WARP_HITSCAN);
        wall_num = wall[wall_num].point2;
    }
    while (wall_num != start_wall);
}

void
FAFhitscan(int32_t x, int32_t y, int32_t z, int16_t sectnum,
           int32_t xvect, int32_t yvect, int32_t zvect,
           hitdata_t* hitinfo, int32_t clipmask)
{
    vec3_t firstpos = { x, y, z };
    int loz, hiz;
    short newsectnum = sectnum;
    int startclipmask = 0;
    SWBOOL plax_found = FALSE;

    if (clipmask == CLIPMASK_MISSILE)
        startclipmask = CLIPMASK_WARP_HITSCAN;

    hitscan(&firstpos, sectnum, xvect, yvect, zvect,
            hitinfo, startclipmask);

    if (hitinfo->sect < 0)
        return;

    if (hitinfo->wall >= 0)
    {
        // hitscan warping
        if (TEST(wall[hitinfo->wall].cstat, CSTAT_WALL_WARP_HITSCAN))
        {
            short dest_sect;

            MONO_PRINT(ds);

            // back it up a bit to get a correct warp location
            hitinfo->x -= xvect>>9;
            hitinfo->y -= yvect>>9;

            // warp to new x,y,z, sectnum
            if (Warp(&hitinfo->x, &hitinfo->y, &hitinfo->z, &hitinfo->sect))
            {
                vec3_t pos = hitinfo->xyz;

                dest_sect = hitinfo->sect;

                // hitscan needs to pass through dest sect
                ResetWallWarpHitscan(dest_sect);

                // NOTE: This could be recursive I think if need be
                hitscan(&pos, hitinfo->sect, xvect, yvect, zvect,
                        hitinfo, startclipmask);

                // reset hitscan block for dest sect
                SetWallWarpHitscan(dest_sect);

                return;
            }
            else
            {
                //DSPRINTF(ds,"hitinfo->x %d, hitinfo->y %d, hitinfo->z %d",hitinfo->x, hitinfo->y, hitinfo->z);
                MONO_PRINT(ds);
                ASSERT(TRUE == FALSE);
            }
        }
    }

    // make sure it hit JUST a sector before doing a check
    if (hitinfo->wall < 0 && hitinfo->sprite < 0)
    {
        if (TEST(sector[hitinfo->sect].extra, SECTFX_WARP_SECTOR))
        {
            if (TEST(wall[sector[hitinfo->sect].wallptr].cstat, CSTAT_WALL_WARP_HITSCAN))
            {
                // hit the floor of a sector that is a warping sector
                if (Warp(&hitinfo->x, &hitinfo->y, &hitinfo->z, &hitinfo->sect))
                {
                    vec3_t pos = hitinfo->xyz;
                    hitscan(&pos, hitinfo->sect, xvect, yvect, zvect,
                            hitinfo, clipmask);

                    return;
                }
            }
            else
            {
                if (WarpPlane(&hitinfo->x, &hitinfo->y, &hitinfo->z, &hitinfo->sect))
                {
                    vec3_t pos = hitinfo->xyz;
                    hitscan(&pos, hitinfo->sect, xvect, yvect, zvect,
                            hitinfo, clipmask);

                    return;
                }
            }
        }

        getzsofslope(hitinfo->sect, hitinfo->x, hitinfo->y, &hiz, &loz);
        if (labs(hitinfo->z - loz) < Z(4))
        {
            if (FAF_ConnectFloor(hitinfo->sect) && !TEST(sector[hitinfo->sect].floorstat, FLOOR_STAT_FAF_BLOCK_HITSCAN))
            {
                updatesectorz(hitinfo->x, hitinfo->y, hitinfo->z + Z(12), &newsectnum);
                plax_found = TRUE;
            }
        }
        else if (labs(hitinfo->z - hiz) < Z(4))
        {
            if (FAF_ConnectCeiling(hitinfo->sect) && !TEST(sector[hitinfo->sect].floorstat, CEILING_STAT_FAF_BLOCK_HITSCAN))
            {
                updatesectorz(hitinfo->x, hitinfo->y, hitinfo->z - Z(12), &newsectnum);
                plax_found = TRUE;
            }
        }
    }

    if (plax_found)
    {
        vec3_t pos = hitinfo->xyz;
        hitscan(&pos, newsectnum, xvect, yvect, zvect,
                hitinfo, clipmask);
    }
}

SWBOOL
FAFcansee(int32_t xs, int32_t ys, int32_t zs, int16_t sects,
          int32_t xe, int32_t ye, int32_t ze, int16_t secte)
{
    int loz, hiz;
    short newsectnum = sects;
    int xvect, yvect, zvect;
    short ang;
    hitdata_t hitinfo;
    int dist;
    SWBOOL plax_found = FALSE;
    vec3_t s = { xs, ys, zs };

    // ASSERT(sects >= 0 && secte >= 0);

    // early out to regular routine
    if ((sects < 0 || !FAF_Sector(sects)) && (secte < 0 || !FAF_Sector(secte)))
    {
        return cansee(xs,ys,zs,sects,xe,ye,ze,secte);
    }

    // get angle
    ang = getangle(xe - xs, ye - ys);

    // get x,y,z, vectors
    xvect = sintable[NORM_ANGLE(ang + 512)];
    yvect = sintable[NORM_ANGLE(ang)];

    // find the distance to the target
    dist = ksqrt(SQ(xe - xs) + SQ(ye - ys));

    if (dist != 0)
    {
        if (xe - xs != 0)
            zvect = scale(xvect, ze - zs, xe - xs);
        else if (ye - ys != 0)
            zvect = scale(yvect, ze - zs, ye - ys);
        else
            zvect = 0;
    }
    else
        zvect = 0;

    hitscan(&s, sects, xvect, yvect, zvect,
            &hitinfo, CLIPMASK_MISSILE);

    if (hitinfo.sect < 0)
        return FALSE;

    // make sure it hit JUST a sector before doing a check
    if (hitinfo.wall < 0 && hitinfo.sprite < 0)
    {
        getzsofslope(hitinfo.sect, hitinfo.x, hitinfo.y, &hiz, &loz);
        if (labs(hitinfo.z - loz) < Z(4))
        {
            if (FAF_ConnectFloor(hitinfo.sect))
            {
                updatesectorz(hitinfo.x, hitinfo.y, hitinfo.z + Z(12), &newsectnum);
                plax_found = TRUE;
            }
        }
        else if (labs(hitinfo.z - hiz) < Z(4))
        {
            if (FAF_ConnectCeiling(hitinfo.sect))
            {
                updatesectorz(hitinfo.x, hitinfo.y, hitinfo.z - Z(12), &newsectnum);
                plax_found = TRUE;
            }
        }
    }
    else
    {
        return cansee(xs,ys,zs,sects,xe,ye,ze,secte);
    }

    if (plax_found)
        return cansee(hitinfo.x,hitinfo.y,hitinfo.z,newsectnum,xe,ye,ze,secte);

    return FALSE;
}

int
GetZadjustment(short sectnum, short hitag)
{
    short i, nexti;
    SPRITEp sp;

    if (sectnum < 0 || !TEST(sector[sectnum].extra, SECTFX_Z_ADJUST))
        return 0L;

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

        if (sp->hitag == hitag && sp->sectnum == sectnum)
        {
            return Z(sp->lotag);
        }
    }

    return 0L;
}

SWBOOL SectorZadjust(int ceilhit, int32_t* hiz, short florhit, int32_t* loz)
{
    extern int PlaxCeilGlobZadjust, PlaxFloorGlobZadjust;
    int z_amt = 0;

    SWBOOL SkipFAFcheck = FALSE;

    if ((int)florhit != -1)
    {
        switch (TEST(florhit, HIT_MASK))
        {
        case HIT_SECTOR:
        {
            short hit_sector = NORM_SECTOR(florhit);

            // don't jack with connect sectors
            if (FAF_ConnectFloor(hit_sector))
            {
                // rippers were dying through the floor in $rock
                if (TEST(sector[hit_sector].floorstat, CEILING_STAT_FAF_BLOCK_HITSCAN))
                    break;

                if (TEST(sector[hit_sector].extra, SECTFX_Z_ADJUST))
                {
                    // see if a z adjust ST1 is around
                    z_amt = GetZadjustment(hit_sector, FLOOR_Z_ADJUST);

                    if (z_amt)
                    {
                        // explicit z adjust overrides Connect Floor
                        *loz += z_amt;
                        SkipFAFcheck = TRUE;
                    }
                }

                break;
            }

            if (!TEST(sector[hit_sector].extra, SECTFX_Z_ADJUST))
                break;

            // see if a z adjust ST1 is around
            z_amt = GetZadjustment(hit_sector, FLOOR_Z_ADJUST);

            if (z_amt)
            {
                // explicit z adjust overrides plax default
                *loz += z_amt;
            }
            else
            // default adjustment for plax
            if (TEST(sector[hit_sector].floorstat, FLOOR_STAT_PLAX))
            {
                *loz += PlaxFloorGlobZadjust;
            }

            break;
        }
        }
    }

    if ((int)ceilhit != -1)
    {
        switch (TEST(ceilhit, HIT_MASK))
        {
        case HIT_SECTOR:
        {
            short hit_sector = NORM_SECTOR(ceilhit);

            // don't jack with connect sectors
            if (FAF_ConnectCeiling(hit_sector))
            {
                if (TEST(sector[hit_sector].extra, SECTFX_Z_ADJUST))
                {
                    // see if a z adjust ST1 is around
                    z_amt = GetZadjustment(hit_sector, CEILING_Z_ADJUST);

                    if (z_amt)
                    {
                        // explicit z adjust overrides Connect Floor
                        *loz += z_amt;
                        SkipFAFcheck = TRUE;
                    }
                }

                break;
            }

            if (!TEST(sector[hit_sector].extra, SECTFX_Z_ADJUST))
                break;

            // see if a z adjust ST1 is around
            z_amt = GetZadjustment(hit_sector, CEILING_Z_ADJUST);

            if (z_amt)
            {
                // explicit z adjust overrides plax default
                *hiz -= z_amt;
            }
            else
            // default adjustment for plax
            if (TEST(sector[hit_sector].ceilingstat, CEILING_STAT_PLAX))
            {
                *hiz -= PlaxCeilGlobZadjust;
            }

            break;
        }
        }
    }

    return SkipFAFcheck;
}

void WaterAdjust(short florhit, int32_t* loz)
{
    switch (TEST(florhit, HIT_MASK))
    {
    case HIT_SECTOR:
    {
        SECT_USERp sectu = SectUser[NORM_SECTOR(florhit)];

        if (sectu && sectu->depth)
            *loz += Z(sectu->depth);
    }
    break;
    case HIT_SPRITE:
        break;
    }
}

void FAFgetzrange(int32_t x, int32_t y, int32_t z, int16_t sectnum,
                  int32_t* hiz, int32_t* ceilhit,
                  int32_t* loz, int32_t* florhit,
                  int32_t clipdist, int32_t clipmask)
{
    int foo1;
    int foo2;
    SWBOOL SkipFAFcheck;

    // IMPORTANT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // This will return invalid FAF ceiling and floor heights inside of analyzesprite
    // because the ceiling and floors get moved out of the way for drawing.

    // early out to regular routine
    if (sectnum < 0 || !FAF_ConnectArea(sectnum))
    {
        getzrange_old(x, y, z, sectnum, hiz,  ceilhit, loz,  florhit, clipdist, clipmask);
        SectorZadjust(*ceilhit, hiz, *florhit, loz);
        WaterAdjust(*florhit, loz);
        return;
    }

    getzrange_old(x, y, z, sectnum, hiz,  ceilhit, loz,  florhit, clipdist, clipmask);
    SkipFAFcheck = SectorZadjust(*ceilhit, hiz, *florhit, loz);
    WaterAdjust(*florhit, loz);

    if (SkipFAFcheck)
        return;

    if (FAF_ConnectCeiling(sectnum))
    {
        short uppersect = sectnum;
        int newz = *hiz - Z(2);

        switch (TEST(*ceilhit, HIT_MASK))
        {
        case HIT_SPRITE:
            return;
        }

        updatesectorz(x, y, newz, &uppersect);
        if (uppersect < 0)
            return; // _ErrMsg(ERR_STD_ARG, "Did not find a sector at %d, %d, %d", x, y, newz);
        getzrange_old(x, y, newz, uppersect, hiz,  ceilhit, &foo1,  &foo2, clipdist, clipmask);
        SectorZadjust(*ceilhit, hiz, -1, NULL);
    }
    else if (FAF_ConnectFloor(sectnum) && !TEST(sector[sectnum].floorstat, FLOOR_STAT_FAF_BLOCK_HITSCAN))
    //if (FAF_ConnectFloor(sectnum))
    {
        short lowersect = sectnum;
        int newz = *loz + Z(2);

        switch (TEST(*florhit, HIT_MASK))
        {
        case HIT_SECTOR:
        {
            break;
        }
        case HIT_SPRITE:
            return;
        }

        updatesectorz(x, y, newz, &lowersect);
        if (lowersect < 0)
            return; // _ErrMsg(ERR_STD_ARG, "Did not find a sector at %d, %d, %d", x, y, newz);
        getzrange_old(x, y, newz, lowersect, &foo1,  &foo2, loz,  florhit, clipdist, clipmask);
        SectorZadjust(-1, NULL, *florhit, loz);
        WaterAdjust(*florhit, loz);
    }
}

void FAFgetzrangepoint(int32_t x, int32_t y, int32_t z, int16_t sectnum,
                       int32_t* hiz, int32_t* ceilhit,
                       int32_t* loz, int32_t* florhit)
{
    int foo1;
    int foo2;
    SWBOOL SkipFAFcheck;

    // IMPORTANT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // This will return invalid FAF ceiling and floor heights inside of analyzesprite
    // because the ceiling and floors get moved out of the way for drawing.

    // early out to regular routine
    if (!FAF_ConnectArea(sectnum))
    {
        getzrangepoint(x, y, z, sectnum, hiz,  ceilhit, loz,  florhit);
        SectorZadjust(*ceilhit, hiz, *florhit, loz);
        WaterAdjust(*florhit, loz);
        return;
    }

    getzrangepoint(x, y, z, sectnum, hiz,  ceilhit, loz,  florhit);
    SkipFAFcheck = SectorZadjust(*ceilhit, hiz, *florhit, loz);
    WaterAdjust(*florhit, loz);

    if (SkipFAFcheck)
        return;

    if (FAF_ConnectCeiling(sectnum))
    {
        short uppersect = sectnum;
        int newz = *hiz - Z(2);
        switch (TEST(*ceilhit, HIT_MASK))
        {
        case HIT_SPRITE:
            return;
        }
        updatesectorz(x, y, newz, &uppersect);
        if (uppersect < 0)
            return; // _ErrMsg(ERR_STD_ARG, "Did not find a sector at %d, %d, %d, sectnum %d", x, y, newz, sectnum);
        getzrangepoint(x, y, newz, uppersect, hiz,  ceilhit, &foo1,  &foo2);
        SectorZadjust(*ceilhit, hiz, -1, NULL);
    }
    else if (FAF_ConnectFloor(sectnum) && !TEST(sector[sectnum].floorstat, FLOOR_STAT_FAF_BLOCK_HITSCAN))
    //if (FAF_ConnectFloor(sectnum))
    {
        short lowersect = sectnum;
        int newz = *loz + Z(2);
        switch (TEST(*florhit, HIT_MASK))
        {
        case HIT_SPRITE:
            return;
        }
        updatesectorz(x, y, newz, &lowersect);
        if (lowersect < 0)
            return; // _ErrMsg(ERR_STD_ARG, "Did not find a sector at %d, %d, %d, sectnum %d", x, y, newz, sectnum);
        getzrangepoint(x, y, newz, lowersect, &foo1,  &foo2, loz,  florhit);
        SectorZadjust(-1, NULL, *florhit, loz);
        WaterAdjust(*florhit, loz);
    }
}

#if 0
SWBOOL
FAF_ConnectCeiling(short sectnum)
{
    return sector[sectnum].ceilingpicnum == FAF_MIRROR_PIC;
}

SWBOOL
FAF_ConnectFloor(short sectnum)
{
    return sector[sectnum].floorpicnum == FAF_MIRROR_PIC;
}
#endif

// doesn't work for blank pics
SWBOOL
PicInView(short tile_num, SWBOOL reset)
{
    if (TEST(gotpic[tile_num >> 3], 1 << (tile_num & 7)))
    {
        if (reset)
            RESET(gotpic[tile_num >> 3], 1 << (tile_num & 7));

        return TRUE;
    }

    return FALSE;
}

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

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

        if (sector[sp->sectnum].ceilingpicnum == FAF_PLACE_MIRROR_PIC)
        {
            sector[sp->sectnum].ceilingpicnum = FAF_MIRROR_PIC;
            SET(sector[sp->sectnum].ceilingstat, CEILING_STAT_PLAX);
        }

        if (sector[sp->sectnum].floorpicnum == FAF_PLACE_MIRROR_PIC)
        {
            sector[sp->sectnum].floorpicnum = FAF_MIRROR_PIC;
            SET(sector[sp->sectnum].floorstat, FLOOR_STAT_PLAX);
        }

        if (sector[sp->sectnum].ceilingpicnum == FAF_PLACE_MIRROR_PIC+1)
            sector[sp->sectnum].ceilingpicnum = FAF_MIRROR_PIC+1;

        if (sector[sp->sectnum].floorpicnum == FAF_PLACE_MIRROR_PIC+1)
            sector[sp->sectnum].floorpicnum = FAF_MIRROR_PIC+1;
    }
}

//This function is like updatesector, but it takes a z-coordinate in addition
//   to help it get the right sector when there's overlapping.  (I may be
//   adding this function to the engine or making the standard updatesector
//   use z's.  Until then, use this.  )

#if 0
void
updatesectorz(int x, int y, int z, short *sectnum)
{
    walltype *wal;
    int i, j, cz, fz;

    ASSERT(*sectnum >=0 && *sectnum <= MAXSECTORS);

    getzsofslope(*sectnum, x, y, &cz, &fz);
    // go ahead and check the current sector
    if ((z >= cz) && (z <= fz))
        if (inside(x, y, *sectnum) != 0)
            return;

    // Test the sectors immediately around your current sector
    if ((*sectnum >= 0) && (*sectnum < numsectors))
    {
        wal = &wall[sector[*sectnum].wallptr];
        j = sector[*sectnum].wallnum;
        do
        {
            i = wal->nextsector;
            if (i >= 0)
            {
                getzsofslope(i, x, y, &cz, &fz);
                if ((z >= cz) && (z <= fz))
                {
                    if (inside(x, y, (short) i) == 1)
                    {
                        *sectnum = i;
                        return;
                    }
                }
            }
            wal++;
            j--;
        }
        while (j != 0);
    }

    // didn't find it yet so test ALL sectors
    for (i = numsectors - 1; i >= 0; i--)
    {
        getzsofslope(i, x, y, &cz, &fz);
        if ((z >= cz) && (z <= fz))
        {
            if (inside(x, y, (short) i) == 1)
            {
                *sectnum = i;
                return;
            }
        }
    }

    *sectnum = -1;
}
#endif

short GlobStackSect[2];

void
GetUpperLowerSector(short match, int x, int y, short *upper, short *lower)
{
    int i;
    short sectorlist[16];
    int sln = 0;
    short SpriteNum, Next;
    SPRITEp sp;

    // keep a list of the last stacked sectors the view was in and
    // check those fisrt
    sln = 0;
    for (i = 0; i < (int)SIZ(GlobStackSect); i++)
    {
        // will not hurt if GlobStackSect is invalid - inside checks for this
        if (inside(x, y, GlobStackSect[i]) == 1)
        {
            SWBOOL found = FALSE;

            TRAVERSE_SPRITE_SECT(headspritesect[GlobStackSect[i]], SpriteNum, Next)
            {
                sp = &sprite[SpriteNum];

                if (sp->statnum == STAT_FAF &&
                    (sp->hitag >= VIEW_LEVEL1 && sp->hitag <= VIEW_LEVEL6)
                    && sp->lotag == match)
                {
                    found = TRUE;
                }
            }

            if (!found)
                continue;

            sectorlist[sln] = GlobStackSect[i];
            sln++;
        }
    }

    // didn't find it yet so test ALL sectors
    if (sln < 2)
    {
        sln = 0;
        for (i = numsectors - 1; i >= 0; i--)
        {
            if (inside(x, y, (short) i) == 1)
            {
                SWBOOL found = FALSE;

                TRAVERSE_SPRITE_SECT(headspritesect[i], SpriteNum, Next)
                {
                    sp = &sprite[SpriteNum];

                    if (sp->statnum == STAT_FAF &&
                        (sp->hitag >= VIEW_LEVEL1 && sp->hitag <= VIEW_LEVEL6)
                        && sp->lotag == match)
                    {
                        found = TRUE;
                    }
                }

                if (!found)
                    continue;

                if (sln < (int)SIZ(GlobStackSect))
                    GlobStackSect[sln] = i;
                if (sln < (int)SIZ(sectorlist))
                    sectorlist[sln] = i;
                sln++;
            }
        }
    }

    // might not find ANYTHING if not tagged right
    if (sln == 0)
    {
        *upper = -1;
        *lower = -1;
        return;
    }
    // Map rooms have NOT been dragged on top of each other
    else if (sln == 1)
    {
        *lower = sectorlist[0];
        *upper = sectorlist[0];
        return;
    }
    // Map rooms HAVE been dragged on top of each other
    // inside will somtimes find that you are in two different sectors if the x,y
    // is exactly on a sector line.
    else if (sln > 2)
    {
        //DSPRINTF(ds, "TOO MANY SECTORS FOUND: x=%d, y=%d, match=%d, num sectors %d, %d, %d, %d, %d, %d", x, y, match, sln, sectorlist[0], sectorlist[1], sectorlist[2], sectorlist[3], sectorlist[4]);
        MONO_PRINT(ds);
        // try again moving the x,y pos around until you only get two sectors
        GetUpperLowerSector(match, x - 1, y, upper, lower);
    }

    if (sln == 2)
    {
        if (sector[sectorlist[0]].floorz < sector[sectorlist[1]].floorz)
        {
            // swap
            // make sectorlist[0] the LOW sector
            short hold;

            hold = sectorlist[0];
            sectorlist[0] = sectorlist[1];
            sectorlist[1] = hold;
        }

        *lower = sectorlist[0];
        *upper = sectorlist[1];
    }
}

SWBOOL
FindCeilingView(short match, int32_t* x, int32_t* y, int32_t z, int16_t* sectnum)
{
    int xoff = 0;
    int yoff = 0;
    short i, nexti;
    SPRITEp sp = NULL;
    int pix_diff;
    int newz;

    save.zcount = 0;

    // Search Stat List For closest ceiling view sprite
    // Get the match, xoff, yoff from this point
    TRAVERSE_SPRITE_STAT(headspritestat[STAT_FAF], i, nexti)
    {
        sp = &sprite[i];

        if (sp->hitag == VIEW_THRU_CEILING && sp->lotag == match)
        {
            xoff = *x - sp->x;
            yoff = *y - sp->y;
            break;
        }
    }

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

        if (sp->lotag == match)
        {
            // determine x,y position
            if (sp->hitag == VIEW_THRU_FLOOR)
            {
                short upper, lower;

                *x = sp->x + xoff;
                *y = sp->y + yoff;

                // get new sector
                GetUpperLowerSector(match, *x, *y, &upper, &lower);
                *sectnum = upper;
                break;
            }
        }
    }

    if (*sectnum < 0)
        return FALSE;

    ASSERT(sp);
    ASSERT(sp->hitag == VIEW_THRU_FLOOR);

    pix_diff = labs(z - sector[sp->sectnum].floorz) >> 8;
    newz = sector[sp->sectnum].floorz + ((pix_diff / 128) + 1) * Z(128);

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

        if (sp->lotag == match)
        {
            // move lower levels ceilings up for the correct view
            if (sp->hitag == VIEW_LEVEL2)
            {
                // save it off
                save.sectnum[save.zcount] = sp->sectnum;
                save.zval[save.zcount] = sector[sp->sectnum].floorz;
                save.pic[save.zcount] = sector[sp->sectnum].floorpicnum;
                save.slope[save.zcount] = sector[sp->sectnum].floorheinum;

                sector[sp->sectnum].floorz = newz;
                // don't change FAF_MIRROR_PIC - ConnectArea
                if (sector[sp->sectnum].floorpicnum != FAF_MIRROR_PIC)
                    sector[sp->sectnum].floorpicnum = FAF_MIRROR_PIC+1;
                sector[sp->sectnum].floorheinum = 0;

                save.zcount++;
                PRODUCTION_ASSERT(save.zcount < ZMAX);
            }
        }
    }

    return TRUE;
}

SWBOOL
FindFloorView(short match, int32_t* x, int32_t* y, int32_t z, int16_t* sectnum)
{
    int xoff = 0;
    int yoff = 0;
    short i, nexti;
    SPRITEp sp = NULL;
    int newz;
    int pix_diff;

    save.zcount = 0;

    // Search Stat List For closest ceiling view sprite
    // Get the match, xoff, yoff from this point
    TRAVERSE_SPRITE_STAT(headspritestat[STAT_FAF], i, nexti)
    {
        sp = &sprite[i];

        if (sp->hitag == VIEW_THRU_FLOOR && sp->lotag == match)
        {
            xoff = *x - sp->x;
            yoff = *y - sp->y;
            break;
        }
    }

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

        if (sp->lotag == match)
        {
            // determine x,y position
            if (sp->hitag == VIEW_THRU_CEILING)
            {
                short upper, lower;

                *x = sp->x + xoff;
                *y = sp->y + yoff;

                // get new sector
                GetUpperLowerSector(match, *x, *y, &upper, &lower);
                *sectnum = lower;
                break;
            }
        }
    }

    if (*sectnum < 0)
        return FALSE;

    ASSERT(sp);
    ASSERT(sp->hitag == VIEW_THRU_CEILING);

    // move ceiling multiple of 128 so that the wall tile will line up
    pix_diff = labs(z - sector[sp->sectnum].ceilingz) >> 8;
    newz = sector[sp->sectnum].ceilingz - ((pix_diff / 128) + 1) * Z(128);

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

        if (sp->lotag == match)
        {
            // move upper levels floors down for the correct view
            if (sp->hitag == VIEW_LEVEL1)
            {
                // save it off
                save.sectnum[save.zcount] = sp->sectnum;
                save.zval[save.zcount] = sector[sp->sectnum].ceilingz;
                save.pic[save.zcount] = sector[sp->sectnum].ceilingpicnum;
                save.slope[save.zcount] = sector[sp->sectnum].ceilingheinum;

                sector[sp->sectnum].ceilingz = newz;

                // don't change FAF_MIRROR_PIC - ConnectArea
                if (sector[sp->sectnum].ceilingpicnum != FAF_MIRROR_PIC)
                    sector[sp->sectnum].ceilingpicnum = FAF_MIRROR_PIC+1;
                sector[sp->sectnum].ceilingheinum = 0;

                save.zcount++;
                PRODUCTION_ASSERT(save.zcount < ZMAX);
            }
        }
    }

    return TRUE;
}

short
ViewSectorInScene(short cursectnum, short level)
{
    int i, nexti;
    SPRITEp sp;
    short match;

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

        if (sp->hitag == level)
        {
            if (cursectnum == sp->sectnum)
            {
                // ignore case if sprite is pointing up
                if (sp->ang == 1536)
                    continue;

                // only gets to here is sprite is pointing down

                // found a potential match
                match = sp->lotag;

                if (!PicInView(FAF_MIRROR_PIC, TRUE))
                    return -1;

                return match;
            }
        }
    }

    return -1;
}

void
DrawOverlapRoom(int tx, int ty, int tz, fix16_t tq16ang, fix16_t tq16horiz, short tsectnum)
{
    short i;
    short match;

    save.zcount = 0;

    match = ViewSectorInScene(tsectnum, VIEW_LEVEL1);
    if (match != -1)
    {
        FindCeilingView(match, &tx, &ty, tz, &tsectnum);

        if (tsectnum < 0)
            return;

        renderDrawRoomsQ16(tx, ty, tz, tq16ang, tq16horiz, tsectnum);
        //FAF_DrawRooms(tx, ty, tz, tq16ang, tq16horiz, tsectnum);

        // reset Z's
        for (i = 0; i < save.zcount; i++)
        {
            sector[save.sectnum[i]].floorz = save.zval[i];
            sector[save.sectnum[i]].floorpicnum = save.pic[i];
            sector[save.sectnum[i]].floorheinum = save.slope[i];
        }

        analyzesprites(tx, ty, tz, FALSE);
        post_analyzesprites();
        renderDrawMasks();

    }
    else
    {
        match = ViewSectorInScene(tsectnum, VIEW_LEVEL2);
        if (match != -1)
        {
            FindFloorView(match, &tx, &ty, tz, &tsectnum);

            if (tsectnum < 0)
                return;

            renderDrawRoomsQ16(tx, ty, tz, tq16ang, tq16horiz, tsectnum);
            //FAF_DrawRooms(tx, ty, tz, tq16ang, tq16horiz, tsectnum);

            // reset Z's
            for (i = 0; i < save.zcount; i++)
            {
                sector[save.sectnum[i]].ceilingz = save.zval[i];
                sector[save.sectnum[i]].ceilingpicnum = save.pic[i];
                sector[save.sectnum[i]].ceilingheinum = save.slope[i];
            }

            analyzesprites(tx, ty, tz, FALSE);
            post_analyzesprites();
            renderDrawMasks();

        }
    }
}

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
*/
//-------------------------------------------------------------------------
#include "build.h"

#include "names2.h"
#include "panel.h"
#include "game.h"
#include "network.h"
#include "tags.h"
#include "sector.h"
#include "text.h"
#include "interp.h"
#include "sprite.h"

short DoRotatorMatch(PLAYERp pp, short match, SWBOOL);
SWBOOL TestRotatorMatchActive(short match);
void InterpSectorSprites(short sectnum, SWBOOL state);
void DoMatchEverything(PLAYERp pp, short match, short state);
void DoRotatorSetInterp(short SpriteNum);
void DoRotatorStopInterp(short SpriteNum);

void ReverseRotator(short SpriteNum)
{
    USERp u = User[SpriteNum];
    ROTATORp r;

    r = u->rotator;

    // if paused go ahead and start it up again
    if (u->Tics)
    {
        u->Tics = 0;
        SetRotatorActive(SpriteNum);
        return;
    }

    // moving toward to OFF pos
    if (r->tgt == 0)
    {
        r->tgt = r->open_dest;
    }
    else if (r->tgt == r->open_dest)
    {
        r->tgt = 0;
    }

    r->vel = -r->vel;
}

SWBOOL
RotatorSwitch(short match, short setting)
{
    SPRITEp sp;
    short i,nexti;
    SWBOOL found = FALSE;

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

        if (sp->lotag == TAG_SPRITE_SWITCH_VATOR && sp->hitag == match)
        {
            found = TRUE;
            AnimateSwitch(sp, setting);
        }
    }

    return found;
}

void SetRotatorActive(short SpriteNum)
{
    USERp u = User[SpriteNum];
    SPRITEp sp = u->SpriteP;
    ROTATORp r;

    r = u->rotator;

    DoRotatorSetInterp(SpriteNum);

    // play activate sound
    DoSoundSpotMatch(SP_TAG2(sp), 1, SOUND_OBJECT_TYPE);

    SET(u->Flags, SPR_ACTIVE);
    u->Tics = 0;

    // moving to the OFF position
    if (r->tgt == 0)
        VatorSwitch(SP_TAG2(sp), OFF);
    else
        VatorSwitch(SP_TAG2(sp), ON);
}

void SetRotatorInactive(short SpriteNum)
{
    USERp u = User[SpriteNum];
    SPRITEp sp = u->SpriteP;

    DoRotatorStopInterp(SpriteNum);

    // play inactivate sound
    DoSoundSpotMatch(SP_TAG2(sp), 2, SOUND_OBJECT_TYPE);

    RESET(u->Flags, SPR_ACTIVE);
}

// called for operation from the space bar
short DoRotatorOperate(PLAYERp pp, short sectnum)
{
    short match;

    match = sector[sectnum].hitag;

    if (match > 0)
    {
        if (TestRotatorMatchActive(match))
            return -1;
        else
            return DoRotatorMatch(pp, match, TRUE);
    }

    return -1;
}

// called from switches and triggers
// returns first vator found
short
DoRotatorMatch(PLAYERp pp, short match, SWBOOL manual)
{
    USERp fu;
    SPRITEp fsp;
    short sectnum;
    short first_vator = -1;

    short i,nexti;

    //RotatorSwitch(match, ON);

    TRAVERSE_SPRITE_STAT(headspritestat[STAT_ROTATOR], i, nexti)
    {
        fsp = &sprite[i];

        if (SP_TAG1(fsp) == SECT_ROTATOR && SP_TAG2(fsp) == match)
        {
            fu = User[i];

            // single play only vator
            // SWBOOL 8 must be set for message to display
            if (TEST_BOOL4(fsp) && (gNet.MultiGameType == MULTI_GAME_COMMBAT || gNet.MultiGameType == MULTI_GAME_AI_BOTS))
            {
                if (pp && TEST_BOOL11(fsp)) PutStringInfo(pp,"This only opens in single play.");
                continue;
            }

            // switch trigger only
            if (SP_TAG3(fsp) == 1)
            {
                // tried to manually operat a switch/trigger only
                if (manual)
                    continue;
            }

            if (first_vator == -1)
                first_vator = i;

            sectnum = fsp->sectnum;

            if (pp && SectUser[sectnum] && SectUser[sectnum]->stag == SECT_LOCK_DOOR && SectUser[sectnum]->number)
            {
                short key_num;

                key_num = SectUser[sectnum]->number;

#if 0
                if (pp->HasKey[key_num - 1])
                {
                    int i;
                    for (i=0; i<numsectors; i++)
                    {
                        if (SectUser[i] && SectUser[i]->stag == SECT_LOCK_DOOR && SectUser[i]->number == key_num)
                            SectUser[i]->number = 0;  // unlock all doors of this type
                    }
                    UnlockKeyLock(key_num);
                }
                else
#endif
                {
                    PutStringInfo(pp, KeyDoorMessage[key_num - 1]);
                    return -1;
                }
            }

            if (TEST(fu->Flags, SPR_ACTIVE))
            {
                ReverseRotator(i);
                continue;
            }

            SetRotatorActive(i);
        }
    }

    return first_vator;
}

SWBOOL
TestRotatorMatchActive(short match)
{
    USERp fu;
    SPRITEp fsp;

    short i,nexti;

    TRAVERSE_SPRITE_STAT(headspritestat[STAT_ROTATOR], i, nexti)
    {
        fsp = &sprite[i];

        if (SP_TAG1(fsp) == SECT_ROTATOR && SP_TAG2(fsp) == match)
        {
            fu = User[i];

            // Does not have to be inactive to be operated
            if (TEST_BOOL6(fsp))
                continue;

            if (TEST(fu->Flags, SPR_ACTIVE) || fu->Tics)
                return TRUE;
        }
    }

    return FALSE;
}

void DoRotatorSetInterp(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    short w,startwall,endwall;

    startwall = sector[sp->sectnum].wallptr;
    endwall = startwall + sector[sp->sectnum].wallnum - 1;

    // move points
    for (w = startwall; w <= endwall; w++)
    {
        setinterpolation(&wall[w].x);
        setinterpolation(&wall[w].y);

        uint16_t const nextwall = wall[w].nextwall;
        if (nextwall < MAXWALLS)
        {
            setinterpolation(&wall[wall[nextwall].point2].x);
            setinterpolation(&wall[wall[nextwall].point2].y);
        }
    }
}

void DoRotatorStopInterp(short SpriteNum)
{
    SPRITEp sp = &sprite[SpriteNum];
    short w,startwall,endwall;

    startwall = sector[sp->sectnum].wallptr;
    endwall = startwall + sector[sp->sectnum].wallnum - 1;

    // move points
    for (w = startwall; w <= endwall; w++)
    {
        stopinterpolation(&wall[w].x);
        stopinterpolation(&wall[w].y);

        uint16_t const nextwall = wall[w].nextwall;
        if (nextwall < MAXWALLS)
        {
            stopinterpolation(&wall[wall[nextwall].point2].x);
            stopinterpolation(&wall[wall[nextwall].point2].y);
        }
    }
}

int DoRotatorMove(short SpriteNum)
{
    USERp u = User[SpriteNum];
    SPRITEp sp = u->SpriteP;
    ROTATORp r;
    short ndx,w,startwall,endwall;
    SPRITEp pivot = NULL;
    int i, nexti;
    vec2_t nxy;
    int dist,closest;
    SWBOOL kill = FALSE;

    r = u->rotator;

    // Example - ang pos moves from 0 to 512 <<OR>> from 0 to -512

    // control SPEED of swinging
    if (r->pos < r->tgt)
    {
        // Increment swing angle
        r->pos += r->speed;
        r->speed += r->vel;

        // if the other way make it equal
        if (r->pos > r->tgt)
            r->pos = r->tgt;
    }

    if (r->pos > r->tgt)
    {
        // Increment swing angle
        r->pos -= r->speed;
        r->speed += r->vel;

        // if the other way make it equal
        if (r->pos < r->tgt)
            r->pos = r->tgt;
    }

    if (r->pos == r->tgt)
    {
        // If ang is OPEN
        if (r->pos == r->open_dest)
        {
            // new tgt is CLOSED (0)
            r->tgt = 0;
            r->vel = -r->vel;
            SetRotatorInactive(SpriteNum);

            if (SP_TAG6(sp))
                DoMatchEverything(NULL, SP_TAG6(sp), -1);

            // wait a bit and close it
            if (u->WaitTics)
                u->Tics = u->WaitTics;
        }
        else
        // If ang is CLOSED then
        if (r->pos == 0)
        {
            short match = SP_TAG2(sp);

            // new tgt is OPEN (open)
            r->tgt = r->open_dest;
            r->speed = r->orig_speed;
            r->vel = labs(r->vel);

            SetRotatorInactive(SpriteNum);

            // set owner swith back to OFF
            // only if ALL vators are inactive
            if (!TestRotatorMatchActive(match))
            {
                //RotatorSwitch(match, OFF);
            }

            if (SP_TAG6(sp) && TEST_BOOL5(sp))
                DoMatchEverything(NULL, SP_TAG6(sp), -1);
        }

        if (TEST_BOOL2(sp))
            kill = TRUE;
    }

    closest = 99999;
    TRAVERSE_SPRITE_STAT(headspritestat[STAT_ROTATOR_PIVOT], i, nexti)
    {
        if (sprite[i].lotag == sp->lotag)
        {
            dist = Distance(sp->x, sp->y, sprite[i].x, sprite[i].y);
            if (dist < closest)
            {
                closest = dist;
                pivot = &sprite[i];
            }
        }
    }

    if (!pivot)
        return 0;

    startwall = sector[sp->sectnum].wallptr;
    endwall = startwall + sector[sp->sectnum].wallnum - 1;

    // move points
    for (w = startwall, ndx = 0; w <= endwall; w++)
    {
        vec2_t const orig = { r->origx[ndx], r->origy[ndx] };
        rotatepoint(pivot->xy, orig, r->pos, &nxy);

        dragpoint(w, nxy.x, nxy.y, 0);
        ndx++;
    }

    if (kill)
    {
        SetRotatorInactive(SpriteNum);
        KillSprite(SpriteNum);
        return 0;
    }

    return 0;
}

int DoRotator(short SpriteNum)
{
    // could move this inside sprite control
    DoRotatorMove(SpriteNum);

    return 0;
}

#include "saveable.h"

static saveable_code saveable_rotator_code[] =
{
    SAVE_CODE(ReverseRotator),
    SAVE_CODE(RotatorSwitch),
    SAVE_CODE(SetRotatorActive),
    SAVE_CODE(SetRotatorInactive),
    SAVE_CODE(DoRotatorOperate),
    SAVE_CODE(DoRotatorMatch),
    SAVE_CODE(TestRotatorMatchActive),
    SAVE_CODE(DoRotatorSetInterp),
    SAVE_CODE(DoRotatorStopInterp),
    SAVE_CODE(DoRotatorMove),
    SAVE_CODE(DoRotator)
};

saveable_module saveable_rotator =
{
    // code
    saveable_rotator_code,
    SIZ(saveable_rotator_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
*/
//-------------------------------------------------------------------------

// rts.c

#include "build.h"
#include "cache1d.h"
#include "baselayer.h"

#include "mytypes.h"
#include "_rts.h"
#include "rts.h"
#include "cache.h"

extern char ds[];
char lumplockbyte[11];

//=============
// STATICS
//=============

static int32_t numlumps = 0;
static intptr_t *lumpcache = NULL;
static lumpinfo_t *lumpinfo = NULL;              // location of each lump on disk

/*
============================================================================

                                                LUMP BASED ROUTINES

============================================================================
*/

/*
====================
=
= RTS_AddFile
=
= All files are optional, but at least one file must be found
= Files with a .rts extension are wadlink files with multiple lumps
= Other files are single lumps with the base filename for the lump name
=
====================
*/

int32_t RTS_AddFile(char *filename)
{
    wadinfo_t  header;
    lumpinfo_t *lump_p;
    int32_t     i;
    int32_t      handle, length;
    int32_t      startlump;
    filelump_t *fileinfo, *fileinfoo;

//
// read the entire file in
//      FIXME: shared opens

    handle = kopen4load(filename, 0);
    if (handle < 0)
    {
        buildprintf("RTS file %s was not found\n",filename);
        return -1;
    }

    startlump = numlumps;

    // WAD file
//   printf("    Adding %s.\n",filename);
    kread(handle, &header, sizeof(header));
    if (strncmp(header.identification,"IWAD",4))
    {
        buildprintf("RTS file %s doesn't have IWAD id\n",filename);
        kclose(handle);
        return -1;
    }
    header.numlumps = B_LITTLE32(header.numlumps);
    header.infotableofs = B_LITTLE32(header.infotableofs);
    length = header.numlumps*sizeof(filelump_t);
    fileinfo = fileinfoo = (filelump_t*)Xmalloc(length);
    if (!fileinfo)
    {
        buildprintf("RTS file could not allocate header info\n");
        kclose(handle);
        return -1;
    }
    klseek(handle, header.infotableofs, SEEK_SET);
    kread(handle, fileinfo, length);

//
// Fill in lumpinfo
//
    lump_p = (lumpinfo_t*)Xrealloc(lumpinfo, (numlumps + header.numlumps)*sizeof(lumpinfo_t));
    if (!lump_p)
    {
        kclose(handle);
        return -1;
    }
    lumpinfo = lump_p;

    numlumps += header.numlumps;

    lump_p = &lumpinfo[startlump];

    for (i=startlump; i<numlumps; i++,lump_p++, fileinfo++)
    {
        lump_p->handle = handle;
        lump_p->position = B_LITTLE32(fileinfo->filepos);
        lump_p->size = B_LITTLE32(fileinfo->size);
        strncpy(lump_p->name, fileinfo->name, 8);
    }

    Xfree(fileinfoo);

    return 0;
}

/*
====================
=
= RTS_Init
=
= Files with a .rts extension are idlink files with multiple lumps
=
====================
*/

void RTS_Init(char *filename)
{
    int32_t length;
    //
    // open all the files, load headers, and count lumps
    //
    numlumps = 0;
    lumpinfo = NULL;   // will be realloced as lumps are added

//   printf("RTS Manager Started\n");
    if (RTS_AddFile(filename)) return;

    if (!numlumps) return;
//      buildprintf ("RTS_Init: no files found");

    //
    // set up caching
    //
    length = (numlumps) * sizeof(*lumpcache);
    lumpcache = (intptr_t*)Xmalloc(length);
    memset(lumpcache,0,length);
}

/*
====================
=
= RTS_Shutdown
=
= shutdown the RTS system
=
====================
*/

void RTS_Shutdown(void)
{
    if (lumpcache)
    {
#if 0
        int32_t i;

        for (i=0; i<numlumps; i++)
        {
            if (lumpcache[i])
            {
                Xfree(lumpcache[i]);
            }
        }
#endif
        Xfree(lumpcache);
    }
    if (lumpinfo) Xfree(lumpinfo);

    numlumps = 0;
    lumpinfo = NULL;
    lumpcache = NULL;
}

/*
====================
=
= RTS_NumSounds
=
====================
*/

int32_t RTS_NumSounds(void)
{
    return numlumps-1;
}

/*
====================
=
= RTS_SoundLength
=
= Returns the buffer size needed to load the given lump
=
====================
*/

int32_t RTS_SoundLength(int32_t lump)
{
    lump++;
    if (lump >= numlumps)
        buildprintf("RTS_SoundLength: %i >= numlumps",lump);
    return lumpinfo[lump].size;
}

/*
====================
=
= RTS_GetSoundName
=
====================
*/

char *RTS_GetSoundName(int32_t i)
{
    i++;
    if (i>=numlumps)
        buildprintf("RTS_GetSoundName: %i >= numlumps",i);
    return &(lumpinfo[i].name[0]);
}

/*
====================
=
= RTS_ReadLump
=
= Loads the lump into the given buffer, which must be >= RTS_SoundLength()
=
====================
*/
void RTS_ReadLump(int32_t lump, intptr_t dest)
{
    lumpinfo_t *l;

    if (lump >= numlumps)
        buildprintf("RTS_ReadLump: %i >= numlumps",lump);
    if (lump < 0)
        buildprintf("RTS_ReadLump: %i < 0",lump);
    l = lumpinfo+lump;
    klseek(l->handle, l->position, SEEK_SET);
    kread(l->handle,(void *)dest,l->size);
}

#if 1
/*
====================
=
= RTS_GetSound
=
====================
*/

// allocates off the cache

void *RTS_GetSound(int32_t lump)
{
    lump++;
    if ((uint16_t)lump >= (uint16_t)numlumps)
        buildprintf("RTS_GetSound: %i >= %i\n",lump,numlumps);

    if (lumpcache[lump] == (intptr_t)NULL)
    {
        lumplockbyte[lump] = CACHE_LOCK_START;
        g_cache.allocateBlock(&lumpcache[lump],(int)RTS_SoundLength(lump-1),&lumplockbyte[lump]);
        RTS_ReadLump(lump, lumpcache[lump]);
    }
    else
    {
        if (lumplockbyte[lump] < CACHE_LOCK_START)
            lumplockbyte[lump] = CACHE_LOCK_START;
        else
            lumplockbyte[lump]++;
    }
    return (void *)lumpcache[lump];
}
#else
/*
====================
=
= RTS_GetSound
=
====================
*/
void *RTS_GetSound(int32_t lump)
{
    lump++;
    if ((uint16_t)lump >= numlumps)
        buildprintf("RTS_GetSound: %i >= numlumps",lump);

    else if (lump < 0)
        buildprintf("RTS_GetSound: %i < 0\n",lump);

    if (lumpcache[lump] == NULL)
    {
        // read the lump in
        lumpcache[lump] = Xmalloc(RTS_SoundLength(lump-1));
        RTS_ReadLump(lump, lumpcache[lump]);
    }
    return (void *)lumpcache[lump];
}
#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.

Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
/*
Copyright (C) 1996, 2003 - 3D Realms Entertainment

This file is part of Duke Nukem 3D version 1.5 - Atomic Edition

Duke Nukem 3D 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: 1996 - Todd Replogle
Prepared for public release: 03/21/2003 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------

//***************************************************************************
//
//    RTS.H
//
//***************************************************************************

#ifndef rts_public__
#define rts_public__

/*
====================
=
= RTS_Init
=
= Files with a .rts extension are idlink files with multiple lumps
=
====================
*/

void RTS_Init(char *filename);
void RTS_Shutdown(void);

/*
====================
=
= RTS_NumSounds
=
====================
*/

int32_t RTS_NumSounds(void);
/*
====================
=
= RTS_SoundLength
=
= Returns the buffer size needed to load the given lump
=
====================
*/

int32_t RTS_SoundLength(int32_t lump);
/*
====================
=
= RTS_GetSoundName
=
====================
*/

char *RTS_GetSoundName(int32_t i);
/*
====================
=
= RTS_GetSound
=
====================
*/
void *RTS_GetSound(int32_t lump);
#endif