[10014] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ScriptController.h" |
---|
[10047] | 30 | #include "infos/PlayerInfo.h" |
---|
[10014] | 31 | #include "core/CoreIncludes.h" |
---|
[10028] | 32 | #include "worldentities/ControllableEntity.h" |
---|
[10045] | 33 | #include "core/LuaState.h" |
---|
[10034] | 34 | #include <cmath> |
---|
[10014] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[10057] | 38 | float scTime=0; /*initialize time, to coordinate eventTime*/ |
---|
[10048] | 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | std::vector<event> eventList; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
[10014] | 48 | RegisterClass(ScriptController); |
---|
| 49 | |
---|
[10028] | 50 | //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context) |
---|
| 51 | ScriptController::ScriptController(Context* context) : ArtificialController(context) |
---|
[10014] | 52 | { |
---|
| 53 | RegisterObject(ScriptController); |
---|
[10028] | 54 | //set_controlled(CE); |
---|
[10047] | 55 | this->ctrlid_ = 0; |
---|
[10014] | 56 | } |
---|
| 57 | |
---|
[10047] | 58 | void ScriptController::takeControl(int ctrlid) |
---|
[10014] | 59 | { |
---|
[10047] | 60 | orxout() << "ScriptController: Taking control" << endl; |
---|
| 61 | orxout() << "This-pointer: " << this << endl; |
---|
| 62 | this->ctrlid_ = ctrlid; |
---|
| 63 | this->entity_ = this->player_->getControllableEntity(); |
---|
| 64 | assert(this->entity_); |
---|
| 65 | |
---|
| 66 | this->entity_->setDestroyWhenPlayerLeft(false); |
---|
| 67 | this->player_->pauseControl(); |
---|
| 68 | this->entity_->setController(this); |
---|
| 69 | this->setControllableEntity(this->entity_); |
---|
[10014] | 70 | } |
---|
| 71 | |
---|
[10047] | 72 | /* Yet to be implemented and tested */ |
---|
| 73 | //void ScriptController::yieldControl() |
---|
| 74 | //{ |
---|
| 75 | //this->player_->startControl(this->entity_); |
---|
| 76 | //this->setActive(false); |
---|
| 77 | //this->controllableEntity_ = NULL; |
---|
| 78 | //} |
---|
| 79 | |
---|
[10014] | 80 | void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 81 | { |
---|
[10028] | 82 | //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode); |
---|
[10014] | 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
[10034] | 86 | const Vector3& ScriptController::getPosition() |
---|
[10020] | 87 | { |
---|
[10047] | 88 | return this->entity_->getPosition(); |
---|
[10028] | 89 | } |
---|
[10014] | 90 | |
---|
[10045] | 91 | ScriptController* ScriptController::getScriptController() |
---|
| 92 | { |
---|
[10046] | 93 | /* Output a message that confirms this function was called */ |
---|
[10045] | 94 | orxout() << "Great success!" << std::endl; |
---|
[10046] | 95 | |
---|
[10047] | 96 | /* Debugging: print all the scriptcontroller object pointers */ |
---|
[10045] | 97 | for(ObjectList<ScriptController>::iterator it = |
---|
| 98 | ObjectList<ScriptController>::begin(); |
---|
| 99 | it != ObjectList<ScriptController>::end(); ++it) |
---|
[10047] | 100 | { orxout() << "Have object in list: " << *it << endl; } |
---|
| 101 | |
---|
| 102 | /* Find the first one with a nonzero ID */ |
---|
| 103 | for(ObjectList<ScriptController>::iterator it = |
---|
| 104 | ObjectList<ScriptController>::begin(); |
---|
| 105 | it != ObjectList<ScriptController>::end(); ++it) |
---|
[10045] | 106 | { |
---|
| 107 | // TODO: do some selection here. Currently just returns the first one |
---|
[10047] | 108 | if( (*it)->getID() > 0 ) |
---|
| 109 | return *it; |
---|
[10045] | 110 | |
---|
| 111 | } |
---|
| 112 | return NULL; |
---|
| 113 | } |
---|
| 114 | |
---|
[10048] | 115 | void ScriptController::execute(event ev) |
---|
| 116 | { |
---|
| 117 | if(ev.fctName=="moveToPosition_beta") |
---|
| 118 | { |
---|
[10057] | 119 | |
---|
[10048] | 120 | moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
[10047] | 125 | void ScriptController::tick(float dt) |
---|
| 126 | { |
---|
[10059] | 127 | |
---|
| 128 | |
---|
| 129 | |
---|
[10047] | 130 | /* If this controller has no entity entry, do nothing */ |
---|
| 131 | if( !(this->entity_) ) |
---|
| 132 | return; |
---|
[10045] | 133 | |
---|
[10047] | 134 | //orxout() << "Rotating!" << endl; |
---|
[10045] | 135 | |
---|
[10047] | 136 | //this->entity_->rotateYaw(-1.0f * 100.0f * dt); |
---|
| 137 | //this->entity_->rotatePitch(0.8f * 100.0f); |
---|
| 138 | |
---|
[10056] | 139 | if(eventList.size() > 0 && eventList[0].eventTime<=scTime) |
---|
[10048] | 140 | { |
---|
| 141 | /*TO DO: execute the function: eventList[0].fctName*/ |
---|
| 142 | |
---|
[10057] | 143 | execute(eventList[0]); |
---|
[10048] | 144 | eventList.erase(eventList.begin()); |
---|
| 145 | } |
---|
| 146 | |
---|
[10047] | 147 | SUPER(ScriptController, tick, dt); |
---|
[10048] | 148 | |
---|
| 149 | scTime=scTime+dt; |
---|
[10047] | 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
[10048] | 153 | |
---|
| 154 | |
---|
[10034] | 155 | void ScriptController::moveToPosition_beta(float x, float y, float z ) |
---|
| 156 | { |
---|
[10059] | 157 | |
---|
| 158 | orxout()<<"moveToPosition_beta executed"<<endl; |
---|
[10047] | 159 | //const Vector3 local = this->getPosition(); |
---|
| 160 | const Vector3 target = Vector3(100*x,100*y,100*z); |
---|
| 161 | //Vector3 way = target-local; |
---|
| 162 | orxout() << "Moving This-pointer: " << this << endl; |
---|
[10038] | 163 | |
---|
[10046] | 164 | |
---|
[10059] | 165 | //this->entity_->lookAt(target); |
---|
| 166 | //this->entity_->moveFrontBack(-1000*target.length()); |
---|
[10034] | 167 | |
---|
[10059] | 168 | if(this->entity_!=NULL) |
---|
| 169 | orxout()<<"not-NULL-entity"<<endl; |
---|
| 170 | |
---|
| 171 | if(this->player_!=NULL) |
---|
| 172 | orxout()<<"not-NULL-player"<<endl; |
---|
| 173 | |
---|
| 174 | orxout() << this->player_->getClientID() << endl; // IMPOSSIBLE TO ACCESS this->player AND this->entity |
---|
| 175 | |
---|
| 176 | //this->entity_ = this->player_->getClientID();//getControllableEntity(); |
---|
| 177 | |
---|
| 178 | //if(this->entity_==this->player_->getControllableEntity()) |
---|
| 179 | //orxout()<<"same entity"<<endl; |
---|
| 180 | |
---|
[10046] | 181 | /* This works fine */ |
---|
| 182 | orxout()<<x<<" "<<y<<" "<<z<<endl; |
---|
[10034] | 183 | } |
---|
| 184 | |
---|
[10048] | 185 | void ScriptController::eventScheduler(std::string instruction, float x, float y, float z, float executionTime) |
---|
| 186 | { |
---|
[10057] | 187 | |
---|
[10059] | 188 | |
---|
[10048] | 189 | /*put data (from LUA) into time-sorted eventList*/ |
---|
| 190 | /*nimmt den befehl und die argumente aus luascript und ertellt einen struct pro event, diese structs werden sortiert nach eventTime*/ |
---|
| 191 | struct event tmp; |
---|
| 192 | tmp.fctName=instruction; |
---|
| 193 | tmp.xCoord=x; |
---|
| 194 | tmp.yCoord=y; |
---|
| 195 | tmp.zCoord=z; |
---|
| 196 | tmp.eventTime=executionTime; |
---|
[10047] | 197 | |
---|
[10059] | 198 | orxout()<<tmp.fctName<<endl; |
---|
| 199 | |
---|
| 200 | if(eventList.size()==0) |
---|
[10048] | 201 | { |
---|
[10059] | 202 | orxout()<<"eventList empty (01)"<<endl; |
---|
| 203 | eventList.insert(eventList.begin(), tmp); |
---|
| 204 | orxout()<<"first event added"<<endl; |
---|
| 205 | } |
---|
[10034] | 206 | |
---|
[10059] | 207 | |
---|
| 208 | for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++) |
---|
[10048] | 209 | { |
---|
[10014] | 210 | |
---|
[10059] | 211 | if(tmp.eventTime<it->eventTime) |
---|
| 212 | { |
---|
| 213 | eventList.insert(it,tmp); |
---|
| 214 | orxout()<<"new event added"<<endl; |
---|
| 215 | } |
---|
[10028] | 216 | |
---|
[10048] | 217 | } |
---|
[10028] | 218 | |
---|
[10059] | 219 | |
---|
| 220 | if(eventList.size()==0) |
---|
| 221 | orxout()<<"eventList empty"<<endl; |
---|
| 222 | |
---|
| 223 | else |
---|
| 224 | orxout()<<"eventList is not empty"<<endl; |
---|
| 225 | |
---|
[10048] | 226 | |
---|
| 227 | } |
---|
[10028] | 228 | |
---|
[10048] | 229 | |
---|
| 230 | |
---|
| 231 | /* TODO: struct event erweitern um mehr funktionen benutzen zu koennen |
---|
| 232 | |
---|
| 233 | mehr funktionen definieren (und dann in execute if(...)) |
---|
| 234 | NB: viele noetige funktionen sind schon in artificial- bzw formationcontroller vorhanden */ |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | |
---|
[10014] | 238 | } |
---|