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 | * Johannes Ritz |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Dynamicmatch.h" |
---|
30 | |
---|
31 | #include "util/Convert.h" |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "network/Host.h" |
---|
34 | #include "infos/PlayerInfo.h" |
---|
35 | #include "worldentities/pawns/Pawn.h" |
---|
36 | #include "worldentities/pawns/SpaceShip.h" |
---|
37 | #include "core/ConfigValueIncludes.h" |
---|
38 | #include "interfaces/TeamColourable.h" |
---|
39 | #include "items/Engine.h" |
---|
40 | #include "tools/Timer.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | CreateUnloadableFactory(Dynamicmatch); |
---|
45 | |
---|
46 | Dynamicmatch::Dynamicmatch(BaseObject* creator) : Gametype(creator) |
---|
47 | { |
---|
48 | RegisterObject(Dynamicmatch); |
---|
49 | this->gameTime_ = 180; |
---|
50 | this->setConfigValues(); |
---|
51 | this->chaser=0; |
---|
52 | this->piggy=1; |
---|
53 | this->onlyChasers=true; |
---|
54 | this->gameEnded_ =false; |
---|
55 | this->timesequence_ = static_cast<int>(this->gameTime_); |
---|
56 | this->friendlyfire=true; |
---|
57 | } |
---|
58 | |
---|
59 | void Dynamicmatch::setConfigValues() |
---|
60 | { |
---|
61 | SetConfigValue(gameTime_, 180);//just for test cases |
---|
62 | SetConfigValue(friendlyfire, true); |
---|
63 | static ColourValue colours[] = |
---|
64 | { |
---|
65 | ColourValue(0.3f, 0.3f, 1.0f), |
---|
66 | ColourValue(1.0f, 0.3f, 0.3f) |
---|
67 | |
---|
68 | }; |
---|
69 | static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue)); |
---|
70 | |
---|
71 | SetConfigValue(partyColours_, defaultcolours); |
---|
72 | } |
---|
73 | |
---|
74 | void Dynamicmatch::setPlayerColour(PlayerInfo* player) // not sure if this is the right place - helper function |
---|
75 | { |
---|
76 | std::map<PlayerInfo*, int>::const_iterator it_player = this->playerParty_.find(player); |
---|
77 | Pawn* pawn = dynamic_cast<Pawn*>(player->getControllableEntity()); |
---|
78 | if (pawn) |
---|
79 | { |
---|
80 | pawn->setRadarObjectColour(this->partyColours_[it_player->second]); //does this work? //what about playerParty_[it_player] instead of it_player->second |
---|
81 | |
---|
82 | std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects(); |
---|
83 | for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it) |
---|
84 | { |
---|
85 | if ((*it)->isA(Class(TeamColourable)))//not sure if this works |
---|
86 | { |
---|
87 | TeamColourable* tc = orxonox_cast<TeamColourable*>(*it); |
---|
88 | tc->setTeamColour(this->partyColours_[it_player->second]); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | int Dynamicmatch::getParty(PlayerInfo* player) // helper function for ArtificialController |
---|
96 | { |
---|
97 | return this->playerParty_[player]; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | bool Dynamicmatch::allowPawnDamage(Pawn* victim, Pawn* originator)//tested - works fine |
---|
102 | { |
---|
103 | |
---|
104 | if (victim && victim->getPlayer()) |
---|
105 | { |
---|
106 | //Case 1: a chaser hits piggy // BUG: playerParty_[originator->getPlayer()]==chaser) is true even if victim is a chaser |
---|
107 | if ((!onlyChasers)&&(playerParty_[originator->getPlayer()]==chaser)&&playerParty_[victim->getPlayer()]==piggy) { |
---|
108 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(originator->getPlayer()); |
---|
109 | if (it != this->players_.end()) |
---|
110 | { |
---|
111 | it->second.frags_++; |
---|
112 | } |
---|
113 | } |
---|
114 | //Case 2: piggy hits chaser |
---|
115 | else if ((!onlyChasers)&&(playerParty_[originator->getPlayer()]==piggy)){ |
---|
116 | //partyswitch: victim bcomes piggy and the orginator(piggy) becomes chaser |
---|
117 | playerParty_[victim->getPlayer()]=piggy; |
---|
118 | playerParty_[originator->getPlayer()]=chaser; |
---|
119 | //announce |
---|
120 | const std::string& messageVictim = victim->getPlayer()->getName() + " is victim"; |
---|
121 | COUT(0) << messageVictim << std::endl; |
---|
122 | Host::Broadcast(messageVictim); |
---|
123 | |
---|
124 | //party switch -> colour switch |
---|
125 | setPlayerColour(victim->getPlayer()); //victim colour |
---|
126 | setPlayerColour(originator->getPlayer());//orginator colour |
---|
127 | |
---|
128 | //Give new pig boost |
---|
129 | SpaceShip* spaceship = dynamic_cast<SpaceShip*>(victim); |
---|
130 | if (spaceship && spaceship->getEngine()) |
---|
131 | { |
---|
132 | spaceship->getEngine()->setSpeedFactor(5); |
---|
133 | WeakPtr<Engine>* ptr = new WeakPtr<Engine>(spaceship->getEngine()); |
---|
134 | new Timer(10, false, &createExecutor(createFunctor(&Dynamicmatch::resetSpeedFactor, this))->setDefaultValue(0, ptr), true); |
---|
135 | } |
---|
136 | |
---|
137 | } |
---|
138 | //Case 3: there are only chasers -> new piggy is needed |
---|
139 | else if (onlyChasers){ |
---|
140 | //Rollenzuweisung victim wird piggy |
---|
141 | playerParty_[victim->getPlayer()]=piggy; |
---|
142 | onlyChasers=false; |
---|
143 | setPlayerColour(victim->getPlayer()); //victim colour |
---|
144 | |
---|
145 | //Give new pig boost |
---|
146 | SpaceShip* spaceship = dynamic_cast<SpaceShip*>(victim); |
---|
147 | if (spaceship && spaceship->getEngine()) |
---|
148 | { |
---|
149 | spaceship->getEngine()->setSpeedFactor(5); |
---|
150 | WeakPtr<Engine>* ptr = new WeakPtr<Engine>(spaceship->getEngine()); |
---|
151 | new Timer(10, false, &createExecutor(createFunctor(&Dynamicmatch::resetSpeedFactor, this))->setDefaultValue(0, ptr), true); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | std::string message("First victim."); |
---|
156 | COUT(0) << message << std::endl; |
---|
157 | Host::Broadcast(message); |
---|
158 | } |
---|
159 | else if (friendlyfire) |
---|
160 | { |
---|
161 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(originator->getPlayer()); |
---|
162 | if (it != this->players_.end()) |
---|
163 | { |
---|
164 | it->second.frags_--; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | return false; |
---|
170 | } |
---|
171 | |
---|
172 | void Dynamicmatch::resetSpeedFactor(WeakPtr<Engine>* ptr) |
---|
173 | { |
---|
174 | if (*ptr) |
---|
175 | { |
---|
176 | (*ptr)->setSpeedFactor(1.0f); |
---|
177 | } |
---|
178 | delete ptr; |
---|
179 | } |
---|
180 | |
---|
181 | bool Dynamicmatch::allowPawnDeath(Pawn* victim, Pawn* originator)// |
---|
182 | { |
---|
183 | return false; |
---|
184 | } |
---|
185 | |
---|
186 | void Dynamicmatch::start() |
---|
187 | { |
---|
188 | Gametype::start(); |
---|
189 | |
---|
190 | std::string message("Don't be a victim!"); |
---|
191 | COUT(0) << message << std::endl; |
---|
192 | Host::Broadcast(message); |
---|
193 | |
---|
194 | } |
---|
195 | |
---|
196 | void Dynamicmatch::end() |
---|
197 | { |
---|
198 | Gametype::end(); |
---|
199 | |
---|
200 | std::string message("Time out. Press F2 to see the soreboard"); |
---|
201 | COUT(0) << message << std::endl; |
---|
202 | Host::Broadcast(message); |
---|
203 | /*for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
204 | { |
---|
205 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
206 | continue; |
---|
207 | |
---|
208 | if (it->second == 1) |
---|
209 | this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID()); |
---|
210 | else |
---|
211 | this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID()); |
---|
212 | }*/ |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | void Dynamicmatch::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) //set party + colouring |
---|
217 | { |
---|
218 | if (!player) |
---|
219 | return; |
---|
220 | playerParty_[player]=chaser;//playerparty |
---|
221 | Dynamicmatch::setPlayerColour(player); //Set playercolour |
---|
222 | } |
---|
223 | |
---|
224 | void Dynamicmatch::playerEntered(PlayerInfo* player) //standardfunction |
---|
225 | { |
---|
226 | |
---|
227 | if (!player)// only for safety |
---|
228 | return; |
---|
229 | |
---|
230 | Gametype::playerEntered(player); |
---|
231 | |
---|
232 | const std::string& message = player->getName() + " entered the game"; |
---|
233 | COUT(0) << message << std::endl; |
---|
234 | Host::Broadcast(message); |
---|
235 | } |
---|
236 | |
---|
237 | bool Dynamicmatch::playerLeft(PlayerInfo* player) //standardfunction |
---|
238 | { |
---|
239 | bool valid_player = Gametype::playerLeft(player); |
---|
240 | |
---|
241 | |
---|
242 | if (valid_player) |
---|
243 | { |
---|
244 | const std::string& message = player->getName() + " left the game"; |
---|
245 | COUT(0) << message << std::endl; |
---|
246 | Host::Broadcast(message); |
---|
247 | //remove player from map |
---|
248 | playerParty_.erase (player); |
---|
249 | } |
---|
250 | |
---|
251 | return valid_player; |
---|
252 | } |
---|
253 | |
---|
254 | bool Dynamicmatch::playerChangedName(PlayerInfo* player) //standardfunction |
---|
255 | { |
---|
256 | bool valid_player = Gametype::playerChangedName(player); |
---|
257 | |
---|
258 | if (valid_player) |
---|
259 | { |
---|
260 | const std::string& message = player->getOldName() + " changed name to " + player->getName(); |
---|
261 | COUT(0) << message << std::endl; |
---|
262 | Host::Broadcast(message); |
---|
263 | } |
---|
264 | |
---|
265 | return valid_player; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | void Dynamicmatch::tick(float dt) |
---|
271 | { |
---|
272 | SUPER(Dynamicmatch, tick, dt);//TODO - was bedeutet diese Zeile |
---|
273 | |
---|
274 | if (this->hasStarted() && !gameEnded_) |
---|
275 | { |
---|
276 | gameTime_ = gameTime_ - dt; |
---|
277 | if (gameTime_<= 0) |
---|
278 | { |
---|
279 | this->gameEnded_ = true; |
---|
280 | this->end(); |
---|
281 | } |
---|
282 | if ( gameTime_ <= timesequence_ && gameTime_ > 0) |
---|
283 | { |
---|
284 | const std::string& message = multi_cast<std::string>(timesequence_) + " seconds left!"; |
---|
285 | |
---|
286 | this->gtinfo_->sendAnnounceMessage(message); |
---|
287 | |
---|
288 | if (timesequence_ >= 30 && timesequence_ <= 60) |
---|
289 | { |
---|
290 | timesequence_ = timesequence_ - 10; |
---|
291 | } |
---|
292 | else if (timesequence_ <= 30) |
---|
293 | { |
---|
294 | timesequence_ = timesequence_ - 5; |
---|
295 | } |
---|
296 | else |
---|
297 | { |
---|
298 | timesequence_ = timesequence_ - 30; |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|