[603] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1502] | 3 | * > www.orxonox.net < |
---|
[603] | 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 | * Benjamin Knecht, beni_at_orxonox.net |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
[1502] | 28 | |
---|
[1039] | 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "NPC.h" |
---|
[603] | 31 | |
---|
[1055] | 32 | #include "core/CoreIncludes.h" |
---|
[1747] | 33 | #include "core/Iterator.h" |
---|
[603] | 34 | |
---|
| 35 | namespace orxonox { |
---|
| 36 | |
---|
[627] | 37 | CreateFactory(NPC); |
---|
| 38 | |
---|
[1293] | 39 | NPC::NPC() : |
---|
| 40 | movable_(true) |
---|
[603] | 41 | { |
---|
[627] | 42 | RegisterObject(NPC); |
---|
[1293] | 43 | registerAllVariables(); |
---|
[603] | 44 | } |
---|
| 45 | |
---|
| 46 | NPC::~NPC() |
---|
| 47 | { |
---|
| 48 | } |
---|
| 49 | |
---|
[617] | 50 | /** |
---|
| 51 | * function to chance values of an element |
---|
| 52 | */ |
---|
| 53 | void NPC::setValues(Vector3 location, Vector3 speed, Vector3 acceleration, bool movable) { |
---|
| 54 | this->setAcceleration(acceleration); |
---|
| 55 | this->setVelocity(speed); |
---|
| 56 | this->translate(location); |
---|
| 57 | movable_ = movable; |
---|
| 58 | } |
---|
[1747] | 59 | |
---|
[1293] | 60 | void NPC::registerAllVariables(){ |
---|
| 61 | Model::registerAllVariables(); |
---|
| 62 | registerVar(&movable_, sizeof(movable_), network::DATA); |
---|
| 63 | } |
---|
[617] | 64 | |
---|
[1747] | 65 | |
---|
[617] | 66 | /** |
---|
| 67 | * calculates the distance between the element and an other point given by temp |
---|
| 68 | */ |
---|
[627] | 69 | float NPC::getDistance(WorldEntity* temp) |
---|
[617] | 70 | { |
---|
| 71 | Vector3 distance = temp->getPosition() - this->getPosition(); |
---|
| 72 | return distance.length(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * updates the data of an element |
---|
| 77 | */ |
---|
[619] | 78 | void NPC::update() |
---|
[617] | 79 | { |
---|
| 80 | |
---|
| 81 | //if element is movable, calculate acceleration |
---|
[627] | 82 | if (this->movable_ == true) calculateAcceleration(); |
---|
[617] | 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
[619] | 87 | * tick this NPC |
---|
| 88 | */ |
---|
| 89 | void NPC::tick(float dt) |
---|
| 90 | { |
---|
[1021] | 91 | update(); |
---|
[627] | 92 | this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt); |
---|
| 93 | this->translate(this->getVelocity()*dt); |
---|
| 94 | this->setAcceleration(Vector3(0,0,0)); |
---|
[619] | 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
[617] | 98 | * calculates the new acceleration of an element |
---|
| 99 | */ |
---|
[627] | 100 | void NPC::calculateAcceleration() |
---|
[617] | 101 | { |
---|
| 102 | //acceleration consisting of flocking-functions |
---|
[627] | 103 | this->setAcceleration(separation() + alignment() + cohesion()); |
---|
[617] | 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * separation-function (keep elements separated, avoid crashs) |
---|
| 108 | */ |
---|
[627] | 109 | Vector3 NPC::separation() |
---|
[617] | 110 | { |
---|
| 111 | Vector3 steering = Vector3(0,0,0); //steeringvector |
---|
| 112 | Vector3 inverseDistance = Vector3(0,0,0); //vector pointing away from possible collisions |
---|
| 113 | int numberOfNeighbour = 0; //number of observed neighbours |
---|
| 114 | float distance = 0; // distance to the actual element |
---|
[1747] | 115 | for(ObjectList<WorldEntity>::iterator it = ObjectList<WorldEntity>::begin(); it; ++it) { //go through all elements |
---|
[627] | 116 | distance = getDistance(*it); //get distance between this and actual |
---|
[617] | 117 | if ((distance > 0) && (distance < SEPERATIONDISTANCE)) { //do only if actual is inside detectionradius |
---|
| 118 | inverseDistance = Vector3(0,0,0); |
---|
[627] | 119 | inverseDistance = this->getPosition() - it->getPosition(); //calculate the distancevector heading towards this |
---|
[617] | 120 | //adaptation of the inverseDistance to the distance |
---|
| 121 | if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;} |
---|
| 122 | if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;} |
---|
| 123 | if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;} |
---|
| 124 | if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;} |
---|
| 125 | steering = steering + inverseDistance; //add up all significant steeringvectors |
---|
| 126 | numberOfNeighbour++; //counts the elements inside the detectionradius |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> separation steeringvector |
---|
| 130 | return steering; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * alignment-function (lead elements to the same heading) |
---|
| 135 | */ |
---|
[627] | 136 | Vector3 NPC::alignment() |
---|
[617] | 137 | { |
---|
| 138 | Vector3 steering = Vector3(0,0,0); //steeringvector |
---|
| 139 | int numberOfNeighbour = 0; //number of observed neighbours |
---|
| 140 | //float distance = 0; |
---|
| 141 | //go through all elements |
---|
[1747] | 142 | for(ObjectList<NPC>::iterator it = ObjectList<NPC>::begin(); it; ++it) { //just working with 3 elements at the moment |
---|
[627] | 143 | float distance = getDistance(*it); //get distance between this and actual |
---|
[617] | 144 | if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) { //check if actual element is inside detectionradius |
---|
[627] | 145 | steering = steering + it->getVelocity(); //add up all speedvectors inside the detectionradius |
---|
[617] | 146 | numberOfNeighbour++; //counts the elements inside the detectionradius |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> alignment steeringvector |
---|
| 150 | return steering; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * cohseion-function (keep elements close to each other) |
---|
| 155 | */ |
---|
[627] | 156 | Vector3 NPC::cohesion() |
---|
[617] | 157 | { |
---|
| 158 | Vector3 steering = Vector3(0,0,0); //steeringvector |
---|
| 159 | int numberOfNeighbour = 0; //number of observed neighbours |
---|
| 160 | //float distance = 0; |
---|
| 161 | //go through all elements |
---|
[1747] | 162 | for(ObjectList<NPC>::iterator it = ObjectList<NPC>::begin(); it; ++it) { //just working with 3 elements at the moment |
---|
[627] | 163 | float distance = getDistance(*it); //get distance between this and actual |
---|
[617] | 164 | if ((distance > 0) && (distance < COHESIONDISTANCE)) { //check if actual element is inside detectionradius |
---|
[627] | 165 | steering = steering + it->getPosition(); //add up all locations of elements inside the detectionradius |
---|
[617] | 166 | numberOfNeighbour++; //counts the elements inside the detectionradius |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | if(numberOfNeighbour > 0) { |
---|
| 170 | steering = steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector |
---|
| 171 | steering = steering - this->getPosition(); //transform the vector for the ship |
---|
| 172 | } |
---|
| 173 | return steering; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | } // end of class NPC |
---|