-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalliance.cc
225 lines (203 loc) · 4.08 KB
/
alliance.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
alliance.cc
(c) Richard Thrippleton
Licensing terms are in the 'LICENSE' file
If that file is not included with this source then permission is not given to use this source in any way whatsoever.
*/
#include <stdio.h>
#include "database.h"
#include "calc.h"
#include "error.h"
#include "constants.h"
#include "planet.h"
#include "equip.h"
#include "ship.h"
#include "alliance.h"
void alliance::init()
{
for(int i=0;i<LIBSIZE;i++)
alliances[i]=NULL;
}
void alliance::loadlib()
{
char nam[12]; //Object name to get
for(int i=0;i<LIBSIZE;i++)
{
sprintf(nam,"Alliance%hd",i);
try
{
database::switchobj(nam);
alliances[i]=new alliance(i);
alliances[i]->load();
}
catch(error it)
{
}
}
}
void alliance::purgeall()
{
for(int i=0;i<LIBSIZE;i++)
if(alliances[i])
delete alliances[i];
}
alliance* alliance::get(int indx)
{
if(indx>=0 && indx<LIBSIZE)
{
if(alliances[indx])
return alliances[indx];
else
return NULL;
}
else
return NULL;
}
void alliance::maketerritories()
{
cord seed; //Where to seed each territory from
for(int i=0;i<LIBSIZE;i++)
{
if(alliances[i])
{
seed.x=calc::rnd(LIMIT/2)-calc::rnd(LIMIT/2);
seed.y=calc::rnd(LIMIT/2)-calc::rnd(LIMIT/2);
alliances[i]->maketerritory(seed);
}
}
}
bool alliance::opposes(alliance* all)
{
if(all==NULL)
return false;
if(this->grp!=all->grp)
return true;
else
return false;
}
equip* alliance::getequip()
{
return eqps[calc::rnd(16)];
}
ship* alliance::getspawn()
{
int shpc[ship::LIBSIZE]; //Count of each ship type
ship* tshp; //Ship being examined
for(int i=0;i<ship::LIBSIZE;i++)
shpc[i]=shpq[i];
for(int i=0;i<ship::ISIZE;i++)
{
tshp=ship::get(i);
if(tshp && tshp->typ>=0 && tshp->typ<=ship::LIBSIZE)
shpc[tshp->typ]--;
}
for(int i=0,j=0;i<ship::LIBSIZE*2;i++)
{
j=calc::rnd(ship::LIBSIZE);
if(shpc[j]>0)
return ship::libget(j);
}
return NULL;
}
int alliance::getai()
{
int aic[32]; //Count of each AI type
ship* tshp; //Ship being examined
for(int i=0;i<32;i++)
aic[i]=aiq[i];
for(int i=0;i<ship::ISIZE;i++)
{
tshp=ship::get(i);
if(tshp && tshp->aity>=0 && tshp->aity<=32)
aic[tshp->aity]--;
}
for(int i=0,j=0;i<64;i++)
{
j=calc::rnd(32);
if(aic[j]>0)
return j;
}
return ship::AI_PATROLLER;
}
alliance::alliance(int self) //Constructor, giving the alliance its own index in the database
{
this->self=self;
}
alliance::~alliance() //Destructor
{
if(self>=0 && self<LIBSIZE)
alliances[self]=NULL;
}
void alliance::maketerritory(cord seed) //Generate the star systems for this alliance at given seed co-ordinates
{
cord lsys; //Star system location
cord lpln; //Location of planet
char snam[65]; //Name of system
char pnam[65]; //Name of planet
int ptyp; //Type of planet
for(int i=0;i<nsys;i++)
{
lsys=seed;
lsys.x=seed.x+calc::rnd(LIMIT/5)-calc::rnd(LIMIT/5);
lsys.y=seed.y+calc::rnd(LIMIT/5)-calc::rnd(LIMIT/5);
lpln=lsys;
for(int j=0,n=calc::rnd(14)+2;j<n;j++)
{
if(j==0)
{
ptyp=planet::STAR;
planet::generatename(snam);
sprintf(pnam,"%s",snam);
}
else
{
if(calc::rnd(100)<pinh)
{
ptyp=planet::INHABITED;
planet::generatename(pnam);
}
else
{
ptyp=planet::UNINHABITED;
sprintf(pnam,"%s %hd",snam,j);
}
}
try
{
new planet(pnam,lpln,ptyp,this);
}
catch(error it)
{
}
lpln.x=lsys.x+calc::rnd(10000)-5000;
lpln.y=lsys.y+calc::rnd(10000)-5000;
}
}
}
void alliance::load()
{
char atsc[33]; //Attribute scratchpad
database::getvalue("Name",nam);
grp=database::getvalue("Group");
nsys=database::getvalue("Systems");
pinh=database::getvalue("PercentInhabited");
ripo=database::getvalue("Ripoff");
trad=database::getvalue("Trading");
spw=ship::libget(database::getvalue("Spawn"));
for(int i=0;i<ship::LIBSIZE;i++)
{
sprintf(atsc,"ShipQuota%hd",i);
shpq[i]=database::getvalue(atsc);
}
for(int i=0;i<32;i++)
{
sprintf(atsc,"AIQuota%hd",i);
aiq[i]=database::getvalue(atsc);
}
for(int i=0;i<16;i++)
{
sprintf(atsc,"Equipment%hd",i);
eqps[i]=equip::get(database::getvalue(atsc));
}
}
alliance* alliance::alliances[LIBSIZE];