-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.hpp
executable file
·134 lines (115 loc) · 3.47 KB
/
controller.hpp
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
#ifndef CONTROLLER_HPP
#define CONTROLLER_HPP
#include "command.hpp"
#include "packet.hpp"
#include "model.hpp"
#include "networknode.hpp"
class Controller
{
public:
Controller(
Model *model,
CommandQueue *commandQueue,
MessageQueue *messageQueue);
virtual ~Controller();
virtual void timepass(float dt) = 0;
bool isWaiting();
virtual std::string waitingFor() = 0;
virtual void disconnected(NetworkNode *net) = 0;
protected:
virtual void drainUnitFuel();
void moveUnits(float dt);
virtual void onTileReached(unitID id) = 0;
virtual void onAngleReached(unitID id) = 0;
void updateUnitFacing(unitID id);
void resolveProjectiles();
virtual void resolveProjectile(unsigned index);
virtual void killUnit(unitID id, int explosion);
virtual void processCommands() = 0;
virtual void processMessages() = 0;
// Convert incoming packets into local commands or messages.
virtual void convertPackets() = 0;
bool waiting;
Model *model;
CommandQueue *incomingCommands;
MessageQueue *incomingMessages;
};
class ServerController : public Controller
{
public:
ServerController(
Model *model,
CommandQueue *commandQueue,
MessageQueue *messageQueue);
void timepass(float dt);
playerID addPlayer(int nodeId, const char *name=NULL);
private:
unitID createUnit(playerID owner, std::string unit, unsigned x, unsigned y);
void checkVictoryConditions();
void pathfindUnit(unitID id);
void stopUnit(unitID id);
void produceUnits(float dt);
void onTileReached(unitID id);
void onAngleReached(unitID id);
void convertPackets();
void processMessages();
void processCommands();
void sendChatMessage(std::string text, int player);
void resolveProjectile(unsigned index);
void killUnit(unitID id, int explosion);
void updateScrapyard(int id);
void damageUnit(int damage, unitID id);
void thinkUnits();
void acquireTargets();
bool fireBurst(Model::Unit *shooter, Model::Unit *target);
void turnAndShoot(Model::Unit *shooter, Model::Unit *target);
bool canAutoTarget(Model::Unit *u, Model::Unit *target);
bool canTarget(Model::Unit *u, Model::Unit *target);
bool canShoot(Model::Unit *u, Model::Unit *target);
bool isBetterTarget(Model::Unit *u, Model::Unit *newTarget, Model::Unit *oldTarget);
void loadUnit(unitID unitId, unitID transportId);
void unloadUnit(unitID transport, unitID unit);
void drainUnitFuel();
void updateUnitFuel(unitID id);
void regenerateUnits();
void convertScrapyard(int sid, playerID player);
void updateUnitState(unitID unit);
std::string waitingFor();
void disconnected(NetworkNode *net);
void playerLost(playerID player);
void playerWon(playerID player);
double nextIdleFuelTime, nextRegenTime;
struct PlayerStats {
std::string name;
int lastHeard;
int node;
bool alive;
};
std::map<playerID, PlayerStats> players;
int frame;
int lastSent;
float window;
bool gameOver;
void sendHeartbeat();
void updateHeartbeat(int player, int time);
};
class ClientController : public Controller
{
public:
ClientController(
Model *model,
CommandQueue *commandQueue,
MessageQueue *messageQueue);
void timepass(float dt);
std::string waitingFor() { return "server"; }
void disconnected(NetworkNode *net);
private:
void onTileReached(unitID id);
void onAngleReached(unitID id);
void convertPackets();
void processMessages();
void processCommands();
playerID playerId;
};
void cleanPathCache();
#endif