Galactic Bloodshed
powercmd.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 /* power.c -- display power report */
6 
7 #include "gb/powercmd.h"
8 
9 #include <cstdio>
10 #include <cstring>
11 
12 #include "gb/GB_server.h"
13 #include "gb/buffers.h"
14 #include "gb/power.h"
15 #include "gb/prof.h"
16 #include "gb/races.h"
17 #include "gb/shlmisc.h"
18 #include "gb/vars.h"
19 #include "gb/victory.h"
20 
21 static void prepare_output_line(Race *, Race *, int, int);
22 
23 void block(const command_t &argv, GameObj &g) {
24  player_t Playernum = g.player;
25  governor_t Governor = g.governor;
26  // TODO(jeffbailey): int APcount = 0;
27  player_t p;
28  racetype *r;
29  racetype *Race;
30  int dummy_;
31  int dummy[2];
32 
33  Race = races[Playernum - 1];
34 
35  if (argv.size() == 3 && argv[1] == "player") {
36  if (!(p = get_player(argv[2]))) {
37  g.out << "No such player.\n";
38  return;
39  }
40  r = races[p - 1];
41  dummy_ = 0; /* Used as flag for finding a block */
42  sprintf(buf, "Race #%d [%s] is a member of ", p, r->name);
43  notify(Playernum, Governor, buf);
44  for (int i = 1; i <= Num_races; i++) {
45  if (isset(Blocks[i - 1].pledge, p) && isset(Blocks[i - 1].invite, p)) {
46  sprintf(buf, "%s%d", (dummy_ == 0) ? " " : ", ", i);
47  notify(Playernum, Governor, buf);
48  dummy_ = 1;
49  }
50  }
51  if (dummy_ == 0)
52  g.out << "no blocks\n";
53  else
54  g.out << "\n";
55 
56  dummy_ = 0; /* Used as flag for finding a block */
57  sprintf(buf, "Race #%d [%s] has been invited to join ", p, r->name);
58  notify(Playernum, Governor, buf);
59  for (int i = 1; i <= Num_races; i++) {
60  if (!isset(Blocks[i - 1].pledge, p) && isset(Blocks[i - 1].invite, p)) {
61  sprintf(buf, "%s%d", (dummy_ == 0) ? " " : ", ", i);
62  notify(Playernum, Governor, buf);
63  dummy_ = 1;
64  }
65  }
66  if (dummy_ == 0)
67  g.out << "no blocks\n";
68  else
69  g.out << "\n";
70 
71  dummy_ = 0; /* Used as flag for finding a block */
72  sprintf(buf, "Race #%d [%s] has pledged ", p, r->name);
73  notify(Playernum, Governor, buf);
74  for (int i = 1; i <= Num_races; i++) {
75  if (isset(Blocks[i - 1].pledge, p) && !isset(Blocks[i - 1].invite, p)) {
76  sprintf(buf, "%s%d", (dummy_ == 0) ? " " : ", ", i);
77  notify(Playernum, Governor, buf);
78  dummy_ = 1;
79  }
80  }
81  if (!dummy_)
82  g.out << "no blocks\n";
83  else
84  g.out << "\n";
85  } else if (argv.size() > 1) {
86  if (!(p = get_player(argv[1]))) {
87  g.out << "No such player,\n";
88  return;
89  }
90  r = races[p - 1];
91  /* list the players who are in this alliance block */
92  dummy[0] = (Blocks[p - 1].invite[0] & Blocks[p - 1].pledge[0]);
93  dummy[1] = (Blocks[p - 1].invite[1] & Blocks[p - 1].pledge[1]);
94  sprintf(buf, " ========== %s Power Report ==========\n",
95  Blocks[p - 1].name);
96  notify(Playernum, Governor, buf);
97  sprintf(buf, " %-64.64s\n", Blocks[p - 1].motto);
98  notify(Playernum, Governor, buf);
99  sprintf(buf,
100  " # Name troops pop money ship plan res "
101  "fuel dest know\n");
102  notify(Playernum, Governor, buf);
103 
104  for (int i = 1; i <= Num_races; i++)
105  if (isset(dummy, i)) {
106  r = races[i - 1];
107  if (!r->dissolved) {
108  sprintf(buf, "%2d %-20.20s ", i, r->name);
109  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].troops, Race, i));
110  strcat(buf, temp);
111  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].popn, Race, i));
112  strcat(buf, temp);
113  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].money, Race, i));
114  strcat(buf, temp);
115  sprintf(temp, "%5s",
116  Estimate_i((int)Power[i - 1].ships_owned, Race, i));
117  strcat(buf, temp);
118  sprintf(temp, "%5s",
119  Estimate_i((int)Power[i - 1].planets_owned, Race, i));
120  strcat(buf, temp);
121  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].resource, Race, i));
122  strcat(buf, temp);
123  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].fuel, Race, i));
124  strcat(buf, temp);
125  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].destruct, Race, i));
126  strcat(buf, temp);
127  sprintf(temp, " %3d%%\n", Race->translate[i - 1]);
128  strcat(buf, temp);
129  notify(Playernum, Governor, buf);
130  }
131  }
132  } else { /* list power report for all the alliance blocks (as of the last
133  update) */
134  sprintf(buf, " ========== Alliance Blocks as of %s ==========\n",
135  Power_blocks.time);
136  notify(Playernum, Governor, buf);
137  sprintf(buf,
138  " # Name memb money popn ship sys res fuel "
139  "dest VPs know\n");
140  notify(Playernum, Governor, buf);
141  for (int i = 1; i <= Num_races; i++)
142  if (Blocks[i - 1].VPs) {
143  sprintf(buf, "%2d %-19.19s%3ld", i, Blocks[i - 1].name,
144  Power_blocks.members[i - 1]);
145  sprintf(temp, "%5s",
146  Estimate_i((int)(Power_blocks.money[i - 1]), Race, i));
147  strcat(buf, temp);
148  sprintf(temp, "%5s",
149  Estimate_i((int)(Power_blocks.popn[i - 1]), Race, i));
150  strcat(buf, temp);
151  sprintf(temp, "%5s",
152  Estimate_i((int)(Power_blocks.ships_owned[i - 1]), Race, i));
153  strcat(buf, temp);
154  sprintf(temp, "%5s",
155  Estimate_i((int)(Power_blocks.systems_owned[i - 1]), Race, i));
156  strcat(buf, temp);
157  sprintf(temp, "%5s",
158  Estimate_i((int)(Power_blocks.resource[i - 1]), Race, i));
159  strcat(buf, temp);
160  sprintf(temp, "%5s",
161  Estimate_i((int)(Power_blocks.fuel[i - 1]), Race, i));
162  strcat(buf, temp);
163  sprintf(temp, "%5s",
164  Estimate_i((int)(Power_blocks.destruct[i - 1]), Race, i));
165  strcat(buf, temp);
166  sprintf(temp, "%5s",
167  Estimate_i((int)(Power_blocks.VPs[i - 1]), Race, i));
168  strcat(buf, temp);
169  sprintf(temp, " %3d%%\n", Race->translate[i - 1]);
170  strcat(buf, temp);
171  notify(Playernum, Governor, buf);
172  }
173  }
174 }
175 
176 void power(const command_t &argv, GameObj &g) {
177  player_t Playernum = g.player;
178  governor_t Governor = g.governor;
179  // TODO(jeffbailey): int APcount = 0;
180  player_t p;
181  racetype *r;
182  racetype *Race;
183  struct vic vic[MAXPLAYERS];
184 
185  p = -1;
186 
187  if (argv.size() >= 2) {
188  if (!(p = get_player(argv[1]))) {
189  g.out << "No such player,\n";
190  return;
191  }
192  r = races[p - 1];
193  }
194 
195  Race = races[Playernum - 1];
196 
197  sprintf(buf,
198  " ========== Galactic Bloodshed Power Report ==========\n");
199  notify(Playernum, Governor, buf);
200 
201  if (Race->God)
202  sprintf(buf,
203  "%s # Name VP mil civ cash ship pl res "
204  "fuel dest morl VNs\n",
205  argv.size() < 2 ? "rank" : "");
206  else
207  sprintf(buf,
208  "%s # Name VP mil civ cash ship pl res "
209  "fuel dest morl know\n",
210  argv.size() < 2 ? "rank" : "");
211  notify(Playernum, Governor, buf);
212 
213  if (argv.size() < 2) {
215  for (int i = 1; i <= Num_races; i++) {
216  p = vic[i - 1].racenum;
217  r = races[p - 1];
218  if (!r->dissolved && Race->translate[p - 1] >= 10) {
219  prepare_output_line(Race, r, p, i);
220  notify(Playernum, Governor, buf);
221  }
222  }
223  } else {
224  r = races[p - 1];
225  prepare_output_line(Race, r, p, 0);
226  notify(Playernum, Governor, buf);
227  }
228 }
229 
230 static void prepare_output_line(racetype *Race, racetype *r, int i, int rank) {
231  if (rank)
232  sprintf(buf, "%2d ", rank);
233  else
234  buf[0] = '\0';
235  sprintf(temp, "[%2d]%s%s%-15.15s %5s", i,
236  isset(Race->allied, i) ? "+" : (isset(Race->atwar, i) ? "-" : " "),
237  isset(r->allied, Race->Playernum)
238  ? "+"
239  : (isset(r->atwar, Race->Playernum) ? "-" : " "),
240  r->name, Estimate_i((int)r->victory_score, Race, i));
241  strcat(buf, temp);
242  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].troops, Race, i));
243  strcat(buf, temp);
244  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].popn, Race, i));
245  strcat(buf, temp);
246  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].money, Race, i));
247  strcat(buf, temp);
248  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].ships_owned, Race, i));
249  strcat(buf, temp);
250  sprintf(temp, "%3s", Estimate_i((int)Power[i - 1].planets_owned, Race, i));
251  strcat(buf, temp);
252  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].resource, Race, i));
253  strcat(buf, temp);
254  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].fuel, Race, i));
255  strcat(buf, temp);
256  sprintf(temp, "%5s", Estimate_i((int)Power[i - 1].destruct, Race, i));
257  strcat(buf, temp);
258  sprintf(temp, "%5s", Estimate_i((int)r->morale, Race, i));
259  strcat(buf, temp);
260  if (Race->God)
261  sprintf(temp, " %3d\n", Sdata.VN_hitlist[i - 1]);
262  else
263  sprintf(temp, " %3d%%\n", Race->translate[i - 1]);
264  strcat(buf, temp);
265 }
#define isset(a, i)
Definition: vars.h:312
static void prepare_output_line(racetype *Race, racetype *r, int i, int rank)
Definition: powercmd.cc:230
void create_victory_list(struct vic vic[MAXPLAYERS])
Definition: victory.cc:60
void block(const command_t &argv, GameObj &g)
Definition: powercmd.cc:23
void power(const command_t &argv, GameObj &g)
Definition: powercmd.cc:176
#define MAXPLAYERS
Definition: vars.h:45
char * Estimate_i(int data, Race *r, int p)
Definition: prof.cc:301