Skip to content

Commit

Permalink
Merge pull request #2 from allejo/dev-1_0_1
Browse files Browse the repository at this point in the history
Add sanity checks + Improving spawning consistency
  • Loading branch information
allejo committed Mar 25, 2016
2 parents 457151c + 6f0406b commit 8c6434c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bztoolkit
Submodule bztoolkit updated 5 files
+16 −11 .gitignore
+25 −543 LICENSE.md
+21 −21 README.md
+19 −15 bzToolkit.h
+46 −53 bzToolkitAPI.h
119 changes: 62 additions & 57 deletions matchTrainerAssistant.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Vladimir "allejo" Jimenez
Copyright (C) 2016 Vladimir "allejo" Jimenez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,52 +20,37 @@
THE SOFTWARE.
*/

#include <cmath>
#include <sstream>

#include "bzfsAPI.h"
#include "bztoolkit/bzToolkitAPI.h"

// Define plug-in name
const std::string PLUGIN_NAME = "Match Trainer Assistant";
std::string PLUGIN_NAME = "Match Trainer Assistant";

// Define plug-in version numbering
const int MAJOR = 1;
const int MINOR = 0;
const int REV = 0;
const int BUILD = 4;
int MAJOR = 1;
int MINOR = 0;
int REV = 1;
int BUILD = 5;

class MatchTrainerAssistant : public bz_Plugin, public bz_CustomSlashCommandHandler
{
public:
virtual const char* Name ();
virtual const char* Name () { return bztk_pluginName(); }
virtual void Init (const char* config);
virtual void Event (bz_EventData *eventData);
virtual void Cleanup (void);

virtual bool SlashCommand (int playerID, bz_ApiString, bz_ApiString, bz_APIStringList*);

bool protectedPos[256];
bool handleSpawn[256];
float lastDeaths[256][4];
float nextSpawnLocation[256][4];
};

BZ_PLUGIN(MatchTrainerAssistant)

const char* MatchTrainerAssistant::Name (void)
{
static std::string pluginBuild = "";

if (!pluginBuild.size())
{
std::ostringstream pluginBuildStream;

pluginBuildStream << PLUGIN_NAME << " " << MAJOR << "." << MINOR << "." << REV << " (" << BUILD << ")";
pluginBuild = pluginBuildStream.str();
}

return pluginBuild.c_str();
}

void MatchTrainerAssistant::Init (const char* /*commandLine*/)
{
// Register our events with Register()
Expand Down Expand Up @@ -109,21 +94,20 @@ void MatchTrainerAssistant::Event (bz_EventData *eventData)

for (int i = 0; i < 4; i++)
{
if (isnan(lastDeaths[spawnData->playerID][i]))
if (isnan(nextSpawnLocation[spawnData->playerID][i]))
{
return;
}
}

if (handleSpawn[spawnData->playerID])
{
spawnData->handled = handleSpawn[spawnData->playerID];
spawnData->pos[0] = lastDeaths[spawnData->playerID][0];
spawnData->pos[1] = lastDeaths[spawnData->playerID][1];
spawnData->pos[2] = lastDeaths[spawnData->playerID][2];
spawnData->rot = lastDeaths[spawnData->playerID][3];
spawnData->handled = true;
spawnData->pos[0] = nextSpawnLocation[spawnData->playerID][0];
spawnData->pos[1] = nextSpawnLocation[spawnData->playerID][1];
spawnData->pos[2] = nextSpawnLocation[spawnData->playerID][2];
spawnData->rot = nextSpawnLocation[spawnData->playerID][3];

protectedPos[spawnData->playerID] = false;
handleSpawn[spawnData->playerID] = false;
}
}
Expand All @@ -133,15 +117,15 @@ void MatchTrainerAssistant::Event (bz_EventData *eventData)
{
bz_PlayerDieEventData_V1* dieData = (bz_PlayerDieEventData_V1*)eventData;

if (!protectedPos[dieData->playerID])
// If they aren't killed by the server, save their last position. This is done because whenever someone does a /spawn, they are killed in order to
// be respawned elsewhere
if (dieData->killerID != ServerPlayer)
{
lastDeaths[dieData->playerID][0] = dieData->state.pos[0];
lastDeaths[dieData->playerID][1] = dieData->state.pos[1];
lastDeaths[dieData->playerID][2] = dieData->state.pos[2];
lastDeaths[dieData->playerID][3] = dieData->state.rotation;
nextSpawnLocation[dieData->playerID][0] = dieData->state.pos[0];
nextSpawnLocation[dieData->playerID][1] = dieData->state.pos[1];
nextSpawnLocation[dieData->playerID][2] = dieData->state.pos[2];
nextSpawnLocation[dieData->playerID][3] = dieData->state.rotation;
}

protectedPos[dieData->playerID] = false;
}
break;

