Galactic Bloodshed
dissolve.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 /* dissolve.c -- commit suicide, nuke all ships and sectors; */
6 
7 #include "gb/dissolve.h"
8 
9 #include <cstdio>
10 #include <cstdlib>
11 
12 #include "gb/GB_server.h"
13 #include "gb/buffers.h"
14 #include "gb/files.h"
15 #include "gb/files_shl.h"
16 #include "gb/races.h"
17 #include "gb/ships.h"
18 #include "gb/shlmisc.h"
19 #include "gb/tele.h"
20 #include "gb/tweakables.h"
21 #include "gb/utils/rand.h"
22 #include "gb/vars.h"
23 
24 void dissolve(const command_t &argv, GameObj &g) {
25  player_t Playernum = g.player;
26  governor_t Governor = g.governor;
27 #ifndef DISSOLVE
28  notify(Playernum, Governor,
29  "Dissolve has been disabled. Please notify diety.\n");
30  return;
31 #else
32 
33  int i;
34  int z;
35  racetype *Race;
36  char racepass[100];
37  char govpass[100];
38 
39  if (Governor) {
40  notify(Playernum, Governor,
41  "Only the leader may dissolve the race. The "
42  "leader has been notified of your "
43  "attempt!!!\n");
44  sprintf(buf, "Governor #%d has attempted to dissolve this race.\n",
45  Governor);
46  notify(Playernum, 0, buf);
47  return;
48  }
49 
50  if (argv.size() < 3) {
51  g.out << "Self-Destruct sequence requires passwords.\n";
52  g.out << "Please use 'dissolve <race password> <leader "
53  "password>'<option> to initiate\n";
54  g.out << "self-destruct sequence.\n";
55  return;
56  }
57  g.out << "WARNING!! WARNING!! WARNING!!\n";
58  g.out << "-------------------------------\n";
59  g.out << "Entering self destruct sequence!\n";
60 
61  sscanf(argv[1].c_str(), "%s", racepass);
62  sscanf(argv[2].c_str(), "%s", govpass);
63 
64  bool waste = false;
65  if (argv.size() > 3) {
66  if (argv[3][0] == 'w') waste = true;
67  }
68 
69  auto [player, governor] = getracenum(racepass, govpass);
70 
71  if (!player || !governor) {
72  g.out << "Password mismatch, self-destruct not initiated!\n";
73  return;
74  }
75 
76  auto n_ships = Numships();
77  for (i = 1; i <= n_ships; i++) {
78  auto sp = getship(i);
79  if (sp->owner != Playernum) continue;
80  kill_ship(Playernum, &*sp);
81  sprintf(buf, "Ship #%d, self-destruct enabled\n", i);
82  notify(Playernum, Governor, buf);
83  putship(&*sp);
84  }
85 
86  getsdata(&Sdata);
87  for (z = 0; z < Sdata.numstars; z++) {
88  getstar(&(Stars[z]), z);
89  if (isset(Stars[z]->explored, Playernum)) {
90  for (i = 0; i < Stars[z]->numplanets; i++) {
91  auto pl = getplanet(z, i);
92 
93  if (pl.info[Playernum - 1].explored &&
94  pl.info[Playernum - 1].numsectsowned) {
95  pl.info[Playernum - 1].fuel = 0;
96  pl.info[Playernum - 1].destruct = 0;
97  pl.info[Playernum - 1].resource = 0;
98  pl.info[Playernum - 1].popn = 0;
99  pl.info[Playernum - 1].troops = 0;
100  pl.info[Playernum - 1].tax = 0;
101  pl.info[Playernum - 1].newtax = 0;
102  pl.info[Playernum - 1].crystals = 0;
103  pl.info[Playernum - 1].numsectsowned = 0;
104  pl.info[Playernum - 1].explored = 0;
105  pl.info[Playernum - 1].autorep = 0;
106  }
107 
108  auto smap = getsmap(pl);
109  for (auto &s : smap) {
110  if (s.owner == Playernum) {
111  s.owner = 0;
112  s.troops = 0;
113  s.popn = 0;
114  if (waste) s.condition = SectorType::SEC_WASTED;
115  }
116  }
117  putsmap(smap, pl);
118  putstar(Stars[z], z);
119  putplanet(pl, Stars[z], i);
120  }
121  }
122  }
123 
124  Race = races[Playernum - 1];
125  Race->dissolved = 1;
126  putrace(Race);
127 
128  sprintf(buf, "%s [%d] has dissolved.\n", Race->name, Playernum);
129  post(buf, DECLARATION);
130 
131 #endif
132 }
133 
134 int revolt(Planet *pl, int victim, int agent) {
135  int x;
136  int y;
137  int hix;
138  int hiy;
139  int lowx;
140  int lowy;
141  racetype *Race;
142  int changed_hands = 0;
143 
144  Race = races[victim - 1];
145 
146  auto smap = getsmap(*pl);
147  /* do the revolt */
148  lowx = 0;
149  lowy = 0;
150  hix = pl->Maxx - 1;
151  hiy = pl->Maxy - 1;
152  for (y = lowy; y <= hiy; y++) {
153  for (x = lowx; x <= hix; x++) {
154  auto &s = smap.get(x, y);
155  if (s.owner == victim && s.popn) {
156  if (success(pl->info[victim - 1].tax)) {
157  if (static_cast<unsigned long>(long_rand(1, s.popn)) >
158  10 * Race->fighters * s.troops) {
159  s.owner = agent; /* enemy gets it */
160  s.popn = int_rand(1, (int)s.popn); /* some people killed */
161  s.troops = 0; /* all troops destroyed */
162  pl->info[victim - 1].numsectsowned -= 1;
163  pl->info[agent - 1].numsectsowned += 1;
164  pl->info[victim - 1].mob_points -= s.mobilization;
165  pl->info[agent - 1].mob_points += s.mobilization;
166  changed_hands++;
167  }
168  }
169  }
170  }
171  }
172  putsmap(smap, *pl);
173 
174  return changed_hands;
175 }
void post(const char *origmsg, int type)
Definition: tele.cc:63
Planet getplanet(const starnum_t star, const planetnum_t pnum)
Definition: files_shl.cc:335
#define isset(a, i)
Definition: vars.h:312
shipnum_t Numships()
Definition: files_shl.cc:1516
void putsmap(SectorMap &map, Planet &p)
Definition: files_shl.cc:1108
#define DISSOLVE
Definition: config.h:29
#define DECLARATION
Definition: files.h:17
void putstar(startype *s, starnum_t snum)
Definition: files_shl.cc:813
void dissolve(const command_t &argv, GameObj &g)
Definition: dissolve.cc:24
void getsdata(struct stardata *S)
Definition: files_shl.cc:272
void putplanet(const Planet &p, startype *star, const int pnum)
Definition: files_shl.cc:934
void getstar(startype **s, int star)
Definition: files_shl.cc:281
SectorMap getsmap(const Planet &p)
Definition: files_shl.cc:522
int revolt(Planet *pl, int victim, int agent)
Definition: dissolve.cc:134
void putrace(Race *r)
Definition: files_shl.cc:808