Galactic Bloodshed
makeuniv.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 /* makeuniv.c -- universe creation program.
6  * Makes various required data files; calls makestar for each star desired. */
7 
8 #include "gb/creator/makeuniv.h"
9 
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 
14 #include "gb/GB_server.h"
15 #include "gb/buffers.h"
16 #include "gb/build.h"
17 #include "gb/creator/makestar.h"
18 #include "gb/creator/namegen.h"
19 #include "gb/files.h"
20 #include "gb/files_shl.h"
21 #include "gb/globals.h"
22 #include "gb/map.h"
23 #include "gb/power.h"
24 #include "gb/races.h"
25 #include "gb/sql/sql.h"
26 #include "gb/tweakables.h"
27 #include "gb/utils/fileutils.h"
28 #include "gb/utils/rand.h"
29 #include "gb/vars.h"
30 
31 int autoname_star = -1;
32 int autoname_plan = -1;
33 int minplanets = -1;
34 int maxplanets = -1;
35 int printplaninfo = 0;
36 int printstarinfo = 0;
37 
38 static int nstars = -1;
39 static int occupied[100][100];
40 static int planetlesschance = 0;
41 
42 int main(int argc, char *argv[]) {
43  int c;
44  int i;
45 
46  /*
47  * Initialize: */
48  srandom(getpid());
49  bzero(&Sdata, sizeof(Sdata));
50 
51  NameGenerator *star_name_gen = nullptr;
52  NameGenerator *planet_name_gen = nullptr;
53 
54  /*
55  * Read the arguments for values: */
56  for (i = 1; i < argc; i++)
57  if (argv[i][0] != '-')
58  goto usage;
59  else
60  switch (argv[i][1]) {
61  case 'a':
62  autoname_star = 1;
63  break;
64  case 'b':
65  autoname_plan = 1;
66  break;
67  case 'e':
68  planetlesschance = atoi(argv[++i]);
69  break;
70  case 'l':
71  minplanets = atoi(argv[++i]);
72  break;
73  case 'm':
74  maxplanets = atoi(argv[++i]);
75  break;
76  case 's':
77  nstars = atoi(argv[++i]);
78  break;
79  case 'v':
80  printplaninfo = 1;
81  break;
82  case 'w':
83  printstarinfo = 1;
84  break;
85  case 'd':
86  autoname_star = 1;
87  autoname_plan = 1;
88  printplaninfo = 1;
89  printstarinfo = 1;
90  nstars = 128;
91  minplanets = 1;
92  maxplanets = 10;
93  break;
94  default:
95  printf("\n");
96  printf("Unknown option \"%s\".\n", argv[i]);
97  usage:
98  printf("\n");
99  printf(
100  "Usage: makeuniv [-a] [-b] [-e E] [-l MIN] [-m MAX] [-s N] [-v] "
101  "[-w]\n");
102  printf(" -a Autoload star names.\n");
103  printf(" -b Autoload planet names.\n");
104  printf(" -d Use all defauls and autoloaded names.\n");
105  printf(" -e E Make E%% of stars have no planets.\n");
106  printf(" -l MIN Other systems will have at least MIN planets.\n");
107  printf(" -m MAX Other systems will have at most MAX planets.\n");
108  printf(" -s S The universe will have S stars.\n");
109  printf(" -v Print info and map of planets generated.\n");
110  printf(" -w Print info on stars generated.\n");
111  printf("\n");
112  exit(0);
113  }
114 
115  /*
116  * Get values for all the switches that still don't have good values. */
117  if (autoname_star == -1) {
118  printf("\nDo you wish to use the file \"%s\" for star names? [y/n]> ",
119  STARLIST);
120  c = getchr();
121  if (c != '\n') getchr();
122  autoname_star = (c == 'y');
123  }
124  if (autoname_plan == -1) {
125  printf("\nDo you wish to use the file \"%s\" for planet names? [y/n]> ",
126  PLANETLIST);
127  c = getchr();
128  if (c != '\n') getchr();
129  autoname_plan = (c == 'y');
130  }
131  while ((nstars < 1) || (nstars >= NUMSTARS)) {
132  printf("Number of stars [1-%d]:", NUMSTARS - 1);
133  scanf("%d", &nstars);
134  }
135  while ((minplanets <= 0) || (minplanets > MAXPLANETS)) {
136  printf("Minimum number of planets per system [1-%d]: ", MAXPLANETS);
137  scanf("%d", &minplanets);
138  }
139  while ((maxplanets < minplanets) || (maxplanets > MAXPLANETS)) {
140  printf("Maximum number of planets per system [%d-%d]: ", minplanets,
141  MAXPLANETS);
142  scanf("%d", &maxplanets);
143  }
144 
147  Sdata.numstars = nstars;
148 
149  Sql db{};
151 
152  for (starnum_t star = 0; star < nstars; star++) {
153  Stars[star] = Makestar(star);
154  }
155 
156 #if 0
157  /*
158  * Try to more evenly space stars. Essentially this is an inverse-gravity
159  * calculation: the nearer two stars are to each other, the more they
160  * repulse each other. Several iterations of this will suffice to move all
161  * of the stars nicely apart. */
162  for (i=0; i<CREAT_UNIV_ITERAT; i++)
163  for (star=0; star<Sdata.numstars; star++) {
164  for (x=0; x<Sdata.numstars; x++) /* star2 */
165  if (x!=star) {
166  /* find inverse of distance squared */
167  att = 10*UNIVSIZE / Distsq(Stars[star]->xpos, Stars[star]->ypos, Stars[x]->xpos, Stars[x]->ypos);
168  xspeed[star] += att * (Stars[star]->xpos - Stars[x]->xpos);
169  if (Stars[star]->xpos>UNIVSIZE || Stars[star]->xpos< -UNIVSIZE)
170  xspeed[star] *= -1;
171  yspeed[star] += att * (Stars[star]->ypos - Stars[x]->ypos);
172  if (Stars[star]->ypos>UNIVSIZE || Stars[star]->ypos< -UNIVSIZE)
173  yspeed[star] *= -1;
174  }
175  Stars[star]->xpos += xspeed[star];
176  Stars[star]->ypos += yspeed[star];
177  }
178 #endif
179 
180  putsdata(&Sdata);
181  for (starnum_t star = 0; star < Sdata.numstars; star++)
182  putstar(Stars[star], star);
183  chmod(STARDATAFL, 00660);
184 
185  EmptyFile(SHIPDATAFL);
186  EmptyFile(SHIPFREEDATAFL);
187  EmptyFile(COMMODDATAFL);
188  EmptyFile(COMMODFREEDATAFL);
189  EmptyFile(PLAYERDATAFL);
190  EmptyFile(RACEDATAFL);
191 
192  {
193  struct power p[MAXPLAYERS];
194  bzero((char *)p, sizeof(p));
195  InitFile(POWFL, p, sizeof(p));
196  }
197 
198  {
199  struct block p[MAXPLAYERS];
200  bzero((char *)p, sizeof(p));
201  InitFile(BLOCKDATAFL, p, sizeof(p));
202  }
203 
204  /*
205  * Telegram files: directory and a file for each player. */
206  mkdir(TELEGRAMDIR, 00770);
207 #if 0
208  /* Why is this not needed any more? */
209  for (i=1; i<MAXPLAYERS; i++) {
210  sprintf(str, "%s.%d", TELEGRAMFL, i );
211  EmptyFile(str) ;
212  }
213 #endif
214 
215  /*
216  * News files: directory and the 4 types of news. */
217  mkdir(NEWSDIR, 00770);
218  EmptyFile(DECLARATIONFL);
219  EmptyFile(TRANSFERFL);
220  EmptyFile(COMBATFL);
221  EmptyFile(ANNOUNCEFL);
222 
224 
225  return 0;
226 }
227 
228 void place_star(startype *star) {
229  int found = 0;
230  int i;
231  int j;
232  while (!found) {
233  star->xpos = (double)int_rand(-UNIVSIZE, UNIVSIZE);
234  star->ypos = (double)int_rand(-UNIVSIZE, UNIVSIZE);
235  /* check to see if another star is nearby */
236  i = 100 * ((int)star->xpos + UNIVSIZE) / (2 * UNIVSIZE);
237  j = 100 * ((int)star->xpos + UNIVSIZE) / (2 * UNIVSIZE);
238  if (!occupied[i][j]) occupied[i][j] = found = 1;
239  }
240 }
#define STARLIST
Definition: files.h:48
#define COMMODFREEDATAFL
Definition: files.h:43
int autoname_plan
Definition: makeuniv.cc:32
#define TRANSFERFL
Definition: files.h:39
void initsqldata()
Definition: files_shl.cc:42
int maxplanets
Definition: makeuniv.cc:34
#define SHIPFREEDATAFL
Definition: files.h:31
#define getchr()
Definition: tweakables.h:210
int printstarinfo
Definition: makeuniv.cc:36
#define POWFL
Definition: files.h:36
#define ANNOUNCEFL
Definition: files.h:41
int main(int argc, char *argv[])
Definition: makeuniv.cc:42
#define PLANETLIST
Definition: files.h:47
startype * Makestar(int)
Definition: makestar.cc:213
void Makestar_init()
Definition: makestar.cc:190
#define PLAYERDATAFL
Definition: files.h:33
static int occupied[100][100]
Definition: makeuniv.cc:39
#define TELEGRAMDIR
Definition: files.h:34
int autoname_star
Definition: makeuniv.cc:31
void PrintStatistics()
Definition: makestar.cc:72
#define COMBATFL
Definition: files.h:40
void putstar(startype *s, starnum_t snum)
Definition: files_shl.cc:813
void place_star(startype *)
Definition: makeuniv.cc:228
#define BLOCKDATAFL
Definition: files.h:29
int printplaninfo
Definition: makeuniv.cc:35
#define SHIPDATAFL
Definition: files.h:30
#define STARDATAFL
Definition: files.h:26
#define UNIVSIZE
Definition: tweakables.h:80
#define MAXPLANETS
Definition: tweakables.h:72
static int planetlesschance
Definition: makeuniv.cc:40
int minplanets
Definition: makeuniv.cc:33
void putsdata(struct stardata *S)
Definition: files_shl.cc:804
#define RACEDATAFL
Definition: files.h:28
#define NUMSTARS
Definition: tweakables.h:71
#define NEWSDIR
Definition: files.h:37
#define COMMODDATAFL
Definition: files.h:42
#define MAXPLAYERS
Definition: vars.h:45
static int nstars
Definition: makeuniv.cc:38
void Makeplanet_init()
Definition: makestar.cc:174
#define DECLARATIONFL
Definition: files.h:38