/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2007 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Fabian 'x3n' Landau co-programmer: */ #include "mover_station_list.h" #include "debug.h" MoverStationList::MoverStationList() { this->first = 0; this->last = 0; this->goForward = true; } MoverStationList::~MoverStationList() { MoverStationListElement *temp1 = this->first; MoverStationListElement *temp2; while (temp1 != 0) { temp2 = temp1->next; delete temp1; temp1 = temp2; } } void MoverStationList::addStation(MoverStation *station) { PRINTF(0)("Füge MoverStation %p hinzu\n", station); if (this->first == 0) { this->first = new MoverStationListElement(station); this->last = this->first; return; } else { if (this->first->station->rank > station->rank) { MoverStationListElement *temp = this->first; this->first = new MoverStationListElement(station); this->first->next = temp; this->first->next->prev = this->first; return; } else { MoverStationListElement *temp1 = this->first; MoverStationListElement *temp2; while (temp1->next != 0) { if (temp1->next->station->rank > station->rank) { temp2 = temp1->next; temp1->next = new MoverStationListElement(station); temp1->next->prev = temp1; temp1->next->next = temp2; temp1->next->next->prev = temp1->next; return; } temp1 = temp1->next; } temp1->next = new MoverStationListElement(station); temp1->next->prev = last; last = temp1->next; } } } MoverStation *MoverStationList::getNextStation(MoverStation *station) { if (this->goForward) { if (station) { MoverStationListElement *temp; if (station->bJumpToRank) { temp = this->first; while (temp != 0) { if (temp->station->rank == station->jumpToRank) return temp->station; temp = temp->next; } } // no "else" here - if jumpToRank wasn't found, we return the following station (as usual) if (station->bMoveToRank) { temp = this->first; while (temp != 0) { if (temp->station->rank == station->moveToRank) return temp->station; temp = temp->next; } } // no "else" here - if moveToRank wasn't found, we return the following station (as usual) temp = this->first; while (temp != 0) { if (temp->station == station) { if (temp->next) { return temp->next->station; } else { this->goForward = false; return this->last->station; } } temp = temp->next; } return 0; } else { if (this->first) return this->first->station; else return 0; } } else { if (station) { MoverStationListElement *temp; if (station->bJumpToRank) { temp = this->last; while (temp != 0) { if (temp->station->rank == station->jumpToRank) return temp->station; temp = temp->prev; } } // no "else" here - if jumpToRank wasn't found, we return the following station (as usual) if (station->bMoveToRank) { temp = this->last; while (temp != 0) { if (temp->station->rank == station->moveToRank) return temp->station; temp = temp->prev; } } // no "else" here - if moveToRank wasn't found, we return the following station (as usual) temp = this->last; while (temp != 0) { if (temp->station == station) { if (temp->prev) { return temp->prev->station; } else { this->goForward = true; return this->first->station; } } temp = temp->prev; } return 0; } else { if (this->last) return this->last->station; else return 0; } } } Vector MoverStationList::getTotalRelCoor(MoverStation *station) { MoverStationListElement *temp = this->first; Vector totalRelCoor = Vector(0, 0, 0); if (station) { if (this->goForward) { while (temp != 0) { totalRelCoor += temp->station->relTargetCoor; if (temp->station == station) break; temp = temp->next; } } else { if (temp) { while (temp->next != 0) { totalRelCoor += temp->next->station->relTargetCoor; if (temp->next->station == station) break; temp = temp->next; } } } } return totalRelCoor; } Vector MoverStationList::getTotalRelDir(MoverStation *station) { MoverStationListElement *temp = this->first; Vector totalRelDir = Vector(0, 0, 0); if (station) { if (this->goForward) { while (temp != 0) { totalRelDir += temp->station->relTargetDir; if (temp->station == station) break; temp = temp->next; } } else { if (temp) { while (temp->next != 0) { totalRelDir += temp->next->station->relTargetDir; if (temp->next->station == station) break; temp = temp->next; } } } } return totalRelDir; } bool MoverStationList::isOpen(MoverStation *station) { if (station) if ((this->goForward && this->last && this->last->station == station) || station->bIsOpen) return true; return false; } bool MoverStationList::isClosed(MoverStation *station) { if (station) if ((!this->goForward && this->first && this->first->station == station) || station->bIsClosed) return true; return false; } bool MoverStationList::changeDirection(bool bReopen, bool bReclose, bool bIsTriggered) { if (this->goForward && bReclose && !bIsTriggered) { this->goForward = false; return true; } if (!this->goForward && bReopen && bIsTriggered) { this->goForward = true; return true; } return false; } Vector MoverStationList::getRelTargetCoor(MoverStation *station) { if (station) { if (!station->bMoveToRank) { if (this->goForward) return station->relTargetCoor; else return station->relTargetCoor * -1; } else { MoverStation *moveToStation = this->getStation(station->moveToRank); if (moveToStation != 0) { if (this->goForward) return (this->getTotalRelCoor(station) - this->getTotalRelCoor(moveToStation)) * -1; else return (this->getTotalRelCoor(station) - this->getTotalRelCoor(moveToStation)); } } } return Vector(0, 0, 0); } Vector MoverStationList::getRelTargetDir(MoverStation *station) { if (station) { if (!station->bMoveToRank) { if (this->goForward) return station->relTargetDir; else return station->relTargetDir * -1; } else { MoverStation *moveToStation = this->getStation(station->moveToRank); if (moveToStation != 0) { if (this->goForward) return (station->relTargetDir - this->getTotalRelDir(moveToStation)) * -1; else return station->relTargetDir - this->getTotalRelDir(moveToStation); } } } return Vector(0, 0, 0); } Vector MoverStationList::getVelocity(MoverStation *station) { if (station) if (station->movingTime != 0) return this->getRelTargetCoor(station) / station->movingTime; return Vector(0, 0, 0); } Vector MoverStationList::getRotation(MoverStation *station) { if (station) if (station->movingTime != 0) return this->getRelTargetDir(station) / station->movingTime; return Vector(0, 0, 0); } OrxSound::SoundBuffer MoverStationList::getStartingSound(MoverStation *station) { if (station) { if (this->goForward) return station->soundBuffer_opening; else return station->soundBuffer_closing; } return OrxSound::ResourceSoundBuffer(""); } OrxSound::SoundBuffer MoverStationList::getEndingSound(MoverStation *station) { if (station) { if (this->goForward) return station->soundBuffer_opened; else return station->soundBuffer_closed; } return OrxSound::ResourceSoundBuffer(""); } OrxSound::SoundBuffer MoverStationList::getMovingSound(MoverStation *station) { if (station) return station->soundBuffer_moving; return OrxSound::ResourceSoundBuffer(""); } MoverStation *MoverStationList::getStation(int rank) { MoverStationListElement *temp; temp = this->first; while (temp != 0) { if (temp->station->rank == rank) return temp->station; temp = temp->next; } return 0; } //////////////////////////////////////////////////////// MoverStationListElement::MoverStationListElement(MoverStation *station) { this->station = station; this->next = 0; this->prev = 0; } MoverStationListElement::~MoverStationListElement() { delete this->station; }