-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposition.cpp
More file actions
128 lines (107 loc) · 2.46 KB
/
position.cpp
File metadata and controls
128 lines (107 loc) · 2.46 KB
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
#include "game.h"
#include "position.h"
Position::Position() : SObject() {
next = NULL;
}
Position::Position(FILE *f) : SObject() {
LoadFrom(f);
}
Position::~Position() {
}
void Position::TakeTurn() {
}
const char *Position::Name() {
if(represents) return represents->Name();
return "Target Location";
};
void Position::AssignTo(SObject *o) {
/*
system = o->Sys();
orbit = o->OrbitDist();
period = o->Period();
startpos = o->OrbitPhase();
location = o->Location();
destination = o->Destination();
arrive_turn = o->ArriveTurn();
depart_turn = o->DepartTurn();
*/
CopyFrom(o);
target = NULL;
distance = 0;
if(o->SType() == SOBJECT_POSITION) represents = ((Position*)o)->Represents();
else represents = o;
}
void Position::ComputeSPos() {
frame = cur_game->frame;
if(represents) {
sxpos = represents->SXPos();
sypos = represents->SYPos();
}
else {
ComputeSLoc(cur_game->turn);
sxpos = (sxloc+10)/20 + 384;
sypos = (syloc+10)/20 + 384;
}
}
void Position::ComputeGPos() {
frame = cur_game->frame;
if(represents) {
gxpos = represents->GXPos();
gypos = represents->GYPos();
}
else {
ComputeGLoc(cur_game->turn);
gxpos = (gxloc+10)/20;
gypos = (gyloc+10)/20;
}
}
#define POS_BLOCKSIZE 1024
static Position *unused_pos = NULL;
static Position *used_pos = NULL;
Position *GetPosition(SObject *s) {
if(!s) return NULL;
Position *ret;
if(!unused_pos) {
int ctr;
unused_pos = new Position[POS_BLOCKSIZE];
for(ctr=0; ctr < (POS_BLOCKSIZE - 1); ++ctr) {
unused_pos[ctr].next = &unused_pos[ctr+1];
}
unused_pos[ctr].next = NULL;
}
ret = unused_pos;
unused_pos = unused_pos->next;
ret->next = used_pos;
used_pos = ret;
ret->AssignTo(s);
return ret;
}
void RecyclePosition(Position *p) {
if(!p) return;
Position **cur = &used_pos;
while(*cur) {
if((*cur) == p) {
*cur = (*cur)->next;
break;
}
cur = &((*cur)->next);
}
p->next = unused_pos;
unused_pos = p;
}
void RemapPositions(SObject *oldo, SObject *newo) {
for(Position *pos = used_pos; pos; pos = pos->next) {
if(pos->represents == oldo) pos->represents = newo;
}
}
void CleanPositions(vector <SObject*> &objs) {
for(int obj = 0; obj < int(objs.size()); ++obj) {
RemapPositions(objs[obj], NULL);
}
}
void Position::SaveTo(FILE *f) {
SObject::SaveTo(f);
}
void Position::LoadFrom(FILE *f) {
SObject::LoadFrom(f);
}