-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobject.cpp
483 lines (415 loc) · 12.2 KB
/
sobject.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "math.h"
#include "sobject.h"
#include "position.h"
#include "system.h"
#include "game.h"
#include "gui.h"
extern int max_factions;
//Orbital Period = sqrt(Distance From Star ^ 3)
vector<int> SObject::size(SOBJECT_MAX, 4);
vector<int> SObject::space(SOBJECT_MAX, 6);
vector<int> SObject::sqoff(SOBJECT_MAX, 9);
void SObject::Init() {
location = NULL;
destination = NULL;
depart_turn = 0;
arrive_turn = 0;
target = NULL;
distance = 0;
seen.resize(max_factions, 0);
known.resize(max_factions, 0);
gturn = -1;
sturn = -1;
}
void SObject::CopyFrom(SObject *s) {
Init();
target = GetPosition(s->target);
location = GetPosition(s->location);
destination = GetPosition(s->destination);
distance = s->distance;
depart_turn = s->depart_turn;
arrive_turn = s->arrive_turn;
system = s->system;
orbit = s->orbit;
startpos = s->startpos;
period = s->period;
frame = s->frame;
gturn = s->gturn;
gxloc = s->gxloc;
gyloc = s->gyloc;
gxpos = s->gxpos;
gypos = s->gypos;
sturn = s->sturn;
sxloc = s->sxloc;
syloc = s->syloc;
sxpos = s->sxpos;
sypos = s->sypos;
for(int ctr=0; ctr<max_factions; ++ctr) seen[ctr] = s->seen[ctr];
for(int ctr=0; ctr<max_factions; ++ctr) known[ctr] = s->known[ctr];
}
SObject::SObject(System *s, int orb) {
Init();
system = s;
orbit = orb;
period = int(sqrt(double(orbit)*double(orbit)*double(orbit)));
startpos = rand()&65535;
}
SObject::SObject() {
Init();
}
SObject::SObject(SObject *o) {
Init();
system = o->Sys();
orbit = o->orbit;
period = o->period;
startpos = o->startpos;
location = GetPosition(o);
}
void SObject::ComputeSLoc(int turn) {
sturn = turn;
double ang = double(startpos)
+ double(turn)*double(256*256*256) / double(period);
sxloc = int(double(orbit) * cos(ang * 2.0 * M_PIl / double(65536)));
syloc = int(double(orbit) * sin(ang * 2.0 * M_PIl / double(65536)));
if(destination) {
int prog, trip = arrive_turn - depart_turn;
if(trip < 1) trip = 1;
if(turn >= arrive_turn) prog = trip;
else if(turn <= depart_turn) prog = 0;
else prog = turn - depart_turn;
sxloc *= (trip-prog);
sxloc += destination->SXLoc(arrive_turn) * prog;
sxloc /= trip;
syloc *= (trip-prog);
syloc += destination->SYLoc(arrive_turn) * prog;
syloc /= trip;
}
}
int SObject::SXLoc(int turn) {
if(turn == sturn) return sxloc;
ComputeSLoc(turn);
return sxloc;
}
int SObject::SYLoc(int turn) {
if(turn == sturn) return syloc;
ComputeSLoc(turn);
return syloc;
}
void SObject::ComputeSPos() {
ComputeSLoc(cur_game->turn);
frame = cur_game->frame;
sxpos = (sxloc+10)/20 + 384;
sypos = (syloc+10)/20 + 384;
if(location && location->Represents()) {
int space = (Space() + location->Represents()->Space()) / 2;
if(abs(location->SXPos() - sxpos) < space
&& abs(location->SYPos() - sypos) < space) {
sxpos += space;
sypos -= location->Represents()->Size()/2;
sypos += Size()/2;
}
}
int collide = 1;
while(collide) {
collide = 0;
int prior = (SType() != SOBJECT_PLANET);
for(int ctr=0; ctr < int(system->objects.size()); ++ctr) {
if(system->objects[ctr] == this) { continue; prior = 0; }
int space = (Space() + system->objects[ctr]->Space()) / 2;
if(system->objects[ctr]->frame == cur_game->frame
&& (system->objects[ctr]->SType() == SOBJECT_PLANET || prior)
&& abs(system->objects[ctr]->SXPos() - sxpos) < space
&& abs(system->objects[ctr]->SYPos() - sypos) < space) {
sypos += space;
collide = 1;
break;
}
}
}
}
int SObject::SXPos() {
if(frame == cur_game->frame) return sxpos;
ComputeSPos();
return sxpos;
}
int SObject::SYPos() {
if(frame == cur_game->frame) return sypos;
ComputeSPos();
return sypos;
}
void SObject::ComputeGLoc(int turn) {
gturn = turn;
gxloc = system->GXLoc(turn);
gyloc = system->GYLoc(turn);
if(destination && destination->system != system) {
int prog, trip = arrive_turn - depart_turn;
if(trip < 1) trip = 1;
if(turn >= arrive_turn) prog = trip;
else if(turn <= depart_turn) prog = 0;
else prog = turn - depart_turn;
gxloc *= (trip-prog);
gxloc += destination->GXLoc(arrive_turn) * prog;
gxloc /= trip;
gyloc *= (trip-prog);
gyloc += destination->GYLoc(arrive_turn) * prog;
gyloc /= trip;
}
}
int SObject::GXLoc(int turn) {
if(turn == gturn) return gxloc;
ComputeGLoc(turn);
return gxloc;
}
int SObject::GYLoc(int turn) {
if(turn == gturn) return gyloc;
ComputeGLoc(turn);
return gyloc;
}
void SObject::ComputeGPos() {
ComputeGLoc(cur_game->turn);
frame = cur_game->frame;
gxpos = 34 + (gxloc+10)/20;
gypos = 34 + (gyloc+10)/20;
if(system && system != this) {
int space = (Space() + system->Space()) / 2;
if(abs(system->GXPos() - gxpos) < space
&& abs(system->GYPos() - gypos) < space) {
gxpos += space;
gypos -= system->Size()/2;
gypos += Size()/2;
}
}
int collide = 1;
while(collide) {
collide = 0;
for(int ctr=0; ctr < int(system->objects.size()); ++ctr) {
if(system->objects[ctr] == this) break;
int space = (Space() + system->objects[ctr]->Space()) / 2;
if(system->objects[ctr]->frame == cur_game->frame
&& abs(system->objects[ctr]->gxpos - gxpos) < space
&& abs(system->objects[ctr]->gypos - gypos) < space) {
gypos += space;
collide = 1;
break;
}
}
}
}
int SObject::GXPos() {
if(frame == cur_game->frame) return gxpos;
ComputeGPos();
return gxpos;
}
int SObject::GYPos() {
if(frame == cur_game->frame) return gypos;
ComputeGPos();
return gypos;
}
SObject::~SObject() {
}
void SObject::TakeTurn() {
target = NULL;
if(destination && cur_game->turn >= arrive_turn) Arrive();
}
//FIXME - HAXXOR
void panel_draw(); // From gui.cpp
void SObject::Arrive() {
SObject *truedest = destination->Represents();
if(!truedest) {
system = destination->Sys();
orbit = destination->OrbitDist();
period = destination->Period();
startpos = destination->OrbitPhase();
arrive_turn = destination->ArriveTurn();
depart_turn = destination->DepartTurn();
Trash(location);
location = destination->Location();
Trash(destination);
destination = destination->Destination();
Trash(target);
target = NULL;
distance = 0;
}
else if(truedest->SType() == SOBJECT_PLANET) {
system = destination->system;
orbit = destination->orbit;
period = destination->period;
startpos = destination->startpos;
Trash(location);
location = destination;
truedest->See(Owner());
truedest->Know(Owner());
destination = NULL;
}
else if(truedest->SType() == SOBJECT_SYSTEM) {
orbit = 6600;
period = int(sqrt(double(orbit)*double(orbit)*double(orbit)));
int xp = system->gxpos - destination->system->gxpos;
int yp = system->gypos - destination->system->gypos;
double tang = double(cur_game->turn) * double(256*256*256) / double(period);
double nang = double(65536) * atan2(double(yp), double(xp)) / 2.0 / M_PIl;
startpos = int(nang - tang);
system->FleetLeaves((Fleet*)this);
system = destination->system;
system->FleetArrives((Fleet*)this);
Trash(location);
location = NULL;
truedest->Know(Owner());
Trash(destination);
destination = NULL;
}
else if(truedest->SType() == SOBJECT_FLEET && SType() == SOBJECT_FLEET
&& Owner() == truedest->Owner()) {
Fleet *flt = (Fleet *)this;
for(int shp=0; shp < flt->NumShips(); ++shp)
((Fleet*)truedest)->AddShip(flt->GetShip(shp));
if(cur_object == this) cur_object = truedest;
panel_draw();
RemapPositions(flt, truedest);
flt->RemoveShips(0);
}
else if(truedest->SType() == SOBJECT_FLEET && SType() == SOBJECT_FLEET) {
Fleet *flt = (Fleet*)this;
flt->Attack((Fleet*)truedest);
system = destination->system;
orbit = destination->orbit;
period = destination->period;
startpos = destination->startpos;
Trash(location);
location = destination->location;
Trash(destination);
destination = destination->destination; //FIXME! Two attacking each other!
}
else if(truedest->SType() == SOBJECT_FLEET) {
system = destination->system;
orbit = destination->orbit;
period = destination->period;
startpos = destination->startpos;
Trash(location);
location = destination->location;
Trash(destination);
destination = destination->destination;
}
}
int SObject::Owner() {
return -1;
}
int SObject::TimeToLocal(int sqdist) {
if(SMove() <= 0) return INT_MIN;
int ret = int(sqrt(double(sqdist)));
ret += SMove()-1;
ret /= SMove();
return ret;
}
int SObject::TimeToGalactic(int sqdist) {
if(GMove() <= 0) return INT_MIN;
//FIXME - Use Physics' Hyperspace Factor, etc...
//int ret = int(sqrt(double(sqdist))*double(1000000));
int ret = int(sqrt(double(sqdist)));
ret += GMove()-1;
ret /= GMove();
return ret;
}
void SObject::Engage() {
if(target == NULL) return;
Trash(destination);
destination = target;
depart_turn = cur_game->turn;
arrive_turn = cur_game->turn + distance;
if(arrive_turn == depart_turn) Arrive();
target = NULL;
}
void SObject::SetCourse(SObject *p) {
if(!p) { target = NULL; return; }
target = GetPosition(p);
int tm = 1, offx, offy, sqd;
if(target->system != system) {
offx = abs(system->GXLoc(cur_game->turn)
- target->system->GXLoc(cur_game->turn));
offy = abs(system->GYLoc(cur_game->turn)
- target->system->GYLoc(cur_game->turn));
sqd = offx*offx + offy*offy;
distance = TimeToGalactic(sqd);
return;
}
if(destination && depart_turn < cur_game->turn) {
SetOrigin();
}
for(int trip=0; ; ++trip) {
offx = abs(target->SXLoc(cur_game->turn + trip)
- SXLoc(cur_game->turn));
offy = abs(target->SYLoc(cur_game->turn + trip)
- SYLoc(cur_game->turn));
sqd = offx*offx + offy*offy;
tm = TimeToLocal(sqd);
if(abs(tm) <= trip) {
distance = (tm == 0) ? 0 : (tm/abs(tm))*trip;
break;
}
}
}
void SObject::SetOrigin() {
int xp = (SXLoc(cur_game->turn));
int yp = (SYLoc(cur_game->turn));
orbit = int(sqrt(double(xp*xp+yp*yp)));
period = int(sqrt(double(orbit)*double(orbit)*double(orbit)));
double tang = double(cur_game->turn) * double(256*256*256) / double(period);
double nang = double(65536) * atan2(double(yp), double(xp)) / 2.0 / M_PIl;
startpos = int(nang - tang);
}
int SObject::SeenBy(int n) {
return seen[n];
}
void SObject::See(int n) {
seen[n] = 1;
}
int SObject::KnownTo(int n) {
return known[n];
}
void SObject::Know(int n) {
known[n] = 1;
}
void SaveSObject(FILE *f, SObject *s) {
if(s->SType() == SOBJECT_FLEET) fprintf(f, "Fleet\n");
else if(s->SType() == SOBJECT_PLANET) fprintf(f, "Planet\n");
else if(s->SType() == SOBJECT_POSITION) fprintf(f, "Position\n");
else if(s->SType() == SOBJECT_SYSTEM) fprintf(f, "System\n");
s->SaveTo(f);
}
SObject *LoadSObject(FILE *f) {
char buf[1024] = {0};
fscanf(f, "%s\n", buf);
if(!strcmp(buf, "Fleet")) return new Fleet(f);
if(!strcmp(buf, "Planet")) return new Planet(f);
if(!strcmp(buf, "Position")) return new Position(f);
// if(!strcmp(buf, "System")) return new System(f);
fprintf(stderr, "Load Failed - Bad SObject Type \"%s\"\n", buf);
return NULL;
}
void SObject::SaveTo(FILE *f) {
fprintf(f, "%d %d %d\n", orbit, startpos, period);
fprintf(f, "%d %d %d %d %d\n", frame, sxpos, sypos, gxpos, gypos);
fprintf(f, "%d %d %d\n", sturn, sxloc, syloc);
fprintf(f, "%d %d %d\n", gturn, gxloc, gyloc);
fprintf(f, "%d %d %d\n", depart_turn, arrive_turn, distance);
for(int ctr=0; ctr<max_factions; ++ctr) {
fprintf(f, "%d %d", seen[ctr], known[ctr]);
if(ctr < max_factions-1) fprintf(f, " ");
}
fprintf(f, "\n");
}
void SObject::LoadFrom(FILE *f) {
Init();
fscanf(f, "%d %d %d\n", &orbit, &startpos, &period);
fscanf(f, "%d %d %d %d %d\n", &frame, &sxpos, &sypos, &gxpos, &gypos);
fscanf(f, "%d %d %d\n", &sturn, &sxloc, &syloc);
fscanf(f, "%d %d %d\n", >urn, &gxloc, &gyloc);
fscanf(f, "%d %d %d\n", &depart_turn, &arrive_turn, &distance);
for(int ctr=0; ctr<max_factions; ++ctr) {
fscanf(f, "%d %d ", &(seen[ctr]), &(known[ctr]));
}
system = cur_system;
}