[11165] | 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 | |
---|
[11183] | 46 | #include "scriptTasks/DebugTask.h" |
---|
| 47 | #include "scriptTasks/stringOutTask.h" |
---|
[11204] | 48 | #include "scriptTasks/MoveToTask.h" |
---|
[11183] | 49 | #include "scriptTasks/Task.h" |
---|
[11165] | 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 | |
---|
[11167] | 65 | // Set default values for all variables |
---|
| 66 | // - pointers to zero |
---|
[11165] | 67 | this->player_ = nullptr; |
---|
| 68 | this->entity_ = nullptr; |
---|
| 69 | |
---|
[11167] | 70 | // - times |
---|
[11165] | 71 | this->scTime_ = 0.0f; |
---|
| 72 | |
---|
[11172] | 73 | this->context_ = context; |
---|
[11183] | 74 | |
---|
| 75 | |
---|
[11165] | 76 | } |
---|
| 77 | |
---|
[11167] | 78 | |
---|
| 79 | |
---|
[11165] | 80 | void NewScriptController::takeControl(int ctrlid) |
---|
| 81 | { |
---|
[11167] | 82 | // Output some debugging information |
---|
[11165] | 83 | orxout(verbose) << "NewScriptController: Taking control" << endl; |
---|
| 84 | orxout(verbose) << "This-pointer: " << this << endl; |
---|
| 85 | |
---|
| 86 | |
---|
[11167] | 87 | // Store the entity pointer in a private variable |
---|
[11165] | 88 | this->entity_ = this->player_->getControllableEntity(); |
---|
| 89 | assert(this->entity_); |
---|
[11167] | 90 | |
---|
| 91 | this->ctrlid_ = ctrlid; |
---|
[11183] | 92 | if (this->ctrlid_ == 0) |
---|
[11167] | 93 | { |
---|
[11183] | 94 | this->ctrlid_ = 1; |
---|
[11167] | 95 | } |
---|
[11165] | 96 | |
---|
[11167] | 97 | // Add the controller here to this entity. Apparently this still leaves |
---|
| 98 | // any preexisting human controllers in place. |
---|
| 99 | |
---|
[11165] | 100 | this->entity_->setDestroyWhenPlayerLeft(false); |
---|
| 101 | this->player_->stopTemporaryControl(); |
---|
| 102 | this->entity_->setController(this); |
---|
| 103 | this->setControllableEntity(this->entity_); |
---|
| 104 | this->entity_->mouseLook(); |
---|
[11204] | 105 | this->entity_->setVisible(true); |
---|
[11165] | 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 | { |
---|
[11167] | 113 | // Call the tick function of the classes we derive from |
---|
| 114 | SUPER(NewScriptController, tick, dt); |
---|
[11165] | 115 | |
---|
[11172] | 116 | |
---|
[11167] | 117 | // If this controller has no entity entry, do nothing |
---|
[11165] | 118 | if( !(this->entity_) ) return; |
---|
| 119 | |
---|
[11190] | 120 | if(!this->taskList_.empty()) |
---|
[11178] | 121 | { |
---|
[11190] | 122 | orxout() << scTime_ << endl; |
---|
[11183] | 123 | |
---|
[11190] | 124 | orxout() << taskList_.size() << endl; |
---|
[11183] | 125 | |
---|
[11190] | 126 | |
---|
| 127 | if(this->taskList_.front()->getStartTime() < this->scTime_) |
---|
[11183] | 128 | { |
---|
[11190] | 129 | activeTasks_.push_back(this->taskList_.front()); |
---|
| 130 | this->taskList_.pop_front(); |
---|
[11183] | 131 | } |
---|
[11178] | 132 | } |
---|
[11183] | 133 | else |
---|
| 134 | { |
---|
[11190] | 135 | orxout() << "no tasks in taskList_" << endl; |
---|
[11183] | 136 | } |
---|
[11190] | 137 | |
---|
| 138 | std::vector<Task*>::iterator it = activeTasks_.begin(); |
---|
| 139 | while (it != activeTasks_.end() ) |
---|
[11183] | 140 | { |
---|
[11190] | 141 | if( !((*it)->update(dt)) ) |
---|
[11183] | 142 | { |
---|
[11190] | 143 | (*(*it)).destroyLater(); |
---|
| 144 | it = activeTasks_.erase(it); |
---|
[11183] | 145 | } |
---|
[11190] | 146 | else |
---|
| 147 | { |
---|
| 148 | it++; |
---|
| 149 | } |
---|
| 150 | |
---|
[11183] | 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
[11190] | 154 | |
---|
[11183] | 155 | scTime_ += dt; |
---|
[11165] | 156 | } |
---|
| 157 | |
---|
[11172] | 158 | void NewScriptController::debugOut(float startTime) |
---|
[11167] | 159 | { |
---|
[11183] | 160 | |
---|
[11190] | 161 | DebugTask* task = new DebugTask(context_); |
---|
| 162 | |
---|
| 163 | task->initialize(startTime); |
---|
| 164 | |
---|
| 165 | bool inserted = false; |
---|
| 166 | |
---|
[11183] | 167 | if(taskList_.empty()) |
---|
| 168 | { |
---|
| 169 | taskList_.push_front(task); |
---|
[11190] | 170 | inserted = true; |
---|
[11183] | 171 | } |
---|
| 172 | |
---|
| 173 | else |
---|
| 174 | { |
---|
[11190] | 175 | for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime |
---|
[11183] | 176 | { |
---|
[11190] | 177 | orxout() << "debugOutTask" << endl; |
---|
[11183] | 178 | |
---|
[11190] | 179 | if(task->getStartTime() < (*it)->getStartTime() ) |
---|
[11183] | 180 | { |
---|
| 181 | taskList_.insert(it, task); |
---|
[11190] | 182 | inserted = true; |
---|
| 183 | break; |
---|
[11183] | 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
[11190] | 187 | |
---|
| 188 | if (!inserted) |
---|
| 189 | { |
---|
| 190 | taskList_.push_back(task); |
---|
| 191 | } |
---|
| 192 | |
---|
[11167] | 193 | } |
---|
[11165] | 194 | |
---|
[11178] | 195 | void NewScriptController::stringOut(float startTime, std::string output) |
---|
| 196 | { |
---|
[11183] | 197 | |
---|
[11190] | 198 | stringOutTask* task = new stringOutTask(context_); |
---|
| 199 | |
---|
| 200 | task->initialize(startTime, output); |
---|
| 201 | |
---|
| 202 | bool inserted = false; |
---|
| 203 | |
---|
[11183] | 204 | if(taskList_.empty()) |
---|
| 205 | { |
---|
| 206 | taskList_.push_front(task); |
---|
[11190] | 207 | inserted = true; |
---|
[11183] | 208 | } |
---|
| 209 | |
---|
| 210 | else |
---|
| 211 | { |
---|
[11190] | 212 | for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime |
---|
[11183] | 213 | { |
---|
[11190] | 214 | orxout() << "stringOutTask" << endl; |
---|
[11183] | 215 | |
---|
[11190] | 216 | if(task->getStartTime() < (*it)->getStartTime() ) |
---|
[11183] | 217 | { |
---|
| 218 | taskList_.insert(it, task); |
---|
[11190] | 219 | inserted = true; |
---|
| 220 | break; |
---|
[11183] | 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
[11190] | 224 | |
---|
| 225 | if (!inserted) |
---|
| 226 | { |
---|
| 227 | taskList_.push_back(task); |
---|
| 228 | } |
---|
| 229 | |
---|
[11178] | 230 | } |
---|
| 231 | |
---|
[11204] | 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 | |
---|
[11172] | 271 | NewScriptController* NewScriptController::getNewScriptController() |
---|
[11167] | 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 | |
---|
[11165] | 293 | } |
---|