-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.cpp
More file actions
116 lines (101 loc) · 2.82 KB
/
system.cpp
File metadata and controls
116 lines (101 loc) · 2.82 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
#include <cstdio>
#include <cstdlib>
#include "math.h"
#include "dict.h"
#include "game.h"
#include "system.h"
extern int max_factions;
System::System(int xp, int yp, int nump, int minl, int atmosl, int pl)
: SObject(this) {
name = dict[rand()%dict_size];
for(int ctr=0; ctr<nump; ++ctr) {
Planet *plan;
if(pl >= 0 && ctr == 2) {
plan = new Planet(this, ctr, (rand()%10000)+1, 30, 30);
objects.push_back(plan);
plan->colonies.push_back(new Colony(pl, plan, 1));
}
else {
int at=0, mi=0;
if(atmosl == 3) at = int(double(sqrt(double(rand()%901))));
else if(atmosl == 2) at = rand()%30;
else if(atmosl == 1) at = (rand()%30)*(rand()%30)/30;
if(minl == 3) mi = int(double(sqrt(double(rand()%901))));
else if(minl == 2) mi = rand()%30;
else if(minl == 1) mi = (rand()%30)*(rand()%30)/30;
plan = new Planet(this, ctr, (rand()%10000)+1, mi, at);
objects.push_back(plan);
}
}
if(pl >= 0) Know(pl);
gxloc = xp;
gyloc = yp;
gxpos = (xp+10)/20;
gypos = (yp+10)/20;
}
System::~System() {
for(int ctr=0; ctr<int(objects.size()); ++ctr) delete objects[ctr];
objects.clear();
}
void System::TakeTurn() {
SObject::TakeTurn();
for(int ctr=0; ctr<int(objects.size()); ++ctr) {
SObject *tmp = objects[ctr];
objects[ctr]->TakeTurn();
if(tmp != objects[ctr]) --ctr;
}
}
int System::Owner() {
for(int ctr=0; ctr<int(objects.size()); ++ctr) {
if(objects[ctr]->SType() == SOBJECT_PLANET
&& ((Planet*)objects[ctr])->colonies.size() > 0)
return ((Planet*)objects[ctr])->colonies[0]->Owner();
}
return -1;
}
void System::FleetLeaves(Fleet *f) {
vector<SObject*>::iterator cur = objects.begin();
while(cur < objects.end()) {
if(*cur == f) {
cur = objects.erase(cur);
continue;
}
++cur;
}
}
void System::FleetArrives(Fleet *f) {
vector<SObject*>::iterator cur = objects.begin();
while(cur < objects.end()) {
if(*cur == f) return;
++cur;
}
objects.push_back(f);
Know(f->Owner());
}
void System::Know(int n) {
SObject::Know(n);
for(int ctr=0; ctr<int(objects.size()); ++ctr) {
objects[ctr]->See(n);
}
}
System::System(FILE *f) : SObject() {
LoadFrom(f);
}
void System::SaveTo(FILE *f) {
SObject::SaveTo(f);
fprintf(f, "%s\n", name.c_str());
fprintf(f, "Objects: %d\n", int(objects.size()));
for(int ctr=0; ctr<int(objects.size()); ++ctr) SaveSObject(f, objects[ctr]);
}
void System::LoadFrom(FILE *f) {
for(int ctr=0; ctr<int(objects.size()); ++ctr) delete objects[ctr];
objects.clear();
cur_system = this;
SObject::LoadFrom(f);
int tmpsz;
system = this;
char buf[1024] = {0};
fscanf(f, "%[^\n]\n", buf); name = buf;
fscanf(f, "Objects: %d\n", &tmpsz);
for(int ctr=0; ctr<tmpsz; ++ctr) objects.push_back(LoadSObject(f));
}