1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
/**
* @brief Globally accessible stuff
* Contains implementation of synced and unsynced global stuff.
*/
#include "GlobalUnsynced.h"
#include "Player.h"
#include "PlayerHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/GlobalConstants.h" // for RANDINT_MAX
#include "Sim/Units/Unit.h" // required by CREG
#include "System/mmgr.h"
#include "System/Config/ConfigHandler.h"
#include "System/Exceptions.h"
#include "System/Util.h"
#include "System/creg/creg_cond.h"
#include "System/Sync/SyncTracer.h"
#include <SDL_timer.h>
#include <time.h>
/**
* @brief global unsynced
*
* Global instance of CGlobalUnsynced
*/
CGlobalUnsynced* gu;
const float CGlobalUnsynced::reconnectSimDrawBalance = 0.15f;
CR_BIND(CGlobalUnsynced, );
CR_REG_METADATA(CGlobalUnsynced, (
CR_MEMBER(modGameTime),
CR_MEMBER(gameTime),
CR_MEMBER(startTime),
CR_MEMBER(myPlayerNum),
CR_MEMBER(myTeam),
CR_MEMBER(myAllyTeam),
CR_MEMBER(spectating),
CR_MEMBER(spectatingFullView),
CR_MEMBER(spectatingFullSelect),
CR_MEMBER(fpsMode),
CR_MEMBER(usRandSeed),
CR_RESERVED(64)
));
CGlobalUnsynced::CGlobalUnsynced()
{
usRandSeed = time(NULL) % ((SDL_GetTicks() + 1) * 9007);
simFPS = 0.0f;
avgSimFrameTime = 0.0f;
avgDrawFrameTime = 0.0f;
modGameTime = 0;
gameTime = 0;
startTime = 0;
myPlayerNum = 0;
myTeam = 1;
myAllyTeam = 1;
myPlayingTeam = -1;
myPlayingAllyTeam = -1;
spectating = false;
spectatingFullView = false;
spectatingFullSelect = false;
fpsMode = false;
playerHandler = new CPlayerHandler();
globalQuit = false;
}
CGlobalUnsynced::~CGlobalUnsynced()
{
SafeDelete(playerHandler);
}
void CGlobalUnsynced::LoadFromSetup(const CGameSetup* setup)
{
playerHandler->LoadFromSetup(setup);
}
/**
* @return unsynced random integer
*
* Returns an unsynced random integer
*/
int CGlobalUnsynced::usRandInt()
{
usRandSeed = (usRandSeed * 214013L + 2531011L);
return (usRandSeed >> 16) & RANDINT_MAX;
}
/**
* @return unsynced random float
*
* returns an unsynced random float
*/
float CGlobalUnsynced::usRandFloat()
{
usRandSeed = (usRandSeed * 214013L + 2531011L);
return float((usRandSeed >> 16) & RANDINT_MAX) / RANDINT_MAX;
}
/**
* @return unsynced random vector
*
* returns an unsynced random vector
*/
float3 CGlobalUnsynced::usRandVector()
{
float3 ret;
do {
ret.x = usRandFloat() * 2 - 1;
ret.y = usRandFloat() * 2 - 1;
ret.z = usRandFloat() * 2 - 1;
} while (ret.SqLength() > 1);
return ret;
}
void CGlobalUnsynced::SetMyPlayer(const int myNumber)
{
myPlayerNum = myNumber;
#ifdef TRACE_SYNC
tracefile.Initialize(myPlayerNum);
#endif
const CPlayer* myPlayer = playerHandler->Player(myPlayerNum);
myTeam = myPlayer->team;
if (!teamHandler->IsValidTeam(myTeam)) {
throw content_error("Invalid MyTeam in player setup");
}
myAllyTeam = teamHandler->AllyTeam(myTeam);
if (!teamHandler->IsValidAllyTeam(myAllyTeam)) {
throw content_error("Invalid MyAllyTeam in player setup");
}
spectating = myPlayer->spectator;
spectatingFullView = myPlayer->spectator;
spectatingFullSelect = myPlayer->spectator;
if (!spectating) {
myPlayingTeam = myTeam;
myPlayingAllyTeam = myAllyTeam;
}
}
CPlayer* CGlobalUnsynced::GetMyPlayer() {
return (playerHandler->Player(myPlayerNum));
}
|