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