-
Notifications
You must be signed in to change notification settings - Fork 0
/
hscomputer.cpp
218 lines (183 loc) · 4.75 KB
/
hscomputer.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
// -----------------------------------------------------------------------
// $Id: hscomputer.cpp,v 1.6 2006/04/04 12:41:11 mark Exp $
// -----------------------------------------------------------------------
#include "pch.h"
#include "hsobjects.h"
#include "hsship.h"
#include "hsconf.h"
#include "hsinterface.h"
#include "hscomputer.h"
CHSSysComputer::CHSSysComputer()
{
m_power_used = 0;
SetType(HSS_COMPUTER);
}
// Call this function to drain power from the computer
// to your system. If enough power is available, true
// is returned, and the power is deducted from the
// total power available.
HS_BOOL8 CHSSysComputer::DrainPower(int amt)
{
if ((m_power_used + amt) > (int) GetCurrentPower())
return false;
m_power_used += amt;
return true;
}
int CHSSysComputer::GetUsedPower()
{
return m_power_used;
}
int CHSSysComputer::GetConsoles()
{
int nConsoles;
CHSShip *cShip;
int idx;
CHSConsole *cCons;
cShip = (CHSShip *) GetOwnerObject();
nConsoles = 0;
// Query consoles
for (idx = 0; idx < MAX_SHIP_CONSOLES; idx++)
{
cCons = cShip->GetConsole(idx);
if (cCons)
{
nConsoles++;
}
}
return nConsoles;
}
int CHSSysComputer::GetPoweredConsoles()
{
int nPConsoles;
CHSShip *cShip;
int idx;
CHSConsole *cCons;
if (!GetOwnerObject())
{
return 0;
}
cShip = (CHSShip *) GetOwnerObject();
nPConsoles = 0;
// Query consoles
for (idx = 0; idx < MAX_SHIP_CONSOLES; idx++)
{
cCons = cShip->GetConsole(idx);
if (cCons)
{
if (cCons->IsOnline())
{
nPConsoles++;
}
}
}
return nPConsoles;
}
void CHSSysComputer::PowerUp(int level)
{
if (GetOwnerObject() && (GetOwnerObject()->GetType() == HST_SHIP))
{
CHSShip *cShip;
cShip = (CHSShip *) GetOwnerObject();
if (hsInterface.
AtrGet(cShip->GetDbref(), "HSMSG_COMPUTER_ACTIVATING"))
{
char *msg = hsInterface.EvalExpression(hsInterface.m_buffer,
cShip->GetDbref(),
cShip->GetDbref(),
cShip->GetDbref());
cShip->NotifySrooms(msg);
}
else
{
cShip->NotifySrooms(HSCONF.computer_activating);
}
if (level > 5)
m_power_used = 5;
else
m_power_used = level;
}
}
void CHSSysComputer::CutPower(int level)
{
if (level >= m_power_used)
return;
int idx;
CHSConsole *cCons;
CHSShip *cShip;
cShip = (CHSShip *) GetOwnerObject();
if (!cShip)
return;
for (idx = 0; idx < MAX_SHIP_CONSOLES; idx++)
{
cCons = cShip->GetConsole(idx);
if (cCons)
{
if (cCons->IsOnline())
{
cCons->PowerDown(hsInterface.ConsoleUser(cCons->m_objnum));
}
}
if (level >= m_power_used)
return;
}
if (level < 5)
m_power_used = level;
}
// Call this function to release power you've drained.
// If you don't, the computer gets confused about how
// much power is used. Kind of like a memory leak!
void CHSSysComputer::ReleasePower(int amt)
{
m_power_used -= amt;
}
// Returns the surplus (or deficit) between power allocated
// to the computer and power drained. If this is negative,
// your system should consider releasing the power. The
// computer won't do it.
int CHSSysComputer::GetPowerSurplus()
{
return (GetCurrentPower() - m_power_used);
}
// Returns the power usage for the computer system. This
// can vary depending on the owner object for the system.
// For example, ships have consoles, weapons, etc.
int CHSSysComputer::GetOptimalPower(HS_BOOL8 bDamage)
{
HS_UINT32 uPower;
if (!GetOwnerObject())
{
return 0;
}
if (GetDamageLevel() == DMG_INOPERABLE)
{
return 0;
}
// Computer requires 5 MW base power
uPower = 5;
// We currently only support ship computers
if (HST_SHIP == GetOwnerObject()->GetType())
{
uPower += TotalShipPower();
}
return uPower;
}
// Support for ship objects with computers. This will
// query various parts of the ship (e.g. consoles) for
// power requirements.
HS_UINT32 CHSSysComputer::TotalShipPower()
{
HS_UINT32 uPower;
CHSShip *cShip;
int idx;
CHSConsole *cCons;
cShip = (CHSShip *) GetOwnerObject();
uPower = 0;
// Query consoles
for (idx = 0; idx < MAX_SHIP_CONSOLES; idx++)
{
cCons = cShip->GetConsole(idx);
if (cCons)
uPower += cCons->GetMaximumPower();
}
return uPower;
}