Galactic Bloodshed
ships.cc
Go to the documentation of this file.
1 // Copyright 2014 The Galactic Bloodshed Authors. All rights reserved.
2 // Use of this source code is governed by a license that can be
3 // found in the COPYING file.
4 
5 #include <boost/format.hpp>
6 #include <cmath>
7 
8 #include "gb/defense.h"
9 #include "gb/ships.h"
10 
11 #include "gb/files_shl.h"
12 
13 // Essentialy everything in this file can move into a Ship class.
14 
15 /* can takeoff & land, is mobile, etc. */
16 unsigned short speed_rating(const Ship &s) { return s.max_speed; }
17 
18 /* has an on/off switch */
19 bool has_switch(const Ship &s) { return Shipdata[s.type][ABIL_HASSWITCH]; }
20 
21 /* can bombard planets */
22 bool can_bombard(const Ship &s) {
23  return Shipdata[s.type][ABIL_GUNS] && (s.type != ShipType::STYPE_MINE);
24 }
25 
26 /* can navigate */
27 bool can_navigate(const Ship &s) {
28  return Shipdata[s.type][ABIL_SPEED] > 0 && s.type != ShipType::OTYPE_TERRA &&
29  s.type != ShipType::OTYPE_VN;
30 }
31 
32 /* can aim at things. */
33 bool can_aim(const Ship &s) {
34  return s.type >= ShipType::STYPE_MIRROR && s.type <= ShipType::OTYPE_TRACT;
35 }
36 
37 /* macros to get ship stats */
38 unsigned long armor(const Ship &s) {
39  return (s.type == ShipType::OTYPE_FACTORY) ? Shipdata[s.type][ABIL_ARMOR]
40  : s.armor * (100 - s.damage) / 100;
41 }
42 
43 long guns(const Ship &s) {
44  return (s.guns == GTYPE_NONE) ? 0
45  : (s.guns == PRIMARY ? s.primary : s.secondary);
46 }
47 
48 population_t max_crew(const Ship &s) {
49  return (s.type == ShipType::OTYPE_FACTORY)
50  ? Shipdata[s.type][ABIL_MAXCREW] - s.troops
51  : s.max_crew - s.troops;
52 }
53 
54 population_t max_mil(const Ship &s) {
55  return (s.type == ShipType::OTYPE_FACTORY)
56  ? Shipdata[s.type][ABIL_MAXCREW] - s.popn
57  : s.max_crew - s.popn;
58 }
59 
60 long max_resource(const Ship &s) {
61  return (s.type == ShipType::OTYPE_FACTORY) ? Shipdata[s.type][ABIL_CARGO]
62  : s.max_resource;
63 }
64 int max_crystals(const Ship &) { return 127; }
65 
66 long max_fuel(const Ship &s) {
67  return (s.type == ShipType::OTYPE_FACTORY) ? Shipdata[s.type][ABIL_FUELCAP]
68  : s.max_fuel;
69 }
70 
71 long max_destruct(const Ship &s) {
72  return (s.type == ShipType::OTYPE_FACTORY) ? Shipdata[s.type][ABIL_DESTCAP]
73  : s.max_destruct;
74 }
75 
76 long max_speed(const Ship &s) {
77  return (s.type == ShipType::OTYPE_FACTORY) ? Shipdata[s.type][ABIL_SPEED]
78  : s.max_speed;
79 }
80 
81 long shipcost(const Ship &s) {
82  return (s.type == ShipType::OTYPE_FACTORY)
83  ? 2 * s.build_cost * s.on + Shipdata[s.type][ABIL_COST]
84  : s.build_cost;
85 }
86 
87 double mass(const Ship &s) { return s.mass; }
88 
89 long shipsight(const Ship &s) {
90  return (s.type == ShipType::OTYPE_PROBE) || s.popn;
91 }
92 
93 long retaliate(const Ship &s) { return s.retaliate; }
94 
95 int size(const Ship &s) { return s.size; }
96 
97 int shipbody(const Ship &s) { return s.size - s.max_hanger; }
98 
99 long hanger(const Ship &s) { return s.max_hanger - s.hanger; }
100 
101 long repair(const Ship &s) {
102  return (s.type == ShipType::OTYPE_FACTORY) ? s.on : max_crew(s);
103 }
104 
105 Shiplist::Iterator::Iterator(shipnum_t a) {
106  auto tmpship = getship(a);
107  if (tmpship) {
108  elem = *tmpship;
109  } else {
110  elem = Ship{};
111  elem.number = 0;
112  }
113 }
114 
115 Shiplist::Iterator &Shiplist::Iterator::operator++() {
116  auto tmpship = getship(elem.nextship);
117  if (tmpship) {
118  elem = *tmpship;
119  } else {
120  elem = Ship{};
121  elem.number = 0;
122  }
123  return *this;
124 }
125 
126 int getdefense(const Ship &ship) {
127  if (landed(ship)) {
128  const auto p = getplanet(ship.storbits, ship.pnumorbits);
129  const auto sect = getsector(p, ship.land_x, ship.land_y);
130  return (2 * Defensedata[sect.condition]);
131  }
132  // No defense
133  return 0;
134 }
135 
136 bool laser_on(const Ship &ship) { return (ship.laser && ship.fire_laser); }
137 
138 bool landed(const Ship &ship) {
139  return (ship.whatdest == ScopeLevel::LEVEL_PLAN && ship.docked);
140 }
141 
142 void capture_stuff(const Ship &ship, GameObj &g) {
143  Shiplist shiplist(ship.ships);
144  for (auto s : shiplist) {
145  capture_stuff(s, g); /* recursive call */
146  s.owner = ship.owner; /* make sure he gets all of the ships landed on it */
147  s.governor = ship.governor;
148  putship(&s);
149  g.out << ship_to_string(s) << " CAPTURED!\n";
150  }
151 }
152 
154  return str(boost::format("%c%lu %s [%d]") % Shipltrs[s.type] % s.number %
155  s.name % s.owner);
156 }
157 
158 double getmass(const Ship &s) {
159  return (1.0 + MASS_ARMOR * s.armor + MASS_SIZE * (s.size - s.max_hanger) +
160  MASS_HANGER * s.max_hanger + MASS_GUNS * s.primary * s.primtype +
161  MASS_GUNS * s.secondary * s.sectype);
162 }
163 
164 unsigned int ship_size(const Ship &s) {
165  double size = 1.0 + SIZE_GUNS * s.primary + SIZE_GUNS * s.secondary +
166  SIZE_CREW * s.max_crew + SIZE_RESOURCE * s.max_resource +
167  SIZE_FUEL * s.max_fuel + SIZE_DESTRUCT * s.max_destruct +
168  s.max_hanger;
169  return (std::floor(size));
170 }
171 
172 double cost(const Ship &s) {
173  /* compute how much it costs to build this ship */
174  double factor = 0.0;
175  factor += (double)Shipdata[s.build_type][ABIL_COST];
176  factor += GUN_COST * (double)s.primary;
177  factor += GUN_COST * (double)s.secondary;
178  factor += CREW_COST * (double)s.max_crew;
179  factor += CARGO_COST * (double)s.max_resource;
180  factor += FUEL_COST * (double)s.max_fuel;
181  factor += AMMO_COST * (double)s.max_destruct;
182  factor +=
183  SPEED_COST * (double)s.max_speed * (double)sqrt((double)s.max_speed);
184  factor += HANGER_COST * (double)s.max_hanger;
185  factor += ARMOR_COST * (double)s.armor * (double)sqrt((double)s.armor);
186  factor += CEW_COST * (double)(s.cew * s.cew_range);
187  /* additional advantages/disadvantages */
188 
189  double advantage = 0.0;
190  advantage += 0.5 * !!s.hyper_drive.has;
191  advantage += 0.5 * !!s.laser;
192  advantage += 0.5 * !!s.cloak;
193  advantage += 0.5 * !!s.mount;
194 
195  factor *= sqrt(1.0 + advantage);
196  return factor;
197 }
198 
199 namespace {
200 void system_cost(double *advantage, double *disadvantage, int value, int base) {
201  double factor;
202 
203  factor = (((double)value + 1.0) / (base + 1.0)) - 1.0;
204  if (factor >= 0.0)
205  *advantage += factor;
206  else
207  *disadvantage -= factor;
208 }
209 } // namespace
210 
211 double complexity(const Ship &s) {
212  double advantage = 0.;
213  double disadvantage = 0.;
214 
215  system_cost(&advantage, &disadvantage, (int)(s.primary),
216  Shipdata[s.build_type][ABIL_GUNS]);
217  system_cost(&advantage, &disadvantage, (int)(s.secondary),
218  Shipdata[s.build_type][ABIL_GUNS]);
219  system_cost(&advantage, &disadvantage, (int)(s.max_crew),
220  Shipdata[s.build_type][ABIL_MAXCREW]);
221  system_cost(&advantage, &disadvantage, (int)(s.max_resource),
222  Shipdata[s.build_type][ABIL_CARGO]);
223  system_cost(&advantage, &disadvantage, (int)(s.max_fuel),
224  Shipdata[s.build_type][ABIL_FUELCAP]);
225  system_cost(&advantage, &disadvantage, (int)(s.max_destruct),
226  Shipdata[s.build_type][ABIL_DESTCAP]);
227  system_cost(&advantage, &disadvantage, (int)(s.max_speed),
228  Shipdata[s.build_type][ABIL_SPEED]);
229  system_cost(&advantage, &disadvantage, (int)(s.max_hanger),
230  Shipdata[s.build_type][ABIL_HANGER]);
231  system_cost(&advantage, &disadvantage, (int)(s.armor),
232  Shipdata[s.build_type][ABIL_ARMOR]);
233  /* additional advantages/disadvantages */
234 
235  // TODO(jeffbailey): document this function in English.
236  double factor = sqrt((1.0 + advantage) * exp(-(double)disadvantage / 10.0));
237  double tmp = COMPLEXITY_FACTOR * (factor - 1.0) /
238  sqrt((double)(Shipdata[s.build_type][ABIL_TECH] + 1)) +
239  1.0;
240  factor = tmp * tmp;
241  return (factor * (double)Shipdata[s.build_type][ABIL_TECH]);
242 }
#define SPEED_COST
Definition: tweakables.h:283
#define CREW_COST
Definition: tweakables.h:279
unsigned short speed_rating(const Ship &s)
Definition: ships.cc:16
population_t max_crew(const Ship &s)
Definition: ships.cc:48
#define MASS_GUNS
Definition: tweakables.h:104
#define HANGER_COST
Definition: tweakables.h:286
bool can_navigate(const Ship &s)
Definition: ships.cc:27
long max_speed(const Ship &s)
Definition: ships.cc:76
unsigned long armor(const Ship &s)
Definition: ships.cc:38
#define ARMOR_COST
Definition: tweakables.h:285
bool laser_on(const Ship &ship)
Definition: ships.cc:136
bool can_bombard(const Ship &s)
Definition: ships.cc:22
bool has_switch(const Ship &s)
Definition: ships.cc:19
std::string ship_to_string(const Ship &s)
Definition: ships.cc:153
long hanger(const Ship &s)
Definition: ships.cc:99
int size(const Ship &s)
Definition: ships.cc:95
int shipbody(const Ship &s)
Definition: ships.cc:97
#define SIZE_RESOURCE
Definition: tweakables.h:108
int getdefense(const Ship &ship)
Definition: ships.cc:126
#define SIZE_FUEL
Definition: tweakables.h:109
#define PRIMARY
Definition: ships.h:15
#define FUEL_COST
Definition: tweakables.h:281
#define GUN_COST
Definition: tweakables.h:278
#define SIZE_GUNS
Definition: tweakables.h:106
population_t max_mil(const Ship &s)
Definition: ships.cc:54
#define COMPLEXITY_FACTOR
Definition: tweakables.h:306
unsigned int ship_size(const Ship &s)
Definition: ships.cc:164
#define SIZE_CREW
Definition: tweakables.h:107
long repair(const Ship &s)
Definition: ships.cc:101
#define MASS_SIZE
Definition: tweakables.h:102
long shipcost(const Ship &s)
Definition: ships.cc:81
long max_resource(const Ship &s)
Definition: ships.cc:60
#define MASS_ARMOR
Definition: tweakables.h:101
long shipsight(const Ship &s)
Definition: ships.cc:89
void system_cost(double *advantage, double *disadvantage, int value, int base)
Definition: ships.cc:200
int max_crystals(const Ship &)
Definition: ships.cc:64
bool can_aim(const Ship &s)
Definition: ships.cc:33
double complexity(const Ship &s)
Definition: ships.cc:211
long retaliate(const Ship &s)
Definition: ships.cc:93
long guns(const Ship &s)
Definition: ships.cc:43
long max_destruct(const Ship &s)
Definition: ships.cc:71
#define CARGO_COST
Definition: tweakables.h:280
double getmass(const Ship &s)
Definition: ships.cc:158
#define SIZE_DESTRUCT
Definition: tweakables.h:110
#define CEW_COST
Definition: tweakables.h:284
#define AMMO_COST
Definition: tweakables.h:282
double mass(const Ship &s)
Definition: ships.cc:87
void capture_stuff(const Ship &ship, GameObj &g)
Definition: ships.cc:142
bool landed(const Ship &ship)
Definition: ships.cc:138
#define MASS_HANGER
Definition: tweakables.h:103
long max_fuel(const Ship &s)
Definition: ships.cc:66
double cost(const Ship &s)
Definition: ships.cc:172