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