1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2007 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Fabian 'x3n' Landau |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | #include "mover_station_list.h" |
---|
17 | #include "debug.h" |
---|
18 | |
---|
19 | |
---|
20 | MoverStationList::MoverStationList() |
---|
21 | { |
---|
22 | this->first = 0; |
---|
23 | this->last = 0; |
---|
24 | this->goForward = true; |
---|
25 | } |
---|
26 | |
---|
27 | MoverStationList::~MoverStationList() |
---|
28 | { |
---|
29 | MoverStationListElement *temp1 = this->first; |
---|
30 | MoverStationListElement *temp2; |
---|
31 | while (temp1 != 0) |
---|
32 | { |
---|
33 | temp2 = temp1->next; |
---|
34 | delete temp1; |
---|
35 | temp1 = temp2; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | void MoverStationList::addStation(MoverStation *station) |
---|
40 | { |
---|
41 | PRINTF(0)("Füge MoverStation %p hinzu\n", station); |
---|
42 | if (this->first == 0) |
---|
43 | { |
---|
44 | this->first = new MoverStationListElement(station); |
---|
45 | this->last = this->first; |
---|
46 | return; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | if (this->first->station->rank > station->rank) |
---|
51 | { |
---|
52 | MoverStationListElement *temp = this->first; |
---|
53 | this->first = new MoverStationListElement(station); |
---|
54 | this->first->next = temp; |
---|
55 | this->first->next->prev = this->first; |
---|
56 | return; |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | MoverStationListElement *temp1 = this->first; |
---|
61 | MoverStationListElement *temp2; |
---|
62 | while (temp1->next != 0) |
---|
63 | { |
---|
64 | if (temp1->next->station->rank > station->rank) |
---|
65 | { |
---|
66 | temp2 = temp1->next; |
---|
67 | temp1->next = new MoverStationListElement(station); |
---|
68 | temp1->next->prev = temp1; |
---|
69 | temp1->next->next = temp2; |
---|
70 | temp1->next->next->prev = temp1->next; |
---|
71 | |
---|
72 | return; |
---|
73 | } |
---|
74 | |
---|
75 | temp1 = temp1->next; |
---|
76 | } |
---|
77 | |
---|
78 | temp1->next = new MoverStationListElement(station); |
---|
79 | temp1->next->prev = last; |
---|
80 | last = temp1->next; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | MoverStation *MoverStationList::getNextStation(MoverStation *station) |
---|
86 | { |
---|
87 | if (this->goForward) |
---|
88 | { |
---|
89 | if (station) |
---|
90 | { |
---|
91 | MoverStationListElement *temp; |
---|
92 | if (station->bJumpToRank) |
---|
93 | { |
---|
94 | temp = this->first; |
---|
95 | while (temp != 0) |
---|
96 | { |
---|
97 | if (temp->station->rank == station->jumpToRank) |
---|
98 | return temp->station; |
---|
99 | |
---|
100 | temp = temp->next; |
---|
101 | } |
---|
102 | } // no "else" here - if jumpToRank wasn't found, we return the following station (as usual) |
---|
103 | |
---|
104 | if (station->bMoveToRank) |
---|
105 | { |
---|
106 | temp = this->first; |
---|
107 | while (temp != 0) |
---|
108 | { |
---|
109 | if (temp->station->rank == station->moveToRank) |
---|
110 | return temp->station; |
---|
111 | |
---|
112 | temp = temp->next; |
---|
113 | } |
---|
114 | } // no "else" here - if moveToRank wasn't found, we return the following station (as usual) |
---|
115 | |
---|
116 | temp = this->first; |
---|
117 | while (temp != 0) |
---|
118 | { |
---|
119 | if (temp->station == station) |
---|
120 | { |
---|
121 | if (temp->next) |
---|
122 | { |
---|
123 | return temp->next->station; |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | this->goForward = false; |
---|
128 | return this->last->station; |
---|
129 | } |
---|
130 | } |
---|
131 | temp = temp->next; |
---|
132 | } |
---|
133 | return 0; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | if (this->first) |
---|
138 | return this->first->station; |
---|
139 | else |
---|
140 | return 0; |
---|
141 | } |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | if (station) |
---|
146 | { |
---|
147 | MoverStationListElement *temp; |
---|
148 | if (station->bJumpToRank) |
---|
149 | { |
---|
150 | temp = this->last; |
---|
151 | while (temp != 0) |
---|
152 | { |
---|
153 | if (temp->station->rank == station->jumpToRank) |
---|
154 | return temp->station; |
---|
155 | |
---|
156 | temp = temp->prev; |
---|
157 | } |
---|
158 | } // no "else" here - if jumpToRank wasn't found, we return the following station (as usual) |
---|
159 | |
---|
160 | if (station->bMoveToRank) |
---|
161 | { |
---|
162 | temp = this->last; |
---|
163 | while (temp != 0) |
---|
164 | { |
---|
165 | if (temp->station->rank == station->moveToRank) |
---|
166 | return temp->station; |
---|
167 | |
---|
168 | temp = temp->prev; |
---|
169 | } |
---|
170 | } // no "else" here - if moveToRank wasn't found, we return the following station (as usual) |
---|
171 | |
---|
172 | temp = this->last; |
---|
173 | while (temp != 0) |
---|
174 | { |
---|
175 | if (temp->station == station) |
---|
176 | { |
---|
177 | if (temp->prev) |
---|
178 | { |
---|
179 | return temp->prev->station; |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | this->goForward = true; |
---|
184 | return this->first->station; |
---|
185 | } |
---|
186 | } |
---|
187 | temp = temp->prev; |
---|
188 | } |
---|
189 | return 0; |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | if (this->last) |
---|
194 | return this->last->station; |
---|
195 | else |
---|
196 | return 0; |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | Vector MoverStationList::getTotalRelCoor(MoverStation *station) |
---|
202 | { |
---|
203 | MoverStationListElement *temp = this->first; |
---|
204 | Vector totalRelCoor = Vector(0, 0, 0); |
---|
205 | |
---|
206 | if (station) |
---|
207 | { |
---|
208 | if (this->goForward) |
---|
209 | { |
---|
210 | while (temp != 0) |
---|
211 | { |
---|
212 | totalRelCoor += temp->station->relTargetCoor; |
---|
213 | if (temp->station == station) |
---|
214 | break; |
---|
215 | |
---|
216 | temp = temp->next; |
---|
217 | } |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | if (temp) |
---|
222 | { |
---|
223 | while (temp->next != 0) |
---|
224 | { |
---|
225 | totalRelCoor += temp->next->station->relTargetCoor; |
---|
226 | if (temp->next->station == station) |
---|
227 | break; |
---|
228 | |
---|
229 | temp = temp->next; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | return totalRelCoor; |
---|
236 | } |
---|
237 | |
---|
238 | Vector MoverStationList::getTotalRelDir(MoverStation *station) |
---|
239 | { |
---|
240 | MoverStationListElement *temp = this->first; |
---|
241 | Vector totalRelDir = Vector(0, 0, 0); |
---|
242 | |
---|
243 | if (station) |
---|
244 | { |
---|
245 | if (this->goForward) |
---|
246 | { |
---|
247 | while (temp != 0) |
---|
248 | { |
---|
249 | totalRelDir += temp->station->relTargetDir; |
---|
250 | if (temp->station == station) |
---|
251 | break; |
---|
252 | |
---|
253 | temp = temp->next; |
---|
254 | } |
---|
255 | } |
---|
256 | else |
---|
257 | { |
---|
258 | if (temp) |
---|
259 | { |
---|
260 | while (temp->next != 0) |
---|
261 | { |
---|
262 | totalRelDir += temp->next->station->relTargetDir; |
---|
263 | if (temp->next->station == station) |
---|
264 | break; |
---|
265 | |
---|
266 | temp = temp->next; |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | return totalRelDir; |
---|
273 | } |
---|
274 | |
---|
275 | bool MoverStationList::isOpen(MoverStation *station) |
---|
276 | { |
---|
277 | if (station) |
---|
278 | if ((this->goForward && this->last && this->last->station == station) || station->bIsOpen) |
---|
279 | return true; |
---|
280 | |
---|
281 | return false; |
---|
282 | } |
---|
283 | |
---|
284 | bool MoverStationList::isClosed(MoverStation *station) |
---|
285 | { |
---|
286 | if (station) |
---|
287 | if ((!this->goForward && this->first && this->first->station == station) || station->bIsClosed) |
---|
288 | return true; |
---|
289 | |
---|
290 | return false; |
---|
291 | } |
---|
292 | |
---|
293 | bool MoverStationList::changeDirection(bool bReopen, bool bReclose, bool bIsTriggered) |
---|
294 | { |
---|
295 | if (this->goForward && bReclose && !bIsTriggered) |
---|
296 | { |
---|
297 | this->goForward = false; |
---|
298 | return true; |
---|
299 | } |
---|
300 | |
---|
301 | if (!this->goForward && bReopen && bIsTriggered) |
---|
302 | { |
---|
303 | this->goForward = true; |
---|
304 | return true; |
---|
305 | } |
---|
306 | |
---|
307 | return false; |
---|
308 | } |
---|
309 | |
---|
310 | Vector MoverStationList::getRelTargetCoor(MoverStation *station) |
---|
311 | { |
---|
312 | if (station) |
---|
313 | { |
---|
314 | if (!station->bMoveToRank) |
---|
315 | { |
---|
316 | if (this->goForward) |
---|
317 | return station->relTargetCoor; |
---|
318 | else |
---|
319 | return station->relTargetCoor * -1; |
---|
320 | } |
---|
321 | else |
---|
322 | { |
---|
323 | MoverStation *moveToStation = this->getStation(station->moveToRank); |
---|
324 | |
---|
325 | if (moveToStation != 0) |
---|
326 | { |
---|
327 | if (this->goForward) |
---|
328 | return (this->getTotalRelCoor(station) - this->getTotalRelCoor(moveToStation)) * -1; |
---|
329 | else |
---|
330 | return (this->getTotalRelCoor(station) - this->getTotalRelCoor(moveToStation)); |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | return Vector(0, 0, 0); |
---|
336 | } |
---|
337 | |
---|
338 | Vector MoverStationList::getRelTargetDir(MoverStation *station) |
---|
339 | { |
---|
340 | if (station) |
---|
341 | { |
---|
342 | if (!station->bMoveToRank) |
---|
343 | { |
---|
344 | if (this->goForward) |
---|
345 | return station->relTargetDir; |
---|
346 | else |
---|
347 | return station->relTargetDir * -1; |
---|
348 | } |
---|
349 | else |
---|
350 | { |
---|
351 | MoverStation *moveToStation = this->getStation(station->moveToRank); |
---|
352 | |
---|
353 | if (moveToStation != 0) |
---|
354 | { |
---|
355 | if (this->goForward) |
---|
356 | return (station->relTargetDir - this->getTotalRelDir(moveToStation)) * -1; |
---|
357 | else |
---|
358 | return station->relTargetDir - this->getTotalRelDir(moveToStation); |
---|
359 | } |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | return Vector(0, 0, 0); |
---|
364 | } |
---|
365 | |
---|
366 | Vector MoverStationList::getVelocity(MoverStation *station) |
---|
367 | { |
---|
368 | if (station) |
---|
369 | if (station->movingTime != 0) |
---|
370 | return this->getRelTargetCoor(station) / station->movingTime; |
---|
371 | |
---|
372 | return Vector(0, 0, 0); |
---|
373 | } |
---|
374 | |
---|
375 | Vector MoverStationList::getRotation(MoverStation *station) |
---|
376 | { |
---|
377 | if (station) |
---|
378 | if (station->movingTime != 0) |
---|
379 | return this->getRelTargetDir(station) / station->movingTime; |
---|
380 | |
---|
381 | return Vector(0, 0, 0); |
---|
382 | } |
---|
383 | |
---|
384 | OrxSound::SoundBuffer MoverStationList::getStartingSound(MoverStation *station) |
---|
385 | { |
---|
386 | if (station) |
---|
387 | { |
---|
388 | if (this->goForward) |
---|
389 | return station->soundBuffer_opening; |
---|
390 | else |
---|
391 | return station->soundBuffer_closing; |
---|
392 | } |
---|
393 | |
---|
394 | return OrxSound::ResourceSoundBuffer(""); |
---|
395 | } |
---|
396 | |
---|
397 | OrxSound::SoundBuffer MoverStationList::getEndingSound(MoverStation *station) |
---|
398 | { |
---|
399 | if (station) |
---|
400 | { |
---|
401 | if (this->goForward) |
---|
402 | return station->soundBuffer_opened; |
---|
403 | else |
---|
404 | return station->soundBuffer_closed; |
---|
405 | } |
---|
406 | |
---|
407 | return OrxSound::ResourceSoundBuffer(""); |
---|
408 | } |
---|
409 | |
---|
410 | OrxSound::SoundBuffer MoverStationList::getMovingSound(MoverStation *station) |
---|
411 | { |
---|
412 | if (station) |
---|
413 | return station->soundBuffer_moving; |
---|
414 | |
---|
415 | return OrxSound::ResourceSoundBuffer(""); |
---|
416 | } |
---|
417 | |
---|
418 | MoverStation *MoverStationList::getStation(int rank) |
---|
419 | { |
---|
420 | MoverStationListElement *temp; |
---|
421 | |
---|
422 | temp = this->first; |
---|
423 | while (temp != 0) |
---|
424 | { |
---|
425 | if (temp->station->rank == rank) |
---|
426 | return temp->station; |
---|
427 | |
---|
428 | temp = temp->next; |
---|
429 | } |
---|
430 | |
---|
431 | return 0; |
---|
432 | } |
---|
433 | |
---|
434 | //////////////////////////////////////////////////////// |
---|
435 | |
---|
436 | |
---|
437 | MoverStationListElement::MoverStationListElement(MoverStation *station) |
---|
438 | { |
---|
439 | this->station = station; |
---|
440 | this->next = 0; |
---|
441 | this->prev = 0; |
---|
442 | } |
---|
443 | |
---|
444 | MoverStationListElement::~MoverStationListElement() |
---|
445 | { |
---|
446 | delete this->station; |
---|
447 | } |
---|