[Gcblue-commits] gcb_wx/src/ai BlackboardInterface.cpp,NONE,1.1 ScriptedTask.cpp,NONE,1.1 ScriptedTa
Status: Alpha
Brought to you by:
ddcforge
Update of /cvsroot/gcblue/gcb_wx/src/ai In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15507/src/ai Modified Files: Blackboard.cpp BlackboardItem.cpp Brain.cpp Task.cpp Added Files: BlackboardInterface.cpp ScriptedTask.cpp ScriptedTaskInterface.cpp SelfPreservation.cpp Log Message: Parallel ai update --- NEW FILE: ScriptedTask.cpp --- /** ** @file ScriptedTask.cpp */ /* Copyright (C) 2005 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB 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. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "stdwx.h" #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "ai/ScriptedTask.h" #include "scriptinterface/tcSimPythonInterface.h" #ifdef _DEBUG #define new DEBUG_NEW #endif using namespace ai; using ScriptInterface::tcSimPythonInterface; /** * @return "" if no key exists */ const std::string& ScriptedTask::GetMemoryText(const std::string& key) { // returned if key not found static const std::string emptyString(""); std::map<std::string, std::string>::const_iterator iter = textMemory.find(key); if (iter != textMemory.end()) { return iter->second; } else { return emptyString; } } void ScriptedTask::SetMemoryText(const std::string& key, const std::string& text) { textMemory[key] = text; } /** * @return 0 if no key exists */ double ScriptedTask::GetMemoryValue(int key) { std::map<int, double>::const_iterator iter = numberMemory.find(key); if (iter != numberMemory.end()) { return iter->second; } else { return 0; } } void ScriptedTask::SetMemoryValue(int key, double value) { numberMemory[key] = value; } const char* ScriptedTask::GetCommandString() { if (commandString.size()) { return commandString.c_str(); } else { commandString = wxString::Format("AI.%s(TaskInterface)\n", taskName.c_str()); return commandString.c_str(); } } void ScriptedTask::Update(double t) { if (IsReadyForUpdate(t)) { tcSimPythonInterface::Get()->CallTaskScript(this, GetCommandString()); FinishUpdate(t); } } ScriptedTask::ScriptedTask(tcPlatformObject* platform_, Blackboard* bb, long id_, double priority_, const std::string& scriptName) : Task(platform_, bb, id_, priority_, scriptName) { } ScriptedTask::~ScriptedTask() { } Index: Blackboard.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/ai/Blackboard.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Blackboard.cpp 4 Feb 2005 18:50:35 -0000 1.1 --- Blackboard.cpp 16 Feb 2005 23:13:49 -0000 1.2 *************** *** 36,43 **** using namespace ai; ! void Blackboard::EraseAll() ! { ! content.clear(); ! } void Blackboard::Erase(const std::string& key, double priority) --- 36,40 ---- using namespace ai; ! void Blackboard::Erase(const std::string& key, double priority) *************** *** 55,58 **** --- 52,85 ---- } + void Blackboard::EraseAll() + { + content.clear(); + } + + void Blackboard::EraseAllFromAuthor(long author) + { + if (author == -1) + { + fprintf(stderr, "Blackboard::EraseAllFromAuthor - invalid author (-1)\n"); + return; + } + + + + for (std::map<std::string, BlackboardItem>::iterator iter = + content.begin(); iter != content.end(); ) + { + if (iter->second.author == author) + { + content.erase(iter++); + } + else + { + ++iter; + } + } + + } + bool Blackboard::KeyExists(const std::string& key) const { *************** *** 79,82 **** --- 106,149 ---- } + /** + * @return -1 if key not found + */ + long Blackboard::ReadAuthor(const std::string& key) + { + std::map<std::string, BlackboardItem>::const_iterator iter = + content.find(key); + if (iter == content.end()) + { + return -1; + } + else + { + return iter->second.author; + } + } + + /** + * @return "" if key not found + */ + const std::string Blackboard::ReadMessage(const std::string& key) + { + // message to return if key not found + static const std::string emptyMessage(""); + + std::map<std::string, BlackboardItem>::const_iterator iter = + content.find(key); + if (iter == content.end()) + { + return emptyMessage; + } + else + { + return iter->second.message; + } + } + + /** + * @return -1.0 if key not found + */ double Blackboard::ReadPriority(const std::string& key) { *************** *** 93,97 **** } ! void Blackboard::Write(const std::string& key, const BlackboardItem& item) { std::map<std::string, BlackboardItem>::iterator iter = --- 160,167 ---- } ! /** ! * @return true if write successful, false if blocked ! */ ! bool Blackboard::Write(const std::string& key, const BlackboardItem& item) { std::map<std::string, BlackboardItem>::iterator iter = *************** *** 103,107 **** if ((iter->second.priority > item.priority) && (iter->second.author != item.author)) { ! return; } iter->second = item; --- 173,177 ---- if ((iter->second.priority > item.priority) && (iter->second.author != item.author)) { ! return false; } iter->second = item; *************** *** 111,118 **** content[key] = item; } ! } ! void Blackboard::Write(const std::string& key, long author, double priority, const std::string& message) { std::map<std::string, BlackboardItem>::iterator iter = --- 181,192 ---- content[key] = item; } ! ! return true; } ! /** ! * @return true if write successful, false if blocked ! */ ! bool Blackboard::Write(const std::string& key, long author, double priority, const std::string& message) { std::map<std::string, BlackboardItem>::iterator iter = *************** *** 124,128 **** if ((iter->second.priority > priority) && (iter->second.author != author)) { ! return; } iter->second.author = author; --- 198,202 ---- if ((iter->second.priority > priority) && (iter->second.author != author)) { ! return false; } iter->second.author = author; *************** *** 134,137 **** --- 208,213 ---- content[key] = BlackboardItem(author, priority, message); } + + return true; } --- NEW FILE: BlackboardInterface.cpp --- /** ** @file BlackboardInterface.cpp */ /* Copyright (C) 2005 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB 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. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "stdwx.h" #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "ai/BlackboardInterface.h" #include "ai/Blackboard.h" #include "ai/BlackboardItem.h" #include <boost/python.hpp> #ifdef _DEBUG #define new DEBUG_NEW #endif using namespace ai; using namespace boost::python; bool BlackboardInterface::pythonInitialized = false; /** * static method that should be called once to expose class * to Python */ void BlackboardInterface::InitPython() { if (pythonInitialized) return; class_<BlackboardInterface>("BlackboardInterface") .def_readonly("author", &BlackboardInterface::author) .def_readonly("priority", &BlackboardInterface::priority) .def("Erase", &BlackboardInterface::Erase) .def("KeyExists", &BlackboardInterface::KeyExists) .def("Read", &BlackboardInterface::Read) .def("ReadAuthor", &BlackboardInterface::ReadAuthor) .def("ReadMessage", &BlackboardInterface::ReadMessage) .def("ReadPriority", &BlackboardInterface::ReadPriority) .def("Write", &BlackboardInterface::Write) ; pythonInitialized = true; BlackboardItem::InitPython(); } /** * Used to create a BlackboardInterface object in Python */ BlackboardInterface BlackboardInterface::GetBlackboardInterface() { return BlackboardInterface(*this); } long BlackboardInterface::GetAuthor() const { return author; } void BlackboardInterface::SetPriority(double priority_) { priority = priority_; } void BlackboardInterface::Erase(const std::string& key) { board->Erase(key, priority); } bool BlackboardInterface::KeyExists(const std::string& key) const { return board->KeyExists(key); } BlackboardItem BlackboardInterface::Read(const std::string& key) { return board->Read(key); } long BlackboardInterface::ReadAuthor(const std::string& key) { return board->ReadAuthor(key); } const std::string BlackboardInterface::ReadMessage(const std::string& key) { return board->ReadMessage(key); } double BlackboardInterface::ReadPriority(const std::string& key) { return board->ReadPriority(key); } /** * @return true if write successful, false if blocked */ bool BlackboardInterface::Write(const std::string& key, const std::string& message) { return board->Write(key, author, priority, message); } /** * Default constructor should never be used */ BlackboardInterface::BlackboardInterface() : board(0), author(-1), priority(0) { wxASSERT(false); if (!pythonInitialized) InitPython(); } BlackboardInterface::BlackboardInterface(const BlackboardInterface& source) : board(source.board), author(source.author), priority(source.priority) { wxASSERT(board); if (!pythonInitialized) InitPython(); } BlackboardInterface::BlackboardInterface(Blackboard* bb, long author_, double priority_) : board(bb), author(author_), priority(priority_) { wxASSERT(board); if (!pythonInitialized) InitPython(); } BlackboardInterface::~BlackboardInterface() { } Index: Brain.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/ai/Brain.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Brain.cpp 4 Feb 2005 18:50:35 -0000 1.1 --- Brain.cpp 16 Feb 2005 23:13:49 -0000 1.2 *************** *** 27,31 **** #include "ai/Brain.h" ! #ifdef _DEBUG --- 27,33 ---- #include "ai/Brain.h" ! #include "ai/Task.h" ! #include "ai/ScriptedTask.h" ! #include "sim/tcPlatformObject.h" #ifdef _DEBUG *************** *** 35,40 **** using namespace ai; ! Brain::Brain() { } --- 37,115 ---- using namespace ai; ! ! std::map<std::string, int> Brain::taskNameLookup; ! ! void Brain::InitTaskNameLookup() ! { ! if (taskNameLookup.find("Test") != taskNameLookup.end()) ! { ! return; // already initialized ! } ! taskNameLookup["Test"] = TEST_TASK; ! } ! ! void Brain::AddTask(const std::string& taskName, double priority_) ! { ! // check taskNameMap for C++ task, otherwise assume python script task ! std::map<std::string, int>::iterator iter = taskNameLookup.find(taskName); ! if (iter == taskNameLookup.end()) ! { ! taskMap[taskName] = new ScriptedTask(platform, &board, ! nextId++, priority_, taskName); ! } ! else ! { ! int taskCode = iter->second; ! // add switch for C++ task addition ! } ! } ! ! void Brain::RemoveTask(const std::string& taskName) { + std::map<std::string, Task*>::iterator iter = taskMap.find(taskName); + if (iter != taskMap.end()) + { + // remove all blackboard entries from the author being erased + board.EraseAllFromAuthor(iter->second->GetAuthor()); + delete iter->second; + taskMap.erase(iter); + } + } + + bool Brain::TaskExists(const std::string& taskName) + { + return (taskMap.find(taskName) != taskMap.end()); + } + + void Brain::Update(double t) + { + if ((t - lastUpdateTime) < updateInterval) + { + return; + } + + // iterate through task map and update tasks + std::map<std::string, Task*>::iterator iter = taskMap.begin(); + std::map<std::string, Task*>::iterator& done = taskMap.end(); + for (;iter != done; ++iter) + { + wxASSERT(iter->second); + + iter->second->Update(t); + } + + lastUpdateTime = t; + } + + Brain::Brain(tcPlatformObject* platform_) + : platform(platform_), + updateInterval(0.125f), + nextId(0) + { + wxASSERT(platform); + + InitTaskNameLookup(); // lazy init, could also have separate call in app init for this + + lastUpdateTime = randf(updateInterval); // randomize update phase } Index: BlackboardItem.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/ai/BlackboardItem.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BlackboardItem.cpp 4 Feb 2005 18:50:35 -0000 1.1 --- BlackboardItem.cpp 16 Feb 2005 23:13:49 -0000 1.2 *************** *** 27,31 **** #include "ai/BlackboardItem.h" ! #ifdef _DEBUG --- 27,31 ---- #include "ai/BlackboardItem.h" ! #include <boost/python.hpp> #ifdef _DEBUG *************** *** 34,37 **** --- 34,60 ---- using namespace ai; + using namespace boost::python; + + bool BlackboardItem::pythonInitialized = false; + + /** + * static method that should be called once to expose class + * to Python + */ + void BlackboardItem::InitPython() + { + if (pythonInitialized) return; + + class_<BlackboardItem>("BlackboardItem") + .def_readonly("author", &BlackboardItem::author) + .def_readonly("priority", &BlackboardItem::priority) + .def_readonly("message", &BlackboardItem::message) + + .def("Valid", &BlackboardItem::Valid) + ; + + pythonInitialized = true; + } + bool BlackboardItem::Valid() const Index: Task.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/ai/Task.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Task.cpp 4 Feb 2005 18:50:35 -0000 1.1 --- Task.cpp 16 Feb 2005 23:13:49 -0000 1.2 *************** *** 27,30 **** --- 27,31 ---- #include "ai/Task.h" + #include "scriptinterface/tcPlatformInterface.h" *************** *** 34,65 **** using namespace ai; ! void Task::SetBoard(Blackboard* bb) { ! commBoard = bb; } ! void Task::SetId(long id_) { } ! void Task::SetPriority(double priority_) { } ! Task::Task(Blackboard* bb, long id_, double priority_) ! : commBoard(bb), ! id(id_), ! priority(priority_) { } ! Task::Task() ! : commBoard(0), ! id(-1), ! priority(0) { } Task::~Task() { --- 35,84 ---- using namespace ai; + using ScriptInterface::tcPlatformInterface; ! ! ! /** ! * Call at the end of every update ! */ ! void Task::FinishUpdate(double t) { ! lastUpdateTime = t; } ! tcPlatformInterface Task::GetPlatformInterface() { + return tcPlatformInterface(platform); } ! bool Task::IsReadyForUpdate(double t) const { + return ((t - lastUpdateTime) >= updateInterval); } ! ! ! void Task::SetUpdateInterval(float interval) { + updateInterval = interval; } ! void Task::Update(double t) { } + Task::Task(tcPlatformObject* platform_, Blackboard* bb, + long id_, double priority_, const std::string& taskName_) + : BlackboardInterface(bb, id_, priority_), + platform(platform_), + taskName(taskName_), + lastUpdateTime(0), + updateInterval(4.0f) + { + wxASSERT(platform); + } + + + Task::~Task() { --- NEW FILE: ScriptedTaskInterface.cpp --- /** ** @file ScriptedTaskInterface.cpp */ /* Copyright (C) 2005 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB 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. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "stdwx.h" #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "ai/ScriptedTaskInterface.h" #include "ai/BlackboardInterface.h" #include "ai/ScriptedTask.h" #include "scriptinterface/tcPlatformInterface.h" #include <boost/python.hpp> #ifdef _DEBUG #define new DEBUG_NEW #endif using namespace ai; using namespace boost::python; using ScriptInterface::tcPlatformInterface; bool ScriptedTaskInterface::pythonInitialized = false; /** * static method that should be called once to expose class * to Python */ void ScriptedTaskInterface::InitPython() { if (pythonInitialized) return; class_<ScriptedTaskInterface>("ScriptedTaskInterface") .def("GetBlackboardInterface", &ScriptedTaskInterface::GetBlackboardInterface) .def("GetPlatformInterface", &ScriptedTaskInterface::GetPlatformInterface) .def("SetUpdateInterval", &ScriptedTaskInterface::SetUpdateInterval) .def("GetMemoryText", &ScriptedTaskInterface::GetMemoryText) .def("SetMemoryText", &ScriptedTaskInterface::SetMemoryText) .def("GetMemoryValue", &ScriptedTaskInterface::GetMemoryValue) .def("SetMemoryValue", &ScriptedTaskInterface::SetMemoryValue) ; pythonInitialized = false; } BlackboardInterface ScriptedTaskInterface::GetBlackboardInterface() { wxASSERT(task); return task->GetBlackboardInterface(); } const std::string ScriptedTaskInterface::GetMemoryText(const std::string& key) { wxASSERT(task); return task->GetMemoryText(key); } double ScriptedTaskInterface::GetMemoryValue(int key) { wxASSERT(task); return task->GetMemoryValue(key); } tcPlatformInterface ScriptedTaskInterface::GetPlatformInterface() { wxASSERT(task); return task->GetPlatformInterface(); } void ScriptedTaskInterface::SetMemoryText(const std::string& key, const std::string& text) { wxASSERT(task); task->SetMemoryText(key, text); } void ScriptedTaskInterface::SetMemoryValue(int key, double value) { wxASSERT(task); return task->SetMemoryValue(key, value); } void ScriptedTaskInterface::SetTask(ScriptedTask* scriptedTask) { task = scriptedTask; wxASSERT(task); } void ScriptedTaskInterface::SetUpdateInterval(float interval) { wxASSERT(task); task->SetUpdateInterval(interval); } /** * Default constructor is defined for Python, SetTask needs to be * called after this constructor */ ScriptedTaskInterface::ScriptedTaskInterface() : task(0) { if (!pythonInitialized) InitPython(); } ScriptedTaskInterface::ScriptedTaskInterface(ScriptedTask* scriptedTask) : task(scriptedTask) { wxASSERT(task); if (!pythonInitialized) InitPython(); } ScriptedTaskInterface::~ScriptedTaskInterface() { } --- NEW FILE: SelfPreservation.cpp --- /** ** @file SelfPreservation.cpp */ /* Copyright (C) 2005 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB 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. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "stdwx.h" #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "ai/SelfPreservation.h" #include "scriptinterface/tcPlatformInterface.h" #ifdef _DEBUG #define new DEBUG_NEW #endif using namespace ai; using ScriptInterface::tcPlatformInterface; /** * version for air objects */ void SelfPreservation::UpdateAir(double t) { tcPlatformInterface platformInterface = GetPlatformInterface(); // avoid crashing float alt_m = platformInterface.GetAltitude(); float terrain_m = platformInterface.GetTerrainElevation(); if (alt_m < terrain_m + 50) { if (!haveAltitudeControl) { haveAltitudeControl = Write("Altitude", ""); } if (haveAltitudeControl) { platformInterface.SetAltitude(terrain_m + 70); } } else { if (haveAltitudeControl) { Erase("Altitude"); haveAltitudeControl = false; } } } void SelfPreservation::Update(double t) { if (!IsReadyForUpdate(t)) return; tcPlatformInterface platformInterface = GetPlatformInterface(); if (platformInterface.IsAir()) { UpdateAir(t); } FinishUpdate(t); } SelfPreservation::SelfPreservation(tcPlatformObject* platform_, Blackboard* bb, long id_, double priority_, const std::string& taskName_) : Task(platform_, bb, id_, priority_, taskName_) { wxASSERT(platform); } SelfPreservation::~SelfPreservation() { } |