# ai_event.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD 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, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "ai_event_types.hpp"
    00013
    00014 #include <queue>
    00015
    00016 struct AIEventData {
    00017   std::queue<AIEvent *> stack;
    00018 };
    00019
    00020 /* static */ void AIEventController::CreateEventPointer()
    00021 {
    00022   assert(AIObject::GetEventPointer() == NULL);
    00023
    00024   AIObject::GetEventPointer() = new AIEventData();
    00025 }
    00026
    00027 /* static */ void AIEventController::FreeEventPointer()
    00028 {
    00029   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00030
    00031   /* Free all waiting events (if any) */
    00032   while (!data->stack.empty()) {
    00033     AIEvent *e = data->stack.front();
    00034     data->stack.pop();
    00035     e->Release();
    00036   }
    00037
    00038   /* Now kill our data pointer */
    00039   delete data;
    00040 }
    00041
    00042 /* static */ bool AIEventController::IsEventWaiting()
    00043 {
    00044   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00045   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00046
    00047   return !data->stack.empty();
    00048 }
    00049
    00050 /* static */ AIEvent *AIEventController::GetNextEvent()
    00051 {
    00052   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00053   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00054
    00055   if (data->stack.empty()) return NULL;
    00056
    00057   AIEvent *e = data->stack.front();
    00058   data->stack.pop();
    00059   return e;
    00060 }
    00061
    00062 /* static */ void AIEventController::InsertEvent(AIEvent *event)
    00063 {
    00064   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00065   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00066
    00067   event->AddRef();
    00068   data->stack.push(event);
    00069 }
    00070