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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "SDL_mouse.h"
#include "SDL_keyboard.h"
#include "InMapDraw.h"
#include "InMapDrawModel.h"
#include "Camera.h"
#include "Game.h"
#include "GlobalUnsynced.h"
#include "ExternalAI/AILegacySupport.h" // {Point, Line}Marker
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "UI/MiniMap.h"
#include "UI/MouseHandler.h"
#include "Map/Ground.h"
#include "Map/ReadMap.h"
#include "Net/Protocol/BaseNetProtocol.h"
#include "Net/Protocol/NetProtocol.h"
#include "Sim/Misc/TeamHandler.h"
#include "System/Net/UnpackPacket.h"
#include "System/EventHandler.h"
#include "System/EventClient.h"
#include "System/Log/ILog.h"
#include "System/Sound/ISound.h"
#include "System/Sound/ISoundChannels.h"
CInMapDraw* inMapDrawer = NULL;
/**
* This simply makes a noice appear when a map point is placed.
* We will only receive an even (and thus make a sound) when we are allwoed to
* know about it.
*/
class CNotificationPeeper : public CEventClient
{
public:
CNotificationPeeper()
: CEventClient("NotificationPeeper", 99, false)
{
blippSound = sound->GetSoundId("MapPoint");
}
virtual bool WantsEvent(const std::string& eventName) {
return (eventName == "MapDrawCmd");
}
virtual bool MapDrawCmd(int playerID, int type, const float3* pos0, const float3* pos1, const std::string* label) {
if (type == MAPDRAW_POINT) {
const CPlayer* sender = playerHandler->Player(playerID);
// if we happen to be in drawAll mode, notify us now
// even if this message is not intented for our ears
LOG("%s added point: %s", sender->name.c_str(), label->c_str());
eventHandler.LastMessagePosition(*pos0);
Channels::UserInterface->PlaySample(blippSound, *pos0);
minimap->AddNotification(*pos0, OnesVector, 1.0f);
}
return false;
}
private:
int blippSound;
};
CInMapDraw::CInMapDraw()
: drawMode(false)
, wantLabel(false)
, lastLeftClickTime(0.0f)
, lastDrawTime(0.0f)
, lastPos(OnesVector)
, allowSpecMapDrawing(true)
, allowLuaMapDrawing(true)
, notificationPeeper(NULL)
{
notificationPeeper = new CNotificationPeeper();
eventHandler.AddClient(notificationPeeper);
}
CInMapDraw::~CInMapDraw()
{
eventHandler.RemoveClient(notificationPeeper);
delete notificationPeeper;
notificationPeeper = NULL;
}
void CInMapDraw::MousePress(int x, int y, int button)
{
float3 pos = GetMouseMapPos();
if (pos.x < 0)
return;
switch (button) {
case SDL_BUTTON_LEFT: {
if (lastLeftClickTime > gu->gameTime - 0.3f) {
PromptLabel(pos);
}
lastLeftClickTime = gu->gameTime;
} break;
case SDL_BUTTON_MIDDLE:{
SendPoint(pos, "", false);
} break;
case SDL_BUTTON_RIGHT: {
SendErase(pos);
} break;
default: {
} break;
}
lastPos = pos;
}
void CInMapDraw::MouseRelease(int x, int y, int button)
{
// TODO implement CInMapDraw::MouseRelease
}
void CInMapDraw::MouseMove(int x, int y, int dx, int dy, int button)
{
float3 pos = GetMouseMapPos();
if (pos.x < 0) {
return;
}
if (mouse->buttons[SDL_BUTTON_LEFT].pressed && (lastDrawTime < (gu->gameTime - 0.05f))) {
SendLine(pos, lastPos, false);
lastDrawTime = gu->gameTime;
lastPos = pos;
}
if (mouse->buttons[SDL_BUTTON_RIGHT].pressed && (lastDrawTime < (gu->gameTime - 0.05f))) {
SendErase(pos);
lastDrawTime = gu->gameTime;
}
}
float3 CInMapDraw::GetMouseMapPos() // TODO move to some more global place?
{
const float dist = CGround::LineGroundCol(camera->GetPos(), camera->GetPos() + (mouse->dir * globalRendering->viewRange * 1.4f), false);
if (dist < 0) {
return float3(-1.0f, 1.0f, -1.0f);
}
float3 pos = camera->GetPos() + (mouse->dir * dist);
pos.ClampInBounds();
return pos;
}
int CInMapDraw::GotNetMsg(boost::shared_ptr<const netcode::RawPacket>& packet)
{
int playerID = -1;
try {
netcode::UnpackPacket pckt(packet, 2);
unsigned char uPlayerID;
pckt >> uPlayerID;
if (!playerHandler->IsValidPlayer(uPlayerID)) {
throw netcode::UnpackPacketException("Invalid player number");
}
playerID = uPlayerID;
unsigned char drawType;
pckt >> drawType;
switch (drawType) {
case MAPDRAW_POINT: {
short int x, z;
pckt >> x;
pckt >> z;
const float3 pos(x, 0, z);
unsigned char fromLua;
pckt >> fromLua;
string label;
pckt >> label;
if (!fromLua || allowLuaMapDrawing) {
inMapDrawerModel->AddPoint(pos, label, playerID);
}
break;
}
case MAPDRAW_LINE: {
short int x1, z1, x2, z2;
pckt >> x1;
pckt >> z1;
pckt >> x2;
pckt >> z2;
const float3 pos1(x1, 0, z1);
const float3 pos2(x2, 0, z2);
unsigned char fromLua;
pckt >> fromLua;
if (!fromLua || allowLuaMapDrawing) {
inMapDrawerModel->AddLine(pos1, pos2, playerID);
}
break;
}
case MAPDRAW_ERASE: {
short int x, z;
pckt >> x;
pckt >> z;
float3 pos(x, 0, z);
inMapDrawerModel->EraseNear(pos, playerID);
break;
}
}
} catch (const netcode::UnpackPacketException& ex) {
LOG_L(L_WARNING, "Got invalid MapDraw: %s", ex.what());
playerID = -1;
}
return playerID;
}
void CInMapDraw::SetSpecMapDrawingAllowed(bool state)
{
allowSpecMapDrawing = state;
LOG("[%s] spectator map-drawing is %s", __FUNCTION__, allowSpecMapDrawing? "enabled": "disabled");
}
void CInMapDraw::SetLuaMapDrawingAllowed(bool state)
{
allowLuaMapDrawing = state;
LOG("[%s] Lua map-drawing is %s", __FUNCTION__, allowLuaMapDrawing? "enabled": "disabled");
}
void CInMapDraw::SendErase(const float3& pos)
{
if (!gu->spectating || allowSpecMapDrawing)
clientNet->Send(CBaseNetProtocol::Get().SendMapErase(gu->myPlayerNum, (short)pos.x, (short)pos.z));
}
void CInMapDraw::SendPoint(const float3& pos, const std::string& label, bool fromLua)
{
if (!gu->spectating || allowSpecMapDrawing)
clientNet->Send(CBaseNetProtocol::Get().SendMapDrawPoint(gu->myPlayerNum, (short)pos.x, (short)pos.z, label, fromLua));
}
void CInMapDraw::SendLine(const float3& pos, const float3& pos2, bool fromLua)
{
if (!gu->spectating || allowSpecMapDrawing)
clientNet->Send(CBaseNetProtocol::Get().SendMapDrawLine(gu->myPlayerNum, (short)pos.x, (short)pos.z, (short)pos2.x, (short)pos2.z, fromLua));
}
void CInMapDraw::SendWaitingInput(const std::string& label)
{
SendPoint(waitingPoint, label, false);
wantLabel = false;
drawMode = false;
}
void CInMapDraw::PromptLabel(const float3& pos)
{
waitingPoint = pos;
game->userWriting = true;
wantLabel = true;
game->userPrompt = "Label: ";
game->ignoreNextChar = true;
inMapDrawer->SetDrawMode(false);
}
void CInMapDraw::GetPoints(std::vector<PointMarker>& points, int pointsSizeMax, const std::list<int>& teamIDs)
{
pointsSizeMax = std::min(pointsSizeMax, inMapDrawerModel->GetNumPoints());
points.clear();
points.reserve(pointsSizeMax);
const std::list<CInMapDrawModel::MapPoint>* pointsInt = NULL;
for (size_t y = 0; (y < inMapDrawerModel->GetDrawQuadY()) && ((int)points.size() < pointsSizeMax); y++) {
for (size_t x = 0; (x < inMapDrawerModel->GetDrawQuadX()) && ((int)points.size() < pointsSizeMax); x++) {
pointsInt = &(inMapDrawerModel->GetDrawQuad(x, y)->points);
for (auto point = pointsInt->cbegin(); (point != pointsInt->cend()) && ((int)points.size() < pointsSizeMax); ++point) {
for (auto it = teamIDs.cbegin(); it != teamIDs.cend(); ++it) {
if (point->GetTeamID() != *it)
continue;
PointMarker pm;
pm.pos = point->GetPos();
pm.color = teamHandler->Team(point->GetTeamID())->color;
pm.label = point->GetLabel().c_str();
points.push_back(pm);
break;
}
}
}
}
}
void CInMapDraw::GetLines(std::vector<LineMarker>& lines, int linesSizeMax, const std::list<int>& teamIDs)
{
linesSizeMax = std::min(linesSizeMax, inMapDrawerModel->GetNumLines());
lines.clear();
lines.reserve(linesSizeMax);
const std::list<CInMapDrawModel::MapLine>* linesInt = NULL;
for (size_t y = 0; (y < inMapDrawerModel->GetDrawQuadY()) && ((int)lines.size() < linesSizeMax); y++) {
for (size_t x = 0; (x < inMapDrawerModel->GetDrawQuadX()) && ((int)lines.size() < linesSizeMax); x++) {
linesInt = &(inMapDrawerModel->GetDrawQuad(x, y)->lines);
for (auto line = linesInt->cbegin(); (line != linesInt->cend()) && ((int)lines.size() < linesSizeMax); ++line) {
for (auto it = teamIDs.cbegin(); it != teamIDs.cend(); ++it) {
if (line->GetTeamID() != *it)
continue;
LineMarker lm;
lm.pos = line->GetPos1();
lm.pos2 = line->GetPos2();
lm.color = teamHandler->Team(line->GetTeamID())->color;
lines.push_back(lm);
break;
}
}
}
}
}
|