Galactic Bloodshed
grant.cc
Go to the documentation of this file.
1 // Copyright 2019 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 /// \file grant.cc
6 
7 #include "gb/commands/grant.h"
8 
9 #include <cctype>
10 #include <cmath>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <ctime>
15 #include <sstream>
16 #include <string>
17 
18 #include "gb/GB_server.h"
19 #include "gb/buffers.h"
20 #include "gb/files.h"
21 #include "gb/files_shl.h"
22 #include "gb/max.h"
23 #include "gb/races.h"
24 #include "gb/ships.h"
25 #include "gb/shlmisc.h"
26 #include "gb/tweakables.h"
27 #include "gb/vars.h"
28 
29 void grant(const command_t &argv, GameObj &g) {
30  player_t Playernum = g.player;
31  governor_t Governor = g.governor;
32  // int APcount = 0; TODO(jeffbailey);
33  racetype *Race;
34  governor_t gov;
35  shipnum_t nextshipno;
36  shipnum_t shipno;
37  Ship *ship;
38 
39  Race = races[Playernum - 1];
40  if (argv.size() < 3) {
41  g.out << "Syntax: grant <governor> star\n";
42  g.out << " grant <governor> ship <shiplist>\n";
43  g.out << " grant <governor> money <amount>\n";
44  return;
45  }
46  if ((gov = std::stoi(argv[1])) > MAXGOVERNORS) {
47  g.out << "Bad governor number.\n";
48  return;
49  }
50  if (!Race->governor[gov].active) {
51  g.out << "That governor is not active.\n";
52  return;
53  }
54  if (argv[2] == "star") {
55  int snum;
56  if (g.level != ScopeLevel::LEVEL_STAR) {
57  g.out << "Please cs to the star system first.\n";
58  return;
59  }
60  snum = g.snum;
61  Stars[snum]->governor[Playernum - 1] = gov;
62  sprintf(buf, "\"%s\" has granted you control of the /%s star system.\n",
63  Race->governor[Governor].name, Stars[snum]->name);
64  warn(Playernum, gov, buf);
65  putstar(Stars[snum], snum);
66  } else if (argv[2] == "ship") {
67  nextshipno = start_shiplist(g, argv[3]);
68  while ((shipno = do_shiplist(&ship, &nextshipno)))
69  if (in_list(Playernum, argv[3], *ship, &nextshipno) &&
70  authorized(Governor, *ship)) {
71  ship->governor = gov;
72  sprintf(buf, "\"%s\" granted you %s at %s\n",
73  Race->governor[Governor].name, ship_to_string(*ship).c_str(),
74  prin_ship_orbits(ship));
75  warn(Playernum, gov, buf);
76  putship(ship);
77  sprintf(buf, "%s granted to \"%s\"\n", ship_to_string(*ship).c_str(),
78  Race->governor[gov].name);
79  notify(Playernum, Governor, buf);
80  free(ship);
81  } else
82  free(ship);
83  } else if (argv[2] == "money") {
84  long amount;
85  if (argv.size() < 4) {
86  g.out << "Indicate the amount of money.\n";
87  return;
88  }
89  amount = std::stoi(argv[3]);
90  if (amount < 0 && Governor) {
91  g.out << "Only leaders may make take away money.\n";
92  return;
93  }
94  if (amount > Race->governor[Governor].money)
95  amount = Race->governor[Governor].money;
96  else if (-amount > Race->governor[gov].money)
97  amount = -Race->governor[gov].money;
98  if (amount >= 0)
99  sprintf(buf, "%ld money granted to \"%s\".\n", amount,
100  Race->governor[gov].name);
101  else
102  sprintf(buf, "%ld money deducted from \"%s\".\n", -amount,
103  Race->governor[gov].name);
104  notify(Playernum, Governor, buf);
105  if (amount >= 0)
106  sprintf(buf, "\"%s\" granted you %ld money.\n",
107  Race->governor[Governor].name, amount);
108  else
109  sprintf(buf, "\"%s\" docked you %ld money.\n",
110  Race->governor[Governor].name, -amount);
111  warn(Playernum, gov, buf);
112  Race->governor[Governor].money -= amount;
113  Race->governor[gov].money += amount;
114  putrace(Race);
115  return;
116  } else
117  g.out << "You can't grant that.\n";
118 }
#define MAXGOVERNORS
Definition: tweakables.h:312
void putstar(startype *s, starnum_t snum)
Definition: files_shl.cc:813
shipnum_t do_shiplist(Ship **s, shipnum_t *nextshipno)
Definition: shlmisc.cc:94
void grant(const command_t &argv, GameObj &g)
Definition: grant.cc:29
void putship(Ship *s)
Definition: files_shl.cc:1317
void putrace(Race *r)
Definition: files_shl.cc:808