-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.js
210 lines (196 loc) · 6.4 KB
/
system.js
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
// Construct an object representing a star system
function System (universe) {
this.universe = universe;
this.planets = [];
this.planets.push(
{
name: randomLargeBodyName(),
xCoord: MAP_WIDTH/2,
yCoord: MAP_HEIGHT/2,
radius: randomNumber(4,6),
class: randomOption(LARGE_BODIES),
mass: randomNumber(1,4)*100,
events: null
});
this.name = this.planets[0].name;
if (this.name.substring(0,3) == "NGC")
this.planet_naming_style = PLANET_NAMING_STYLE_SCIENTIFIC;
else
this.planet_naming_style = [PLANET_NAMING_STYLE_COMMON, PLANET_NAMING_STYLE_SCIENTIFIC, PLANET_NAMING_STYLE_SYSTEM_DERIVED].random();
this.ships = [];
this.map = [];
this.addRandomPlanets();
this.generateMap();
this.addRandomAnomalies();
this.khanFleet = false;
let encounters = [
{
prob: 10,
opt: SHIP_FLAG_KHAN
},
{
prob: 20,
opt: SHIP_FLAG_MERCHANT
},
{
prob: 40,
opt: SHIP_FLAG_PIRATE
},
{
prob: 30,
opt: SHIP_FLAG_UNKNOWN
}
];
switch (randomOption(encounters)) {
case SHIP_FLAG_UNKNOWN:
break;
case SHIP_FLAG_MERCHANT:
for (var count = 0; count < randomNumber(1, 3); count++) {
let s = new Ship(this.randomUnoccupiedSpace(), [1,-2], randomOption([{prob: 50, opt: SHIP_TYPE_SLOOP}, {prob: 50, opt: SHIP_TYPE_FRIGATE}]), SHIP_FLAG_MERCHANT);
this.ships.push(s);
}
break;
case SHIP_FLAG_PIRATE:
for (var count = 0; count < randomNumber(1, 2); count++) {
let s = new Ship(this.randomUnoccupiedSpace(), [1,-2], SHIP_TYPE_FRIGATE, SHIP_FLAG_PIRATE);
this.ships.push(s);
}
for (var count = 0; count < randomNumber(0, 2); count++) {
let s = new Ship(this.randomUnoccupiedSpace(), [1,-2], SHIP_TYPE_SLOOP, SHIP_FLAG_PIRATE);
this.ships.push(s);
}
if (percentChance(25))
this.ships.push(new Ship(this.randomUnoccupiedSpace(), [1,-2], SHIP_TYPE_TRANSPORT, SHIP_FLAG_PIRATE));
break;
case SHIP_FLAG_KHAN:
for (var count = 0; count < randomNumber(1, 2); count++) {
let s = new Ship(this.randomUnoccupiedSpace(), [1,-2], SHIP_TYPE_FRIGATE, SHIP_FLAG_KHAN);
this.ships.push(s);
}
for (var count = 0; count < randomNumber(0, 3); count++) {
let s = new Ship(this.randomUnoccupiedSpace(), [1,-2], SHIP_TYPE_SLOOP, SHIP_FLAG_KHAN);
this.ships.push(s);
}
break;
}
var n_stations = randomNumber(0, 1);
for (var count = 0; count < n_stations; count++) {
let s = new Ship(this.randomUnoccupiedSpace(), [0,0], SHIP_TYPE_STATION, SHIP_FLAG_MERCHANT);
if (percentChance(20)) {
s.event = new SpaceStationEvent();
} else {
s.event = new ShopEvent();
}
this.ships.push(s);
}
this.waypoints = [];
for (let n = 0; n < 3; n++) {
let rus = this.randomUnoccupiedSpace();
let asfn = new ROT.Path.AStar(rus[0], rus[1], (x,y) => { //change these positions
return !_.get(system.map, [x, y, 'forbiddenToAI'], true);
});
this.waypoints.push(asfn);
}
this.pending_events = [];
this.bgm = bgms.random();
}
System.prototype = {
clearTile: function (x, y) {
this.map[x][y] = {
terrain: randomOption([{ prob: 80, opt: TERRAIN_NONE_EMPTY}, {prob: 15, opt: TERRAIN_NONE_DIM_STAR}, {prob: 5, opt: TERRAIN_NONE_BRIGHT_STAR}]),
body: null,
forbiddenToAI: false
}
},
generateMap: function()
{
for(var i = 0; i < MAP_WIDTH; i++) {
this.map[i] = [];
for(var j = 0; j < MAP_HEIGHT; j++) {
this.clearTile(i, j);
}
}
this.planets.forEach((p) => {
if(p.class == BODY_QUASAR) {
p.radius = 3; //smaller than other stellar bodies
}
if (p.radius == 0) {
this.map[p.xCoord][p.yCoord].body = p;
this.map[p.xCoord][p.yCoord].terrain = randomOption(TERRAINS[p.class]);
this.map[p.xCoord][p.yCoord].forbiddenToAI = true;
} else {
if (_.has(JETS, p.class)) {
drawJet(p.xCoord, p.yCoord, p.radius+1, (x, y) => {
this.map[x][y].terrain = randomOption(JETS[p.class]);
this.map[x][y].forbiddenToAI = true;
});
}
if (_.has(CORONAS, p.class)) {
drawSquareBody(p.xCoord, p.yCoord, p.radius+1, (x, y) => {
this.map[x][y].terrain = randomOption(CORONAS[p.class]);
this.map[x][y].forbiddenToAI = true;
});
}
drawPseudoSphericalBody(p.xCoord, p.yCoord, p.radius, (x, y) => {
this.map[x][y].body = p;
this.map[x][y].terrain = randomOption(TERRAINS[p.class]);
this.map[x][y].forbiddenToAI = true;
});
}
});
},
removeShip: function(s) {
let index = this.ships.indexOf(s);
if (index > -1) {
this.ships.splice(index, 1);
}
},
randomUnoccupiedSpace: function() {
var collision = true;
while (collision) {
x = randomNumber(2,MAP_WIDTH-2);
y = randomNumber(2,MAP_HEIGHT-2);
collision = false;
this.ships.forEach( (s) => {
if (s.xCoord == x && s.yCoord == y)
collision = true;
});
if (this.map[x][y].body != null)
collision = true;
}
return([x,y]);
},
addRandomAnomalies: function() {
var n_anomalies = randomNumber(0, 2);
for (i = 0; i < n_anomalies; i++) {
let space = this.randomUnoccupiedSpace();
let anomaly = new Planet(space[0], space[1], BODY_ANOMALY, 0, this);
this.planets.push(anomaly);
this.map[anomaly.xCoord][anomaly.yCoord].body = anomaly;
this.map[anomaly.xCoord][anomaly.yCoord].terrain = TERRAIN_ANOMALY;
}
},
addRandomPlanets: function() {
var n_planets = randomNumber(0, 3);
for (i = 0; i < n_planets; i++) {
let r = randomNumber(2,3);
var collision = true;
for (var attempts = 0; attempts < 1000; attempts++) {
x = randomNumber(8,MAP_WIDTH-8);
y = randomNumber(8,MAP_HEIGHT-8);
collision = false;
for (var count = 0; count < this.planets.length; count++) {
if (Math.abs(this.planets[count].xCoord - x) < this.planets[count].radius + r + 1)
collision = true;
if (Math.abs(this.planets[count].yCoord - y) < this.planets[count].radius + r + 1)
collision = true;
}
if (!collision)
break;
}
if (!collision) {
this.planets.push(new Planet(x, y, randomOption(SMALL_BODIES), r, this));
}
}
}
}