Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/pps/FabianLandau/mover/environments/mover_station_list.cc @ 4844

Last change on this file since 4844 was 4844, checked in by landauf, 17 years ago
File size: 11.8 KB
Line 
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
20MoverStationList::MoverStationList()
21{
22    this->first = 0;
23    this->last = 0;
24    this->goForward = true;
25}
26
27MoverStationList::~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
39void 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
85MoverStation *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
201Vector 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
238Quaternion MoverStationList::getTotalRelDir(MoverStation *station)
239{
240    MoverStationListElement *temp = this->first;
241    Quaternion totalRelDir = Quaternion(0, 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
275bool 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
284bool 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
293bool 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
310Vector 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
338Quaternion 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.inverse();
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)).inverse();
357                else
358                    return (station->relTargetDir / this->getTotalRelDir(moveToStation));
359            }
360        }
361    }
362   
363    return Quaternion(0, Vector(0, 0, 0));
364}
365
366Vector MoverStationList::getVelocity(MoverStation *station)
367{
368    Vector velocity = Vector(0, 0, 0);
369
370    if (station)
371        if (station->movingTime != 0)
372            velocity = this->getRelTargetCoor(station) / station->movingTime;
373
374    return velocity;
375}
376
377Quaternion MoverStationList::getRotation(MoverStation *station)
378{
379    Quaternion rotation = Quaternion(0, Vector(0, 0, 0));
380
381    if (station)
382        if (station->movingTime != 0)
383            rotation = Quaternion(this->getRelTargetDir(station).w / station->movingTime, this->getRelTargetDir(station).v);
384
385    return rotation;
386}
387
388OrxSound::SoundBuffer MoverStationList::getStartingSound(MoverStation *station)
389{
390    if (station)
391    {
392        if (this->goForward)
393            return station->soundBuffer_opening;
394        else
395            return station->soundBuffer_closing;
396    }
397
398    return OrxSound::ResourceSoundBuffer("");
399}
400
401OrxSound::SoundBuffer MoverStationList::getEndingSound(MoverStation *station)
402{
403    if (station)
404    {
405        if (this->goForward)
406            return station->soundBuffer_opened;
407        else
408            return station->soundBuffer_closed;
409    }
410
411    return OrxSound::ResourceSoundBuffer("");
412}
413
414OrxSound::SoundBuffer MoverStationList::getMovingSound(MoverStation *station)
415{
416    if (station)
417        return station->soundBuffer_moving;
418
419    return OrxSound::ResourceSoundBuffer("");
420}
421
422MoverStation *MoverStationList::getStation(int rank)
423{
424    MoverStationListElement *temp;
425
426    temp = this->first;
427    while (temp != 0)
428    {
429        if (temp->station->rank == rank)
430            return temp->station;
431
432        temp = temp->next;
433    }
434   
435    return 0;
436}
437
438////////////////////////////////////////////////////////
439
440
441MoverStationListElement::MoverStationListElement(MoverStation *station)
442{
443    this->station = station;
444    this->next = 0;
445    this->prev = 0;
446}
447
448MoverStationListElement::~MoverStationListElement()
449{
450    delete this->station;
451}
Note: See TracBrowser for help on using the repository browser.