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