Galactic Bloodshed
moveplanet.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 /* moveplanet.c -- move the planet in orbit around its star. */
6 
7 #include "gb/moveplanet.h"
8 
9 #include <cmath>
10 
11 #include "gb/doturn.h"
12 #include "gb/ships.h"
13 #include "gb/tweakables.h"
14 #include "gb/vars.h"
15 
16 void moveplanet(int starnum, Planet *planet, int planetnum) {
17  double dist;
18  double xadd;
19  double yadd;
20  double phase;
21  double period;
22  int sh;
23  Ship *ship;
24 
25  if (planet->popn || planet->ships) Stinfo[starnum][planetnum].inhab = 1;
26 
27  StarsInhab[starnum] =
28  !!(Stars[starnum]->inhabited[0] + Stars[starnum]->inhabited[1]);
29  StarsExpl[starnum] =
30  !!(Stars[starnum]->explored[0] + Stars[starnum]->explored[1]);
31 
32  Stars[starnum]->inhabited[0] = Stars[starnum]->inhabited[1] = 0;
33  if (!StarsExpl[starnum]) return; /* no one's explored the star yet */
34 
35  dist = hypot((double)(planet->ypos), (double)(planet->xpos));
36 
37  phase = atan2((double)(planet->ypos), (double)(planet->xpos));
38  period =
39  dist * sqrt((double)(dist / (SYSTEMGRAVCONST * Stars[starnum]->gravity)));
40  /* keppler's law */
41 
42  xadd = dist * cos((double)(-1. / period + phase)) - planet->xpos;
43  yadd = dist * sin((double)(-1. / period + phase)) - planet->ypos;
44  /* one update time unit - planets orbit counter-clockwise */
45 
46  /* adjust ships in orbit around the planet */
47  sh = planet->ships;
48  while (sh) {
49  ship = ships[sh];
50  ship->xpos += xadd;
51  ship->ypos += yadd;
52  sh = ship->nextship;
53  }
54 
55  planet->xpos += xadd;
56  planet->ypos += yadd;
57 }
void moveplanet(int, Planet *, int)
Definition: moveplanet.cc:16
#define SYSTEMGRAVCONST
Definition: tweakables.h:191