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 "LastManStanding.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "network/Host.h" |
---|
33 | #include "infos/PlayerInfo.h" |
---|
34 | #include "worldentities/pawns/Pawn.h" |
---|
35 | #include "core/ConfigValueIncludes.h" |
---|
36 | #include "util/Convert.h" |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | CreateUnloadableFactory(LastManStanding); |
---|
41 | |
---|
42 | LastManStanding::LastManStanding(BaseObject* creator) : Deathmatch(creator) |
---|
43 | { |
---|
44 | RegisterObject(LastManStanding); |
---|
45 | this->bForceSpawn_=true; |
---|
46 | this->lives=4; |
---|
47 | this->playersAlive=0; |
---|
48 | this->timeRemaining=20.0f; |
---|
49 | this->setHUDTemplate("LastmanstandingHUD"); |
---|
50 | } |
---|
51 | |
---|
52 | void LastManStanding::spawnDeadPlayersIfRequested() |
---|
53 | { |
---|
54 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
55 | if (it->second.state_ == PlayerState::Dead) |
---|
56 | { |
---|
57 | bool alive = (0<playerLives_[it->first]); |
---|
58 | if (alive&&(it->first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
59 | this->spawnPlayer(it->first); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void LastManStanding::setConfigValues() |
---|
65 | { |
---|
66 | SetConfigValue(lives, 4); |
---|
67 | SetConfigValue(timeRemaining, 20.0f); |
---|
68 | } |
---|
69 | |
---|
70 | bool LastManStanding::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
71 | { |
---|
72 | if (originator && originator->getPlayer())// only for safety |
---|
73 | { |
---|
74 | this->timeToAct_[originator->getPlayer()]=timeRemaining; |
---|
75 | } |
---|
76 | return true; |
---|
77 | } |
---|
78 | |
---|
79 | bool LastManStanding::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
80 | { |
---|
81 | if (!victim||!victim->getPlayer())// only for safety |
---|
82 | return true; |
---|
83 | playerLives_[victim->getPlayer()]=playerLives_[victim->getPlayer()]-1; |
---|
84 | if (playerLives_[victim->getPlayer()]<=0)//if player lost all lives |
---|
85 | { |
---|
86 | this->playersAlive--; |
---|
87 | const std::string& message = victim->getPlayer()->getName() + " has lost all lives"; |
---|
88 | COUT(0) << message << std::endl; |
---|
89 | Host::Broadcast(message); |
---|
90 | } |
---|
91 | return true; |
---|
92 | } |
---|
93 | |
---|
94 | void LastManStanding::end() |
---|
95 | { |
---|
96 | Gametype::end(); |
---|
97 | |
---|
98 | for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it) |
---|
99 | { |
---|
100 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
101 | continue; |
---|
102 | |
---|
103 | if (it->second > 0) |
---|
104 | this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID()); |
---|
105 | else |
---|
106 | this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID()); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | void LastManStanding::playerEntered(PlayerInfo* player) |
---|
111 | { |
---|
112 | if (!player)// only for safety |
---|
113 | return; |
---|
114 | Deathmatch::playerEntered(player); |
---|
115 | |
---|
116 | playerLives_[player]=lives; |
---|
117 | this->playersAlive++; |
---|
118 | this->timeToAct_[player]=timeRemaining; |
---|
119 | //Update: EachPlayer's "Players in Game"-HUD |
---|
120 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
121 | { |
---|
122 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
123 | continue; |
---|
124 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
125 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | bool LastManStanding::playerLeft(PlayerInfo* player) |
---|
130 | { |
---|
131 | bool valid_player = Deathmatch::playerLeft(player); |
---|
132 | if (valid_player) |
---|
133 | { |
---|
134 | this->playersAlive--; |
---|
135 | //this->playerLives_[player].erase (player); not necessary? |
---|
136 | //Update: EachPlayer's "Players in Game"-HUD |
---|
137 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
138 | { |
---|
139 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
140 | continue; |
---|
141 | const std::string& message1 = "Remaining Players: "+ multi_cast<std::string>(playersAlive); |
---|
142 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | return valid_player; |
---|
147 | } |
---|
148 | |
---|
149 | void LastManStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
150 | { |
---|
151 | if (!player) |
---|
152 | return; |
---|
153 | this->timeToAct_[player]=timeRemaining+3.0f;//reset timer |
---|
154 | //Update: Individual Players "lifes"-HUD |
---|
155 | std::map<PlayerInfo*, Player>::iterator it2 = this->players_.find(player); |
---|
156 | if (it2 != this->players_.end()) |
---|
157 | { |
---|
158 | const std::string& message = "Your Lives: " +multi_cast<std::string>(playerLives_[player]); |
---|
159 | this->gtinfo_->sendFadingMessage(message,it2->first->getClientID()); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | void LastManStanding::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
164 | { |
---|
165 | //Update: EachPlayer's "Players in Game"-HUD |
---|
166 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
167 | { |
---|
168 | if (it->first->getClientID() == CLIENTID_UNKNOWN) |
---|
169 | continue; |
---|
170 | const std::string& message1 = "Remaining Players : "+ multi_cast<std::string>(playersAlive); |
---|
171 | this->gtinfo_->sendStaticMessage(message1,it->first->getClientID(),ColourValue(1.0f, 1.0f, 0.5f)); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | const int LastManStanding::playerGetLives(PlayerInfo* player) |
---|
176 | { |
---|
177 | if (player) |
---|
178 | return playerLives_[player]; |
---|
179 | else |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | void LastManStanding::killPlayer(PlayerInfo* player) |
---|
184 | { |
---|
185 | if(!player) |
---|
186 | return; |
---|
187 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
188 | if (it != this->players_.end()) |
---|
189 | { |
---|
190 | if(!player->getControllableEntity()) |
---|
191 | {return;} |
---|
192 | Pawn* pawn = dynamic_cast<Pawn*>(player->getControllableEntity()); |
---|
193 | if(!pawn) |
---|
194 | {return;} |
---|
195 | pawn->kill(); |
---|
196 | this->timeToAct_[player]=timeRemaining+3.0f;//reset timer |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | void LastManStanding::tick(float dt) |
---|
201 | { |
---|
202 | SUPER(LastManStanding, tick, dt); |
---|
203 | if(this->hasStarted()&&(!this->hasEnded())) |
---|
204 | { |
---|
205 | if ((this->hasStarted()&&(playersAlive<=1)))//last player remaining |
---|
206 | { |
---|
207 | this->end(); |
---|
208 | } |
---|
209 | for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it) |
---|
210 | { |
---|
211 | it->second-=dt; |
---|
212 | if (it->second<timeRemaining/6)//Warning message |
---|
213 | { |
---|
214 | const std::string& message = "Camper Warning! Don't forget to shoot."; |
---|
215 | this->gtinfo_->sendFadingMessage(message,it->first->getClientID()); |
---|
216 | } |
---|
217 | if (it->second<0.0f) |
---|
218 | { |
---|
219 | it->second=timeRemaining+3.0f;//reset timer |
---|
220 | if (playerGetLives(it->first)>0) |
---|
221 | this->killPlayer(it->first); |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|