[11519] | 1 | |
---|
| 2 | #include "scriptable_controller_api.h" |
---|
| 3 | #include "luatb.h" |
---|
| 4 | #include "scriptable_controller.h" |
---|
[11562] | 5 | #include "tools/Timer.h" |
---|
[11662] | 6 | #include "worldentities/pawns/Pawn.h" |
---|
| 7 | #include "infos/Bot.h" |
---|
| 8 | #include "worldentities/pawns/ModularSpaceShip.h" |
---|
[11999] | 9 | //#include "../modules/objects/collisionshapes/SphereCollisionShape.h" |
---|
| 10 | #include "graphics/Model.h" |
---|
[12006] | 11 | #include "worldentities/pawns/ScriptableControllerDrone.h" |
---|
[12390] | 12 | #include "worldentities/pawns/SpaceShip.h" |
---|
[11519] | 13 | |
---|
[11999] | 14 | |
---|
[12390] | 15 | |
---|
[11519] | 16 | namespace orxonox |
---|
| 17 | { |
---|
| 18 | |
---|
[11854] | 19 | const double ScriptableControllerAPI::periodic_interval = 0.5; |
---|
[11606] | 20 | |
---|
[11854] | 21 | ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller) |
---|
| 22 | { |
---|
| 23 | this->lua_ = lua; |
---|
| 24 | this->controller_ = controller; |
---|
[11519] | 25 | |
---|
[11854] | 26 | // Haven't found a shorter way yet to write that... We need C++17! |
---|
| 27 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); |
---|
[11606] | 28 | |
---|
[11854] | 29 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); |
---|
| 30 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); |
---|
| 31 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); |
---|
| 32 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); |
---|
| 33 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); |
---|
| 34 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled"); |
---|
| 35 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit"); |
---|
[11583] | 36 | |
---|
[11854] | 37 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition"); |
---|
| 38 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation"); |
---|
| 39 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity"); |
---|
| 40 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity"); |
---|
[11638] | 41 | |
---|
[11854] | 42 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); |
---|
| 43 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn"); |
---|
[11606] | 44 | |
---|
[11974] | 45 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawnTest)>::registerFunction<&ScriptableControllerAPI::spawnTest>(this, lua, "spawnTest"); |
---|
[12390] | 46 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawnZombie)>::registerFunction<&ScriptableControllerAPI::spawnZombie>(this, lua, "spawnZombie"); |
---|
[11519] | 47 | |
---|
| 48 | |
---|
[11854] | 49 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::myTestFunction)>::registerFunction<&ScriptableControllerAPI::myTestFunction>(this, lua, "mytestfunction"); |
---|
[11902] | 50 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::moveControllableEntity)>::registerFunction<&ScriptableControllerAPI::moveControllableEntity>(this, lua, "moveControllableEntity"); |
---|
[11562] | 51 | |
---|
[11519] | 52 | |
---|
[11583] | 53 | |
---|
[11854] | 54 | this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); |
---|
| 55 | } |
---|
[11519] | 56 | |
---|
[11854] | 57 | ScriptableControllerAPI::~ScriptableControllerAPI() |
---|
| 58 | { |
---|
| 59 | lua_close(this->lua_); |
---|
| 60 | } |
---|
[11519] | 61 | |
---|
[11854] | 62 | void ScriptableControllerAPI::orxPrint(std::string msg) |
---|
| 63 | { |
---|
| 64 | orxout(user_info) << msg << std::endl; |
---|
| 65 | } |
---|
[11519] | 66 | |
---|
[11854] | 67 | void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout) |
---|
| 68 | { |
---|
| 69 | // Kills itself when the timer fires |
---|
| 70 | new Timer(timeout, false, callback, true); |
---|
| 71 | } |
---|
[11519] | 72 | |
---|
[11911] | 73 | //void ScriptableControllerAPI::registerPeriodically(std::function<void (void)> callback, double timeout) |
---|
| 74 | //{ |
---|
| 75 | // // Kills itself when the timer fires |
---|
| 76 | // new Timer(timeout, false, callback, true); |
---|
| 77 | //} |
---|
| 78 | |
---|
[11854] | 79 | void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance) |
---|
| 80 | { |
---|
| 81 | WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1); |
---|
| 82 | WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2); |
---|
[11519] | 83 | |
---|
[11854] | 84 | if(entity1 != nullptr && entity2 != nullptr) |
---|
| 85 | this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback)); |
---|
| 86 | } |
---|
[11519] | 87 | |
---|
[11854] | 88 | void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance) |
---|
| 89 | { |
---|
| 90 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
[11519] | 91 | |
---|
[11854] | 92 | if(entity != nullptr) |
---|
| 93 | this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback)); |
---|
| 94 | } |
---|
[11606] | 95 | |
---|
[11854] | 96 | void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) |
---|
| 97 | { |
---|
| 98 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
[11606] | 99 | |
---|
[11854] | 100 | if(entity != nullptr) |
---|
| 101 | this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback)); |
---|
| 102 | } |
---|
[11606] | 103 | |
---|
[11854] | 104 | void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) |
---|
[11662] | 105 | { |
---|
[11854] | 106 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
| 107 | |
---|
| 108 | if(entity != nullptr) |
---|
| 109 | this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback)); |
---|
[11662] | 110 | } |
---|
| 111 | |
---|
[11854] | 112 | void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id) |
---|
[11662] | 113 | { |
---|
[11854] | 114 | this->pawnDestroyedHandlers_[id].push_back(callback); |
---|
[11662] | 115 | } |
---|
| 116 | |
---|
[11854] | 117 | void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id) |
---|
[11662] | 118 | { |
---|
[11854] | 119 | this->pawnHitHandlers_[id].push_back(callback); |
---|
[11662] | 120 | } |
---|
| 121 | |
---|
[11854] | 122 | void ScriptableControllerAPI::killPawn(std::string id) |
---|
[11662] | 123 | { |
---|
[11854] | 124 | Pawn *pawn = this->controller_->getPawnByID(id); |
---|
| 125 | if(pawn == nullptr) |
---|
| 126 | orxout(user_warning) << "Trying to kill an unknown pawn" << std::endl; |
---|
| 127 | else |
---|
| 128 | pawn->kill(); |
---|
[11662] | 129 | } |
---|
[11854] | 130 | |
---|
| 131 | void ScriptableControllerAPI::spawn(std::string type, std::string id) |
---|
[11662] | 132 | { |
---|
[11854] | 133 | if(this->controller_->getWorldEntityByID(id) != nullptr) |
---|
| 134 | { |
---|
| 135 | orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl; |
---|
| 136 | return; |
---|
| 137 | } |
---|
[11662] | 138 | |
---|
[11854] | 139 | Identifier *identifier = ClassByString(type); |
---|
| 140 | if(!identifier) |
---|
| 141 | { |
---|
| 142 | orxout(user_error) << "Script tried to spawn unknown object" << std::endl; |
---|
| 143 | return; |
---|
| 144 | } |
---|
[11662] | 145 | |
---|
[11854] | 146 | if(!identifier->isLoadable()) |
---|
| 147 | { |
---|
| 148 | orxout(user_error) << "Script tried to spawn unloadable object" << std::endl; |
---|
| 149 | return; |
---|
| 150 | } |
---|
[11662] | 151 | |
---|
[11854] | 152 | WorldEntity *entity; |
---|
| 153 | Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext()); |
---|
[11662] | 154 | |
---|
[11928] | 155 | orxout(user_error) << "First hit!" << std::endl; |
---|
| 156 | |
---|
[11854] | 157 | if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier())) |
---|
| 158 | { |
---|
| 159 | entity = orxonox_cast<WorldEntity*>(obj); |
---|
| 160 | } |
---|
| 161 | else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier())) |
---|
| 162 | { |
---|
| 163 | // TODO This does not work yet because somehow the controllable entity is not set |
---|
| 164 | // yet at this stage. |
---|
| 165 | // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity(); |
---|
[11911] | 166 | |
---|
| 167 | |
---|
| 168 | //use TEMPLATES in the map to define objects that are not present on the map yet |
---|
[11854] | 169 | return; |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl; |
---|
[11901] | 174 | |
---|
[11854] | 175 | return; |
---|
| 176 | } |
---|
[11638] | 177 | |
---|
[11928] | 178 | orxout(user_error) << "Second hit!" << std::endl; |
---|
| 179 | |
---|
[11854] | 180 | if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier())) |
---|
| 181 | this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity)); |
---|
[11638] | 182 | |
---|
[11854] | 183 | if(entity->isA(ClassIdentifier<Pawn>::getIdentifier())) |
---|
| 184 | this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity)); |
---|
[11638] | 185 | |
---|
[11854] | 186 | this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity)); |
---|
[11928] | 187 | |
---|
[11974] | 188 | |
---|
[11928] | 189 | orxout(user_error) << "Third and final hit!" << std::endl; |
---|
[11974] | 190 | |
---|
[11854] | 191 | } |
---|
[11638] | 192 | |
---|
[11974] | 193 | |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | void ScriptableControllerAPI::spawnTest(std::string id) |
---|
| 198 | { |
---|
| 199 | |
---|
| 200 | if(this->controller_->getWorldEntityByID(id) != nullptr) |
---|
| 201 | { |
---|
| 202 | orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl; |
---|
| 203 | return; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
[12006] | 207 | Identifier *identifier = ClassByString("ScriptableControllerDrone"); |
---|
[11974] | 208 | |
---|
| 209 | |
---|
| 210 | if(!identifier) |
---|
| 211 | { |
---|
| 212 | orxout(user_error) << "Script tried to spawn unknown object" << std::endl; |
---|
| 213 | return; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if(!identifier->isLoadable()) |
---|
| 217 | { |
---|
| 218 | orxout(user_error) << "Script tried to spawn unloadable object" << std::endl; |
---|
| 219 | return; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | WorldEntity *entity; |
---|
| 225 | Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext()); |
---|
| 226 | |
---|
| 227 | orxout(user_status) << "First hit!" << std::endl; |
---|
| 228 | |
---|
| 229 | if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier())) |
---|
| 230 | { |
---|
| 231 | orxout(user_status) << "Is WorldEntity!" << std::endl; |
---|
| 232 | entity = orxonox_cast<WorldEntity*>(obj); |
---|
| 233 | } |
---|
| 234 | else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier())) |
---|
| 235 | { |
---|
| 236 | // TODO This does not work yet because somehow the controllable entity is not set |
---|
| 237 | // yet at this stage. |
---|
| 238 | // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity(); |
---|
| 239 | |
---|
| 240 | orxout(user_status) << "Is PlayerInfo!" << std::endl; |
---|
| 241 | |
---|
| 242 | //use TEMPLATES in the map to define objects that are not present on the map yet |
---|
| 243 | return; |
---|
| 244 | } |
---|
| 245 | else |
---|
| 246 | { |
---|
| 247 | orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl; |
---|
| 248 | |
---|
| 249 | return; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier())) { |
---|
| 254 | orxout(user_status) << "Is MobileEntity!" << std::endl; |
---|
| 255 | this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity)); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | if(entity->isA(ClassIdentifier<Pawn>::getIdentifier())) { |
---|
| 259 | orxout(user_status) << "Is Pawn!" << std::endl; |
---|
| 260 | this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity)); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity)); |
---|
| 264 | |
---|
| 265 | if(this->controller_->getPawnByID(id) != nullptr) { |
---|
| 266 | orxout(user_status) << "Pawn is indeed available!" << std::endl; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | |
---|
[11999] | 271 | ///////////////GOLD!!!!!!!!!!!!!!!//////////////////////// |
---|
| 272 | Pawn* pawn = this->controller_->getPawnByID(id); |
---|
| 273 | //Attach to pawn |
---|
[12006] | 274 | ScriptableControllerDrone* drone = new ScriptableControllerDrone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something) |
---|
[11999] | 275 | |
---|
[12006] | 276 | drone->addTemplate("ScriptableControllerDroneTemplate"); //ScriptableControllerDroneTemplate spaceshipescort |
---|
[11974] | 277 | |
---|
[11999] | 278 | Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30); |
---|
| 279 | drone->setPosition(spawnPosition); |
---|
| 280 | //drone->moveFrontBack(1.0); |
---|
[11974] | 281 | |
---|
| 282 | |
---|
| 283 | orxout(user_status) << "Final hit!" << std::endl; |
---|
| 284 | } |
---|
| 285 | |
---|
[12390] | 286 | void ScriptableControllerAPI::spawnZombie(std::string id) |
---|
| 287 | { |
---|
| 288 | Identifier *identifier = ClassByString("SpaceShip"); |
---|
[11974] | 289 | |
---|
[12390] | 290 | if(!identifier) |
---|
| 291 | { |
---|
| 292 | orxout(user_error) << "Script tried to spawn unknown object" << std::endl; |
---|
| 293 | return; |
---|
| 294 | } |
---|
[11974] | 295 | |
---|
[12390] | 296 | if(!identifier->isLoadable()) |
---|
| 297 | { |
---|
| 298 | orxout(user_error) << "Script tried to spawn unloadable object" << std::endl; |
---|
| 299 | return; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | WorldEntity *entity; |
---|
| 304 | Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext()); |
---|
[11974] | 305 | |
---|
| 306 | |
---|
| 307 | |
---|
[12390] | 308 | orxout(user_status) << "First hit!" << std::endl; |
---|
| 309 | |
---|
| 310 | if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier())) |
---|
| 311 | { |
---|
| 312 | orxout(user_status) << "Is WorldEntity!" << std::endl; |
---|
| 313 | entity = orxonox_cast<WorldEntity*>(obj); |
---|
| 314 | } |
---|
| 315 | else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier())) |
---|
| 316 | { |
---|
| 317 | // TODO This does not work yet because somehow the controllable entity is not set |
---|
| 318 | // yet at this stage. |
---|
| 319 | // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity(); |
---|
| 320 | |
---|
| 321 | orxout(user_status) << "Is PlayerInfo!" << std::endl; |
---|
| 322 | |
---|
| 323 | //use TEMPLATES in the map to define objects that are not present on the map yet |
---|
| 324 | return; |
---|
| 325 | } |
---|
| 326 | else |
---|
| 327 | { |
---|
| 328 | orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl; |
---|
| 329 | |
---|
| 330 | return; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier())) { |
---|
| 334 | orxout(user_status) << "Is MobileEntity!" << std::endl; |
---|
| 335 | this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity)); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | if(entity->isA(ClassIdentifier<Pawn>::getIdentifier())) { |
---|
| 341 | orxout(user_status) << "Is Pawn!" << std::endl; |
---|
| 342 | this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity)); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity)); |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | ///////////////GOLD!!!!!!!!!!!!!!!//////////////////////// |
---|
| 349 | Pawn* pawn = this->controller_->getPawnByID(id); |
---|
| 350 | |
---|
| 351 | //Attach to pawn |
---|
| 352 | SpaceShip* drone = new SpaceShip(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something) |
---|
| 353 | |
---|
| 354 | drone->addTemplate("spaceshipzombie"); //ScriptableControllerDroneTemplate spaceshipescort |
---|
| 355 | |
---|
| 356 | Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(500,20,500); |
---|
| 357 | drone->setPosition(spawnPosition); |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | |
---|
| 361 | |
---|
[11854] | 362 | void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z) |
---|
[11638] | 363 | { |
---|
[11854] | 364 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
| 365 | if(entity == nullptr) |
---|
| 366 | { |
---|
| 367 | orxout(user_warning) << "Trying to set position of an unknown object" << std::endl; |
---|
| 368 | return; |
---|
| 369 | } |
---|
[11638] | 370 | |
---|
[11854] | 371 | const Vector3 &old = entity->getPosition(); |
---|
[11638] | 372 | |
---|
[11854] | 373 | // If one of the values is NaN, don't change that value |
---|
| 374 | x = std::isnan(x) ? old.x : x; |
---|
| 375 | y = std::isnan(y) ? old.y : y; |
---|
| 376 | z = std::isnan(z) ? old.z : z; |
---|
[11638] | 377 | |
---|
[11854] | 378 | entity->setPosition(x, y, z); |
---|
| 379 | } |
---|
[11638] | 380 | |
---|
[11854] | 381 | void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle) |
---|
| 382 | { |
---|
| 383 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
| 384 | if(entity == nullptr) |
---|
| 385 | { |
---|
| 386 | orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl; |
---|
| 387 | return; |
---|
| 388 | } |
---|
[11673] | 389 | |
---|
[11854] | 390 | Vector3 old_axis; |
---|
| 391 | Degree old_angle; |
---|
[11638] | 392 | |
---|
[11854] | 393 | entity->getOrientation().ToAngleAxis(old_angle, old_axis); |
---|
[11638] | 394 | |
---|
[11854] | 395 | // If one of the values is NaN, don't change that value |
---|
| 396 | x = std::isnan(x) ? old_axis.x : x; |
---|
| 397 | y = std::isnan(y) ? old_axis.y : y; |
---|
| 398 | z = std::isnan(z) ? old_axis.z : z; |
---|
| 399 | angle = std::isnan(x) ? old_angle.valueDegrees() : angle; |
---|
[11638] | 400 | |
---|
| 401 | |
---|
[11854] | 402 | entity->setOrientation(Vector3(x, y, z), Degree(angle)); |
---|
| 403 | } |
---|
[11638] | 404 | |
---|
[11854] | 405 | void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z) |
---|
[11638] | 406 | { |
---|
[11854] | 407 | MobileEntity *entity = this->controller_->getMobileEntityByID(id); |
---|
| 408 | if(entity == nullptr) |
---|
| 409 | { |
---|
| 410 | orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl; |
---|
| 411 | return; |
---|
| 412 | } |
---|
[11638] | 413 | |
---|
[11854] | 414 | const Vector3 &old = entity->getVelocity(); |
---|
[11638] | 415 | |
---|
[11854] | 416 | // If one of the values is NaN, don't change that value |
---|
| 417 | x = std::isnan(x) ? old.x : x; |
---|
| 418 | y = std::isnan(y) ? old.y : y; |
---|
| 419 | z = std::isnan(z) ? old.z : z; |
---|
[11638] | 420 | |
---|
[11854] | 421 | entity->setVelocity(x, y, z); |
---|
| 422 | } |
---|
[11638] | 423 | |
---|
[11854] | 424 | void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z) |
---|
| 425 | { |
---|
| 426 | MobileEntity *entity = this->controller_->getMobileEntityByID(id); |
---|
| 427 | if(entity == nullptr) |
---|
| 428 | { |
---|
| 429 | orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl; |
---|
| 430 | return; |
---|
| 431 | } |
---|
[11606] | 432 | |
---|
[11854] | 433 | const Vector3 &old = entity->getAngularVelocity(); |
---|
[11662] | 434 | |
---|
[11854] | 435 | // If one of the values is NaN, don't change that value |
---|
| 436 | x = std::isnan(x) ? old.x : x; |
---|
| 437 | y = std::isnan(y) ? old.y : y; |
---|
| 438 | z = std::isnan(z) ? old.z : z; |
---|
[11673] | 439 | |
---|
[11854] | 440 | entity->setAngularVelocity(x, y, z); |
---|
[11673] | 441 | } |
---|
| 442 | |
---|
[11854] | 443 | void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn) |
---|
[11673] | 444 | { |
---|
[11854] | 445 | for(auto callback : this->pawnDestroyedHandlers_[id]) |
---|
| 446 | callback(id); |
---|
[11606] | 447 | |
---|
[11854] | 448 | this->pawnDestroyedHandlers_.erase(id); |
---|
[11606] | 449 | |
---|
[11854] | 450 | // We need to delete those handlers as well, they're no longer valid |
---|
| 451 | auto near_obj_handler = this->nearObjectHandlers_.begin(); |
---|
| 452 | while(near_obj_handler != this->nearObjectHandlers_.end()) |
---|
[11583] | 453 | { |
---|
[11854] | 454 | if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn) |
---|
| 455 | near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); |
---|
| 456 | else |
---|
| 457 | near_obj_handler++; |
---|
[11583] | 458 | } |
---|
[11854] | 459 | |
---|
| 460 | auto near_point_handler = this->nearPointHandlers_.begin(); |
---|
| 461 | while(near_point_handler != this->nearPointHandlers_.end()) |
---|
[11583] | 462 | { |
---|
[11854] | 463 | if(near_point_handler->entity_ == pawn) |
---|
| 464 | near_point_handler = this->nearPointHandlers_.erase(near_point_handler); |
---|
| 465 | else |
---|
| 466 | near_point_handler++; |
---|
[11583] | 467 | } |
---|
[11519] | 468 | |
---|
[11854] | 469 | auto area_handler = this->areaHandlers_.begin(); |
---|
| 470 | while(area_handler != this->areaHandlers_.end()) |
---|
[11583] | 471 | { |
---|
[11854] | 472 | if(area_handler->entity_ == pawn) |
---|
| 473 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
| 474 | else |
---|
| 475 | area_handler++; |
---|
[11583] | 476 | } |
---|
| 477 | } |
---|
| 478 | |
---|
[11854] | 479 | void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield) |
---|
[11583] | 480 | { |
---|
[11854] | 481 | for(auto callback : this->pawnHitHandlers_[target_id]) |
---|
| 482 | callback(target_id, source_id, new_health, new_shield); |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | void ScriptableControllerAPI::periodic() |
---|
| 486 | { |
---|
| 487 | // Near object |
---|
| 488 | auto near_obj_handler = this->nearObjectHandlers_.begin(); |
---|
| 489 | while(near_obj_handler != this->nearObjectHandlers_.end()) |
---|
[11583] | 490 | { |
---|
[11854] | 491 | if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_) |
---|
[11583] | 492 | { |
---|
[11854] | 493 | near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_); |
---|
| 494 | near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); |
---|
[11583] | 495 | } |
---|
| 496 | else |
---|
| 497 | { |
---|
[11854] | 498 | near_obj_handler++; |
---|
[11583] | 499 | } |
---|
| 500 | } |
---|
[11854] | 501 | |
---|
| 502 | // Near point |
---|
| 503 | auto near_point_handler = this->nearPointHandlers_.begin(); |
---|
| 504 | while(near_point_handler != this->nearPointHandlers_.end()) |
---|
[11583] | 505 | { |
---|
[11854] | 506 | if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_) |
---|
[11583] | 507 | { |
---|
[11854] | 508 | near_point_handler->callback_(near_point_handler->id_); |
---|
| 509 | near_point_handler = this->nearPointHandlers_.erase(near_point_handler); |
---|
[11583] | 510 | } |
---|
| 511 | else |
---|
| 512 | { |
---|
[11854] | 513 | near_point_handler++; |
---|
[11583] | 514 | } |
---|
| 515 | } |
---|
[11854] | 516 | |
---|
| 517 | // Areas |
---|
| 518 | auto area_handler = this->areaHandlers_.begin(); |
---|
| 519 | while(area_handler != this->areaHandlers_.end()) |
---|
| 520 | { |
---|
| 521 | if(area_handler->entity_->getPosition() > area_handler->start_point_ && |
---|
| 522 | area_handler->entity_->getPosition() < area_handler->end_point_) |
---|
| 523 | { |
---|
| 524 | if(area_handler->atEnter_) |
---|
| 525 | { |
---|
| 526 | area_handler->callback_(area_handler->id_); |
---|
| 527 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
| 528 | } |
---|
| 529 | else |
---|
| 530 | { |
---|
| 531 | area_handler++; |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | else |
---|
| 535 | { |
---|
| 536 | if(!area_handler->atEnter_) |
---|
| 537 | { |
---|
| 538 | area_handler->callback_(area_handler->id_); |
---|
| 539 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
| 540 | } |
---|
| 541 | else |
---|
| 542 | { |
---|
| 543 | area_handler++; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | |
---|
[11583] | 548 | } |
---|
[11519] | 549 | |
---|
[11854] | 550 | |
---|
| 551 | //// TESTTESTTESTTESTTESTTEST |
---|
| 552 | double ScriptableControllerAPI::myTestFunction(double x, double y) |
---|
| 553 | { |
---|
| 554 | double z = x + y; |
---|
| 555 | orxout(user_info) << "Result = " << z << endl; |
---|
| 556 | return z; |
---|
| 557 | } |
---|
| 558 | |
---|
[11928] | 559 | |
---|
[11902] | 560 | void ScriptableControllerAPI::moveControllableEntity(std::string id, double x, double y, double z) |
---|
| 561 | { |
---|
| 562 | MobileEntity *entity = this->controller_->getMobileEntityByID(id); |
---|
[11999] | 563 | |
---|
[11902] | 564 | if(entity == nullptr) |
---|
| 565 | { |
---|
[11936] | 566 | orxout(user_warning) << "Trying to move an unknown object" << std::endl; |
---|
[11902] | 567 | return; |
---|
| 568 | } |
---|
[11901] | 569 | |
---|
[11902] | 570 | Identifier *identifier = ClassByString("ControllableEntity"); |
---|
| 571 | |
---|
| 572 | ControllableEntity *controllable_entity; |
---|
| 573 | |
---|
| 574 | if(identifier->isA(ClassIdentifier<ControllableEntity>::getIdentifier())) |
---|
| 575 | { |
---|
[11999] | 576 | orxout(user_info) << "Before final cast..."<< endl; |
---|
[11902] | 577 | controllable_entity = orxonox_cast<ControllableEntity*>(entity); |
---|
[11999] | 578 | orxout(user_info) << "After final cast..."<< endl; |
---|
[11936] | 579 | //ATTACHED COLLISION SHAPE is MANDATORY in order to move the entity |
---|
[11902] | 580 | controllable_entity->moveFrontBack(x); |
---|
| 581 | controllable_entity->moveRightLeft(y); |
---|
| 582 | controllable_entity->moveUpDown(z); |
---|
[11999] | 583 | orxout(user_info) << "After move..."<< endl; |
---|
[11902] | 584 | } |
---|
| 585 | |
---|
| 586 | return; |
---|
| 587 | |
---|
| 588 | } |
---|
[11519] | 589 | } |
---|