# 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 "../../stdafx.h"
    00013 #include "ai_event_types.hpp"
    00014
    00015 #include <queue>
    00016
    00017 struct AIEventData {
    00018   std::queue<AIEvent *> stack;
    00019 };
    00020
    00021 /* static */ void AIEventController::CreateEventPointer()
    00022 {
    00023   assert(AIObject::GetEventPointer() == NULL);
    00024
    00025   AIObject::GetEventPointer() = new AIEventData();
    00026 }
    00027
    00028 /* static */ void AIEventController::FreeEventPointer()
    00029 {
    00030   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00031
    00032   /* Free all waiting events (if any) */
    00033   while (!data->stack.empty()) {
    00034     AIEvent *e = data->stack.front();
    00035     data->stack.pop();
    00036     e->Release();
    00037   }
    00038
    00039   /* Now kill our data pointer */
    00040   delete data;
    00041 }
    00042
    00043 /* static */ bool AIEventController::IsEventWaiting()
    00044 {
    00045   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00046   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00047
    00048   return !data->stack.empty();
    00049 }
    00050
    00051 /* static */ AIEvent *AIEventController::GetNextEvent()
    00052 {
    00053   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00054   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00055
    00056   if (data->stack.empty()) return NULL;
    00057
    00058   AIEvent *e = data->stack.front();
    00059   data->stack.pop();
    00060   return e;
    00061 }
    00062
    00063 /* static */ void AIEventController::InsertEvent(AIEvent *event)
    00064 {
    00065   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00066   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00067
    00068   event->AddRef();
    00069   data->stack.push(event);
    00070 }
    00071