Galactic Bloodshed
cs.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/cs.h"
6 
7 #include <cstdio>
8 #include <cstdlib>
9 
10 #include "gb/GB_server.h"
11 #include "gb/files_shl.h"
12 #include "gb/getplace.h"
13 #include "gb/races.h"
14 #include "gb/ships.h"
15 #include "gb/vars.h"
16 
17 void center(const command_t &argv, GameObj &g) {
18  if (argv.size() != 2) {
19  g.out << "center: which star?\n";
20  }
21  auto where = getplace(g, argv[1], 1);
22 
23  if (where.err) {
24  g.out << "center: bad scope.\n";
25  return;
26  }
27  if (where.level == ScopeLevel::LEVEL_SHIP) {
28  g.out << "CHEATER!!!\n";
29  return;
30  }
31  g.lastx[1] = Stars[where.snum]->xpos;
32  g.lasty[1] = Stars[where.snum]->ypos;
33 }
34 
35 void cs(const command_t &argv, GameObj &g) {
36  const player_t Playernum = g.player;
37  const governor_t Governor = g.governor;
38  racetype *Race = races[Playernum - 1];
39 
40  if (argv.size() == 1) {
41  /* chdir to def scope */
42  g.level = Race->governor[Governor].deflevel;
43  if ((g.snum = Race->governor[Governor].defsystem) >= Sdata.numstars)
44  g.snum = Sdata.numstars - 1;
45  if ((g.pnum = Race->governor[Governor].defplanetnum) >=
46  Stars[g.snum]->numplanets)
47  g.pnum = Stars[g.snum]->numplanets - 1;
48  g.shipno = 0;
49  g.lastx[0] = g.lasty[0] = 0.0;
50  g.lastx[1] = Stars[g.snum]->xpos;
51  g.lasty[1] = Stars[g.snum]->ypos;
52  return;
53  }
54  if (argv.size() == 2) {
55  /* chdir to specified scope */
56 
57  auto where = getplace(g, argv[1], 0);
58 
59  if (where.err) {
60  g.out << "cs: bad scope.\n";
61  g.lastx[0] = g.lasty[0] = 0.0;
62  return;
63  }
64 
65  /* fix lastx, lasty coordinates */
66 
67  switch (g.level) {
68  case ScopeLevel::LEVEL_UNIV:
69  g.lastx[0] = g.lasty[0] = 0.0;
70  break;
71  case ScopeLevel::LEVEL_STAR:
72  if (where.level == ScopeLevel::LEVEL_UNIV) {
73  g.lastx[1] = Stars[g.snum]->xpos;
74  g.lasty[1] = Stars[g.snum]->ypos;
75  } else
76  g.lastx[0] = g.lasty[0] = 0.0;
77  break;
78  case ScopeLevel::LEVEL_PLAN: {
79  const auto planet = getplanet(g.snum, g.pnum);
80  if (where.level == ScopeLevel::LEVEL_STAR && where.snum == g.snum) {
81  g.lastx[0] = planet.xpos;
82  g.lasty[0] = planet.ypos;
83  } else if (where.level == ScopeLevel::LEVEL_UNIV) {
84  g.lastx[1] = Stars[g.snum]->xpos + planet.xpos;
85  g.lasty[1] = Stars[g.snum]->ypos + planet.ypos;
86  } else
87  g.lastx[0] = g.lasty[0] = 0.0;
88  } break;
89  case ScopeLevel::LEVEL_SHIP:
90  auto s = getship(g.shipno);
91  if (!s->docked) {
92  switch (where.level) {
93  case ScopeLevel::LEVEL_UNIV:
94  g.lastx[1] = s->xpos;
95  g.lasty[1] = s->ypos;
96  break;
97  case ScopeLevel::LEVEL_STAR:
98  if (s->whatorbits >= ScopeLevel::LEVEL_STAR &&
99  s->storbits == where.snum) {
100  /* we are going UP from the ship.. change last*/
101  g.lastx[0] = s->xpos - Stars[s->storbits]->xpos;
102  g.lasty[0] = s->ypos - Stars[s->storbits]->ypos;
103  } else
104  g.lastx[0] = g.lasty[0] = 0.0;
105  break;
106  case ScopeLevel::LEVEL_PLAN:
107  if (s->whatorbits == ScopeLevel::LEVEL_PLAN &&
108  s->storbits == where.snum && s->pnumorbits == where.pnum) {
109  /* same */
110  const auto planet = getplanet(s->storbits, s->pnumorbits);
111  g.lastx[0] = s->xpos - Stars[s->storbits]->xpos - planet.xpos;
112  g.lasty[0] = s->ypos - Stars[s->storbits]->ypos - planet.ypos;
113  } else
114  g.lastx[0] = g.lasty[0] = 0.0;
115  break;
116  case ScopeLevel::LEVEL_SHIP:
117  g.lastx[0] = g.lasty[0] = 0.0;
118  break;
119  }
120  } else
121  g.lastx[0] = g.lasty[0] = 0.0;
122  break;
123  }
124  g.level = where.level;
125  g.snum = where.snum;
126  g.pnum = where.pnum;
127  g.shipno = where.shipno;
128  } else if (argv.size() == 3 && argv[1][1] == 'd') {
129  /* make new def scope */
130  auto where = getplace(g, argv[2], 0);
131 
132  if (!where.err && where.level != ScopeLevel::LEVEL_SHIP) {
133  Race->governor[Governor].deflevel = where.level;
134  Race->governor[Governor].defsystem = where.snum;
135  Race->governor[Governor].defplanetnum = where.pnum;
136  putrace(Race);
137 
138  g.out << "New home system is " << Dispplace(where) << "\n";
139  } else {
140  g.out << "cs: bad home system.\n";
141  }
142  }
143 }
Planet getplanet(const starnum_t star, const planetnum_t pnum)
Definition: files_shl.cc:335
void center(const command_t &argv, GameObj &g)
Definition: cs.cc:17
void cs(const command_t &argv, GameObj &g)
Definition: cs.cc:35
void putrace(Race *r)
Definition: files_shl.cc:808