-
Notifications
You must be signed in to change notification settings - Fork 0
/
hspace.cpp
279 lines (233 loc) · 6.38 KB
/
hspace.cpp
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
// -----------------------------------------------------------------------
//! $Id: hspace.cpp,v 1.6 2007/08/19 17:41:27 grapenut_org Exp $
// -----------------------------------------------------------------------
#include "pch.h"
// This has to come before the other includes,
// or Windows really screws up
#ifdef WIN32
#undef OPAQUE /* Clashes with flags.h */
#endif
#ifndef WIN32
#include <strings.h>
#include <sys/time.h>
#endif
#include "hspace.h"
#include "hsuniverse.h"
#include "hsuniversedb.h"
#include "hsinterface.h"
#include "hscmds.h"
#include "hsfuncs.h"
#include "hsconf.h"
#include "hsdb.h"
#include "hsutils.h"
#include "hsserver.h"
#include "hspilot.h"
extern FILE *spacelog_fp;
// The one and only, HSpace instance!
CHSpace HSpace;
CHSpace::CHSpace() :
m_cycling(false),
m_attrs_need_cleaning(false),
m_cycle_ms(0)
{
}
// Initializes HSpace. The reboot variable indicates whether the
// server is in reboot mode, in which the proceses will be swapped
// instead of starting from cold boot.
void CHSpace::InitSpace(int reboot)
{
hsInitMutex();
// Copy our logfile to a save copy -- no worries if it fails
rename("space/space.log", "space/space.log.old");
// Open our space log.
fopen_s(&spacelog_fp, "space/space.log", "w");
if(NULL == spacelog_fp)
{
fopen_s(&spacelog_fp, "space.log", "w");
if(NULL == spacelog_fp)
{
spacelog_fp = stderr;
}
}
hsInterface.SetupInterfaceCommands();
// Load the config file first.
if(!HSCONF.LoadConfigFile(HSPACE_CONFIG_FILE))
{
hs_log("CHSpace::InitSpace() - Configuration file loading failed.");
return;
}
InitPilots();
if(!dbHSDB.LoadDatabases())
{
hs_log("CHSpace::InitSpace() - Database loading failed!");
return;
}
// Auto cycle?
if(HSCONF.autostart)
{
hs_log("Automatic cycling enabled.");
m_cycling = true;
}
if(HSCONF.admin_server)
{
CHSServer::GetInstance().StartServer(HSCONF.admin_server_port);
hs_log(hsInterface.
HSPrintf("ADMIN SERVER: Started remote admin server on port %d.",
HSCONF.admin_server_port));
}
}
void CHSpace::Shutdown()
{
if(HSCONF.admin_server)
{
CHSServer::GetInstance().ShutdownServer();
}
hsDestroyMutex();
}
// Called once per second externally to do all of the stuff that
// HSpace needs to do each cycle.
void CHSpace::DoCycle()
{
#ifndef WIN32
struct timeval tv;
#endif
double firstms, secondms;
if(!m_cycling)
return;
hsEnterMutex();
// Run the server
if(HSCONF.admin_server)
{
CHSServer::GetInstance().DoCycle();
}
// Grab the current milliseconds
#ifdef WIN32
firstms = timeGetTime();
#else
gettimeofday(&tv, (struct timezone *) NULL);
firstms = (tv.tv_sec * 1000000) + tv.tv_usec;
#endif
// If attributes need cleaned up, do that.
if(m_attrs_need_cleaning)
{
dbHSDB.CleanupDBAttrs();
m_attrs_need_cleaning = false;
}
// Do cyclic stuff for space objects.
THSUniverseIterator tIter;
HS_BOOL8 bIter;
for (bIter = CHSUniverseDB::GetInstance().GetFirstUniverse(tIter); bIter;
bIter = CHSUniverseDB::GetInstance().GetNextUniverse(tIter))
{
CHSUniverse *pUniverse = tIter.pValue;
// Grab all of the objects in the universe, and tell them
// to cycle.
THSObjectIterator tIterator;
HS_BOOL8 bContinue;
for (bContinue = pUniverse->GetFirstObject(tIterator); bContinue;
bContinue = pUniverse->GetNextObject(tIterator))
{
CHS3DObject *pObject = tIterator.pValue;
pObject->DoCycle();
}
} // Cycle stuff done for CHS3DObjects
// Grab the current milliseconds, and subtract from the first
#ifdef WIN32
secondms = timeGetTime();
m_cycle_ms = secondms - firstms;
#else
gettimeofday(&tv, (struct timezone *) NULL);
secondms = (tv.tv_sec * 1000000) + tv.tv_usec;
m_cycle_ms = (secondms - firstms) * .001;
#endif
hsLeaveMutex();
}
void CHSpace::DumpDatabases()
{
dbHSDB.DumpDatabases();
m_attrs_need_cleaning = true;
}
char *CHSpace::DoFunction(char *func, HS_DBREF executor, char **args)
{
HSFUNC *hFunc;
hFunc = hsFindFunction(func);
if(!hFunc)
{
return "#-1 HSpace function not found.";
}
else
{
return hFunc->func(executor, args);
}
}
void CHSpace::DoCommand(char *cmd, const char *switches, char *arg_left,
char *arg_right, HS_DBREF player)
{
HSPACE_COMMAND *hCmd;
hsEnterMutex();
if(!_stricmp(cmd, "@space"))
{
hCmd = hsFindCommand(switches, hsSpaceCommandArray);
}
else if(!_stricmp(cmd, "@nav"))
{
hCmd = hsFindCommand(switches, hsNavCommandArray);
}
else if(!_stricmp(cmd, "@console"))
{
hCmd = hsFindCommand(switches, hsConCommandArray);
}
else if(!_stricmp(cmd, "@eng"))
{
hCmd = hsFindCommand(switches, hsEngCommandArray);
}
else
{
hsInterface.Notify(player, "Invalid HSpace command.");
hsLeaveMutex();
return;
}
if(!hCmd)
{
hsInterface.Notify(player,
"Invalid HSpace switch supplied with command.");
hsLeaveMutex();
return;
}
// Check the permissions of the command
if(hCmd->perms & HCP_GOD)
{
if(hsInterface.GetGodDbref() != player)
{
hsInterface.Notify(player, "HSpace command restricted to God.");
hsLeaveMutex();
return;
}
}
if(hCmd->perms & HCP_WIZARD)
{
if(!hsInterface.IsWizard(player))
{
hsInterface.Notify(player,
"HSpace command restricted to wizards.");
hsLeaveMutex();
return;
}
}
// Log the command first
if(HSCONF.log_commands)
{
hs_log(hsInterface.HSPrintf("CMD: (#%d) %s/%s %s=%s",
player, cmd, switches, arg_left, arg_right));
}
// Perms look ok, call the function.
hCmd->func(player, arg_left, arg_right);
hsLeaveMutex();
}
// Turns the space cycle on/off
void CHSpace::SetCycle(HS_BOOL8 bStart)
{
hs_log(hsInterface.HSPrintf("Space cycling %s.",
bStart ? "enabled" : "disabled"));
m_cycling = bStart;
}