Galactic Bloodshed
perm.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 /* perm.c -- randomly permute a sector list */
6 
7 #include "gb/perm.h"
8 
9 #include "gb/tweakables.h"
10 #include "gb/utils/rand.h"
11 #include "gb/vars.h"
12 
13 static struct map { char x, y; } xymap[(MAX_X + 1) * (MAX_Y + 1)];
14 
15 /* make a random list of sectors. */
16 void PermuteSects(const Planet &planet) {
17  int t = planet.Maxy * planet.Maxx;
18 
19  for (int i = 0, x = 0, y = 0; i < t; i++) {
20  xymap[i].x = x;
21  xymap[i].y = y;
22  if (++x >= planet.Maxx) {
23  x = 0;
24  y++;
25  }
26  }
27  for (int i = 0; i < t; i++) {
28  struct map sw = xymap[i];
29  int j = int_rand(0, t - 1);
30  xymap[i] = xymap[j];
31  xymap[j] = sw;
32  }
33 }
34 
35 /* get the next x,y sector in the list. if r=1, reset the counter.
36  * increments the counter & returns whether or not this reset it to 0.
37  */
38 
39 int Getxysect(const Planet &p, int *x, int *y, int r) {
40  static int getxy;
41  static int max;
42 
43  if (r) {
44  getxy = 0;
45  max = p.Maxx * p.Maxy;
46  } else {
47  *x = xymap[getxy].x;
48  *y = xymap[getxy].y;
49  if (++getxy > max) getxy = 0;
50  }
51  return getxy;
52 }
char x
Definition: perm.cc:13
void PermuteSects(const Planet &planet)
Definition: perm.cc:16
char y
Definition: perm.cc:13
#define MAX_X
Definition: tweakables.h:75
#define MAX_Y
Definition: tweakables.h:76
Definition: perm.cc:13
int Getxysect(const Planet &p, int *x, int *y, int r)
Definition: perm.cc:39