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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include "StdAfx.h"
// Player.cpp: implementation of the CPlayer class.
//
//////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <SDL_mouse.h>
#include "mmgr.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Game/Player.h"
#include "Game/PlayerHandler.h"
#include "Game/Camera.h"
#include "Game/CameraHandler.h"
#include "Game/GameHelper.h"
#include "Game/UI/MouseHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Weapons/Weapon.h"
#include "Sim/Units/COB/CobInstance.h"
#include "System/myMath.h"
#include "System/EventHandler.h"
#include "System/GlobalUnsynced.h"
#include "System/NetProtocol.h"
CR_BIND(CPlayer,);
CR_REG_METADATA(CPlayer, (
CR_MEMBER(name),
CR_MEMBER(countryCode),
CR_MEMBER(rank),
CR_MEMBER(spectator),
CR_MEMBER(team),
CR_MEMBER(active),
CR_MEMBER(playerNum),
// CR_MEMBER(readyToStart),
// CR_MEMBER(cpuUsage),
// CR_MEMBER(ping),
// CR_MEMBER(currentStats),
// CR_MEMBER(controlledTeams),
CR_RESERVED(32)
));
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPlayer::CPlayer()
{
memset(¤tStats, 0, sizeof(Statistics));
active = false;
cpuUsage = 0;
ping = 0;
dccs.playerControlledUnit = 0;
myControl.forward = 0;
myControl.back = 0;
myControl.left = 0;
myControl.right = 0;
myControl.mouse1 = 0;
myControl.mouse2 = 0;
myControl.viewDir = float3(0, 0, 1);
myControl.targetPos = float3(0, 0, 1);
myControl.targetDist = 1000;
myControl.target = 0;
myControl.myController = this;
}
CPlayer::~CPlayer()
{
}
void CPlayer::SetControlledTeams()
{
controlledTeams.clear();
if (gs->godMode) {
// anyone can control any unit
for (int t = 0; t < teamHandler->ActiveTeams(); t++) {
controlledTeams.insert(t);
}
return;
}
if (!spectator) {
// my team
controlledTeams.insert(team);
}
// AI teams
const CSkirmishAIHandler::id_ai_t aiIds = skirmishAIHandler.GetAllSkirmishAIs();
for (CSkirmishAIHandler::id_ai_t::const_iterator ai = aiIds.begin(); ai != aiIds.end(); ++ai) {
const bool isHostedByUs = (ai->second.hostPlayer == playerNum);
if (isHostedByUs) {
controlledTeams.insert(ai->second.team);
}
}
}
void CPlayer::UpdateControlledTeams()
{
for (int p = 0; p < playerHandler->ActivePlayers(); p++) {
CPlayer* player = playerHandler->Player(p);
if (player) {
player->SetControlledTeams();
}
}
}
void CPlayer::StartSpectating()
{
if (spectator) {
return;
}
spectator = true;
if (dccs.playerControlledUnit)
StopControllingUnit();
if (playerHandler->Player(gu->myPlayerNum) == this) { //TODO bad hack
gu->spectating = true;
gu->spectatingFullView = true;
gu->spectatingFullSelect = true;
}
eventHandler.PlayerChanged(playerNum);
}
void CPlayer::GameFrame(int frameNum)
{
if (!active || !dccs.playerControlledUnit)
return;
CUnit* unit = dccs.playerControlledUnit;
DirectControlStruct* dc = &myControl;
const int piece = unit->script->AimFromWeapon(0);
const float3 relPos = unit->script->GetPiecePos(piece);
float3 pos = unit->pos +
unit->frontdir * relPos.z +
unit->updir * relPos.y +
unit->rightdir * relPos.x;
pos += UpVector * 7;
dccs.oldDCpos = pos;
CUnit* hit;
float dist = helper->TraceRayTeam(pos, dc->viewDir, unit->maxRange, hit, 1, unit, teamHandler->AllyTeam(team));
dc->target = hit;
// prevent dgunning using FPS view if outside dgun-limit
if (uh->limitDgun && unit->unitDef->isCommander && unit->pos.SqDistance(teamHandler->Team(unit->team)->startPos) > Square(uh->dgunRadius)) {
return;
}
if (hit) {
dc->targetDist = dist;
dc->targetPos = hit->pos;
if (!dc->mouse2) {
unit->AttackUnit(hit, true);
}
} else {
if (dist > unit->maxRange * 0.95f)
dist = unit->maxRange * 0.95f;
dc->targetDist = dist;
dc->targetPos = pos + dc->viewDir * dc->targetDist;
if (!dc->mouse2) {
unit->AttackGround(dc->targetPos, true);
for (std::vector<CWeapon*>::iterator wi = unit->weapons.begin(); wi != unit->weapons.end(); ++wi) {
float d = dc->targetDist;
if (d > (*wi)->range * 0.95f)
d = (*wi)->range * 0.95f;
float3 p = pos + dc->viewDir * d;
(*wi)->AttackGround(p, true);
}
}
}
}
void CPlayer::StopControllingUnit()
{
CUnit* unit = dccs.playerControlledUnit;
unit->directControl = 0;
unit->AttackUnit(0, true);
if (gu->directControl == dccs.playerControlledUnit) {
assert(playerHandler->Player(gu->myPlayerNum) == this);
gu->directControl = 0;
/* Switch back to the camera we were using before. */
camHandler->PopMode();
if (mouse->locked && !mouse->wasLocked) {
mouse->locked = false;
mouse->ShowMouse();
}
}
dccs.playerControlledUnit = 0;
}
void DirectControlClientState::SendStateUpdate(bool* camMove) {
unsigned char state = 0;
if (camMove[0]) { state |= (1 << 0); }
if (camMove[1]) { state |= (1 << 1); }
if (camMove[2]) { state |= (1 << 2); }
if (camMove[3]) { state |= (1 << 3); }
if (mouse->buttons[SDL_BUTTON_LEFT].pressed) { state |= (1 << 4); }
if (mouse->buttons[SDL_BUTTON_RIGHT].pressed) { state |= (1 << 5); }
shortint2 hp = GetHAndPFromVector(camera->forward);
if (hp.x != oldHeading || hp.y != oldPitch || state != oldState) {
oldHeading = hp.x;
oldPitch = hp.y;
oldState = state;
net->Send(CBaseNetProtocol::Get().SendDirectControlUpdate(gu->myPlayerNum, state, hp.x, hp.y));
}
}
|