Expand All @@ -150,13 +134,12 @@ void MatchTrainerAssistant::Event (bz_EventData *eventData)
{
bz_PlayerJoinPartEventData_V1* joinPartData = (bz_PlayerJoinPartEventData_V1*)eventData;

protectedPos[joinPartData->playerID] = false;
handleSpawn[joinPartData->playerID] = false;

lastDeaths[joinPartData->playerID][0] = NAN;
lastDeaths[joinPartData->playerID][1] = NAN;
lastDeaths[joinPartData->playerID][2] = NAN;
lastDeaths[joinPartData->playerID][3] = NAN;
nextSpawnLocation[joinPartData->playerID][0] = NAN;
nextSpawnLocation[joinPartData->playerID][1] = NAN;
nextSpawnLocation[joinPartData->playerID][2] = NAN;
nextSpawnLocation[joinPartData->playerID][3] = NAN;
}
break;

Expand All @@ -178,6 +161,8 @@ bool MatchTrainerAssistant::SlashCommand(int playerID, bz_ApiString command, bz_

if (command == "spawn")
{
int maxXY = bz_getBZDBInt("_worldSize") / 2;

if (params->size() > 0)
{
if (params->size() != 1 && params->size() != 4)
Expand All @@ -203,28 +188,49 @@ bool MatchTrainerAssistant::SlashCommand(int playerID, bz_ApiString command, bz_
return true;
}

lastDeaths[playerID][0] = target->lastKnownState.pos[0];
lastDeaths[playerID][1] = target->lastKnownState.pos[1];
lastDeaths[playerID][2] = target->lastKnownState.pos[2];
lastDeaths[playerID][3] = target->lastKnownState.rotation;
nextSpawnLocation[playerID][0] = target->lastKnownState.pos[0];
nextSpawnLocation[playerID][1] = target->lastKnownState.pos[1];
nextSpawnLocation[playerID][2] = target->lastKnownState.pos[2];
nextSpawnLocation[playerID][3] = target->lastKnownState.rotation;
}
else
{
lastDeaths[playerID][0] = std::atof(params->get(0).c_str());
lastDeaths[playerID][1] = std::atof(params->get(1).c_str());
lastDeaths[playerID][2] = std::atof(params->get(2).c_str());
lastDeaths[playerID][3] = std::atof(params->get(3).c_str());
float posX = std::atof(params->get(0).c_str());
float posY = std::atof(params->get(1).c_str());
float posZ = std::atof(params->get(2).c_str());
float rot = std::atof(params->get(3).c_str());

if (std::abs(posX) > maxXY || std::abs(posY) > maxXY)
{
bz_sendTextMessagef(BZ_SERVER, playerID, "The following %s value (%.2f) is outside of this world",
(std::abs(posX) > maxXY) ? "X" : "Y",
(std::abs(posX) > maxXY) ? posX : posY);

return true;
}

float maxZ = bz_getWorldMaxHeight();

if (posZ < 0 || (maxZ > 0 && posZ > maxZ))
{
bz_sendTextMessagef(BZ_SERVER, playerID, "The following Z value (%.2f) is outside of this world", posZ);

return true;
}

nextSpawnLocation[playerID][0] = posX;
nextSpawnLocation[playerID][1] = posY;
nextSpawnLocation[playerID][2] = posZ;
nextSpawnLocation[playerID][3] = rot;
}
}

protectedPos[playerID] = true;
handleSpawn[playerID] = true;

if (pr->spawned)
{
bz_killPlayer(playerID, BZ_SERVER);
bz_killPlayer(playerID, ServerPlayer);
}

handleSpawn[playerID] = true;
bztk_forcePlayerSpawn(playerID);

return true;
Expand All @@ -242,8 +248,7 @@ bool MatchTrainerAssistant::SlashCommand(int playerID, bz_ApiString command, bz_
}
else if (command == "die")
{
protectedPos[playerID] = true;
bz_killPlayer(playerID, BZ_SERVER);
bz_killPlayer(playerID, false, playerID);

return true;
}
Expand Down

0 comments on commit 8c6434c

Please sign in to comment.