Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Component.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: Component.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Generic ship system sub-component class
13 */
14 
15 #include "MemDebug.h"
16 #include "Component.h"
17 #include "System.h"
18 #include "Game.h"
19 
20 // +----------------------------------------------------------------------+
21 
23 : repair_time(0.0f), replace_time(0.0f), spares(0), affects(0)
24 { }
25 
26 // +----------------------------------------------------------------------+
27 
29 { }
30 
31 // +----------------------------------------------------------------------+
32 
34 : design(d), system(s),
35 status(NOMINAL), availability(100.0f), time_remaining(0.0f),
36 spares(0), jerried(0)
37 {
38  if (design)
39  spares = design->spares;
40 }
41 
42 // +----------------------------------------------------------------------+
43 
45 : design(c.design), system(c.system),
46 status(c.status), availability(c.availability), time_remaining(c.time_remaining),
47 spares(c.spares), jerried(c.jerried)
48 {
49 }
50 
51 // +--------------------------------------------------------------------+
52 
54 { }
55 
56 // +--------------------------------------------------------------------+
57 
58 void
60 {
61  if (status > NOMINAL) {
62  time_remaining -= (float) seconds;
63 
64  // when repairs are complete:
65  if (time_remaining <= 0) {
66  if (status == REPAIR) {
67  // did we just jerry-rig a failed component?
68  if (availability < 50)
69  jerried++;
70 
71  if (jerried < 5)
72  availability += 50.0f - 10 * jerried;
73  if (availability > 100) availability = 100.0f;
74  }
75  else {
76  availability = 100.0f;
77  }
78 
79  if (availability > 99)
80  status = NOMINAL;
81  else if (availability > 49)
82  status = DEGRADED;
83  else
84  status = CRITICAL;
85 
86  time_remaining = 0.0f;
87 
88  if (system)
89  system->CalcStatus();
90  }
91  }
92 }
93 
94 // +--------------------------------------------------------------------+
95 
96 void
97 Component::ApplyDamage(double damage)
98 {
99  availability -= (float) damage;
100  if (availability < 1) availability = 0.0f;
101 
102  if (status < REPLACE) {
103  if (availability > 99)
104  status = NOMINAL;
105  else if (availability > 49)
106  status = DEGRADED;
107  else
108  status = CRITICAL;
109  }
110 
111  if (system)
112  system->CalcStatus();
113 }
114 
115 // +--------------------------------------------------------------------+
116 
117 void
119 {
120  if (status < NOMINAL) {
121  status = REPAIR;
123 
124  if (system)
125  system->CalcStatus();
126  }
127 }
128 
129 // +--------------------------------------------------------------------+
130 
131 void
133 {
134  if (status <= NOMINAL) {
135  status = REPLACE;
136  spares--;
138 
139  if (system)
140  system->CalcStatus();
141  }
142 }
143 
144 // +--------------------------------------------------------------------+
145 
146 float
148 {
149  if (status > NOMINAL && availability > 50)
150  return 50.0f;
151 
152  return availability;
153 }
154 
155 float
157 {
158  return (float) time_remaining;
159 }
160 
161 int
163 {
164  return spares;
165 }
166 
167 bool
169 {
170  return jerried?true:false;
171 }
172 
173 int
175 {
176  return jerried;
177 }