-
Notifications
You must be signed in to change notification settings - Fork 4
/
Vessel.pde
269 lines (228 loc) · 6.42 KB
/
Vessel.pde
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
/**
* @class Vessel
* @desc Transport vessel/vehicle
*/
class Vessel
{
Route route;
private int destIndex = 0;
boolean terminated = false;
float _x;
float _y;
float linkLength;
float routeLengthPx;
private float speed;
Point destCoords;
Point originCoords;
Stop dest;
Stop origin;
String[] stops;
Trip trip;
int journeyNum = 1;
int nullStops = 0;
float xDist;
float yDist;
float edgeLength;
public Vessel(Route route) {
this.route = route;
this.stops = this.route.getStops().clone();
//debug("created vessel on " + route.name + " route");
this._x = 0;
this._y = 0;
this.setSpeed(BUS_SPEED);
}
public Vessel(Trip trip) {
this.route = trip.getRoute();
this.trip = trip;
this.stops = this.route.getStops().clone();
//debug("created vessel on " + route.name + " route");
this._x = 0;
this._y = 0;
this.routeLengthPx = this.route.getRouteLengthPixels();
Float f = new Float((this.routeLengthPx/trip.getDuration())*net.getTripSpeed());
this.setSpeed(f);
debug("speed is "+ this.speed);
}
private void placeAtOrigin()
{
this.refreshDestination();
if(this.terminated) return;
if(this.dest == null) {
this.nullStops++;
this.setNextDest();
if(!this.terminated) {
this.placeAtOrigin();
} else {
debug("NO ORIGIN FOR ROUTE " + this.route.name);
}
return;
}
this.originCoords = dest.getCoords();
this.origin = dest;
this._x = this.originCoords.x;
this._y = this.originCoords.y;
//debug(this.dest.getId() + " origin");
this.drawme();
}
public boolean atDepot()
{
return (this._x == 0);
}
public void update() {
if(this.atDepot() && !this.terminated) this.placeAtOrigin();
if(this.origin == null) return;
if(dest == null) {
this.setNextDest();
this.nullStops++;
//debug(destID+" doesnt exist\n");
return;
}
this.moveToDest();
if(this.atDest()) {
this.setNextDest();
//this.printTrajectory();
}
this.drawme();
}
private void setSpeed(float speed) {
this.speed = speed;
}
private void printTrajectory()
{
if(!(dest == null)) {
print("drawing vessel on " + this.route.name +" with dest " + dest.getId() + " " + destCoords.x + "," + destCoords.y + "\n");
//dest.printme();
//this.route.printme();
}
}
private void moveToDest()
{
float xdiff,ydiff;
if(net.isInTripsMode()) {
this.edgeLength = (this.linkLength > 0) ? this.linkLength : this.routeLengthPx;
xdiff = this.xDist * this.speed/edgeLength;
ydiff = this.yDist * this.speed/edgeLength;
}
else if(cp.dynamics == ControlPanel.SWARM) {
xdiff = (this.destCoords.x - this._x) * this.speed;
ydiff = (this.destCoords.y - this._y) * this.speed;
} else {
if(!this.beyondMidPoint()) { //accelerate to midpoint
xdiff = 0.05 + this.distanceFromOrigin() * this.speed/80;
ydiff = 0.05 + this.distanceFromOrigin() * this.speed/80;
} else { //decelerate to dest
xdiff = (this.destCoords.x - this._x) * this.speed;
ydiff = (this.destCoords.y - this._y) * this.speed;
}
}
if(!net.isInTripsMode()) {
if(abs(xdiff) > MAX_SPEED) xdiff *= MAX_SPEED/abs(xdiff);
if(abs(ydiff) > MAX_SPEED) ydiff *= MAX_SPEED/abs(ydiff);
}
this._x += xdiff;
this._y += ydiff;
}
private boolean beyondMidPoint()
{
return this.distanceFromOrigin() > (this.linkLength/2);
}
private float distanceFromDest()
{
return sqrt(pow(abs(this.dest._x - this._x),2) + pow(abs(this.dest._y - this._y),2));
}
private float distanceFromOrigin()
{
if(this.origin != null) {
return sqrt(pow(abs(this._x - this.origin._x),2) + pow(abs(this._y - this.origin._y),2));
} else {
return 0;
}
}
private void refreshDestination()
{
String destID = this.getStops()[destIndex];
Stop destStop = (Stop) net.aStop.get(ltrim(destID));
if(destStop != null) {
this.setDestination(destStop);
}
}
private Stop getDestination()
{
return this.dest;
}
public void setDestinationIndex(int index)
{
this.destIndex = index;
}
private void setDestination(Stop dest)
{
this.dest = dest;
this.destCoords = dest.getCoords();
}
private void calcLinkLength()
{
if(this.origin != null) {
this.linkLength = sqrt(pow(abs(this.dest._x - this.origin._x),2) + pow(abs(this.dest._y - this.origin._y),2));
}
}
public void setNextDest()
{
if(destIndex < this.getStops().length-1) {
Stop orig = this.getDestination();
if(orig != null) this.origin = orig;
this.originCoords = this.origin.getCoords();
destIndex++;
this.refreshDestination();
this.calcLinkLength();
if(net.isInTripsMode()) {
this.xDist = this.destCoords.x - this.originCoords.x;
this.yDist = this.destCoords.y - this.originCoords.y;
//debug("linkdist "+ this.linkLength + " " + this.routeLengthPx);
}
if(cp.dynamics == ControlPanel.BUS) {
this.calcLinkLength();
}
} else {
terminated = true;
if((this.getStops().length - this.nullStops) > 4) {
if(this.journeyNum < 3 && net.isIncrementalGrowth()) {
net.createVessel(this.route);
}
}
if(net.peaked && net.isIncrementalGrowth()) {
net.removeVessel();
net.removeVessel();
}
this.journeyNum++;
destIndex = 0;
this.reverseStops();
this.route.nullStops = this.nullStops;
this.nullStops = 0;
//this.refreshDestination();
}
}
private void reverseStops()
{
String[] cstops = this.stops.clone();
List<String> sttops = Arrays.asList(cstops);
Collections.reverse(sttops);
this.stops = (String[]) sttops.toArray();
}
private String[] getStops()
{
return this.stops;
}
public void drawme()
{
int alphax = trails ? 7 : 30;
fill(#FFFFFF,alphax);
stroke(#FFFFFF,alphax);
if(net.isInTripsMode() && this.journeyNum > 1) {
return;//no render
}
ellipse(this._x + net.getXOffset(),this._y - net.getYOffset(),1,1);
}
private boolean atDest(){
return (abs(this.destCoords.x - this._x) < 1 && abs(this.destCoords.y - this._y) < 1);
}
}