| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Paul Lehmann | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 |  /* | 
|---|
| 30 |   * Currently available lua commands: | 
|---|
| 31 |   * | 
|---|
| 32 |   * IMPORTANT: ALL COMMANDS DO REQUIRE 7 PARAMETERS TO BE PROVIDED. FILL UP WITH ZEROES IN UNIMPORTANT PLACES. | 
|---|
| 33 |   * | 
|---|
| 34 |   * Command             | Abbreviation | Parameter 1          | '' 2     | '' 3    | '' 4                 | '' 5     | '' 6     | '' 7 | 
|---|
| 35 |   * | 
|---|
| 36 |   * "Move And Look"     | mal          | GoTo X Coordinate    |  '' Y ''  | '' Z '' | LookAt X Coordinate  |  '' Y '' |  '' Y '' | Duration | 
|---|
| 37 |   * "Rotate And Look"   | ral          | GoTo X Coordinate    |  '' Y ''  | '' Z '' | Axis (1=x, 2=y, 3=z) |     -    |     -    | Duration | 
|---|
| 38 |   * "Spiral"            | spi          | GoTo X Coordinate    |  '' Y ''  | '' Z '' |          -           |     -    |     -    | Duration | 
|---|
| 39 |   * "Transition Look"   | chl          | From X Coordinate    |  '' Y ''  | '' Z '' | To X Coordinate      |  '' Y '' |  '' Y '' | Duration | 
|---|
| 40 |   * "rotate round X crd"| rotX         | anchor coordinate    | angle(rad)|    -    |                      |          |          | Duration | 
|---|
| 41 |   * "Idle (Do nothing)" | idle         | Duration | 
|---|
| 42 |   */ | 
|---|
| 43 |  | 
|---|
| 44 | #include "NewScriptController.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include "scriptTasks/DebugTask.h" | 
|---|
| 47 | #include "scriptTasks/stringOutTask.h" | 
|---|
| 48 | #include "scriptTasks/MoveToTask.h" | 
|---|
| 49 | #include "scriptTasks/Task.h" | 
|---|
| 50 | #include "infos/PlayerInfo.h" | 
|---|
| 51 | #include "core/CoreIncludes.h" | 
|---|
| 52 | #include "worldentities/ControllableEntity.h" | 
|---|
| 53 | #include "core/LuaState.h" | 
|---|
| 54 | #include "util/Math.h" | 
|---|
| 55 |  | 
|---|
| 56 | namespace orxonox | 
|---|
| 57 | { | 
|---|
| 58 |     RegisterClass(NewScriptController); | 
|---|
| 59 |  | 
|---|
| 60 |     NewScriptController::NewScriptController(Context* context) : ArtificialController(context) | 
|---|
| 61 |     { | 
|---|
| 62 |         RegisterObject(NewScriptController); | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 |         // Set default values for all variables  | 
|---|
| 66 |         // - pointers to zero  | 
|---|
| 67 |         this->player_ = nullptr; | 
|---|
| 68 |         this->entity_ = nullptr; | 
|---|
| 69 |  | 
|---|
| 70 |         // - times | 
|---|
| 71 |         this->scTime_ = 0.0f; | 
|---|
| 72 |  | 
|---|
| 73 |         this->context_ = context; | 
|---|
| 74 |  | 
|---|
| 75 |          | 
|---|
| 76 |     } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 |     void NewScriptController::takeControl(int ctrlid) | 
|---|
| 81 |     { | 
|---|
| 82 |         // Output some debugging information  | 
|---|
| 83 |         orxout(verbose) << "NewScriptController: Taking control" << endl; | 
|---|
| 84 |         orxout(verbose) << "This-pointer: " << this << endl;  | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 |         // Store the entity pointer in a private variable | 
|---|
| 88 |         this->entity_ = this->player_->getControllableEntity(); | 
|---|
| 89 |         assert(this->entity_); | 
|---|
| 90 |  | 
|---|
| 91 |         this->ctrlid_ = ctrlid; | 
|---|
| 92 |         if (this->ctrlid_ == 0) | 
|---|
| 93 |         { | 
|---|
| 94 |           this->ctrlid_ = 1; | 
|---|
| 95 |         } | 
|---|
| 96 |           | 
|---|
| 97 |         // Add the controller here to this entity. Apparently this still leaves | 
|---|
| 98 |         // any preexisting human controllers in place.  | 
|---|
| 99 |           | 
|---|
| 100 |         this->entity_->setDestroyWhenPlayerLeft(false); | 
|---|
| 101 |         this->player_->stopTemporaryControl(); | 
|---|
| 102 |         this->entity_->setController(this); | 
|---|
| 103 |         this->setControllableEntity(this->entity_); | 
|---|
| 104 |         this->entity_->mouseLook(); | 
|---|
| 105 |         this->entity_->setVisible(true); | 
|---|
| 106 |          | 
|---|
| 107 |         // TODO take the human Controllers control  dont forget to give it back in the destructor | 
|---|
| 108 |     } | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 |     void NewScriptController::tick(float dt) | 
|---|
| 112 |     { | 
|---|
| 113 |         // Call the tick function of the classes we derive from  | 
|---|
| 114 |         SUPER(NewScriptController, tick, dt); | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 |         // If this controller has no entity entry, do nothing  | 
|---|
| 118 |         if( !(this->entity_) ) return; | 
|---|
| 119 |  | 
|---|
| 120 |         if(!this->taskList_.empty()) | 
|---|
| 121 |         { | 
|---|
| 122 |            orxout() << scTime_ << endl; | 
|---|
| 123 |  | 
|---|
| 124 |            orxout() << taskList_.size() << endl; | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 |           if(this->taskList_.front()->getStartTime() < this->scTime_) | 
|---|
| 128 |           { | 
|---|
| 129 |               activeTasks_.push_back(this->taskList_.front()); | 
|---|
| 130 |               this->taskList_.pop_front(); | 
|---|
| 131 |           } | 
|---|
| 132 |         } | 
|---|
| 133 |         else | 
|---|
| 134 |         { | 
|---|
| 135 |           orxout() << "no tasks in taskList_" << endl; | 
|---|
| 136 |         } | 
|---|
| 137 |  | 
|---|
| 138 |         std::vector<Task*>::iterator it = activeTasks_.begin(); | 
|---|
| 139 |         while (it != activeTasks_.end() ) | 
|---|
| 140 |         { | 
|---|
| 141 |           if( !((*it)->update(dt)) ) | 
|---|
| 142 |           { | 
|---|
| 143 |             (*(*it)).destroyLater(); | 
|---|
| 144 |             it = activeTasks_.erase(it); | 
|---|
| 145 |           } | 
|---|
| 146 |           else | 
|---|
| 147 |           { | 
|---|
| 148 |             it++; | 
|---|
| 149 |           } | 
|---|
| 150 |  | 
|---|
| 151 |         } | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 |         scTime_ += dt; | 
|---|
| 156 |     } | 
|---|
| 157 |  | 
|---|
| 158 |     void NewScriptController::debugOut(float startTime) | 
|---|
| 159 |     { | 
|---|
| 160 |  | 
|---|
| 161 |       DebugTask* task = new DebugTask(context_); | 
|---|
| 162 |  | 
|---|
| 163 |       task->initialize(startTime); | 
|---|
| 164 |  | 
|---|
| 165 |       bool inserted = false; | 
|---|
| 166 |  | 
|---|
| 167 |       if(taskList_.empty()) | 
|---|
| 168 |       { | 
|---|
| 169 |         taskList_.push_front(task); | 
|---|
| 170 |         inserted = true; | 
|---|
| 171 |       } | 
|---|
| 172 |  | 
|---|
| 173 |       else | 
|---|
| 174 |       { | 
|---|
| 175 |         for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime | 
|---|
| 176 |         { | 
|---|
| 177 |           orxout() << "debugOutTask" << endl; | 
|---|
| 178 |  | 
|---|
| 179 |           if(task->getStartTime() < (*it)->getStartTime() ) | 
|---|
| 180 |           { | 
|---|
| 181 |             taskList_.insert(it, task); | 
|---|
| 182 |             inserted = true; | 
|---|
| 183 |             break; | 
|---|
| 184 |           } | 
|---|
| 185 |         } | 
|---|
| 186 |       } | 
|---|
| 187 |  | 
|---|
| 188 |       if (!inserted) | 
|---|
| 189 |       { | 
|---|
| 190 |         taskList_.push_back(task); | 
|---|
| 191 |       } | 
|---|
| 192 |        | 
|---|
| 193 |     } | 
|---|
| 194 |  | 
|---|
| 195 |     void NewScriptController::stringOut(float startTime, std::string output) | 
|---|
| 196 |     { | 
|---|
| 197 |  | 
|---|
| 198 |       stringOutTask* task = new stringOutTask(context_); | 
|---|
| 199 |  | 
|---|
| 200 |       task->initialize(startTime, output); | 
|---|
| 201 |  | 
|---|
| 202 |       bool inserted = false; | 
|---|
| 203 |  | 
|---|
| 204 |       if(taskList_.empty()) | 
|---|
| 205 |       { | 
|---|
| 206 |         taskList_.push_front(task); | 
|---|
| 207 |         inserted = true; | 
|---|
| 208 |       } | 
|---|
| 209 |  | 
|---|
| 210 |       else | 
|---|
| 211 |       { | 
|---|
| 212 |         for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime | 
|---|
| 213 |         { | 
|---|
| 214 |           orxout() << "stringOutTask" << endl; | 
|---|
| 215 |  | 
|---|
| 216 |           if(task->getStartTime() < (*it)->getStartTime() ) | 
|---|
| 217 |           { | 
|---|
| 218 |             taskList_.insert(it, task); | 
|---|
| 219 |             inserted = true; | 
|---|
| 220 |             break; | 
|---|
| 221 |           } | 
|---|
| 222 |         } | 
|---|
| 223 |       } | 
|---|
| 224 |  | 
|---|
| 225 |       if (!inserted) | 
|---|
| 226 |       { | 
|---|
| 227 |         taskList_.push_back(task); | 
|---|
| 228 |       } | 
|---|
| 229 |  | 
|---|
| 230 |     } | 
|---|
| 231 |  | 
|---|
| 232 |     void NewScriptController::moveTo(float startTime, float x, float y, float z, float velocity) | 
|---|
| 233 |     { | 
|---|
| 234 |  | 
|---|
| 235 |       MoveToTask* task = new MoveToTask(context_); | 
|---|
| 236 |  | 
|---|
| 237 |       Vector3 destination = Vector3(x,y,z); | 
|---|
| 238 |  | 
|---|
| 239 |       task->initialize(startTime, player_, destination, velocity); | 
|---|
| 240 |  | 
|---|
| 241 |       bool inserted = false; | 
|---|
| 242 |  | 
|---|
| 243 |       if(taskList_.empty()) | 
|---|
| 244 |       { | 
|---|
| 245 |         taskList_.push_front(task); | 
|---|
| 246 |         inserted = true; | 
|---|
| 247 |       } | 
|---|
| 248 |  | 
|---|
| 249 |       else | 
|---|
| 250 |       { | 
|---|
| 251 |         for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime | 
|---|
| 252 |         { | 
|---|
| 253 |           orxout() << "stringOutTask" << endl; | 
|---|
| 254 |  | 
|---|
| 255 |           if(task->getStartTime() < (*it)->getStartTime() ) | 
|---|
| 256 |           { | 
|---|
| 257 |             taskList_.insert(it, task); | 
|---|
| 258 |             inserted = true; | 
|---|
| 259 |             break; | 
|---|
| 260 |           } | 
|---|
| 261 |         } | 
|---|
| 262 |       } | 
|---|
| 263 |  | 
|---|
| 264 |       if (!inserted) | 
|---|
| 265 |       { | 
|---|
| 266 |         taskList_.push_back(task); | 
|---|
| 267 |       } | 
|---|
| 268 |  | 
|---|
| 269 |     } | 
|---|
| 270 |  | 
|---|
| 271 |     NewScriptController* NewScriptController::getNewScriptController()  | 
|---|
| 272 |     { | 
|---|
| 273 |       /* Output a message that confirms this function was called */ | 
|---|
| 274 |       orxout() << "Great success!" << std::endl; | 
|---|
| 275 |  | 
|---|
| 276 |       /* Debugging: print all the scriptcontroller object pointers */ | 
|---|
| 277 |       for(NewScriptController* controller : ObjectList<NewScriptController>()) | 
|---|
| 278 |       { orxout() << "Have object in list: " << controller << endl; } | 
|---|
| 279 |  | 
|---|
| 280 |       /* Find the first one with a nonzero ID */ | 
|---|
| 281 |       for(NewScriptController* controller : ObjectList<NewScriptController>()) | 
|---|
| 282 |       {  | 
|---|
| 283 |         // TODO: do some selection here. Currently just returns the first one | 
|---|
| 284 |         if( controller->getID() > 0 ) | 
|---|
| 285 |         { orxout() << "Controller to return: " << controller << endl; | 
|---|
| 286 |           return controller; | 
|---|
| 287 |         } | 
|---|
| 288 |        | 
|---|
| 289 |       } | 
|---|
| 290 |       return nullptr; | 
|---|
| 291 |     } | 
|---|
| 292 |  | 
|---|
| 293 | } | 
|---|