raze/source/games/sw/src/rotator.cpp

381 lines
8.9 KiB
C++
Raw Normal View History

//-------------------------------------------------------------------------
/*
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 "ns.h"
#include "build.h"
#include "names2.h"
#include "panel.h"
2020-08-05 22:18:45 +00:00
#include "misc.h"
#include "network.h"
#include "tags.h"
#include "sector.h"
#include "interpolate.h"
#include "sprite.h"
#include "quotemgr.h"
BEGIN_SW_NS
2021-11-02 18:53:03 +00:00
void DoRotatorMatch(PLAYERp pp, short match, bool);
2020-09-09 18:32:24 +00:00
bool TestRotatorMatchActive(short match);
void DoMatchEverything(PLAYERp pp, short match, short state);
2021-11-02 18:53:03 +00:00
void DoRotatorSetInterp(DSWActor*);
void DoRotatorStopInterp(DSWActor*);
2021-11-02 18:41:37 +00:00
void ReverseRotator(DSWActor* actor)
{
ROTATORp r;
2021-12-25 22:16:26 +00:00
r = actor->user.rotator.Data();
// if paused go ahead and start it up again
2021-12-25 22:16:26 +00:00
if (actor->user.Tics)
{
2021-12-25 22:16:26 +00:00
actor->user.Tics = 0;
2021-11-02 18:41:37 +00:00
SetRotatorActive(actor);
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;
}
2021-11-02 18:41:37 +00:00
bool RotatorSwitch(short match, short setting)
{
2020-09-09 18:32:24 +00:00
bool found = false;
2021-11-02 18:41:37 +00:00
SWStatIterator it(STAT_DEFAULT);
while (auto actor = it.Next())
{
if (actor->spr.lotag == TAG_SPRITE_SWITCH_VATOR && actor->spr.hitag == match)
{
found = true;
2021-12-24 16:32:27 +00:00
AnimateSwitch(actor, setting);
}
}
return found;
}
2021-11-02 18:41:37 +00:00
void SetRotatorActive(DSWActor* actor)
{
ROTATORp r;
2021-12-25 22:16:26 +00:00
r = actor->user.rotator.Data();
2021-11-02 18:53:03 +00:00
DoRotatorSetInterp(actor);
// play activate sound
DoSoundSpotMatch(SP_TAG2(actor), 1, SOUND_OBJECT_TYPE);
actor->user.Flags |= (SPR_ACTIVE);
2021-12-25 22:16:26 +00:00
actor->user.Tics = 0;
// moving to the OFF position
if (r->tgt == 0)
VatorSwitch(SP_TAG2(actor), false);
else
VatorSwitch(SP_TAG2(actor), true);
}
2021-11-02 18:53:03 +00:00
void SetRotatorInactive(DSWActor* actor)
{
2021-11-02 18:53:03 +00:00
DoRotatorStopInterp(actor);
// play inactivate sound
DoSoundSpotMatch(SP_TAG2(actor), 2, SOUND_OBJECT_TYPE);
2021-12-27 17:58:15 +00:00
actor->user.Flags &= ~(SPR_ACTIVE);
}
// called for operation from the space bar
2021-11-25 15:55:46 +00:00
void DoRotatorOperate(PLAYERp pp, sectortype* sect)
{
2021-11-25 15:55:46 +00:00
short match = sect->hitag;
if (match > 0)
{
2021-11-02 18:53:03 +00:00
if (!TestRotatorMatchActive(match))
DoRotatorMatch(pp, match, true);
}
}
// called from switches and triggers
// returns first vator found
2021-11-02 18:53:03 +00:00
void DoRotatorMatch(PLAYERp pp, short match, bool manual)
{
2021-11-02 18:41:37 +00:00
DSWActor* firstVator = nullptr;
//RotatorSwitch(match, ON);
2021-11-02 18:41:37 +00:00
SWStatIterator it(STAT_ROTATOR);
while (auto actor = it.Next())
{
if (SP_TAG1(actor) == SECT_ROTATOR && SP_TAG2(actor) == match)
{
// single play only vator
2020-09-09 18:32:24 +00:00
// bool 8 must be set for message to display
2021-12-24 23:32:23 +00:00
if (TEST_BOOL4(actor) && (gNet.MultiGameType == MULTI_GAME_COMMBAT || gNet.MultiGameType == MULTI_GAME_AI_BOTS))
{
2021-12-24 23:32:23 +00:00
if (pp && TEST_BOOL11(actor)) PutStringInfo(pp, GStrings("TXT_SPONLY"));
continue;
}
// switch trigger only
2021-12-24 23:32:23 +00:00
if (SP_TAG3(actor) == 1)
{
// tried to manually operat a switch/trigger only
if (manual)
continue;
}
2021-11-02 18:41:37 +00:00
if (firstVator == nullptr)
firstVator = actor;
auto sect = actor->sector();
2021-11-20 22:20:43 +00:00
if (pp && sect->hasU() && sect->stag == SECT_LOCK_DOOR && sect->number)
{
2021-11-20 22:20:43 +00:00
int key_num = sect->number;
{
PutStringInfo(pp, quoteMgr.GetQuote(QUOTE_DOORMSG + key_num - 1));
2021-11-02 18:53:03 +00:00
return;
}
}
2021-12-27 18:34:06 +00:00
if (actor->user.Flags & (SPR_ACTIVE))
{
2021-11-02 18:41:37 +00:00
ReverseRotator(actor);
continue;
}
2021-11-02 18:41:37 +00:00
SetRotatorActive(actor);
}
}
}
2021-11-02 18:53:03 +00:00
bool TestRotatorMatchActive(short match)
{
2021-11-02 18:53:03 +00:00
SWStatIterator it(STAT_ROTATOR);
while (auto actor = it.Next())
{
if (SP_TAG1(actor) == SECT_ROTATOR && SP_TAG2(actor) == match)
{
// Does not have to be inactive to be operated
2021-12-24 23:32:23 +00:00
if (TEST_BOOL6(actor))
continue;
2021-12-27 18:34:06 +00:00
if (actor->user.Flags & (SPR_ACTIVE) || actor->user.Tics)
return true;
}
}
return false;
}
2021-11-02 18:53:03 +00:00
void DoRotatorSetInterp(DSWActor* actor)
{
for(auto& wal : wallsofsector(actor->sector()))
{
StartInterpolation(&wal, Interp_Wall_X);
StartInterpolation(&wal, Interp_Wall_Y);
if (wal.twoSided())
{
auto w2 = wal.nextWall()->point2Wall();
StartInterpolation(w2, Interp_Wall_X);
StartInterpolation(w2, Interp_Wall_Y);
}
}
}
2021-11-02 18:53:03 +00:00
void DoRotatorStopInterp(DSWActor* actor)
{
for (auto& wal : wallsofsector(actor->sector()))
{
StopInterpolation(&wal, Interp_Wall_X);
StopInterpolation(&wal, Interp_Wall_Y);
if (wal.twoSided())
{
auto w2 = wal.nextWall()->point2Wall();
StopInterpolation(w2, Interp_Wall_X);
StopInterpolation(w2, Interp_Wall_Y);
}
}
}
2021-11-02 18:53:03 +00:00
int DoRotator(DSWActor* actor)
{
ROTATORp r;
short ndx,w,startwall,endwall;
2021-12-24 23:45:19 +00:00
DSWActor* pivot = nullptr;
vec2_t nxy;
int dist,closest;
2020-09-09 18:32:24 +00:00
bool kill = false;
2021-12-25 22:16:26 +00:00
r = actor->user.rotator.Data();
// 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;
2021-11-02 18:53:03 +00:00
SetRotatorInactive(actor);
if (SP_TAG6(actor))
DoMatchEverything(nullptr, SP_TAG6(actor), -1);
// wait a bit and close it
2021-12-25 22:16:26 +00:00
if (actor->user.WaitTics)
actor->user.Tics = actor->user.WaitTics;
}
else
// If ang is CLOSED then
if (r->pos == 0)
{
short match = SP_TAG2(actor);
// new tgt is OPEN (open)
r->tgt = r->open_dest;
r->speed = r->orig_speed;
r->vel = labs(r->vel);
2021-11-02 18:53:03 +00:00
SetRotatorInactive(actor);
2021-11-02 23:14:09 +00:00
// set Owner swith back to OFF
// only if ALL vators are inactive
if (!TestRotatorMatchActive(match))
{
//RotatorSwitch(match, OFF);
}
if (SP_TAG6(actor) && TEST_BOOL5(actor))
DoMatchEverything(nullptr, SP_TAG6(actor), -1);
}
if (TEST_BOOL2(actor))
kill = true;
}
closest = 99999;
2021-11-06 08:29:38 +00:00
SWStatIterator it(STAT_ROTATOR_PIVOT);
while (auto itActor = it.Next())
{
2021-12-24 23:45:19 +00:00
if (itActor->spr.lotag == actor->spr.lotag)
{
2021-12-24 23:45:19 +00:00
dist = Distance(actor->spr.pos.X, actor->spr.pos.Y, itActor->spr.pos.X, itActor->spr.pos.Y);
if (dist < closest)
{
closest = dist;
2021-12-24 23:45:19 +00:00
pivot = itActor;
}
}
}
if (!pivot)
return 0;
// move points
2021-11-24 21:08:41 +00:00
ndx = 0;
for(auto& wal : wallsofsector(actor->sector()))
{
vec2_t const orig = { r->origX[ndx], r->origY[ndx] };
2021-12-24 23:45:19 +00:00
rotatepoint(pivot->spr.pos.vec2, orig, r->pos, &nxy);
2021-12-22 09:28:51 +00:00
dragpoint(&wal, nxy.X, nxy.Y);
ndx++;
}
if (kill)
{
2021-11-02 18:53:03 +00:00
SetRotatorInactive(actor);
2021-10-30 20:53:24 +00:00
KillActor(actor);
return 0;
}
return 0;
}
#include "saveable.h"
static saveable_code saveable_rotator_code[] =
{
SAVE_CODE(DoRotator)
};
saveable_module saveable_rotator =
{
// code
saveable_rotator_code,
SIZ(saveable_rotator_code),
// data
nullptr,0
};
END_SW_NS