[Orxonox-commit 6941] r11562 - in code/branches/ScriptableController_HS17/src: libraries/tools orxonox orxonox/infos orxonox/scriptablecontroller orxonox/worldentities
kohlia at orxonox.net
kohlia at orxonox.net
Mon Nov 13 17:25:10 CET 2017
Author: kohlia
Date: 2017-11-13 17:25:09 +0100 (Mon, 13 Nov 2017)
New Revision: 11562
Modified:
code/branches/ScriptableController_HS17/src/libraries/tools/Timer.cc
code/branches/ScriptableController_HS17/src/libraries/tools/Timer.h
code/branches/ScriptableController_HS17/src/orxonox/Level.cc
code/branches/ScriptableController_HS17/src/orxonox/Level.h
code/branches/ScriptableController_HS17/src/orxonox/infos/HumanPlayer.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc
code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.cc
code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.h
Log:
Figuring out when the different things are ready in orxonox
Modified: code/branches/ScriptableController_HS17/src/libraries/tools/Timer.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/libraries/tools/Timer.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/libraries/tools/Timer.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -157,6 +157,14 @@
this->setTimer(interval, bLoop, executor, bKillAfterCall);
}
+ Timer::Timer(float interval, bool bLoop, std::function<void ()> func, bool bKillAfterCall)
+ {
+ this->init();
+ RegisterObject(Timer);
+
+ this->setTimer(interval, bLoop, func, bKillAfterCall);
+ }
+
/**
@brief Initializes the Timer
*/
@@ -192,13 +200,24 @@
this->bLoop_ = bLoop;
this->executor_ = executor;
this->bActive_ = true;
+ this->isStdFunction_ = false;
this->time_ = this->interval_;
this->bKillAfterCall_ = bKillAfterCall;
- executor->getFunctor()->setSafeMode(true);
+ if(executor != nullptr)
+ executor->getFunctor()->setSafeMode(true);
}
+ void Timer::setTimer(float interval, bool bLoop, std::function<void ()> func, bool bKillAfterCall)
+ {
+ // Without the cast, the call would be ambiguous, because nullptr is castable to
+ // both, ExecutorPtr and std::function.
+ this->setTimer(interval, bLoop, static_cast<ExecutorPtr>(nullptr), bKillAfterCall);
+ this->function_ = func;
+ this->isStdFunction_ = true;
+ }
+
/**
@brief Calls the executor and destroys the timer if requested.
*/
@@ -206,7 +225,10 @@
{
bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
- (*this->executor_)();
+ if(this->isStdFunction_)
+ this->function_();
+ else
+ (*this->executor_)();
if (temp)
delete this;
Modified: code/branches/ScriptableController_HS17/src/libraries/tools/Timer.h
===================================================================
--- code/branches/ScriptableController_HS17/src/libraries/tools/Timer.h 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/libraries/tools/Timer.h 2017-11-13 16:25:09 UTC (rev 11562)
@@ -107,8 +107,10 @@
Timer();
Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
+ Timer(float interval, bool bLoop, std::function<void (void)> func, bool bKillAfterCall = false);
void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
+ void setTimer(float interval, bool bLoop, std::function<void (void)> func, bool bKillAfterCall = false);
void run();
@@ -152,7 +154,9 @@
void init();
ExecutorPtr executor_; //!< The executor of the function that will be called when the time expires
+ std::function<void (void)> function_;
+ bool isStdFunction_;
long long interval_; //!< The time-interval in micro seconds
bool bLoop_; //!< If true, the executor gets called every @a interval seconds
bool bActive_; //!< If true, the timer ticks and calls the executor if the time's up
Modified: code/branches/ScriptableController_HS17/src/orxonox/Level.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/Level.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/Level.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -56,6 +56,7 @@
this->registerVariables();
this->xmlfilename_ = this->getFilename();
this->xmlfile_ = nullptr;
+ this->controller_.reset(new ScriptableController());
}
Level::~Level()
@@ -83,12 +84,6 @@
XMLPortObject(Level, MeshLodInformation, "lodinformation", addLodInfo, getLodInfo, xmlelement, mode);
XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
-
- if(this->level_script_ != "")
- {
- this->controller_ = new ScriptableController(this->getContext());
- this->controller_->runScript(this->level_script_);
- }
}
void Level::registerVariables()
@@ -176,8 +171,7 @@
void Level::addObject(BaseObject* object)
{
this->objects_.push_back(object);
- if(this->controller_ != nullptr)
- object->registerToScriptableController(this->controller_);
+ object->registerToScriptableController(this->controller_.get());
}
BaseObject* Level::getObject(unsigned int index) const
@@ -213,6 +207,12 @@
{
orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl;
player->switchGametype(this->getGametype());
+
+ if(player->isHumanPlayer() && player->isLocalPlayer())
+ {
+ this->getScriptableController()->setPlayer(player);
+ this->controller_->runScript(this->level_script_);
+ }
}
void Level::playerLeft(PlayerInfo* player)
Modified: code/branches/ScriptableController_HS17/src/orxonox/Level.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/Level.h 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/Level.h 2017-11-13 16:25:09 UTC (rev 11562)
@@ -56,7 +56,10 @@
MeshLodInformation* getLodInfo(std::string meshName) const;
+ inline ScriptableController *getScriptableController(void)
+ { return this->controller_.get(); }
+
private:
void registerVariables();
void addObject(BaseObject* object);
@@ -91,7 +94,7 @@
std::list<BaseObject*> objects_;
std::map<std::string,MeshLodInformation*> lodInformation_;
- ScriptableController* controller_;
+ std::unique_ptr<ScriptableController> controller_;
std::string level_script_;
};
}
Modified: code/branches/ScriptableController_HS17/src/orxonox/infos/HumanPlayer.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/infos/HumanPlayer.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/infos/HumanPlayer.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -37,6 +37,8 @@
#include "controllers/NewHumanController.h"
#include "gametypes/Gametype.h"
#include "overlays/OverlayGroup.h"
+#include "Level.h"
+#include "scriptablecontroller/scriptable_controller.h"
namespace orxonox
{
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -1,19 +1,12 @@
#include "scriptable_controller.h"
#include "luatb.h"
+#include "infos/PlayerInfo.h"
namespace orxonox
{
-RegisterUnloadableClass(ScriptableController);
-
// Used https://csl.name/post/lua-and-cpp/ as a reference
-ScriptableController::ScriptableController(Context* context)
- : BaseObject(context)
-{
- RegisterObject(ScriptableController);
-}
-
int ScriptableController::runScript(const std::string &file_path)
{
int ret;
@@ -56,18 +49,28 @@
return 0;
}
+void ScriptableController::setPlayer(PlayerInfo *player)
+{
+ this->player_ = player;
+}
+
void ScriptableController::registerWorldEntity(int id, WorldEntity *obj)
{
+ orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl;
this->worldEntities_[id] = obj;
}
void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj)
{
+ orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl;
this->worldEntities_[id] = obj;
}
WorldEntity *ScriptableController::getWorldEntityByID(int id) const
{
+ if(id == 0)
+ return this->player_->getControllableEntity();
+
auto obj = this->worldEntities_.find(id);
return obj != this->worldEntities_.end() ? obj->second : nullptr;
}
@@ -74,34 +77,58 @@
ControllableEntity *ScriptableController::getControllableEntityByID(int id) const
{
+ if(id == 0)
+ return this->player_->getControllableEntity();
+
auto obj = this->controllabelEntities_.find(id);
return obj != this->controllabelEntities_.end() ? obj->second : nullptr;
}
-void ScriptableController::registerTimeout(std::function<void ()> callback, double timeout)
+void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback)
{
- this->timeouts.push_back(std::make_pair(callback, static_cast<float>(timeout)));
+ orxout(user_info) << "NearObject registering (id 1: " << obj1 << ", id 2: " << obj2 << ")" << std::endl;
+
+ WorldEntity *entity1 = this->getWorldEntityByID(obj1);
+ WorldEntity *entity2 = this->getWorldEntityByID(obj2);
+
+ NearObjectHandler *handler1 = new NearObjectHandler(entity2, distance, callback);
+ NearObjectHandler *handler2 = new NearObjectHandler(entity1, distance, callback);
+
+ this->nearObjectHandlers_[entity1].push_front(std::unique_ptr<NearObjectHandler>(handler1));
+ this->nearObjectHandlers_[entity2].push_front(std::unique_ptr<NearObjectHandler>(handler2));
+
+ handler1->otherHandler_ = this->nearObjectHandlers_[entity2].begin();
+ handler2->otherHandler_ = this->nearObjectHandlers_[entity1].begin();
}
-void ScriptableController::tick(float dt)
+void ScriptableController::objectMoved(WorldEntity *obj)
{
- auto timeout = this->timeouts.begin();
+ // Temp
+ return;
- while(timeout != this->timeouts.end())
+ auto &nearObjectHandlers = this->nearObjectHandlers_[obj];
+ auto handler = nearObjectHandlers.begin();
+ while(handler != nearObjectHandlers.end())
{
- timeout->second -= dt;
- if(timeout->second <= 0)
+ WorldEntity *other = (*handler)->otherObject_;
+ if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_)
{
- timeout->first();
- timeout = this->timeouts.erase(timeout);
+ (*handler)->callback_(obj->getID(), other->getID());
+
+ auto otherHandler = (*handler)->otherHandler_;
+ handler = nearObjectHandlers.erase(handler);
+ if(nearObjectHandlers.empty())
+ this->nearObjectHandlers_.erase(obj);
+
+ this->nearObjectHandlers_[other].erase(otherHandler);
+ if(this->nearObjectHandlers_[other].empty())
+ this->nearObjectHandlers_.erase(other);
}
else
{
- timeout++;
+ handler++;
}
}
-
- Tickable::tick(dt);
}
void ScriptableController::printLuaError(lua_State *lua)
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h 2017-11-13 16:25:09 UTC (rev 11562)
@@ -16,13 +16,24 @@
namespace orxonox
{
-class ScriptableController : public BaseObject, public Tickable
+class ScriptableController
{
public:
- explicit ScriptableController(Context *context);
+ struct NearObjectHandler
+ {
+ NearObjectHandler(WorldEntity *otherObject, double distance, std::function<void (int, int)> callback)
+ : otherObject_(otherObject), distance_(distance), callback_(callback)
+ {}
+ WorldEntity *otherObject_;
+ double distance_;
+ std::function<void (int, int)> callback_;
+ std::list<std::unique_ptr<NearObjectHandler> >::iterator otherHandler_;
+ };
+
int runScript(const std::string &file_path);
+ void setPlayer(PlayerInfo *player);
void registerWorldEntity(int id, WorldEntity *obj);
void registerControllableEntity(int id, ControllableEntity *obj);
@@ -29,17 +40,18 @@
WorldEntity *getWorldEntityByID(int id) const;
ControllableEntity *getControllableEntityByID(int id) const;
- void registerTimeout(std::function<void (void)> callback, double timeout);
+ void addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int, int)> callback);
- virtual void tick(float dt) override;
+ void objectMoved(WorldEntity *obj);
private:
std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
- ControllableEntity *player_; // TODO
+ PlayerInfo *player_;
std::map<int, WorldEntity*> worldEntities_;
std::map<int, ControllableEntity*> controllabelEntities_;
- std::list<std::pair<std::function<void (void)>, float> > timeouts;
+ std::map<WorldEntity*, std::list<std::unique_ptr<NearObjectHandler> > > nearObjectHandlers_;
+
void printLuaError(lua_State *lua);
};
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -2,6 +2,7 @@
#include "scriptable_controller_api.h"
#include "luatb.h"
#include "scriptable_controller.h"
+#include "tools/Timer.h"
namespace orxonox
{
@@ -12,6 +13,7 @@
this->controller_ = controller;
// Haven't found a shorter way yet to write that...
+ LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter");
@@ -25,15 +27,20 @@
lua_close(this->lua_);
}
+void ScriptableControllerAPI::orxPrint(std::string msg)
+{
+ orxout(user_info) << msg << std::endl;
+}
+
void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
{
- // TODO Extend timer class to accept std::function
- this->controller_->registerTimeout(callback, timeout);
+ // Kills itself when the timer fires
+ new Timer(timeout, false, callback, true);
}
int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)
{
-
+ //this->controller_->addNearObjectHandler(obj1, obj1, distance, callback);
}
int ScriptableControllerAPI::registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz)
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h 2017-11-13 16:25:09 UTC (rev 11562)
@@ -21,6 +21,7 @@
void testOutput(std::function<void(std::string)> callback);
+ void orxPrint(std::string msg);
void registerAfterTimeout(std::function<void (void)> callback, double timeout);
int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);
int registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);
Modified: code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -35,6 +35,8 @@
#include "core/XMLPort.h"
#include "Scene.h"
+#include "Level.h"
+#include "scriptablecontroller/scriptable_controller.h"
namespace orxonox
{
@@ -112,6 +114,7 @@
}
this->node_->setPosition(position);
+ this->getLevel()->getScriptableController()->objectMoved(this);
}
void MobileEntity::setOrientation(const Quaternion& orientation)
Modified: code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.cc 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.cc 2017-11-13 16:25:09 UTC (rev 11562)
@@ -183,6 +183,8 @@
XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
// Attached collision shapes
XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode);
+
+ orxout(user_info) << "ID loaded" << std::endl;
}
void WorldEntity::registerVariables()
@@ -279,6 +281,7 @@
void WorldEntity::registerToScriptableController(ScriptableController *controller)
{
+ orxout(user_info) << "Registering object to SC (" << this->id_ << ")" << std::endl;
controller->registerWorldEntity(this->id_, this);
}
Modified: code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.h 2017-11-13 15:34:53 UTC (rev 11561)
+++ code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.h 2017-11-13 16:25:09 UTC (rev 11562)
@@ -110,6 +110,9 @@
virtual void changedActivity(void) override;
virtual void changedVisibility(void) override;
+ inline int getID(void)
+ { return this->id_; }
+
inline void setID(int id)
{ this->id_ = id; }
More information about the Orxonox-commit
mailing list