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 | * Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "WingmanController.h" |
---|
30 | |
---|
31 | |
---|
32 | namespace orxonox |
---|
33 | { |
---|
34 | |
---|
35 | RegisterClass(WingmanController); |
---|
36 | |
---|
37 | |
---|
38 | WingmanController::WingmanController(Context* context) : CommonController(context) |
---|
39 | { |
---|
40 | RegisterObject(WingmanController); |
---|
41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
42 | this->myLeader_ = 0; |
---|
43 | } |
---|
44 | |
---|
45 | WingmanController::~WingmanController() |
---|
46 | { |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | CommonController* WingmanController::findNewLeader() |
---|
51 | { |
---|
52 | |
---|
53 | if (!this->getControllableEntity()) |
---|
54 | return 0; |
---|
55 | |
---|
56 | CommonController* closestLeader = 0; |
---|
57 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
58 | |
---|
59 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
60 | { |
---|
61 | //0ptr? |
---|
62 | if (!it || !it->isLeader() || !(it->getControllableEntity())) |
---|
63 | continue; |
---|
64 | //same team? |
---|
65 | if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) |
---|
66 | continue; |
---|
67 | //is equal to this? |
---|
68 | if (it->getControllableEntity() == this->getControllableEntity()) |
---|
69 | continue; |
---|
70 | |
---|
71 | float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
72 | if (distance < minDistance && !(it->hasWingman())) |
---|
73 | { |
---|
74 | closestLeader = *it; |
---|
75 | minDistance = distance; |
---|
76 | } |
---|
77 | |
---|
78 | } |
---|
79 | if (closestLeader) |
---|
80 | { |
---|
81 | if (closestLeader->setWingman(this)) |
---|
82 | return closestLeader; |
---|
83 | } |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | void WingmanController::action() |
---|
88 | { |
---|
89 | if (!this->myLeader_) |
---|
90 | { |
---|
91 | CommonController* newLeader = findNewLeader(); |
---|
92 | this->myLeader_ = newLeader; |
---|
93 | if (newLeader) |
---|
94 | orxout(internal_error) << "new Leader set" << endl; |
---|
95 | else |
---|
96 | orxout(internal_error) << "0 leader" << endl; |
---|
97 | |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | |
---|
102 | } |
---|
103 | } |
---|
104 | /*//collect data for AI behaviour |
---|
105 | Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0); |
---|
106 | Vector3* meanOfAlliesPtr = new Vector3(0.0,0.0,0.0); |
---|
107 | Vector3 meanOfAllies = *meanOfAlliesPtr; |
---|
108 | Vector3 meanOfEnemies = *meanOfEnemiesPtr; |
---|
109 | |
---|
110 | |
---|
111 | for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it) |
---|
112 | { |
---|
113 | |
---|
114 | Gametype* gt=this->getGametype(); |
---|
115 | if (!gt) |
---|
116 | { |
---|
117 | gt=it->getGametype(); |
---|
118 | } |
---|
119 | if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt)) |
---|
120 | { |
---|
121 | enemies_.push_back(*it); |
---|
122 | } |
---|
123 | else { |
---|
124 | allies_.push_back(*it); |
---|
125 | } |
---|
126 | } |
---|
127 | if (enemies_.size() != 0 && allies_.size() != 0){ |
---|
128 | for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it) |
---|
129 | meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition(); |
---|
130 | |
---|
131 | meanOfEnemies /= enemies_.size(); |
---|
132 | |
---|
133 | for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it) |
---|
134 | meanOfAllies += (*it)->getControllableEntity()->getWorldPosition(); |
---|
135 | |
---|
136 | meanOfAllies /= allies_.size(); |
---|
137 | |
---|
138 | //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl; |
---|
139 | orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl; |
---|
140 | orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl; |
---|
141 | orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl; |
---|
142 | }*/ |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | void WingmanController::tick(float dt) |
---|
147 | { |
---|
148 | //------------------------------------------------------- |
---|
149 | |
---|
150 | |
---|
151 | if (!this->isActive()) |
---|
152 | return; |
---|
153 | //--------------------------Stay in formation-------------------------- |
---|
154 | if (this->bHasTargetPosition_) |
---|
155 | { |
---|
156 | //targetPosition_ and targetOrientation_ are set by the Leader in its action() |
---|
157 | this->moveToTargetPosition(); |
---|
158 | } |
---|
159 | |
---|
160 | //--------------------------Attack same target as the Leader-------------------------- |
---|
161 | |
---|
162 | /*if (this->target_) |
---|
163 | { |
---|
164 | this->aimAtTarget(); |
---|
165 | this->doFire(); |
---|
166 | } |
---|
167 | */ |
---|
168 | |
---|
169 | //orxout(internal_error) << "I am " << this << endl; |
---|
170 | |
---|
171 | |
---|
172 | SUPER(WingmanController, tick, dt); |
---|
173 | } |
---|
174 | |
---|
175 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
176 | { |
---|
177 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
178 | |
---|
179 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | } |
---|