Skip to content

Commit

Permalink
Make world weapon API only use vectors.
Browse files Browse the repository at this point in the history
add utility functions to convert old angles or targets into a vector.
remove ability for API to override shot params so players can expect consistent behavior from shots.
extend shot meta data to include strings.
  • Loading branch information
JeffM2501 authored and allejo committed Feb 25, 2018
1 parent 8ce5e87 commit e001476
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 134 deletions.
28 changes: 16 additions & 12 deletions include/bzfsAPI.h
Expand Up @@ -1037,7 +1037,7 @@ class BZF_API bz_ServerShotFiredEventData_V1 : public bz_EventData
bz_ApiString flagType;
float lifetime;
float pos[3];
float lookAt[3];
float velocity[3];
bz_eTeamType team;
};

Expand Down Expand Up @@ -1630,24 +1630,28 @@ BZF_API bool bz_sendTextMessagef(int from, bz_eTeamType to, const char* fmt, ...
BZF_API bool bz_sentFetchResMessage ( int playerID, const char* URL );

// world weapons
DEPRECATED BZF_API bool bz_fireWorldWep ( const char* flagType, float lifetime, int fromPlayer, float *pos, float tilt, float direction, int shotID, float dt, bz_eTeamType shotTeam = eRogueTeam );
DEPRECATED BZF_API bool bz_fireWorldWep( const char* flagType, float lifetime, int fromPlayer, float *pos, float tilt, float direction, float speed, int* shotID, float dt, bz_eTeamType shotTeam = eRogueTeam );
DEPRECATED BZF_API bool bz_fireWorldWep( const char* flagType, float lifetime, int fromPlayer, float *pos, float tilt, float direction, int* shotID, float dt, bz_eTeamType shotTeam = eRogueTeam );
DEPRECATED BZF_API int bz_fireWorldGM ( int targetPlayerID, float lifetime, float *pos, float tilt, float direction, float dt, bz_eTeamType shotTeam = eRogueTeam);
// deprecated API, will be removed in next version
BZF_API bool bz_fireWorldWep ( const char* flagType, float lifetime, int fromPlayer, float *pos, float tilt, float direction, int shotID, float dt, bz_eTeamType shotTeam = eRogueTeam );
BZF_API bool bz_fireWorldWep( const char* flagType, float lifetime, int fromPlayer, float *pos, float tilt, float direction, float speed, int* shotID, float dt, bz_eTeamType shotTeam = eRogueTeam );
BZF_API bool bz_fireWorldWep( const char* flagType, float lifetime, int fromPlayer, float *pos, float tilt, float direction, int* shotID, float dt, bz_eTeamType shotTeam = eRogueTeam );
BZF_API int bz_fireWorldGM ( int targetPlayerID, float lifetime, float *pos, float tilt, float direction, float dt, bz_eTeamType shotTeam = eRogueTeam);

BZF_API uint32_t bz_fireServerShot(const char* shotType, float lifetime, float origin[3], float lookAtVector[3], bz_eTeamType color = eRogueTeam, int targetPlayerId = -1);
BZF_API uint32_t bz_fireServerShot(const char* shotType, float lifetime, float origin[3], float tilt, float direction, bz_eTeamType color = eRogueTeam, int targetPlayerId = -1);
// new server shot API
BZF_API uint32_t bz_fireServerShot(const char* shotType, float origin[3], float vector[3], bz_eTeamType color = eRogueTeam, int targetPlayerId = -1);

BZF_API const bz_ApiString bz_getShotMetaData(const uint32_t shotGUID, const char* name);
BZF_API bool bz_hasShotMetaData(const uint32_t shotGUID, const char* name);
BZF_API void bz_setShotMetaData (const uint32_t shotGUID, const char* name, uint32_t value);
BZF_API void bz_setShotMetaData(const uint32_t shotGUID, const char* name, const char* value);
BZF_API bool bz_shotHasMetaData (const uint32_t shotGUID, const char* name);

BZF_API uint32_t bz_getShotMetaData (int fromPlayer, int shotID, const char* name);
BZF_API void bz_setShotMetaData (int fromPlayer, int shotID, const char* name, uint32_t value);
BZF_API bool bz_shotHasMetaData (int fromPlayer, int shotID, const char* name);
BZF_API uint32_t bz_getShotMetaDataI(const uint32_t shotGUID, const char* name);
BZF_API const char* bz_getShotMetaDataS(const uint32_t shotGUID, const char* name);

BZF_API uint32_t bz_getShotGUID (int fromPlayer, int shotID);

// geometry utils
BZF_API bool bz_vectorFromPoints(const float p1[3], const float p2[3], float outVec[3]);
BZF_API bool bz_vectorFromRotations(const float tilt, const float rotation, float outVec[3]);

typedef struct {
int year;
int month;
Expand Down
33 changes: 20 additions & 13 deletions src/bzfs/GameKeeper.cxx
Expand Up @@ -370,6 +370,25 @@ void GameKeeper::Player::setMaxShots(int _maxShots)
maxShots = _maxShots;
}

float GetShotLifetime(FlagType* flagType)
{
float lifeTime(BZDB.eval(StateDatabase::BZDB_RELOADTIME));
if (flagType == Flags::RapidFire)
lifeTime *= BZDB.eval(StateDatabase::BZDB_RFIREADLIFE);
else if (flagType == Flags::MachineGun)
lifeTime *= BZDB.eval(StateDatabase::BZDB_MGUNADLIFE);
else if (flagType == Flags::GuidedMissile)
lifeTime *= BZDB.eval(StateDatabase::BZDB_GMADLIFE) + .01f;
else if (flagType == Flags::Laser)
lifeTime *= BZDB.eval(StateDatabase::BZDB_LASERADLIFE);
else if (flagType == Flags::ShockWave)
lifeTime *= BZDB.eval(StateDatabase::BZDB_SHOCKADLIFE);
else if (flagType == Flags::Thief)
lifeTime *= BZDB.eval(StateDatabase::BZDB_THIEFADLIFE);

return lifeTime;
}

bool GameKeeper::Player::addShot(int id, int salt, FiringInfo &firingInfo)
{
float now((float)TimeKeeper::getCurrent().getSeconds());
Expand All @@ -388,19 +407,7 @@ bool GameKeeper::Player::addShot(int id, int salt, FiringInfo &firingInfo)

shotsInfo.resize(maxShots);

float lifeTime(BZDB.eval(StateDatabase::BZDB_RELOADTIME));
if (firingInfo.flagType == Flags::RapidFire)
lifeTime *= BZDB.eval(StateDatabase::BZDB_RFIREADLIFE);
else if (firingInfo.flagType == Flags::MachineGun)
lifeTime *= BZDB.eval(StateDatabase::BZDB_MGUNADLIFE);
else if (firingInfo.flagType == Flags::GuidedMissile)
lifeTime *= BZDB.eval(StateDatabase::BZDB_GMADLIFE) + .01f;
else if (firingInfo.flagType == Flags::Laser)
lifeTime *= BZDB.eval(StateDatabase::BZDB_LASERADLIFE);
else if (firingInfo.flagType == Flags::ShockWave)
lifeTime *= BZDB.eval(StateDatabase::BZDB_SHOCKADLIFE);
else if (firingInfo.flagType == Flags::Thief)
lifeTime *= BZDB.eval(StateDatabase::BZDB_THIEFADLIFE);
float lifeTime = GetShotLifetime(firingInfo.flagType);

ShotInfo myShot;
myShot.firingInfo = firingInfo;
Expand Down
31 changes: 31 additions & 0 deletions src/bzfs/ShotManager.cxx
Expand Up @@ -227,6 +227,37 @@ namespace Shots
{
}

void Shot::SetMetaData(const std::string& name, const char* data)
{
if (MetaData.find(name) == MetaData.end())
MetaData[name] = MetaDataItem();
MetaData[name].DataS = data;
}

void Shot::SetMetaData(const std::string& name, uint32_t data)
{
if (MetaData.find(name) == MetaData.end())
MetaData[name] = MetaDataItem();
MetaData[name].DataI = data;
}

bool Shot::HasMetaData(const std::string& name)
{
return MetaData.find(name) != MetaData.end();
}

const char * Shot::GetMetaDataS(const std::string& name)
{
auto itr = MetaData.find(name);
return itr == MetaData.end() ? nullptr : itr->second.DataS.c_str();
}

uint32_t Shot::GetMetaDataI(const std::string& name)
{
auto itr = MetaData.find(name);
return itr == MetaData.end() ? 0 : itr->second.DataI;
}

bool Shot::Update()
{
if (Logic.Update(*this))
Expand Down
22 changes: 19 additions & 3 deletions src/bzfs/ShotManager.h
Expand Up @@ -67,15 +67,23 @@ class Shot
FlightLogic &Logic;

double LifeTime;

class MetaDataItem
{
public:
std::string Name;
std::string DataS;
uint32_t DataI;
};

std::map<std::string, MetaDataItem> MetaData;

public:
fvec3 StartPosition;
fvec3 LastUpdatePosition;
double LastUpdateTime;
double StartTime;

std::map<std::string, std::string> stringMetaData;
std::map<std::string, uint32_t> intMetaData;

FiringInfo Info;

PlayerId Target;
Expand Down Expand Up @@ -103,6 +111,14 @@ class Shot
bool CollideBox ( fvec3 &center, fvec3 size, float rotation ) {return Logic.CollideBox(*this,center,size,rotation);}
bool CollideSphere ( fvec3 &center, float radius ) {return Logic.CollideSphere(*this,center,radius);}
bool CollideCylinder ( fvec3 &center, float height, float radius) {return Logic.CollideCylinder(*this,center,height,radius);}

// meta data API
void SetMetaData(const std::string& name, const char* data);
void SetMetaData(const std::string& name, uint32_t data);
bool HasMetaData(const std::string& name);
const char * GetMetaDataS(const std::string& name);
uint32_t GetMetaDataI(const std::string& name);

};

typedef std::shared_ptr<Shot> ShotRef;
Expand Down
34 changes: 18 additions & 16 deletions src/bzfs/WorldWeapons.cxx
Expand Up @@ -29,7 +29,7 @@
#include "bzfs.h"
#include "ShotManager.h"

uint32_t WorldWeapons::fireShot(FlagType* type, float lifetime, float *pos, float tilt, float direction, float shotSpeed, int *shotID, float delayTime, TeamColor teamColor, PlayerId targetPlayerID)
uint32_t WorldWeapons::fireShot(FlagType* type, float lifetime, const float origin[3], const float vector[3], float shotSpeed, int *shotID, float delayTime, TeamColor teamColor, PlayerId targetPlayerID)
{
if (!BZDB.isTrue(StateDatabase::BZDB_WEAPONS)) {
return INVALID_SHOT_GUID;
Expand All @@ -42,23 +42,22 @@ uint32_t WorldWeapons::fireShot(FlagType* type, float lifetime, float *pos, floa
firingInfo.flagType = type;
firingInfo.lifetime = lifetime;
firingInfo.shot.player = ServerPlayer;
memmove(firingInfo.shot.pos, pos, 3 * sizeof(float));
memmove(firingInfo.shot.pos, origin, 3 * sizeof(float));

if (shotSpeed < 0)
shotSpeed = BZDB.eval(StateDatabase::BZDB_SHOTSPEED);
const float tiltFactor = cosf(tilt);

firingInfo.shot.vel[0] = shotSpeed * tiltFactor * cosf(direction);
firingInfo.shot.vel[1] = shotSpeed * tiltFactor * sinf(direction);
firingInfo.shot.vel[2] = shotSpeed * sinf(tilt);
for (int i = 0; i < 3; i++)
firingInfo.shot.vel[i] = vector[i] * shotSpeed;

firingInfo.shot.dt = delayTime;
firingInfo.shot.team = teamColor;

if (shotID != NULL && shotID == 0) {
if (shotID != nullptr && shotID == 0) {
*shotID = getNewWorldShotID();
firingInfo.shot.id = *shotID;
}
else if (shotID == NULL) {
else if (shotID == nullptr) {
firingInfo.shot.id = getNewWorldShotID();
}
else {
Expand Down Expand Up @@ -87,12 +86,10 @@ uint32_t WorldWeapons::fireShot(FlagType* type, float lifetime, float *pos, floa
event.guid = shotGUID;
event.flagType = type->flagAbbv;
event.lifetime = lifetime;
event.pos[0] = pos[0];
event.pos[1] = pos[1];
event.pos[2] = pos[2];
event.lookAt[0] = cosf(direction);
event.lookAt[1] = sinf(direction);
event.lookAt[2] = sinf(tilt);
for (int i = 0; i < 3; i++){
event.pos[i] = origin[i];
event.velocity[i] = firingInfo.shot.vel[i];
}
event.team = convertTeam(teamColor);

WorldEventManager worldEventManager;
Expand Down Expand Up @@ -148,7 +145,9 @@ void WorldWeapons::fire()
if (w->nextTime <= nowTime) {
FlagType type = *(w->type); // non-const copy

fireShot(&type, BZDB.eval(StateDatabase::BZDB_RELOADTIME), w->origin, w->tilt, w->direction, w->direction, NULL, 0, w->teamColor);
float vec[3] = { 0,0,0 };
bz_vectorFromRotations(w->tilt, w->direction, vec);
fireShot(&type, BZDB.eval(StateDatabase::BZDB_RELOADTIME), w->origin, vec, -1, nullptr, 0, w->teamColor);

//Set up timer for next shot, and eat any shots that have been missed
while (w->nextTime <= nowTime) {
Expand Down Expand Up @@ -283,7 +282,10 @@ void WorldWeaponGlobalEventHandler::process (bz_EventData *eventData)
if (capEvent->teamCapped != team)
return;

world->getWorldWeapons().fireShot(type, BZDB.eval(StateDatabase::BZDB_RELOADTIME), origin, tilt, direction, -1, NULL, 0);
float vec[3] = { 0,0,0 };
bz_vectorFromRotations(tilt, direction, vec);

world->getWorldWeapons().fireShot(type, BZDB.eval(StateDatabase::BZDB_RELOADTIME), origin, vec, -1, NULL, 0);
}

// Local Variables: ***
Expand Down
2 changes: 1 addition & 1 deletion src/bzfs/WorldWeapons.h
Expand Up @@ -46,7 +46,7 @@ class WorldWeapons
int packSize() const;
void *pack(void *buf) const;

uint32_t fireShot(FlagType* type, float lifetime, float *pos, float tilt, float direction, float shotSpeed, int *shotID, float delayTime, TeamColor teamColor = RogueTeam, PlayerId targetPlayerID = -1);
uint32_t fireShot(FlagType* type, float lifetime, const float origin[3], const float vector[3], float shotSpeed, int *shotID, float delayTime, TeamColor teamColor = RogueTeam, PlayerId targetPlayerID = -1);

private:
struct Weapon
Expand Down

0 comments on commit e001476

Please sign in to comment.