742 lines
21 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-09-05 00:52:03 +09:00
based on code of pysnip (c) Mathias Kaerlev 2011-2012.
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades 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 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades 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.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
2014-03-09 19:24:28 +01:00
#include <cstdarg>
#include <cstdlib>
#include <ctime>
#include "Client.h"
2014-03-09 21:43:38 +09:00
#include "Fonts.h"
#include <Core/FileManager.h>
#include <Core/IStream.h>
#include <Core/Settings.h>
#include <Core/Strings.h>
2014-03-09 21:43:38 +09:00
2013-08-18 16:18:06 +09:00
#include "IAudioChunk.h"
#include "IAudioDevice.h"
2014-03-09 21:43:38 +09:00
2013-08-18 16:18:06 +09:00
#include "CenterMessageView.h"
2014-03-09 21:43:38 +09:00
#include "ChatWindow.h"
#include "ClientPlayer.h"
#include "ClientUI.h"
2014-03-09 21:43:38 +09:00
#include "HurtRingView.h"
2013-08-18 16:18:06 +09:00
#include "LimboView.h"
#include "MapView.h"
2013-08-18 16:18:06 +09:00
#include "PaletteView.h"
2014-03-09 21:43:38 +09:00
#include "ScoreboardView.h"
#include "TCProgressView.h"
2014-03-09 21:43:38 +09:00
#include "Corpse.h"
2014-03-09 21:43:38 +09:00
#include "ILocalEntity.h"
#include "SmokeSpriteEntity.h"
#include "GameMap.h"
#include "GameMapWrapper.h"
#include "Weapon.h"
#include "World.h"
2014-03-09 21:43:38 +09:00
#include "NetClient.h"
2013-08-18 16:18:06 +09:00
2016-11-19 21:03:51 +09:00
DEFINE_SPADES_SETTING(cg_chatBeep, "1");
2013-08-18 16:18:06 +09:00
2016-11-19 21:03:51 +09:00
DEFINE_SPADES_SETTING(cg_serverAlert, "1");
2014-03-11 03:42:04 +09:00
DEFINE_SPADES_SETTING(cg_skipDeadPlayersWhenDead, "1");
2017-01-05 03:03:39 +09:00
SPADES_SETTING(cg_playerName);
2013-08-18 16:18:06 +09:00
namespace spades {
namespace client {
Client::Client(IRenderer *r, IAudioDevice *audioDev, const ServerAddress &host,
2017-01-05 03:03:39 +09:00
FontManager *fontManager)
2017-11-18 21:15:07 +00:00
: playerName(cg_playerName.operator std::string().substr(0, 15)),
logStream(nullptr),
2017-11-18 21:15:07 +00:00
hostname(host),
renderer(r),
audioDevice(audioDev),
2017-11-18 21:15:07 +00:00
time(0.f),
readyToClose(false),
2017-11-18 21:15:07 +00:00
worldSubFrame(0.f),
frameToRendererInit(5),
timeSinceInit(0.f),
2017-11-18 21:15:07 +00:00
hasLastTool(false),
lastPosSentTime(0.f),
lastAliveTime(0.f),
lastKills(0),
2017-11-18 21:15:07 +00:00
hasDelayedReload(false),
localFireVibrationTime(-1.f),
grenadeVibration(0.f),
grenadeVibrationSlow(0.f),
scoreboardVisible(false),
flashlightOn(false),
hitFeedbackIconState(0.f),
hitFeedbackFriendly(false),
focalLength(20.f),
targetFocalLength(20.f),
autoFocusEnabled(true),
2017-11-18 21:15:07 +00:00
inGameLimbo(false),
fontManager(fontManager),
alertDisappearTime(-10000.f),
2017-11-18 21:15:07 +00:00
lastMyCorpse(nullptr),
corpseSoftTimeLimit(30.f), // FIXME: this is not used
corpseSoftLimit(6),
corpseHardLimit(16),
2017-11-18 21:15:07 +00:00
nextScreenShotIndex(0),
nextMapShotIndex(0) {
2013-08-18 16:18:06 +09:00
SPADES_MARK_FUNCTION();
2013-08-26 01:27:44 +09:00
SPLog("Initializing...");
2013-08-18 16:18:06 +09:00
renderer->SetFogDistance(128.f);
renderer->SetFogColor(MakeVector3(.8f, 1.f, 1.f));
chatWindow.reset(new ChatWindow(this, GetRenderer(), fontManager->GetGuiFont(), false));
killfeedWindow.reset(
new ChatWindow(this, GetRenderer(), fontManager->GetGuiFont(), true));
2014-03-09 21:43:38 +09:00
hurtRingView.reset(new HurtRingView(this));
centerMessageView.reset(new CenterMessageView(this, fontManager->GetLargeFont()));
2014-03-09 21:43:38 +09:00
mapView.reset(new MapView(this, false));
largeMapView.reset(new MapView(this, true));
scoreboard.reset(new ScoreboardView(this));
limbo.reset(new LimboView(this));
paletteView.reset(new PaletteView(this));
tcView.reset(new TCProgressView(this));
scriptedUI.Set(new ClientUI(renderer, audioDev, fontManager, this), false);
2014-03-09 21:43:38 +09:00
renderer->SetGameMap(nullptr);
2013-08-18 16:18:06 +09:00
}
2013-08-18 16:18:06 +09:00
void Client::SetWorld(spades::client::World *w) {
SPADES_MARK_FUNCTION();
if (world.get() == w) {
2013-08-18 16:18:06 +09:00
return;
}
2013-12-02 19:42:38 +09:00
scriptedUI->CloseUI();
2013-08-18 16:18:06 +09:00
RemoveAllCorpses();
2014-03-09 21:43:38 +09:00
RemoveAllLocalEntities();
2013-08-18 16:18:06 +09:00
lastHealth = 0;
lastHurtTime = -100.f;
hurtRingView->ClearAll();
scoreboardVisible = false;
flashlightOn = false;
for (size_t i = 0; i < clientPlayers.size(); i++) {
if (clientPlayers[i]) {
clientPlayers[i]->Invalidate();
}
}
clientPlayers.clear();
if (world) {
2014-03-09 21:43:38 +09:00
world->SetListener(nullptr);
renderer->SetGameMap(nullptr);
audioDevice->SetGameMap(nullptr);
world = nullptr;
map = nullptr;
2013-08-18 16:18:06 +09:00
}
2014-03-09 21:43:38 +09:00
world.reset(w);
if (world) {
2013-08-26 01:27:44 +09:00
SPLog("World set");
2014-03-09 21:43:38 +09:00
// initialize player view objects
clientPlayers.resize(world->GetNumPlayerSlots());
for (size_t i = 0; i < world->GetNumPlayerSlots(); i++) {
Player *p = world->GetPlayer(static_cast<unsigned int>(i));
if (p) {
clientPlayers[i] = new ClientPlayer(p, this);
} else {
2014-03-09 21:43:38 +09:00
clientPlayers[i] = nullptr;
}
}
2013-08-18 16:18:06 +09:00
world->SetListener(this);
map = world->GetMap();
renderer->SetGameMap(map);
audioDevice->SetGameMap(map);
2013-08-26 19:24:15 +09:00
NetLog("------ World Loaded ------");
} else {
2013-08-26 01:27:44 +09:00
SPLog("World removed");
2013-08-26 19:24:15 +09:00
NetLog("------ World Unloaded ------");
2013-08-18 16:18:06 +09:00
}
2013-08-18 16:18:06 +09:00
limbo->SetSelectedTeam(2);
limbo->SetSelectedWeapon(RIFLE_WEAPON);
2013-08-18 16:18:06 +09:00
worldSubFrame = 0.f;
worldSetTime = time;
2014-03-09 21:43:38 +09:00
inGameLimbo = false;
2013-08-18 16:18:06 +09:00
}
2013-08-18 16:18:06 +09:00
Client::~Client() {
SPADES_MARK_FUNCTION();
2013-08-26 19:24:15 +09:00
NetLog("Disconnecting");
DrawDisconnectScreen();
if (logStream) {
2013-08-26 19:24:15 +09:00
SPLog("Closing netlog");
2014-03-09 21:43:38 +09:00
logStream.reset();
2013-08-26 19:24:15 +09:00
}
if (net) {
SPLog("Disconnecting");
net->Disconnect();
2014-03-09 21:43:38 +09:00
net.reset();
}
2013-08-26 01:27:44 +09:00
SPLog("Disconnected");
2013-08-18 16:18:06 +09:00
RemoveAllLocalEntities();
RemoveAllCorpses();
2014-03-09 21:43:38 +09:00
renderer->SetGameMap(nullptr);
audioDevice->SetGameMap(nullptr);
for (size_t i = 0; i < clientPlayers.size(); i++) {
if (clientPlayers[i]) {
clientPlayers[i]->Invalidate();
}
}
2014-03-09 21:43:38 +09:00
clientPlayers.clear();
2013-11-27 16:30:54 +09:00
scriptedUI->ClientDestroyed();
2014-03-09 21:43:38 +09:00
tcView.reset();
limbo.reset();
scoreboard.reset();
mapView.reset();
largeMapView.reset();
chatWindow.reset();
killfeedWindow.reset();
paletteView.reset();
centerMessageView.reset();
hurtRingView.reset();
world.reset();
}
2014-03-09 21:43:38 +09:00
/** Initiate an initialization which likely to take some time */
void Client::DoInit() {
renderer->Init();
SmokeSpriteEntity::Preload(renderer);
2013-09-06 11:44:36 +09:00
renderer->RegisterImage("Textures/Fluid.png");
renderer->RegisterImage("Textures/WaterExpl.png");
renderer->RegisterImage("Gfx/White.tga");
audioDevice->RegisterSound("Sounds/Weapons/Block/Build.opus");
audioDevice->RegisterSound("Sounds/Weapons/Impacts/FleshLocal1.opus");
audioDevice->RegisterSound("Sounds/Weapons/Impacts/FleshLocal2.opus");
audioDevice->RegisterSound("Sounds/Weapons/Impacts/FleshLocal3.opus");
audioDevice->RegisterSound("Sounds/Weapons/Impacts/FleshLocal4.opus");
audioDevice->RegisterSound("Sounds/Misc/SwitchMapZoom.opus");
audioDevice->RegisterSound("Sounds/Misc/OpenMap.opus");
audioDevice->RegisterSound("Sounds/Misc/CloseMap.opus");
audioDevice->RegisterSound("Sounds/Player/Flashlight.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep1.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep2.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep3.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep4.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep5.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep6.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep7.opus");
audioDevice->RegisterSound("Sounds/Player/Footstep8.opus");
audioDevice->RegisterSound("Sounds/Player/Wade1.opus");
audioDevice->RegisterSound("Sounds/Player/Wade2.opus");
audioDevice->RegisterSound("Sounds/Player/Wade3.opus");
audioDevice->RegisterSound("Sounds/Player/Wade4.opus");
audioDevice->RegisterSound("Sounds/Player/Wade5.opus");
audioDevice->RegisterSound("Sounds/Player/Wade6.opus");
audioDevice->RegisterSound("Sounds/Player/Wade7.opus");
audioDevice->RegisterSound("Sounds/Player/Wade8.opus");
audioDevice->RegisterSound("Sounds/Player/Run1.opus");
audioDevice->RegisterSound("Sounds/Player/Run2.opus");
audioDevice->RegisterSound("Sounds/Player/Run3.opus");
audioDevice->RegisterSound("Sounds/Player/Run4.opus");
audioDevice->RegisterSound("Sounds/Player/Run5.opus");
audioDevice->RegisterSound("Sounds/Player/Run6.opus");
audioDevice->RegisterSound("Sounds/Player/Run7.opus");
audioDevice->RegisterSound("Sounds/Player/Run8.opus");
audioDevice->RegisterSound("Sounds/Player/Run9.opus");
audioDevice->RegisterSound("Sounds/Player/Run10.opus");
audioDevice->RegisterSound("Sounds/Player/Run11.opus");
audioDevice->RegisterSound("Sounds/Player/Run12.opus");
audioDevice->RegisterSound("Sounds/Player/Jump.opus");
audioDevice->RegisterSound("Sounds/Player/Land.opus");
audioDevice->RegisterSound("Sounds/Player/WaterJump.opus");
audioDevice->RegisterSound("Sounds/Player/WaterLand.opus");
audioDevice->RegisterSound("Sounds/Weapons/SwitchLocal.opus");
audioDevice->RegisterSound("Sounds/Weapons/Switch.opus");
audioDevice->RegisterSound("Sounds/Weapons/Restock.opus");
audioDevice->RegisterSound("Sounds/Weapons/RestockLocal.opus");
audioDevice->RegisterSound("Sounds/Weapons/AimDownSightLocal.opus");
2013-09-06 11:44:36 +09:00
renderer->RegisterImage("Gfx/Ball.png");
renderer->RegisterModel("Models/Player/Dead.kv6");
2016-07-16 00:14:59 +09:00
renderer->RegisterImage("Gfx/Spotlight.png");
renderer->RegisterImage("Gfx/Glare.png");
2013-09-06 11:44:36 +09:00
renderer->RegisterModel("Models/Weapons/Spade/Spade.kv6");
renderer->RegisterModel("Models/Weapons/Block/Block2.kv6");
renderer->RegisterModel("Models/Weapons/Grenade/Grenade.kv6");
renderer->RegisterModel("Models/Weapons/SMG/Weapon.kv6");
renderer->RegisterModel("Models/Weapons/SMG/WeaponNoMagazine.kv6");
renderer->RegisterModel("Models/Weapons/SMG/Magazine.kv6");
renderer->RegisterModel("Models/Weapons/Rifle/Weapon.kv6");
renderer->RegisterModel("Models/Weapons/Rifle/WeaponNoMagazine.kv6");
renderer->RegisterModel("Models/Weapons/Rifle/Magazine.kv6");
renderer->RegisterModel("Models/Weapons/Shotgun/Weapon.kv6");
renderer->RegisterModel("Models/Weapons/Shotgun/WeaponNoPump.kv6");
renderer->RegisterModel("Models/Weapons/Shotgun/Pump.kv6");
renderer->RegisterModel("Models/Player/Arm.kv6");
renderer->RegisterModel("Models/Player/UpperArm.kv6");
renderer->RegisterModel("Models/Player/LegCrouch.kv6");
renderer->RegisterModel("Models/Player/TorsoCrouch.kv6");
renderer->RegisterModel("Models/Player/Leg.kv6");
renderer->RegisterModel("Models/Player/Torso.kv6");
renderer->RegisterModel("Models/Player/Arms.kv6");
renderer->RegisterModel("Models/Player/Head.kv6");
renderer->RegisterModel("Models/MapObjects/Intel.kv6");
2013-09-06 11:44:36 +09:00
renderer->RegisterModel("Models/MapObjects/CheckPoint.kv6");
2016-07-16 00:14:59 +09:00
renderer->RegisterImage("Gfx/Bullet/7.62mm.png");
renderer->RegisterImage("Gfx/Bullet/9mm.png");
renderer->RegisterImage("Gfx/Bullet/12gauge.png");
2013-09-06 11:44:36 +09:00
renderer->RegisterImage("Gfx/CircleGradient.png");
2013-09-12 23:26:08 +09:00
renderer->RegisterImage("Gfx/HurtSprite.png");
2013-12-16 22:54:04 +09:00
renderer->RegisterImage("Gfx/HurtRing2.png");
renderer->RegisterImage("Gfx/Intel.png");
audioDevice->RegisterSound("Sounds/Feedback/Chat.opus");
2016-08-03 17:08:45 +02:00
if (mumbleLink.init())
SPLog("Mumble linked");
else
SPLog("Mumble link failed");
2017-01-05 03:03:39 +09:00
mumbleLink.setContext(hostname.ToString(false));
2016-08-03 17:08:45 +02:00
mumbleLink.setIdentity(playerName);
2016-11-22 23:08:35 +02:00
2017-01-05 03:03:39 +09:00
SPLog("Started connecting to '%s'", hostname.ToString(true).c_str());
2014-03-09 21:43:38 +09:00
net.reset(new NetClient(this));
net->Connect(hostname);
// decide log file name
2017-01-05 03:03:39 +09:00
std::string fn = hostname.ToString(false);
std::string fn2;
{
time_t t;
struct tm tm;
::time(&t);
tm = *localtime(&t);
char buf[256];
sprintf(buf, "%04d%02d%02d%02d%02d%02d_", tm.tm_year + 1900, tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
fn2 = buf;
}
for (size_t i = 0; i < fn.size(); i++) {
char c = fn[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
fn2 += c;
} else {
fn2 += '_';
}
}
fn2 = "NetLogs/" + fn2 + ".log";
try {
2014-03-09 21:43:38 +09:00
logStream.reset(FileManager::OpenForWriting(fn2.c_str()));
SPLog("Netlog Started at '%s'", fn2.c_str());
} catch (const std::exception &ex) {
2013-09-20 23:03:32 +02:00
SPLog("Failed to open netlog file '%s' (%s)", fn2.c_str(), ex.what());
}
}
2013-08-18 16:18:06 +09:00
void Client::RunFrame(float dt) {
SPADES_MARK_FUNCTION();
2014-03-31 23:09:47 +09:00
fpsCounter.MarkFrame();
if (frameToRendererInit > 0) {
// waiting for renderer initialization
DrawStartupScreen();
frameToRendererInit--;
if (frameToRendererInit == 0) {
DoInit();
} else {
return;
}
}
timeSinceInit += std::min(dt, .03f);
2014-03-09 21:43:38 +09:00
// update network
try {
if (net->GetStatus() == NetClientStatusConnected)
2013-08-18 16:18:06 +09:00
net->DoEvents(0);
else
2013-11-28 00:46:45 +09:00
net->DoEvents(10);
} catch (const std::exception &ex) {
if (net->GetStatus() == NetClientStatusNotConnected) {
2013-08-26 19:24:15 +09:00
SPLog("Disconnected because of error:\n%s", ex.what());
NetLog("Disconnected because of error:\n%s", ex.what());
2013-08-18 16:18:06 +09:00
throw;
} else {
2013-08-26 19:24:15 +09:00
SPLog("Exception while processing network packets (ignored):\n%s", ex.what());
}
2013-08-18 16:18:06 +09:00
}
2013-08-18 16:18:06 +09:00
hurtRingView->Update(dt);
centerMessageView->Update(dt);
mapView->Update(dt);
largeMapView->Update(dt);
UpdateAutoFocus(dt);
if (world) {
2014-03-09 21:43:38 +09:00
UpdateWorld(dt);
2016-08-03 17:08:45 +02:00
mumbleLink.update(world->GetLocalPlayer());
} else {
2013-08-18 16:18:06 +09:00
renderer->SetFogColor(MakeVector3(0.f, 0.f, 0.f));
}
2013-08-18 16:18:06 +09:00
chatWindow->Update(dt);
2013-08-26 17:53:14 +09:00
killfeedWindow->Update(dt);
2013-08-18 16:18:06 +09:00
limbo->Update(dt);
2014-03-09 21:43:38 +09:00
// CreateSceneDefinition also can be used for sounds
SceneDefinition sceneDef = CreateSceneDefinition();
2013-08-18 16:18:06 +09:00
lastSceneDef = sceneDef;
2013-08-18 16:18:06 +09:00
// Update sounds
try {
audioDevice->Respatialize(sceneDef.viewOrigin, sceneDef.viewAxis[2],
sceneDef.viewAxis[1]);
} catch (const std::exception &ex) {
SPLog("Audio subsystem returned error (ignored):\n%s", ex.what());
2013-08-18 16:18:06 +09:00
}
2013-08-18 16:18:06 +09:00
// render scene
DrawScene();
2013-08-18 16:18:06 +09:00
// draw 2d
Draw2D();
2013-11-27 16:30:54 +09:00
// draw scripted GUI
scriptedUI->RunFrame(dt);
if (scriptedUI->WantsClientToBeClosed())
2013-11-27 16:30:54 +09:00
readyToClose = true;
2013-08-18 16:18:06 +09:00
// Well done!
renderer->FrameDone();
renderer->Flip();
// reset all "delayed actions" (in case we forget to reset these)
hasDelayedReload = false;
2013-08-18 16:18:06 +09:00
time += dt;
}
bool Client::IsLimboViewActive() {
if (world) {
if (!world->GetLocalPlayer()) {
2014-03-09 21:43:38 +09:00
return true;
} else if (inGameLimbo) {
2014-03-09 21:43:38 +09:00
return true;
}
2014-02-01 21:55:30 +09:00
}
return false;
}
2014-03-09 21:43:38 +09:00
void Client::SpawnPressed() {
WeaponType weap = limbo->GetSelectedWeapon();
int team = limbo->GetSelectedTeam();
inGameLimbo = false;
if (team == 2)
2014-03-09 21:43:38 +09:00
team = 255;
2017-01-08 19:03:09 +09:00
if (!world->GetLocalPlayer() || world->GetLocalPlayer()->GetTeamId() >= 2) {
2014-03-09 21:43:38 +09:00
// join
2017-02-22 08:19:14 +09:00
if (team == 255) {
// weaponId doesn't matter for spectators, but
// NetClient doesn't like invalid weapon ID
weap = WeaponType::RIFLE_WEAPON;
}
net->SendJoin(team, weap, playerName, lastKills);
} else {
2013-08-18 16:18:06 +09:00
Player *p = world->GetLocalPlayer();
if (p->GetTeamId() != team) {
2014-03-09 21:43:38 +09:00
net->SendTeamChange(team);
}
if (team != 2 && p->GetWeapon()->GetWeaponType() != weap) {
2014-03-09 21:43:38 +09:00
net->SendWeaponChange(weap);
2013-08-18 16:18:06 +09:00
}
}
}
void Client::ShowAlert(const std::string &contents, AlertType type) {
2014-03-11 03:42:04 +09:00
float timeout;
switch (type) {
case AlertType::Notice: timeout = 2.5f; break;
case AlertType::Warning: timeout = 3.f; break;
case AlertType::Error: timeout = 3.f; break;
2014-03-11 03:42:04 +09:00
}
ShowAlert(contents, type, timeout);
}
void Client::ShowAlert(const std::string &contents, AlertType type, float timeout,
bool quiet) {
2014-03-11 03:42:04 +09:00
alertType = type;
alertContents = contents;
alertDisappearTime = time + timeout;
alertAppearTime = time;
if (type != AlertType::Notice && !quiet) {
2014-03-11 03:42:04 +09:00
PlayAlertSound();
}
}
2014-03-11 03:42:04 +09:00
void Client::PlayAlertSound() {
Handle<IAudioChunk> chunk = audioDevice->RegisterSound("Sounds/Feedback/Alert.opus");
2014-03-11 03:42:04 +09:00
audioDevice->PlayLocal(chunk, AudioParam());
}
2014-03-09 21:43:38 +09:00
/** Records chat message/game events to the log file. */
void Client::NetLog(const char *format, ...) {
char buf[4096];
va_list va;
va_start(va, format);
vsnprintf(buf, sizeof(buf), format, va);
2014-03-09 21:43:38 +09:00
va_end(va);
std::string str = buf;
2014-03-09 21:43:38 +09:00
time_t t;
struct tm tm;
::time(&t);
tm = *localtime(&t);
2014-03-09 21:43:38 +09:00
std::string timeStr = asctime(&tm);
2014-03-09 21:43:38 +09:00
// remove '\n' in the end of the result of asctime().
timeStr.resize(timeStr.size() - 1);
snprintf(buf, sizeof(buf), "%s %s\n", timeStr.c_str(), str.c_str());
buf[sizeof(buf) - 1] = 0;
2014-03-09 21:43:38 +09:00
std::string outStr = EscapeControlCharacters(buf);
2014-03-09 21:43:38 +09:00
printf("%s", outStr.c_str());
if (logStream) {
2014-03-09 21:43:38 +09:00
logStream->Write(outStr);
logStream->Flush();
2013-12-09 17:36:05 +09:00
}
}
2014-03-09 21:43:38 +09:00
#pragma mark - Snapshots
void Client::TakeMapShot() {
try {
2013-11-22 19:34:20 +09:00
std::string name = MapShotPath();
2014-03-09 21:43:38 +09:00
{
std::unique_ptr<IStream> stream(FileManager::OpenForWriting(name.c_str()));
try {
2014-03-09 21:43:38 +09:00
GameMap *map = GetWorld()->GetMap();
if (map == nullptr) {
2014-03-09 21:43:38 +09:00
SPRaise("No map loaded");
}
map->Save(stream.get());
} catch (...) {
2014-03-09 21:43:38 +09:00
throw;
2013-11-22 19:34:20 +09:00
}
}
2013-11-22 19:34:20 +09:00
std::string msg;
2014-02-15 03:51:29 +09:00
msg = _Tr("Client", "Map saved: {0}", name);
2014-03-11 03:42:04 +09:00
ShowAlert(msg, AlertType::Notice);
} catch (const Exception &ex) {
2014-03-11 03:42:04 +09:00
std::string msg;
msg = _Tr("Client", "Saving map failed: ");
msg += ex.GetShortMessage();
ShowAlert(msg, AlertType::Error);
SPLog("Saving map failed: %s", ex.what());
} catch (const std::exception &ex) {
2013-11-22 19:34:20 +09:00
std::string msg;
2014-02-15 03:51:29 +09:00
msg = _Tr("Client", "Saving map failed: ");
2014-03-11 03:42:04 +09:00
msg += ex.what();
ShowAlert(msg, AlertType::Error);
SPLog("Saving map failed: %s", ex.what());
2013-11-22 19:34:20 +09:00
}
}
2013-11-22 19:34:20 +09:00
std::string Client::MapShotPath() {
char buf[256];
for (int i = 0; i < 10000; i++) {
2013-11-22 19:34:20 +09:00
sprintf(buf, "Mapshots/shot%04d.vxl", nextScreenShotIndex);
if (FileManager::FileExists(buf)) {
2013-11-22 19:34:20 +09:00
nextScreenShotIndex++;
if (nextScreenShotIndex >= 10000)
2013-11-22 19:34:20 +09:00
nextScreenShotIndex = 0;
continue;
}
2013-11-22 19:34:20 +09:00
return buf;
}
2013-11-22 19:34:20 +09:00
SPRaise("No free file name");
}
2014-03-09 21:43:38 +09:00
#pragma mark - Chat Messages
void Client::PlayerSentChatMessage(spades::client::Player *p, bool global,
const std::string &msg) {
2014-03-09 21:43:38 +09:00
{
std::string s;
if (global)
2017-02-11 16:38:24 +09:00
//! Prefix added to global chat messages.
//!
//! Example: [Global] playername (Red) blah blah
//!
//! Crowdin warns that this string shouldn't be translated,
//! but it actually can be.
//! The extra whitespace is not a typo.
2014-03-09 21:43:38 +09:00
s = _Tr("Client", "[Global] ");
s += ChatWindow::TeamColorMessage(p->GetName(), p->GetTeamId());
s += ": ";
s += msg;
chatWindow->AddMessage(s);
2013-09-12 23:30:03 +09:00
}
2014-03-09 21:43:38 +09:00
{
std::string s;
if (global)
2014-03-09 21:43:38 +09:00
s = "[Global] ";
s += p->GetName();
s += ": ";
s += msg;
auto col = p->GetTeamId() < 2 ? world->GetTeam(p->GetTeamId()).color
: IntVector3::Make(255, 255, 255);
scriptedUI->RecordChatLog(
s, MakeVector4(col.x / 255.f, col.y / 255.f, col.z / 255.f, 0.8f));
2013-08-18 16:18:06 +09:00
}
if (global)
NetLog("[Global] %s (%s): %s", p->GetName().c_str(),
world->GetTeam(p->GetTeamId()).name.c_str(), msg.c_str());
2014-03-09 21:43:38 +09:00
else
NetLog("[Team] %s (%s): %s", p->GetName().c_str(),
world->GetTeam(p->GetTeamId()).name.c_str(), msg.c_str());
if ((!IsMuted()) && (int)cg_chatBeep) {
Handle<IAudioChunk> chunk = audioDevice->RegisterSound("Sounds/Feedback/Chat.opus");
2014-03-09 21:43:38 +09:00
audioDevice->PlayLocal(chunk, AudioParam());
2013-08-18 16:18:06 +09:00
}
}
2014-03-09 21:43:38 +09:00
void Client::ServerSentMessage(const std::string &msg) {
NetLog("%s", msg.c_str());
scriptedUI->RecordChatLog(msg, Vector4::Make(1.f, 1.f, 1.f, 0.8f));
if (cg_serverAlert) {
if (msg.substr(0, 3) == "N% ") {
2014-03-11 03:42:04 +09:00
ShowAlert(msg.substr(3), AlertType::Notice);
return;
}
if (msg.substr(0, 3) == "!% ") {
2014-03-11 03:42:04 +09:00
ShowAlert(msg.substr(3), AlertType::Error);
return;
}
if (msg.substr(0, 3) == "%% ") {
2014-03-11 03:42:04 +09:00
ShowAlert(msg.substr(3), AlertType::Warning);
return;
}
if (msg.substr(0, 3) == "C% ") {
centerMessageView->AddMessage(msg.substr(3));
return;
}
2014-03-11 03:42:04 +09:00
}
2014-03-11 03:42:04 +09:00
chatWindow->AddMessage(msg);
2013-08-18 16:18:06 +09:00
}
2014-03-09 21:43:38 +09:00
#pragma mark - Follow / Spectate
2014-03-09 21:43:38 +09:00
void Client::FollowNextPlayer(bool reverse) {
SPAssert(world->GetLocalPlayer());
auto &localPlayer = *world->GetLocalPlayer();
int myTeam = localPlayer.GetTeamId();
bool localPlayerIsSpectator = localPlayer.IsSpectator();
int nextId = FollowsNonLocalPlayer(GetCameraMode()) ? followedPlayerId :
world->GetLocalPlayerIndex();
do {
2014-03-09 21:43:38 +09:00
reverse ? --nextId : ++nextId;
if (nextId >= static_cast<int>(world->GetNumPlayerSlots()))
2014-03-09 21:43:38 +09:00
nextId = 0;
if (nextId < 0)
nextId = static_cast<int>(world->GetNumPlayerSlots() - 1);
2014-03-09 21:43:38 +09:00
Player *p = world->GetPlayer(nextId);
if (p == nullptr || p->IsSpectator()) {
// Do not follow a non-existent player or spectator
2014-03-09 21:43:38 +09:00
continue;
}
if (!localPlayerIsSpectator && p->GetTeamId() != myTeam) {
2014-03-09 21:43:38 +09:00
continue;
}
if (!localPlayerIsSpectator && cg_skipDeadPlayersWhenDead && !p->IsAlive()) {
// Skip dead players unless the local player is not a spectator
continue;
}
if (p->GetFront().GetPoweredLength() < .01f) {
// Do not follow a player with an invalid state
2014-03-09 21:43:38 +09:00
continue;
}
2014-03-09 21:43:38 +09:00
break;
} while (nextId != followedPlayerId);
followedPlayerId = nextId;
if (followedPlayerId == world->GetLocalPlayerIndex()) {
followCameraState.enabled = false;
} else {
followCameraState.enabled = true;
}
2014-03-09 21:43:38 +09:00
}
2013-08-18 16:18:06 +09:00
}
}