[603] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Benjamin Knecht, beni_at_orxonox.net |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[977] | 28 | #include "OrxonoxStableHeaders.h" |
---|
| 29 | |
---|
[708] | 30 | #include "../core/CoreIncludes.h" |
---|
[603] | 31 | #include "NPC.h" |
---|
| 32 | |
---|
| 33 | namespace orxonox { |
---|
| 34 | |
---|
[627] | 35 | CreateFactory(NPC); |
---|
| 36 | |
---|
[603] | 37 | NPC::NPC() |
---|
| 38 | { |
---|
[627] | 39 | RegisterObject(NPC); |
---|
[617] | 40 | movable_ = true; |
---|
[977] | 41 | registerAllVariables(); |
---|
[603] | 42 | } |
---|
| 43 | |
---|
| 44 | NPC::~NPC() |
---|
| 45 | { |
---|
| 46 | } |
---|
| 47 | |
---|
[627] | 48 | void NPC::loadParams(TiXmlElement* xmlElem) |
---|
| 49 | { |
---|
| 50 | Model::loadParams(xmlElem); |
---|
| 51 | } |
---|
| 52 | |
---|
[617] | 53 | /** |
---|
| 54 | * function to chance values of an element |
---|
| 55 | */ |
---|
| 56 | void NPC::setValues(Vector3 location, Vector3 speed, Vector3 acceleration, bool movable) { |
---|
| 57 | this->setAcceleration(acceleration); |
---|
| 58 | this->setVelocity(speed); |
---|
| 59 | this->translate(location); |
---|
| 60 | movable_ = movable; |
---|
| 61 | } |
---|
[977] | 62 | |
---|
| 63 | void NPC::registerAllVariables(){ |
---|
| 64 | Model::registerAllVariables(); |
---|
| 65 | registerVar(&movable_, sizeof(movable_), network::DATA); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | bool NPC::create(){ |
---|
| 69 | Model::create(); |
---|
| 70 | return true; |
---|
| 71 | } |
---|
[617] | 72 | |
---|
| 73 | /** |
---|
| 74 | * calculates the distance between the element and an other point given by temp |
---|
| 75 | */ |
---|
[627] | 76 | float NPC::getDistance(WorldEntity* temp) |
---|
[617] | 77 | { |
---|
| 78 | Vector3 distance = temp->getPosition() - this->getPosition(); |
---|
| 79 | return distance.length(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * updates the data of an element |
---|
| 84 | */ |
---|
[619] | 85 | void NPC::update() |
---|
[617] | 86 | { |
---|
| 87 | |
---|
| 88 | //if element is movable, calculate acceleration |
---|
[627] | 89 | if (this->movable_ == true) calculateAcceleration(); |
---|
[617] | 90 | |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
[619] | 94 | * tick this NPC |
---|
| 95 | */ |
---|
| 96 | void NPC::tick(float dt) |
---|
| 97 | { |
---|
[919] | 98 | update(); |
---|
[627] | 99 | this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt); |
---|
| 100 | this->translate(this->getVelocity()*dt); |
---|
| 101 | this->setAcceleration(Vector3(0,0,0)); |
---|
[619] | 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
[617] | 105 | * calculates the new acceleration of an element |
---|
| 106 | */ |
---|
[627] | 107 | void NPC::calculateAcceleration() |
---|
[617] | 108 | { |
---|
| 109 | //acceleration consisting of flocking-functions |
---|
[627] | 110 | this->setAcceleration(separation() + alignment() + cohesion()); |
---|
[617] | 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * separation-function (keep elements separated, avoid crashs) |
---|
| 115 | */ |
---|
[627] | 116 | Vector3 NPC::separation() |
---|
[617] | 117 | { |
---|
| 118 | Vector3 steering = Vector3(0,0,0); //steeringvector |
---|
| 119 | Vector3 inverseDistance = Vector3(0,0,0); //vector pointing away from possible collisions |
---|
| 120 | int numberOfNeighbour = 0; //number of observed neighbours |
---|
| 121 | float distance = 0; // distance to the actual element |
---|
[627] | 122 | for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::start(); it; ++it) { //go through all elements |
---|
| 123 | distance = getDistance(*it); //get distance between this and actual |
---|
[617] | 124 | if ((distance > 0) && (distance < SEPERATIONDISTANCE)) { //do only if actual is inside detectionradius |
---|
| 125 | inverseDistance = Vector3(0,0,0); |
---|
[627] | 126 | inverseDistance = this->getPosition() - it->getPosition(); //calculate the distancevector heading towards this |
---|
[617] | 127 | //adaptation of the inverseDistance to the distance |
---|
| 128 | if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;} |
---|
| 129 | if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;} |
---|
| 130 | if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;} |
---|
| 131 | if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;} |
---|
| 132 | steering = steering + inverseDistance; //add up all significant steeringvectors |
---|
| 133 | numberOfNeighbour++; //counts the elements inside the detectionradius |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> separation steeringvector |
---|
| 137 | return steering; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * alignment-function (lead elements to the same heading) |
---|
| 142 | */ |
---|
[627] | 143 | Vector3 NPC::alignment() |
---|
[617] | 144 | { |
---|
| 145 | Vector3 steering = Vector3(0,0,0); //steeringvector |
---|
| 146 | int numberOfNeighbour = 0; //number of observed neighbours |
---|
| 147 | //float distance = 0; |
---|
| 148 | //go through all elements |
---|
[627] | 149 | for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) { //just working with 3 elements at the moment |
---|
| 150 | float distance = getDistance(*it); //get distance between this and actual |
---|
[617] | 151 | if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) { //check if actual element is inside detectionradius |
---|
[627] | 152 | steering = steering + it->getVelocity(); //add up all speedvectors inside the detectionradius |
---|
[617] | 153 | numberOfNeighbour++; //counts the elements inside the detectionradius |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> alignment steeringvector |
---|
| 157 | return steering; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * cohseion-function (keep elements close to each other) |
---|
| 162 | */ |
---|
[627] | 163 | Vector3 NPC::cohesion() |
---|
[617] | 164 | { |
---|
| 165 | Vector3 steering = Vector3(0,0,0); //steeringvector |
---|
| 166 | int numberOfNeighbour = 0; //number of observed neighbours |
---|
| 167 | //float distance = 0; |
---|
| 168 | //go through all elements |
---|
[627] | 169 | for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) { //just working with 3 elements at the moment |
---|
| 170 | float distance = getDistance(*it); //get distance between this and actual |
---|
[617] | 171 | if ((distance > 0) && (distance < COHESIONDISTANCE)) { //check if actual element is inside detectionradius |
---|
[627] | 172 | steering = steering + it->getPosition(); //add up all locations of elements inside the detectionradius |
---|
[617] | 173 | numberOfNeighbour++; //counts the elements inside the detectionradius |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | if(numberOfNeighbour > 0) { |
---|
| 177 | steering = steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector |
---|
| 178 | steering = steering - this->getPosition(); //transform the vector for the ship |
---|
| 179 | } |
---|
| 180 | return steering; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | } // end of class NPC |
---|