-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.py
245 lines (201 loc) · 6.7 KB
/
Data.py
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
from __future__ import division;
import math;
import random;
import SimObject;
import Graphics;
import Path;
import Passenger;
import Service;
def Time(hr = 0, min = 0, sec = 0):
if hr < 2:
hr += 24;
return hr * 60 + min + sec / 60;
try:
import pygame;
clock = pygame.time.Clock();
except ImportError:
pass;
frameTime = 120;
# the time (in minutes) elapsing every tick (= 1/30s)
timeStep = 0.5 / 30;
# that time in seconds, for physics purposes
deltaT = timeStep * 60;
def UpdateTime():
global frameTime, deltaT;
frameTime += timeStep;
deltaT = timeStep * 60;
while frameTime > 1560:
frameTime -= 1440;
while frameTime < 120:
frameTime += 1440;
clock.tick(30);
def GetLinks(node):
if node in links:
return links[node];
else:
return {};
nodes = {};
places = {};
links = {};
trainCompositions = {};
trains = {};
def AddLink(attrs, begin, end):
link = Link(attrs, begin, end);
if begin in links:
links[begin][end] = link;
else:
links[begin] = {end: link};
if end in links:
links[end][begin] = link;
else:
links[end] = {begin: link};
# find name of node if it exists, else return its number
def NodeName(node):
for place in places:
if places[place] == node:
return place;
return node;
def Place(place):
if isinstance(place, str):
return place;
if "S".join(place) in places: # check if the platform was defined
return "S".join(place);
else:
return place[0];
class Link(SimObject.SimObject):
def __init__(self, attrs, a, b):
SimObject.SimObject.__init__(self, attrs);
# ensure a < b
if a > b:
a, b = b, a;
if a == b:
raise ValueError("Can't make a link from a node to itself!");
self.a = a;
self.b = b;
def Draw(self, screen):
pygame.draw.line(screen, (0, 0, 0), Graphics.GetPos(nodes[self.a].pos), Graphics.GetPos(nodes[self.b].pos), 1);
class Node(SimObject.SimObject):
def __init__(self, attrs, name, pos):
SimObject.SimObject.__init__(self, attrs);
self.name = name;
self.pos = pos;
self.station = None;
def Draw(self, screen):
screenPos = Graphics.GetPos(self.pos);
text = Graphics.font.render(str(self.name), 1, (0, 0, 0));
textpos = text.get_rect().move(screenPos);
screen.blit(text, textpos);
class Train(SimObject.SimObject):
def __init__(self, attrs, composition, serviceNames, service):
SimObject.SimObject.__init__(self, attrs);
self.composition = composition;
self.serviceNames = serviceNames;
self.service = service;
self.order = 0;
# start out at our first service's starting position
self.pos = nodes[places[self.service[self.order][1]]].pos;
self.v = 0;
self.distance = 0;
self.CreateNewPath();
self.passengers = [];
def CreateNewPath(self):
self.path = Path.FindRoute(places[self.service[self.order][1]], places[self.service[self.order + 1][1]]);
def Arrived(self):
# warn if we're too late by at least 1 minute
plannedTime = self.service[self.order + 1][0];
if plannedTime < frameTime - 1:
print(u"{} arrived +{} at {}".format(self.composition, int(frameTime - plannedTime), NodeName(self.path[0])));
# we just arrived, so let passengers get off
currentNode = nodes[self.path[0]];
for passenger in self.passengers:
if passenger.ShouldDisembark(currentNode):
self.passengers.remove(passenger);
# send the passenger to their following train
if len(passenger.route) > 1:
passenger.pos = currentNode.station;
passenger.pos.passengers.append(passenger);
# since we arrived, stop the train
self.v = 0;
self.distance = 0;
self.pos = currentNode.pos; # at the place we arrived
if plannedTime < frameTime:
self.Depart();
def Depart(self):
# if we stopped at a station
currentNode = nodes[self.path[0]];
if currentNode.station != None:
currentStation = currentNode.station;
# take in passengers
for passenger in currentStation.passengers:
if passenger.ShouldEmbark(self):
passenger.pos = None;
passenger.route = passenger.route[1:];
self.passengers.append(passenger);
currentStation.passengers.remove(passenger);
self.order += 1;
self.path = [];
# the acceleration in meters per second^2
def GetAcceleration(self):
lineDistance = Path.Distance(nodes[self.path[0]].pos, nodes[self.path[1]].pos);
# determine our movement
a = 0;
if len(self.path) == 2 and self.v * (self.service[self.order + 1][0] - frameTime) * 60 > (lineDistance - self.distance):
# brake for an upcoming station
if self.v > 10:
a = -2;
else:
# basically imaginary values
if self.v <= 10 / 0.25:
a = 0.25;
else:
a = 10 / self.v;
# roll and wind resistance
a -= 0.005 * self.v;
return a;
def Update(self):
try:
if self.path == []:
self.CreateNewPath();
elif self.path == False: # no way to continue
return;
elif len(self.path) == 1: # we arrived at the destination
self.Arrived();
else:
a = self.GetAcceleration();
# move the train
self.distance += self.v * deltaT;
self.v += a * deltaT;
if self.v <= 0:
self.v = 0;
self.distance += 0.5 * a * deltaT ** 2;
lineDistance = Path.Distance(nodes[self.path[0]].pos, nodes[self.path[1]].pos);
if lineDistance <= 0 or self.distance > lineDistance:
del self.path[0]; # go to the next node in our path
self.distance -= lineDistance;
# recalculate path distance
if len(self.path) > 1:
lineDistance = Path.Distance(nodes[self.path[0]].pos, nodes[self.path[1]].pos);
else:
lineDistance = 0;
# make sure we don't go off the end of the route
if lineDistance > 0:
self.pos = Path.Move(nodes[self.path[1]].pos, nodes[self.path[0]].pos, self.distance / lineDistance);
else:
self.pos = nodes[self.path[0]].pos;
except:
print(self.serviceNames, self.path, self.service, self.order);
raise;
def Draw(self, screen):
screenPos = Graphics.GetPos(self.pos);
pygame.draw.circle(screen, (min(255, len(self.passengers)), max(0, 255 - len(self.passengers)), 0), screenPos, 2, 0);
if Graphics.scale > 1024:
if self.service[self.order + 1][0] < frameTime - 1:
text = Graphics.font.render("{} +{} (p: {})".format(self.composition, int(frameTime - self.service[self.order + 1][0]), len(self.passengers)), 1, (0, 0, 0));
else:
text = Graphics.font.render("{} (p: {})".format(self.composition, len(self.passengers)), 1, (0, 0, 0));
textpos = text.get_rect().move((screenPos[0], screenPos[1]));
pygame.draw.rect(screen, (255, 255, 255), (screenPos[0], screenPos[1], 100, 12));
pygame.draw.rect(screen, (0, 0, 0), (screenPos[0], screenPos[1], 100, 12), 1);
screen.blit(text, textpos);
def __str__(self):
return "Data.Train, composition={0}, serviceNames={1}, service={2}".format(str(self.composition), str(self.serviceNames), str(self.service));