-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
ShipAICmd.h
246 lines (201 loc) · 6.51 KB
/
ShipAICmd.h
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
// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#ifndef _SHIPAICMD_H
#define _SHIPAICMD_H
#include "GameSaveError.h"
#include "JsonFwd.h"
#include "DynamicBody.h"
#include "FixedGuns.h"
#include "FrameId.h"
#include "ship/Propulsion.h"
class Ship;
class Space;
class SpaceStation;
class AICommand {
public:
// This enum is solely to make the serialization work
enum CmdName { // <enum scope='AICommand::CmdName' name=ShipAICmdName public>
CMD_NONE,
CMD_DOCK,
CMD_FLYTO,
CMD_FLYAROUND,
CMD_KILL,
CMD_KAMIKAZE,
CMD_HOLDPOSITION,
CMD_FORMATION
};
AICommand(DynamicBody *dBody, CmdName name) :
m_dBody(dBody),
m_cmdName(name)
{
m_dBody->AIMessage(DynamicBody::AIERROR_NONE);
}
virtual ~AICommand() {}
virtual bool TimeStepUpdate() = 0;
bool ProcessChild(); // returns false if child is active
virtual void GetStatusText(char *str)
{
if (m_child)
m_child->GetStatusText(str);
else
strcpy(str, "AI state unknown");
}
// Serialisation functions
static AICommand *LoadFromJson(const Json &jsonObj);
AICommand(const Json &jsonObj, CmdName name);
virtual void SaveToJson(Json &jsonObj);
virtual void PostLoadFixup(Space *space);
// Signal functions
virtual void OnDeleted(const Body *body)
{
if (m_child) m_child->OnDeleted(body);
}
CmdName GetType() const { return m_cmdName; }
protected:
DynamicBody *m_dBody;
Propulsion *m_prop;
std::unique_ptr<AICommand> m_child;
bool m_is_flyto = false;
CmdName m_cmdName;
int m_dBodyIndex; // deserialisation
};
class AICmdDock : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdDock(DynamicBody *dBody, SpaceStation *target);
virtual void GetStatusText(char *str);
virtual void SaveToJson(Json &jsonObj);
AICmdDock(const Json &jsonObj);
virtual void PostLoadFixup(Space *space);
virtual void OnDeleted(const Body *body);
private:
enum EDockingStates {
eDockGetDataStart = 0, // 0: get data for docking start pos
eDockFlyToStart = 1, // 1: Fly to docking start pos
eDockGetDataEnd = 2, // 2: get data for docking end pos
eDockFlyToEnd = 3, // 3: Fly to docking end pos
eDockingComplete = 4,
eInvalidDockingStage = 5
};
SpaceStation *m_target;
vector3d m_dockpos; // offset in target's frame
vector3d m_dockdir;
vector3d m_dockupdir;
EDockingStates m_state; // see TimeStepUpdate()
int m_targetIndex; // used during deserialisation
void IncrementState()
{
switch (m_state) {
case eDockGetDataStart: m_state = eDockFlyToStart; break;
case eDockFlyToStart: m_state = eDockGetDataEnd; break;
case eDockGetDataEnd: m_state = eDockFlyToEnd; break;
case eDockFlyToEnd: m_state = eDockingComplete; break;
case eDockingComplete: m_state = eInvalidDockingStage; break;
case eInvalidDockingStage: break;
}
}
};
class AICmdFlyTo : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdFlyTo(DynamicBody *dBody, FrameId targframeId, const vector3d &posoff, double endvel, bool tangent);
AICmdFlyTo(DynamicBody *dBody, Body *target);
virtual void GetStatusText(char *str);
virtual void SaveToJson(Json &jsonObj);
AICmdFlyTo(const Json &jsonObj);
virtual void PostLoadFixup(Space *space);
virtual void OnDeleted(const Body *body);
private:
Body *m_target; // target for vicinity. Either this or targframe is 0
double m_dist; // vicinity distance
FrameId m_targframeId; // target frame for waypoint
vector3d m_posoff; // offset in target frame
double m_endvel; // target speed in direction of motion at end of path, positive only
bool m_tangent; // true if path is to a tangent of the target frame's body
int m_state;
int m_targetIndex; // used during deserialisation
vector3d m_reldir; // target direction relative to ship at last frame change
FrameId m_frameId; // last frame of ship
bool m_suicideRecovery;
};
class AICmdFlyAround : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdFlyAround(DynamicBody *dBody, Body *obstructor, double relalt, int mode = 2);
AICmdFlyAround(DynamicBody *dBody, Body *obstructor, double alt, double vel, int mode = 1);
virtual void GetStatusText(char *str);
virtual void SaveToJson(Json &jsonObj);
AICmdFlyAround(const Json &jsonObj);
virtual void PostLoadFixup(Space *space);
virtual void OnDeleted(const Body *body)
{
AICommand::OnDeleted(body);
// check against obstructor?
}
void SetTargPos(const vector3d &targpos)
{
m_targpos = targpos;
m_targmode = 0;
}
protected:
void Setup(Body *obstructor, double alt, double vel, int mode);
double MaxVel(double targdist, double targalt);
private:
Body *m_obstructor; // body to fly around
int m_obstructorIndex; // deserialisation
double m_alt, m_vel;
int m_targmode; // 0 targpos termination, 1 infinite, 2+ orbital termination
vector3d m_targpos; // target position in ship space
};
class AICmdKill : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdKill(DynamicBody *dBody, Ship *target);
AICmdKill(const Json &jsonObj);
~AICmdKill();
// don't actually need to save all this crap
virtual void SaveToJson(Json &jsonObj);
void PostLoadFixup(Space *space);
virtual void OnDeleted(const Body *body);
const Ship* GetTarget() const { return m_target; }
private:
FixedGuns *m_fguns;
Ship *m_target;
double m_leadTime, m_evadeTime, m_closeTime;
vector3d m_leadOffset, m_leadDrift, m_lastVel;
int m_targetIndex; // used during deserialisation
};
class AICmdKamikaze : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdKamikaze(DynamicBody *dBody, Body *target);
virtual void SaveToJson(Json &jsonObj);
AICmdKamikaze(const Json &jsonObj);
virtual void PostLoadFixup(Space *space);
virtual void OnDeleted(const Body *body);
const Body* GetTarget() const { return m_target; }
private:
Body *m_target;
int m_targetIndex; // used during deserialisation
};
class AICmdHoldPosition : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdHoldPosition(DynamicBody *dBody);
AICmdHoldPosition(const Json &jsonObj);
};
class AICmdFormation : public AICommand {
public:
virtual bool TimeStepUpdate();
AICmdFormation(DynamicBody *dBody, DynamicBody *target, const vector3d &posoff);
void GetStatusText(char *str);
virtual void SaveToJson(Json &jsonObj);
AICmdFormation(const Json &jsonObj);
virtual void PostLoadFixup(Space *space);
virtual void OnDeleted(const Body *body);
private:
DynamicBody *m_target; // target frame for waypoint
vector3d m_posoff; // offset in target frame
int m_targetIndex; // used during deserialisation
};
#endif /* _SHIPAICMD_H */