made victory reporting better in multiplayer games

This commit is contained in:
Dave White 2003-10-21 11:20:35 +00:00
parent 15ed0f3448
commit 4dae65c945
7 changed files with 37 additions and 44 deletions

View file

@ -10,6 +10,7 @@
See the COPYING file for more details.
*/
#include "actions.hpp"
#include "display.hpp"
#include "game.hpp"
@ -19,6 +20,7 @@
#include "language.hpp"
#include "map.hpp"
#include "pathfind.hpp"
#include "playlevel.hpp"
#include "replay.hpp"
#include "sound.hpp"
#include "util.hpp"
@ -695,33 +697,47 @@ void advance_unit(const game_data& info,
units.insert(std::pair<gamemap::location,unit>(loc,new_unit));
}
int check_victory(std::map<gamemap::location,unit>& units)
void check_victory(std::map<gamemap::location,unit>& units,
const std::vector<team>& teams)
{
std::set<int> seen_leaders;
std::vector<int> seen_leaders;
for(std::map<gamemap::location,unit>::const_iterator i = units.begin();
i != units.end(); ++i) {
if(i->second.can_recruit())
seen_leaders.insert(i->second.side());
seen_leaders.push_back(i->second.side());
}
//if only one leader remains standing, his team has won
if(seen_leaders.size() == 1)
return *seen_leaders.begin();
bool found_enemies = false;
bool found_human = false;
//if the player (team 1) isn't here, the player has lost
if(seen_leaders.count(1) == 0)
return 2;
for(size_t n = 0; n != seen_leaders.size(); ++n) {
const size_t side1 = seen_leaders[n]-1;
for(size_t m = n+1; m != seen_leaders.size(); ++m) {
const size_t side2 = seen_leaders[m];
if(side1 < teams.size() && teams[side1].is_enemy(side2)) {
found_enemies = true;
}
}
if(side1 < teams.size() && teams[side1].is_human()) {
found_human = true;
}
}
if(found_enemies == false) {
throw end_level_exception(found_human ? VICTORY : DEFEAT);
}
//remove any units which are leaderless
for(std::map<gamemap::location,unit>::iterator j = units.begin();
j != units.end(); ++j) {
if(seen_leaders.count(j->second.side()) == 0) {
if(std::find(seen_leaders.begin(),seen_leaders.end(),j->second.side())
== seen_leaders.end()) {
units.erase(j);
j = units.begin();
}
}
return -1;
}
const time_of_day& timeofday_at(const gamestatus& status,

View file

@ -84,7 +84,8 @@ void advance_unit(const game_data& info,
bool under_leadership(const std::map<gamemap::location,unit>& units,
const gamemap::location& loc);
int check_victory(std::map<gamemap::location,unit>& units);
void check_victory(std::map<gamemap::location,unit>& units,
const std::vector<team>& teams);
//gets the time of day at a certain tile. Certain tiles may have a time of
//day that differs from 'the' time of day, if a unit that brings light is there

View file

@ -372,11 +372,7 @@ void do_move(display& disp, const gamemap& map, const game_data& gameinfo,
game_events::fire("attack",to,target_loc);
if(units.count(to) && units.count(target_loc)) {
attack(disp,map,to,target_loc,weapon,units,state,gameinfo,false);
const int res = check_victory(units);
if(res == 1)
throw end_level_exception(VICTORY);
else if(res > 1)
throw end_level_exception(DEFEAT);
check_victory(units,teams);
}
std::cerr << "done attacking...\n";
@ -568,11 +564,7 @@ void do_move(display& disp, const gamemap& map, const game_data& gameinfo,
if(units.count(attacker) && units.count(defender)) {
attack(disp,map,attacker,defender,weapon,units,state,
gameinfo,false);
const int res = check_victory(units);
if(res == 1)
throw end_level_exception(VICTORY);
else if(res > 1)
throw end_level_exception(DEFEAT);
check_victory(units,teams);
}
break;

View file

@ -247,12 +247,7 @@ LEVEL_RESULT play_level(game_data& gameinfo, config& terrain_config,
game_events::pump();
const int victory = check_victory(units);
if(victory > 1) {
throw end_level_exception(DEFEAT);
} else if(victory == 1) {
throw end_level_exception(VICTORY);
}
check_victory(units,teams);
}
//time has run out

View file

@ -355,14 +355,7 @@ bool turn_slice(game_data& gameinfo, game_state& state_of_game,
gui.invalidate_unit();
gui.draw(); //clear the screen
const int winner = check_victory(units);
//if the player has won
if(winner == team_num) {
throw end_level_exception(VICTORY);
} else if(winner >= 1) {
throw end_level_exception(DEFEAT);
}
check_victory(units,teams);
}
}

View file

@ -526,11 +526,7 @@ bool do_replay(display& disp, const gamemap& map, const game_data& gameinfo,
if(u != units.end() && tgt != units.end()) {
attack(disp,map,src,dst,weapon_num,units,state,gameinfo,false);
const int res = check_victory(units);
if(res == 1)
throw end_level_exception(VICTORY);
else if(res > 1)
throw end_level_exception(DEFEAT);
check_victory(units,teams);
}
u = units.find(src);

View file

@ -29,7 +29,7 @@ textbox::textbox(display& disp, int width, const std::string& text)
{
std::fill(previousKeyState_,
previousKeyState_+CHAR_LENGTH,true);
static const SDL_Rect area = {0,0,1024,768};
static const SDL_Rect area = disp.screen_area();
height_ = font::draw_text(NULL,area,font_size,font::NORMAL_COLOUR,
"ABCD",0,0).h;
}
@ -89,7 +89,7 @@ void textbox::draw() const
int pos = 1;
std::string str(1,'x');
static const SDL_Rect clip = {0,0,1024,768};
static const SDL_Rect clip = disp_.screen_area();
//draw the text
for(size_t i = firstOnScreen_; i < text_.size(); ++i) {