2008-09-27 15:43:57 -04:00
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2004 SuperTuxKart-Team
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "modes/linear_world.hpp"
|
2009-07-19 15:50:20 -04:00
|
|
|
#include "states_screens/race_gui.hpp"
|
2009-03-13 09:50:24 -04:00
|
|
|
|
2009-01-23 00:23:22 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
#include "audio/sound_manager.hpp"
|
2008-09-28 22:29:33 -04:00
|
|
|
#include "network/network_manager.hpp"
|
2009-01-22 17:27:13 -05:00
|
|
|
#include "tracks/track.hpp"
|
|
|
|
#include "utils/constants.hpp"
|
2009-07-08 08:34:39 -04:00
|
|
|
#include "utils/string_utils.hpp"
|
2009-01-23 00:23:22 -05:00
|
|
|
#include "utils/translation.hpp"
|
2008-09-27 15:43:57 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
LinearWorld::LinearWorld() : World()
|
|
|
|
{
|
2009-01-15 18:05:50 -05:00
|
|
|
m_kart_display_info = NULL;
|
2008-12-15 18:37:25 -05:00
|
|
|
} // LinearWorld
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2008-11-07 21:07:54 -05:00
|
|
|
void LinearWorld::init()
|
|
|
|
{
|
|
|
|
World::init();
|
2008-09-27 15:43:57 -04:00
|
|
|
const unsigned int kart_amount = m_kart.size();
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
m_kart_display_info = new KartIconDisplayInfo[kart_amount];
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
for(unsigned int n=0; n<kart_amount; n++)
|
|
|
|
{
|
|
|
|
KartInfo info;
|
2009-05-21 23:23:48 -04:00
|
|
|
info.m_track_sector = QuadGraph::UNKNOWN_SECTOR;
|
|
|
|
info.m_last_valid_sector = QuadGraph::UNKNOWN_SECTOR;
|
2008-12-25 13:49:34 -05:00
|
|
|
info.m_last_valid_race_lap = -1;
|
2009-07-13 08:22:27 -04:00
|
|
|
info.m_lap_start_time = 0;
|
2009-08-18 06:33:21 -04:00
|
|
|
m_track->getQuadGraph().findRoadSector(m_kart[n]->getXYZ(),
|
2009-05-21 23:23:48 -04:00
|
|
|
&info.m_track_sector);
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//If m_track_sector == UNKNOWN_SECTOR, then the kart is not on top of
|
|
|
|
//the road, so we have to use another function to find the sector.
|
2009-05-21 23:23:48 -04:00
|
|
|
info.m_on_road = info.m_track_sector != QuadGraph::UNKNOWN_SECTOR;
|
2008-12-21 19:33:25 -05:00
|
|
|
if (!info.m_on_road)
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
|
|
|
info.m_track_sector =
|
2009-05-27 20:43:12 -04:00
|
|
|
m_track->getQuadGraph().findOutOfRoadSector(m_kart[n]->getXYZ(),
|
|
|
|
QuadGraph::UNKNOWN_SECTOR );
|
2008-09-27 15:43:57 -04:00
|
|
|
}
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2009-05-21 23:23:48 -04:00
|
|
|
m_track->getQuadGraph().spatialToTrack(&info.m_curr_track_coords,
|
|
|
|
m_kart[n]->getXYZ(),
|
|
|
|
info.m_track_sector );
|
2008-12-15 18:37:25 -05:00
|
|
|
|
2009-07-07 20:44:55 -04:00
|
|
|
info.m_race_lap = 0;
|
2009-07-13 08:22:27 -04:00
|
|
|
info.m_lap_start_time = 0;
|
2008-09-27 15:43:57 -04:00
|
|
|
info.m_time_at_last_lap = 99999.9f;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
m_kart_info.push_back(info);
|
2008-12-15 18:37:25 -05:00
|
|
|
} // next kart
|
|
|
|
|
|
|
|
} // init
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-30 16:38:11 -04:00
|
|
|
LinearWorld::~LinearWorld()
|
|
|
|
{
|
2009-01-15 18:05:50 -05:00
|
|
|
// In case that a track is not found, m_kart_display info was never
|
|
|
|
// initialised.
|
|
|
|
if(m_kart_display_info)
|
|
|
|
delete[] m_kart_display_info;
|
2008-12-15 18:37:25 -05:00
|
|
|
} // ~LinearWorld
|
|
|
|
|
2008-09-30 16:38:11 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-29 11:14:14 -04:00
|
|
|
void LinearWorld::restartRace()
|
|
|
|
{
|
|
|
|
World::restartRace();
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-29 11:14:14 -04:00
|
|
|
const unsigned int kart_amount = m_kart.size();
|
|
|
|
for(unsigned int n=0; n<kart_amount; n++)
|
|
|
|
{
|
|
|
|
KartInfo& info = m_kart_info[n];
|
2009-05-21 23:23:48 -04:00
|
|
|
info.m_track_sector = QuadGraph::UNKNOWN_SECTOR;
|
|
|
|
info.m_last_valid_sector = QuadGraph::UNKNOWN_SECTOR;
|
2009-07-13 08:22:27 -04:00
|
|
|
info.m_lap_start_time = 0;
|
2009-08-18 06:33:21 -04:00
|
|
|
m_track->getQuadGraph().findRoadSector(m_kart[n]->getXYZ(),
|
2009-05-21 23:23:48 -04:00
|
|
|
&info.m_track_sector);
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-29 11:14:14 -04:00
|
|
|
//If m_track_sector == UNKNOWN_SECTOR, then the kart is not on top of
|
|
|
|
//the road, so we have to use another function to find the sector.
|
2009-05-21 23:23:48 -04:00
|
|
|
info.m_on_road = info.m_track_sector != QuadGraph::UNKNOWN_SECTOR;
|
2008-12-21 19:33:25 -05:00
|
|
|
if (!info.m_on_road)
|
2008-09-29 11:14:14 -04:00
|
|
|
{
|
|
|
|
info.m_track_sector =
|
2009-05-27 20:43:12 -04:00
|
|
|
m_track->getQuadGraph().findOutOfRoadSector(m_kart[n]->getXYZ(),
|
|
|
|
QuadGraph::UNKNOWN_SECTOR );
|
2008-09-29 11:14:14 -04:00
|
|
|
}
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2009-05-21 23:23:48 -04:00
|
|
|
m_track->getQuadGraph().spatialToTrack(&info.m_curr_track_coords,
|
|
|
|
m_kart[n]->getXYZ(),
|
|
|
|
info.m_track_sector );
|
2009-07-07 20:44:55 -04:00
|
|
|
info.m_race_lap = 0;
|
2009-07-13 08:22:27 -04:00
|
|
|
info.m_lap_start_time = -0;
|
2008-09-29 11:14:14 -04:00
|
|
|
info.m_time_at_last_lap = 99999.9f;
|
|
|
|
|
2008-09-29 11:43:58 -04:00
|
|
|
updateRacePosition(m_kart[n], info);
|
2008-11-30 22:58:58 -05:00
|
|
|
} // next kart
|
|
|
|
} // restartRace
|
2008-12-15 18:37:25 -05:00
|
|
|
|
2008-09-29 11:14:14 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-27 15:43:57 -04:00
|
|
|
void LinearWorld::update(float delta)
|
2009-08-18 06:33:21 -04:00
|
|
|
{
|
2008-12-15 18:37:25 -05:00
|
|
|
// run generic parent stuff that applies to all modes. It
|
|
|
|
// especially updates the kart positions.
|
2008-09-27 15:43:57 -04:00
|
|
|
World::update(delta);
|
|
|
|
|
2008-12-15 18:37:25 -05:00
|
|
|
const unsigned int kart_amount = race_manager->getNumKarts();
|
2008-12-15 01:35:17 -05:00
|
|
|
|
2008-12-15 18:37:25 -05:00
|
|
|
// Do stuff specific to this subtype of race.
|
|
|
|
// ------------------------------------------
|
2008-09-27 15:43:57 -04:00
|
|
|
for(unsigned int n=0; n<kart_amount; n++)
|
|
|
|
{
|
|
|
|
KartInfo& kart_info = m_kart_info[n];
|
|
|
|
Kart* kart = m_kart[n];
|
2008-12-21 02:43:41 -05:00
|
|
|
|
|
|
|
// Nothing to do for karts that are currently being rescued.
|
|
|
|
if(kart->isRescue()) continue;
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
// ---------- deal with sector data ---------
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-29 21:54:09 -04:00
|
|
|
// update sector variables
|
2008-09-27 15:43:57 -04:00
|
|
|
int prev_sector = kart_info.m_track_sector;
|
2009-08-18 06:33:21 -04:00
|
|
|
m_track->getQuadGraph().findRoadSector(kart->getXYZ(),
|
2009-05-21 23:23:48 -04:00
|
|
|
&kart_info.m_track_sector);
|
2009-01-12 19:16:09 -05:00
|
|
|
|
2009-05-21 23:23:48 -04:00
|
|
|
kart_info.m_on_road = kart_info.m_track_sector != QuadGraph::UNKNOWN_SECTOR;
|
2009-05-27 20:43:12 -04:00
|
|
|
if(kart_info.m_on_road)
|
|
|
|
{
|
|
|
|
kart_info.m_last_valid_sector = kart_info.m_track_sector;
|
|
|
|
}
|
|
|
|
else
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2008-12-21 19:33:25 -05:00
|
|
|
// Kart off road. Find the closest sector instead.
|
|
|
|
kart_info.m_track_sector =
|
2009-05-27 20:43:12 -04:00
|
|
|
m_track->getQuadGraph().findOutOfRoadSector(kart->getXYZ(), prev_sector );
|
2009-08-18 06:33:21 -04:00
|
|
|
}
|
|
|
|
|
2008-12-21 02:43:41 -05:00
|
|
|
// Update track coords (=progression)
|
2009-08-18 06:33:21 -04:00
|
|
|
m_track->getQuadGraph().spatialToTrack(&kart_info.m_curr_track_coords,
|
2009-05-21 23:23:48 -04:00
|
|
|
kart->getXYZ(),
|
|
|
|
kart_info.m_track_sector );
|
2008-12-21 02:43:41 -05:00
|
|
|
|
2008-12-15 18:37:25 -05:00
|
|
|
} // for n
|
|
|
|
|
|
|
|
// Update all positions. This must be done after _all_ karts have
|
|
|
|
// updated their position and laps etc, otherwise inconsistencies
|
|
|
|
// (like two karts at same position) can occur.
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
for(unsigned int i=0; i<kart_amount; i++)
|
|
|
|
{
|
|
|
|
// ---------- update rank ------
|
2009-08-18 06:33:21 -04:00
|
|
|
if(!m_kart[i]->hasFinishedRace() && !m_kart[i]->isEliminated())
|
2008-12-15 18:37:25 -05:00
|
|
|
{
|
|
|
|
updateRacePosition(m_kart[i], m_kart_info[i]);
|
|
|
|
// During the last lap update the estimated finish time.
|
|
|
|
// This is used to play the faster music, and by the AI
|
|
|
|
if(m_kart_info[i].m_race_lap == race_manager->getNumLaps()-1)
|
|
|
|
m_kart_info[i].m_estimated_finish = estimateFinishTimeForKart(m_kart[i]);
|
2008-12-21 19:21:43 -05:00
|
|
|
checkForWrongDirection(i);
|
2008-12-15 18:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2008-12-21 19:33:25 -05:00
|
|
|
// FIXME: Debug output in case that the double position error
|
2008-12-15 18:37:25 -05:00
|
|
|
// occurs again. It can most likely be removed.
|
|
|
|
int pos_used[10];
|
|
|
|
for(int i=0; i<10; i++) pos_used[i]=-99;
|
|
|
|
for(unsigned int i=0; i<kart_amount; i++)
|
|
|
|
{
|
|
|
|
if(pos_used[m_kart[i]->getPosition()]!=-99)
|
|
|
|
{
|
|
|
|
for(unsigned int j =0; j<kart_amount; j++)
|
|
|
|
{
|
|
|
|
printf("j %d pos %d finished %d laps %d distance %f\n",
|
|
|
|
j, m_kart[j]->getPosition(),
|
|
|
|
m_kart[j]->hasFinishedRace(),
|
|
|
|
m_kart_info[j].m_race_lap,
|
|
|
|
getDistanceDownTrackForKart(m_kart[j]->getWorldKartId()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos_used[m_kart[i]->getPosition()]=i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // update
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2009-07-07 20:44:55 -04:00
|
|
|
/** Is called by check structures if a kart starts a new lap. Note that the
|
|
|
|
* new check structure does not trigger a new lap the first time a kart
|
|
|
|
* crosses the starting line!
|
|
|
|
* \param kart_index Index of the kart.
|
|
|
|
*/
|
|
|
|
void LinearWorld::newLap(unsigned int kart_index)
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2009-07-13 08:22:27 -04:00
|
|
|
KartInfo &kart_info = m_kart_info[kart_index];
|
|
|
|
Kart *kart = m_kart[kart_index];
|
2009-07-07 20:44:55 -04:00
|
|
|
// Only increase the lap counter and set the new time if the
|
|
|
|
// kart hasn't already finished the race (otherwise the race_gui
|
|
|
|
// will begin another countdown).
|
|
|
|
if(kart_info.m_race_lap+1 <= race_manager->getNumLaps())
|
|
|
|
{
|
|
|
|
setTimeAtLapForKart(getTime(), kart->getWorldKartId() );
|
|
|
|
kart_info.m_race_lap++ ;
|
|
|
|
}
|
|
|
|
// Race finished
|
|
|
|
if(kart_info.m_race_lap >= race_manager->getNumLaps() && raceHasLaps())
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2009-07-07 20:44:55 -04:00
|
|
|
// A client wait does not detect race finished by itself, it will
|
|
|
|
// receive a message from the server. So a client does not do
|
|
|
|
// anything here.
|
|
|
|
if(network_manager->getMode()!=NetworkManager::NW_CLIENT)
|
|
|
|
kart->raceFinished(getTime());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
float time_per_lap;
|
|
|
|
if (kart_info.m_race_lap == 1) // just completed first lap
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2009-07-07 20:44:55 -04:00
|
|
|
time_per_lap=getTime();
|
2008-09-27 15:43:57 -04:00
|
|
|
}
|
2009-07-07 20:44:55 -04:00
|
|
|
else //completing subsequent laps
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2009-07-07 20:44:55 -04:00
|
|
|
time_per_lap=getTime() - kart_info.m_lap_start_time;
|
2008-09-27 15:43:57 -04:00
|
|
|
}
|
2009-07-07 20:44:55 -04:00
|
|
|
|
|
|
|
// if new fastest lap
|
|
|
|
if(time_per_lap < getFastestLapTime() && raceHasLaps())
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2009-07-07 20:44:55 -04:00
|
|
|
setFastestLap(kart, time_per_lap);
|
2009-08-18 06:33:21 -04:00
|
|
|
m_race_gui->addMessage(_("New fastest lap"), NULL,
|
2009-07-15 23:24:17 -04:00
|
|
|
2.0f, 40, video::SColor(255, 100, 210, 100));
|
2009-07-12 07:54:21 -04:00
|
|
|
std::string s = StringUtils::timeToString(time_per_lap);
|
|
|
|
|
|
|
|
std::ostringstream m_fastest_lap_message;
|
|
|
|
m_fastest_lap_message << s << ": " << kart->getName();
|
2009-08-18 06:33:21 -04:00
|
|
|
m_race_gui->addMessage(m_fastest_lap_message.str(), NULL,
|
2009-07-15 23:24:17 -04:00
|
|
|
2.0f, 40, video::SColor(255, 100, 210, 100));
|
2009-07-07 20:44:55 -04:00
|
|
|
} // end if new fastest lap
|
2008-09-27 15:43:57 -04:00
|
|
|
}
|
2009-07-07 20:44:55 -04:00
|
|
|
kart_info.m_lap_start_time = getTime();
|
|
|
|
} // newLap
|
2008-12-15 18:37:25 -05:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int LinearWorld::getSectorForKart(const int kart_id) const
|
|
|
|
{
|
|
|
|
return m_kart_info[kart_id].m_track_sector;
|
2008-12-16 08:21:55 -05:00
|
|
|
} // getSectorForKart
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float LinearWorld::getDistanceDownTrackForKart(const int kart_id) const
|
|
|
|
{
|
|
|
|
return m_kart_info[kart_id].m_curr_track_coords.getY();
|
2008-12-16 08:21:55 -05:00
|
|
|
} // getDistanceDownTrackForKart
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float LinearWorld::getDistanceToCenterForKart(const int kart_id) const
|
|
|
|
{
|
|
|
|
return m_kart_info[kart_id].m_curr_track_coords.getX();
|
2008-12-16 08:21:55 -05:00
|
|
|
} // getDistanceToCenterForKart
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int LinearWorld::getLapForKart(const int kart_id) const
|
|
|
|
{
|
|
|
|
return m_kart_info[kart_id].m_race_lap;
|
2008-12-16 08:21:55 -05:00
|
|
|
} // getLapForKart
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void LinearWorld::setTimeAtLapForKart(float t, const int kart_id)
|
|
|
|
{
|
|
|
|
m_kart_info[kart_id].m_time_at_last_lap=t;
|
2008-12-16 08:21:55 -05:00
|
|
|
} // setTimeAtLapForKart
|
|
|
|
|
2008-12-08 18:23:33 -05:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/** Returns the estimated finishing time. Only valid during the last lap!
|
|
|
|
* \param kart_id Id of the kart.
|
|
|
|
*/
|
|
|
|
float LinearWorld::getEstimatedFinishTime(const int kart_id) const
|
|
|
|
{
|
|
|
|
assert(m_kart_info[kart_id].m_race_lap == race_manager->getNumLaps()-1);
|
|
|
|
return m_kart_info[kart_id].m_estimated_finish;
|
|
|
|
} // getEstimatedFinishTime
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float LinearWorld::getTimeAtLapForKart(const int kart_id) const
|
|
|
|
{
|
|
|
|
return m_kart_info[kart_id].m_time_at_last_lap;
|
2008-12-16 08:21:55 -05:00
|
|
|
} // getTimeAtLapForKart
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2009-07-12 07:54:21 -04:00
|
|
|
KartIconDisplayInfo* LinearWorld::getKartsDisplayInfo()
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
|
|
|
int laps_of_leader = -1;
|
|
|
|
float time_of_leader = -1;
|
|
|
|
// Find the best time for the lap. We can't simply use
|
|
|
|
// the time of the kart at position 1, since the kart
|
|
|
|
// might have been overtaken by now
|
2008-09-28 06:39:52 -04:00
|
|
|
const unsigned int kart_amount = race_manager->getNumKarts();
|
2008-09-27 15:43:57 -04:00
|
|
|
for(unsigned int i = 0; i < kart_amount ; i++)
|
|
|
|
{
|
|
|
|
KartIconDisplayInfo& rank_info = m_kart_display_info[i];
|
|
|
|
Kart* kart = m_kart[i];
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
// reset color
|
|
|
|
rank_info.r = 1.0;
|
|
|
|
rank_info.g = 1.0;
|
|
|
|
rank_info.b = 1.0;
|
|
|
|
rank_info.lap = -1;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
if(kart->isEliminated()) continue;
|
|
|
|
const float lap_time = getTimeAtLapForKart(kart->getWorldKartId());
|
|
|
|
const int current_lap = getLapForKart( kart->getWorldKartId() );
|
|
|
|
rank_info.lap = current_lap;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
if(current_lap > laps_of_leader)
|
|
|
|
{
|
|
|
|
// more laps than current leader --> new leader and new time computation
|
|
|
|
laps_of_leader = current_lap;
|
|
|
|
time_of_leader = lap_time;
|
|
|
|
} else if(current_lap == laps_of_leader)
|
|
|
|
{
|
|
|
|
// Same number of laps as leader: use fastest time
|
|
|
|
time_of_leader=std::min(time_of_leader,lap_time);
|
|
|
|
}
|
2008-09-30 21:15:34 -04:00
|
|
|
}
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-30 21:15:34 -04:00
|
|
|
// we now know the best time of the lap. fill the remaining bits of info
|
|
|
|
for(unsigned int i = 0; i < kart_amount ; i++)
|
2009-08-18 06:33:21 -04:00
|
|
|
{
|
2008-09-30 21:15:34 -04:00
|
|
|
KartIconDisplayInfo& rank_info = m_kart_display_info[i];
|
|
|
|
KartInfo& kart_info = m_kart_info[i];
|
|
|
|
Kart* kart = m_kart[i];
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-30 21:15:34 -04:00
|
|
|
const int position = kart->getPosition();
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-30 20:46:00 -04:00
|
|
|
if(laps_of_leader>0 && // Don't compare times when crossing the start line first
|
2008-09-30 21:15:34 -04:00
|
|
|
(getTime() - getTimeAtLapForKart(kart->getWorldKartId())<5.0f || rank_info.lap != laps_of_leader) &&
|
2008-12-16 04:02:14 -05:00
|
|
|
raceHasLaps())
|
2008-09-27 15:43:57 -04:00
|
|
|
{ // Display for 5 seconds
|
2009-07-08 08:34:39 -04:00
|
|
|
std::string str;
|
2008-09-27 15:43:57 -04:00
|
|
|
if(position==1)
|
|
|
|
{
|
2009-07-08 08:34:39 -04:00
|
|
|
str = " "+StringUtils::timeToString(getTimeAtLapForKart(kart->getWorldKartId()));
|
2008-09-27 15:43:57 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float timeBehind;
|
2009-08-18 06:33:21 -04:00
|
|
|
timeBehind = (kart_info.m_race_lap==laps_of_leader
|
|
|
|
? getTimeAtLapForKart(kart->getWorldKartId())
|
2008-12-16 04:02:14 -05:00
|
|
|
: getTime())
|
|
|
|
- time_of_leader;
|
2009-07-08 08:34:39 -04:00
|
|
|
str="+"+StringUtils::timeToString(timeBehind);
|
2008-09-27 15:43:57 -04:00
|
|
|
}
|
|
|
|
rank_info.time = str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rank_info.time = "";
|
|
|
|
}
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
int numLaps = race_manager->getNumLaps();
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
if(kart_info.m_race_lap>=numLaps)
|
|
|
|
{ // kart is finished, display in green
|
|
|
|
rank_info.g = rank_info.b = 0;
|
|
|
|
}
|
|
|
|
else if(kart_info.m_race_lap>=0 && numLaps>1)
|
|
|
|
{
|
|
|
|
rank_info.g = rank_info.b = 1.0f-(float)kart_info.m_race_lap/((float)numLaps-1.0f);
|
|
|
|
}
|
|
|
|
} // next kart
|
2009-08-18 06:33:21 -04:00
|
|
|
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
return m_kart_display_info;
|
2008-12-16 08:21:55 -05:00
|
|
|
} // getKartsDisplayInfo
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void LinearWorld::terminateRace()
|
|
|
|
{
|
|
|
|
World::terminateRace();
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
// if some karts have not yet finished the race yet, estimate
|
|
|
|
// their times and use these values to proceed without waiting
|
2008-09-28 06:39:52 -04:00
|
|
|
const unsigned int kart_amount = m_kart.size();
|
2008-09-27 15:43:57 -04:00
|
|
|
for ( Karts::size_type i = 0; i < kart_amount; ++i)
|
|
|
|
{
|
2008-12-16 08:21:55 -05:00
|
|
|
// Eliminated karts have already called raceFinished.
|
|
|
|
if(!m_kart[i]->hasFinishedRace() && !m_kart[i]->isEliminated())
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2008-12-08 18:23:33 -05:00
|
|
|
const float est_finish_time = m_kart_info[i].m_estimated_finish;
|
2008-09-27 15:43:57 -04:00
|
|
|
m_kart[i]->raceFinished(est_finish_time);
|
|
|
|
} // if !hasFinishedRace
|
|
|
|
} // for i
|
2008-12-16 04:02:14 -05:00
|
|
|
} // terminateRace
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2008-10-16 15:53:37 -04:00
|
|
|
void LinearWorld::raceResultOrder( int* order )
|
|
|
|
{
|
|
|
|
const unsigned int NUM_KARTS = race_manager->getNumKarts();
|
|
|
|
for(unsigned int i=0; i < NUM_KARTS; i++)
|
|
|
|
{
|
|
|
|
order[RaceManager::getKart(i)->getPosition()-1] = i; // even for eliminated karts
|
|
|
|
}
|
2008-12-16 08:21:55 -05:00
|
|
|
} // raceResultOrder
|
|
|
|
|
2008-09-27 16:49:11 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-12-08 18:23:33 -05:00
|
|
|
float LinearWorld::estimateFinishTimeForKart(Kart* kart)
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
|
|
|
// Estimate the arrival time of any karts that haven't arrived
|
|
|
|
// yet by using their average speed up to now and the distance
|
2009-08-18 06:33:21 -04:00
|
|
|
// still to race. This approach guarantees that the order of
|
|
|
|
// the karts won't change anymore (karts ahead will have a
|
|
|
|
// higher average speed and therefore finish the race earlier
|
2008-09-27 15:43:57 -04:00
|
|
|
// than karts further behind), so the position doesn't have to
|
|
|
|
// be updated to get the correct scoring.
|
2008-12-08 18:23:33 -05:00
|
|
|
const KartInfo &kart_info = m_kart_info[kart->getWorldKartId()];
|
2008-12-16 04:02:14 -05:00
|
|
|
float distance_covered = kart_info.m_race_lap * m_track->getTrackLength()
|
2008-09-27 15:43:57 -04:00
|
|
|
+ getDistanceDownTrackForKart(kart->getWorldKartId());
|
|
|
|
// In case that a kart is rescued behind start line, or ...
|
|
|
|
if(distance_covered<0) distance_covered =1.0f;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-12-16 04:02:14 -05:00
|
|
|
const float full_distance = race_manager->getNumLaps()*m_track->getTrackLength();
|
|
|
|
const float average_speed = distance_covered/getTime();
|
2009-08-18 06:33:21 -04:00
|
|
|
|
|
|
|
// Finish time is the time needed for the whole race with
|
2008-09-27 15:43:57 -04:00
|
|
|
// the average speed computed above.
|
2008-12-11 14:43:25 -05:00
|
|
|
return getTime() + (full_distance - distance_covered) / average_speed;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
} // estimateFinishTime
|
2008-12-21 19:21:43 -05:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-27 16:49:11 -04:00
|
|
|
/** Decide where to drop a rescued kart
|
|
|
|
*/
|
2008-09-27 16:32:07 -04:00
|
|
|
void LinearWorld::moveKartAfterRescue(Kart* kart, btRigidBody* body)
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
|
|
|
KartInfo& info = m_kart_info[kart->getWorldKartId()];
|
2008-12-15 20:42:22 -05:00
|
|
|
|
2009-08-18 06:33:21 -04:00
|
|
|
// If the kart is off road, rescue it to the last valid track position
|
|
|
|
// instead of the current one (since the sector might be determined by
|
2008-12-15 20:42:22 -05:00
|
|
|
// being closest to it, which allows shortcuts like drive towards another
|
|
|
|
// part of the lap, press rescue, and be rescued to this other part of
|
|
|
|
// the track (example: math class, drive towards the left after start,
|
|
|
|
// when hitting the books, press rescue --> you are rescued to the
|
|
|
|
// end of the track).
|
|
|
|
if(!info.m_on_road)
|
2008-12-25 13:49:34 -05:00
|
|
|
{
|
2008-12-15 20:42:22 -05:00
|
|
|
info.m_track_sector = info.m_last_valid_sector;
|
2008-12-25 13:49:34 -05:00
|
|
|
}
|
2008-12-25 14:07:56 -05:00
|
|
|
info.m_race_lap = info.m_last_valid_race_lap;
|
2008-12-10 19:45:54 -05:00
|
|
|
// FIXME - removing 1 here makes it less likely to fall in a rescue loop since the kart
|
|
|
|
// moves back on each attempt. This is still a weak hack. Also some other code depends
|
|
|
|
// on 1 being substracted, like 'forceRescue'
|
2008-09-27 15:43:57 -04:00
|
|
|
if ( info.m_track_sector > 0 ) info.m_track_sector-- ;
|
2008-10-03 21:44:08 -04:00
|
|
|
info.m_last_valid_sector = info.m_track_sector;
|
|
|
|
if ( info.m_last_valid_sector > 0 ) info.m_last_valid_sector --;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-12-16 04:02:14 -05:00
|
|
|
kart->setXYZ( m_track->trackToSpatial(info.m_track_sector) );
|
2009-08-18 06:33:21 -04:00
|
|
|
|
|
|
|
btQuaternion heading(btVector3(0.0f, 0.0f, 1.0f),
|
2009-05-18 00:08:00 -04:00
|
|
|
m_track->getAngle(info.m_track_sector) );
|
2008-09-27 15:43:57 -04:00
|
|
|
kart->setRotation(heading);
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2009-08-18 07:05:40 -04:00
|
|
|
// A certain epsilon is added here to the Z coordinate, in case
|
2008-09-27 16:32:07 -04:00
|
|
|
// that the drivelines are somewhat under the track. Otherwise, the
|
2009-08-18 07:05:40 -04:00
|
|
|
// kart might be placed a little bit under the track, triggering
|
|
|
|
// a rescue, ... (experimentally found value)
|
|
|
|
float epsilon = 0.5f * kart->getKartHeight();
|
|
|
|
|
2008-09-27 16:32:07 -04:00
|
|
|
btTransform pos;
|
2009-08-18 07:05:40 -04:00
|
|
|
pos.setOrigin(kart->getXYZ()+btVector3(0, 0, kart->getKartHeight() + epsilon));
|
2008-09-27 16:32:07 -04:00
|
|
|
pos.setRotation(btQuaternion(btVector3(0.0f, 0.0f, 1.0f),
|
2009-05-18 00:08:00 -04:00
|
|
|
m_track->getAngle(info.m_track_sector)));
|
2008-09-27 16:32:07 -04:00
|
|
|
|
|
|
|
body->setCenterOfMassTransform(pos);
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2009-08-18 07:05:40 -04:00
|
|
|
//project kart to surface of track
|
|
|
|
bool kart_over_ground = m_physics->projectKartDownwards(kart);
|
|
|
|
|
|
|
|
if (kart_over_ground)
|
|
|
|
{
|
|
|
|
//add vertical offset so that the kart starts off above track
|
|
|
|
|
|
|
|
//TODO - offset needs to be a configurable parameter
|
|
|
|
//float vertical_offset = 0.5f * kart->getKartHeight();
|
|
|
|
//body->translate(btVector3(0, 0, vertical_offset));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WARNING: invalid position after rescue for kart %s on track %s.\n",
|
|
|
|
(kart->getIdent().c_str()), m_track->getIdent().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-07 20:29:02 -05:00
|
|
|
} // moveKartAfterRescue
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-27 16:49:11 -04:00
|
|
|
/** Find the position (rank) of 'kart' and update it accordingly
|
2008-09-27 15:43:57 -04:00
|
|
|
*/
|
|
|
|
void LinearWorld::updateRacePosition ( Kart* kart, KartInfo& kart_info )
|
|
|
|
{
|
|
|
|
int p = 1 ;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
const unsigned int kart_amount = m_kart.size();
|
2008-12-15 18:37:25 -05:00
|
|
|
const int my_id = kart->getWorldKartId();
|
|
|
|
const int my_laps = getLapForKart(my_id);
|
|
|
|
const float my_progression = getDistanceDownTrackForKart(my_id);
|
2008-09-28 06:39:52 -04:00
|
|
|
for ( unsigned int j = 0 ; j < kart_amount ; j++ )
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
2008-10-09 19:33:14 -04:00
|
|
|
if(j == kart->getWorldKartId()) continue; // don't compare a kart with itself
|
2009-08-18 06:33:21 -04:00
|
|
|
if(m_kart[j]->isEliminated()) continue; // dismiss eliminated karts
|
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
// Count karts ahead of the current kart, i.e. kart that are already
|
|
|
|
// finished (the current kart k has not yet finished!!), have done more
|
|
|
|
// laps, or the same number of laps, but a greater distance.
|
2008-12-15 18:37:25 -05:00
|
|
|
if(m_kart[j]->hasFinishedRace()) { p++; continue; }
|
|
|
|
|
|
|
|
/* has done more or less lapses */
|
|
|
|
int other_laps = getLapForKart(m_kart[j]->getWorldKartId());
|
|
|
|
if (other_laps != my_laps)
|
|
|
|
{
|
|
|
|
if(other_laps > my_laps) p++; // Other kart has more lapses
|
2009-08-18 06:33:21 -04:00
|
|
|
continue;
|
2008-12-15 18:37:25 -05:00
|
|
|
}
|
|
|
|
// Now both karts have the same number of lapses. Test progression.
|
2009-08-18 06:33:21 -04:00
|
|
|
// A kart is ahead if it's driven further, or driven the same
|
2008-12-15 18:37:25 -05:00
|
|
|
// distance, but started further to the back.
|
|
|
|
float other_progression = getDistanceDownTrackForKart(m_kart[j]->getWorldKartId());
|
|
|
|
if(other_progression > my_progression ||
|
|
|
|
(other_progression == my_progression &&
|
|
|
|
m_kart[j]->getInitialPosition() > kart->getInitialPosition()) )
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} //next kart
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-09-27 15:43:57 -04:00
|
|
|
kart->setPosition(p);
|
|
|
|
// Switch on faster music if not already done so, if the
|
|
|
|
// first kart is doing its last lap, and if the estimated
|
|
|
|
// remaining time is less than 30 seconds.
|
2009-08-18 06:33:21 -04:00
|
|
|
if(!m_faster_music_active &&
|
|
|
|
kart_info.m_race_lap == race_manager->getNumLaps()-1 &&
|
2008-12-08 18:23:33 -05:00
|
|
|
p==1 &&
|
|
|
|
useFastMusicNearEnd() &&
|
2008-12-11 14:43:25 -05:00
|
|
|
kart_info.m_estimated_finish > 0 &&
|
2008-12-08 18:23:33 -05:00
|
|
|
kart_info.m_estimated_finish - getTime() < 30.0f )
|
2008-09-27 15:43:57 -04:00
|
|
|
{
|
|
|
|
sound_manager->switchToFastMusic();
|
|
|
|
m_faster_music_active=true;
|
|
|
|
}
|
|
|
|
} // updateRacePosition
|
2008-12-21 19:33:25 -05:00
|
|
|
|
2008-12-21 19:21:43 -05:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/** Checks if a kart is going in the wrong direction. This is done only for
|
|
|
|
* player karts to display a message to the player.
|
|
|
|
* \param i Kart id.
|
|
|
|
*/
|
|
|
|
void LinearWorld::checkForWrongDirection(unsigned int i)
|
|
|
|
{
|
|
|
|
if(!m_kart[i]->isPlayerKart()) return;
|
2008-12-21 19:33:25 -05:00
|
|
|
if(!m_kart_info[i].m_on_road) return;
|
2008-12-21 19:21:43 -05:00
|
|
|
|
2009-07-12 07:54:21 -04:00
|
|
|
// FIXME: Don't do this if the in-game option menu is on the screen!
|
|
|
|
// if(option_menu) return;
|
2008-12-21 19:21:43 -05:00
|
|
|
|
|
|
|
const Kart *kart=m_kart[i];
|
2009-05-18 00:08:00 -04:00
|
|
|
// If the kart can go in more than one directions from the current track
|
|
|
|
// don't do any reverse message handling, since it is likely that there
|
|
|
|
// will be one direction in which it isn't going backwards anyway.
|
|
|
|
if(m_track->getQuadGraph().getNumberOfSuccessors(m_kart_info[i].m_track_sector)>1)
|
|
|
|
return;
|
2009-08-18 06:33:21 -04:00
|
|
|
|
2008-12-21 19:21:43 -05:00
|
|
|
// check if the player is going in the wrong direction
|
|
|
|
float angle_diff = kart->getHPR().getHeading() -
|
2009-05-18 00:08:00 -04:00
|
|
|
m_track->getAngle(m_kart_info[i].m_track_sector);
|
2008-12-21 19:21:43 -05:00
|
|
|
if(angle_diff > M_PI) angle_diff -= 2*M_PI;
|
|
|
|
else if (angle_diff < -M_PI) angle_diff += 2*M_PI;
|
|
|
|
// Display a warning message if the kart is going back way (unless
|
|
|
|
// the kart has already finished the race).
|
2009-07-02 20:51:31 -04:00
|
|
|
if (( angle_diff > DEGREE_TO_RAD* 120.0f ||
|
|
|
|
angle_diff < -DEGREE_TO_RAD*120.0f) &&
|
2008-12-21 19:21:43 -05:00
|
|
|
kart->getVelocityLC().getY() > 0.0f &&
|
|
|
|
!kart->hasFinishedRace() )
|
|
|
|
{
|
2009-07-12 07:54:21 -04:00
|
|
|
m_race_gui->addMessage(_("WRONG WAY!"), kart, -1.0f, 60);
|
2008-12-21 19:21:43 -05:00
|
|
|
} // if angle is too big
|
|
|
|
} // checkForWrongDirection
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|