Galactic Bloodshed
explore.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 "gb/explore.h"
6 
7 #include <cmath>
8 #include <cstdio>
9 #include <cstdlib>
10 
11 #include "gb/GB_server.h"
12 #include "gb/buffers.h"
13 #include "gb/files_shl.h"
14 #include "gb/getplace.h"
15 #include "gb/map.h"
16 #include "gb/max.h"
17 #include "gb/power.h"
18 #include "gb/races.h"
19 #include "gb/ships.h"
20 #include "gb/tech.h"
21 #include "gb/tweakables.h"
22 #include "gb/vars.h"
23 
24 void distance(const command_t &argv, GameObj &g) {
25  const player_t Playernum = g.player;
26  const governor_t Governor = g.governor;
27  placetype from;
28  placetype to;
29  double x0;
30  double y0;
31  double x1;
32  double y1;
33  double dist;
34 
35  if (argv.size() < 3) {
36  g.out << "Syntax: 'distance <from> <to>'.\n";
37  return;
38  }
39 
40  from = getplace(g, argv[1], 1);
41  if (from.err) {
42  sprintf(buf, "Bad scope '%s'.\n", argv[1].c_str());
43  notify(Playernum, Governor, buf);
44  return;
45  }
46  to = getplace(g, argv[2], 1);
47  if (to.err) {
48  sprintf(buf, "Bad scope '%s'.\n", argv[2].c_str());
49  notify(Playernum, Governor, buf);
50  }
51 
52  x0 = 0.0;
53  y0 = 0.0;
54  x1 = 0.0;
55  y1 = 0.0;
56  /* get position in absolute units */
57  if (from.level == ScopeLevel::LEVEL_SHIP) {
58  auto ship = getship(from.shipno);
59  if (ship->owner != Playernum) {
60  g.out << "Nice try.\n";
61  return;
62  }
63  x0 = ship->xpos;
64  y0 = ship->ypos;
65  } else if (from.level == ScopeLevel::LEVEL_PLAN) {
66  const auto p = getplanet(from.snum, from.pnum);
67  x0 = p.xpos + Stars[from.snum]->xpos;
68  y0 = p.ypos + Stars[from.snum]->ypos;
69  } else if (from.level == ScopeLevel::LEVEL_STAR) {
70  x0 = Stars[from.snum]->xpos;
71  y0 = Stars[from.snum]->ypos;
72  }
73 
74  if (to.level == ScopeLevel::LEVEL_SHIP) {
75  auto ship = getship(to.shipno);
76  if (ship->owner != Playernum) {
77  g.out << "Nice try.\n";
78  return;
79  }
80  x1 = ship->xpos;
81  y1 = ship->ypos;
82  } else if (to.level == ScopeLevel::LEVEL_PLAN) {
83  const auto p = getplanet(to.snum, to.pnum);
84  x1 = p.xpos + Stars[to.snum]->xpos;
85  y1 = p.ypos + Stars[to.snum]->ypos;
86  } else if (to.level == ScopeLevel::LEVEL_STAR) {
87  x1 = Stars[to.snum]->xpos;
88  y1 = Stars[to.snum]->ypos;
89  }
90  /* compute the distance */
91  dist = sqrt(Distsq(x0, y0, x1, y1));
92  sprintf(buf, "Distance = %f\n", dist);
93  notify(Playernum, Governor, buf);
94 }
95 
96 void star_locations(const command_t &argv, GameObj &g) {
97  const player_t Playernum = g.player;
98  const governor_t Governor = g.governor;
99  int i;
100  double dist;
101  double x;
102  double y;
103  int max;
104 
105  x = g.lastx[1];
106  y = g.lasty[1];
107 
108  if (argv.size() > 1)
109  max = std::stoi(argv[1]);
110  else
111  max = 999999;
112 
113  for (i = 0; i < Sdata.numstars; i++) {
114  dist = sqrt(Distsq(Stars[i]->xpos, Stars[i]->ypos, x, y));
115  if ((int)dist <= max) {
116  sprintf(buf, "(%2d) %20.20s (%8.0f,%8.0f) %7.0f\n", i, Stars[i]->name,
117  Stars[i]->xpos, Stars[i]->ypos, dist);
118  notify(Playernum, Governor, buf);
119  }
120  }
121 }
122 
123 void exploration(const command_t &argv, GameObj &g) {
124  const player_t Playernum = g.player;
125  const governor_t Governor = g.governor;
126  int starq;
127  int j;
128  placetype where;
129  racetype *Race;
130 
131  starq = -1;
132 
133  if (argv.size() == 2) {
134  where = getplace(g, argv[1], 0);
135  if (where.err) {
136  sprintf(buf, "explore: bad scope.\n");
137  notify(Playernum, Governor, buf);
138  return;
139  }
140  if (where.level == ScopeLevel::LEVEL_SHIP ||
141  where.level == ScopeLevel::LEVEL_UNIV) {
142  sprintf(buf, "Bad scope '%s'.\n", argv[1].c_str());
143  notify(Playernum, Governor, buf);
144  return;
145  }
146  starq = where.snum;
147  }
148 
149  Race = races[Playernum - 1];
150 
151  getsdata(&Sdata);
152  sprintf(buf, " ========== Exploration Report ==========\n");
153  notify(Playernum, Governor, buf);
154  sprintf(buf, " Global action points : [%2d]\n", Sdata.AP[Playernum - 1]);
155  notify(Playernum, Governor, buf);
156  sprintf(
157  buf,
158  " Star (stability)[AP] # Planet [Attributes] Type (Compatibility)\n");
159  notify(Playernum, Governor, buf);
160  for (starnum_t star = 0; star < Sdata.numstars; star++)
161  if ((starq == -1) || (starq == star)) {
162  getstar(&(Stars[star]), star);
163  if (isset(Stars[star]->explored, Playernum))
164  for (planetnum_t i = 0; i < Stars[star]->numplanets; i++) {
165  const auto pl = getplanet(star, i);
166  if (i == 0) {
167  if (Race->tech >= TECH_SEE_STABILITY) {
168  sprintf(buf, "\n%13s (%2d)[%2d]\n", Stars[star]->name,
169  Stars[star]->stability, Stars[star]->AP[Playernum - 1]);
170  notify(Playernum, Governor, buf);
171  } else {
172  sprintf(buf, "\n%13s (/?/?)[%2d]\n", Stars[star]->name,
173  Stars[star]->AP[Playernum - 1]);
174  notify(Playernum, Governor, buf);
175  }
176  }
177 
178  sprintf(buf, "\t\t ");
179  notify(Playernum, Governor, buf);
180 
181  sprintf(buf, " #%d. %-15s [ ", i + 1, Stars[star]->pnames[i]);
182  notify(Playernum, Governor, buf);
183  if (pl.info[Playernum - 1].explored) {
184  sprintf(buf, "Ex ");
185  notify(Playernum, Governor, buf);
186  if (pl.info[Playernum - 1].autorep) {
187  sprintf(buf, "Rep ");
188  notify(Playernum, Governor, buf);
189  }
190  if (pl.info[Playernum - 1].numsectsowned) {
191  sprintf(buf, "Inhab ");
192  notify(Playernum, Governor, buf);
193  }
194  if (pl.slaved_to) {
195  sprintf(buf, "SLAVED ");
196  notify(Playernum, Governor, buf);
197  }
198  for (j = 1; j <= Num_races; j++)
199  if (j != Playernum && pl.info[j - 1].numsectsowned) {
200  sprintf(buf, "%d ", j);
201  notify(Playernum, Governor, buf);
202  }
203  if (pl.conditions[TOXIC] > 70) {
204  sprintf(buf, "TOXIC ");
205  notify(Playernum, Governor, buf);
206  }
207  sprintf(buf, "] %s %2.0f%%\n", Planet_types[pl.type],
208  compatibility(pl, Race));
209  notify(Playernum, Governor, buf);
210  } else {
211  sprintf(buf, "No Data ]\n");
212  notify(Playernum, Governor, buf);
213  }
214  }
215  }
216 }
#define Distsq(x1, y1, x2, y2)
Definition: tweakables.h:218
void exploration(const command_t &argv, GameObj &g)
Definition: explore.cc:123
Planet getplanet(const starnum_t star, const planetnum_t pnum)
Definition: files_shl.cc:335
void star_locations(const command_t &argv, GameObj &g)
Definition: explore.cc:96
#define isset(a, i)
Definition: vars.h:312
#define TECH_SEE_STABILITY
Definition: tweakables.h:183
#define TOXIC
Definition: tweakables.h:39
double compatibility(const Planet &planet, const Race *race)
Definition: max.cc:37
void distance(const command_t &argv, GameObj &g)
Definition: explore.cc:24
void getsdata(struct stardata *S)
Definition: files_shl.cc:272
void getstar(startype **s, int star)
Definition: files_shl.cc:281