2011-08-04 19:33:41 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2011-2015 Joerg Henrichs
|
2011-08-04 19:33:41 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-09-17 02:30:28 -04:00
|
|
|
#include "tracks/track_sector.hpp"
|
|
|
|
|
2011-09-11 20:28:46 -04:00
|
|
|
#include "modes/linear_world.hpp"
|
2011-09-11 19:59:49 -04:00
|
|
|
#include "modes/world.hpp"
|
2018-07-29 03:59:34 -04:00
|
|
|
#include "network/network_string.hpp"
|
2011-09-11 19:59:49 -04:00
|
|
|
#include "tracks/check_manager.hpp"
|
|
|
|
#include "tracks/check_structure.hpp"
|
2016-09-15 03:47:17 -04:00
|
|
|
#include "tracks/arena_graph.hpp"
|
|
|
|
#include "tracks/arena_node.hpp"
|
2016-09-17 02:30:28 -04:00
|
|
|
#include "tracks/drive_graph.hpp"
|
|
|
|
#include "tracks/drive_node.hpp"
|
2011-08-04 19:33:41 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Initialises the object, and sets the current graph node to be undefined.
|
|
|
|
*/
|
|
|
|
TrackSector::TrackSector()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
} // TrackSector
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void TrackSector::reset()
|
|
|
|
{
|
2018-09-27 14:40:40 -04:00
|
|
|
m_current_graph_node = Graph::UNKNOWN_SECTOR;
|
|
|
|
m_last_valid_graph_node = Graph::UNKNOWN_SECTOR;
|
|
|
|
m_estimated_valid_graph_node = Graph::UNKNOWN_SECTOR;
|
|
|
|
m_on_road = false;
|
|
|
|
m_last_triggered_checkline = -1;
|
2011-08-04 19:33:41 -04:00
|
|
|
} // reset
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Updates the current graph node index, and the track coordinates for
|
|
|
|
* the specified point.
|
|
|
|
* \param xyz The new coordinates to search the graph node for.
|
|
|
|
*/
|
2016-09-15 03:47:17 -04:00
|
|
|
void TrackSector::update(const Vec3 &xyz, bool ignore_vertical)
|
2011-08-04 19:33:41 -04:00
|
|
|
{
|
|
|
|
int prev_sector = m_current_graph_node;
|
2016-09-15 03:47:17 -04:00
|
|
|
const ArenaGraph* ag = ArenaGraph::get();
|
|
|
|
std::vector<int>* test_nodes = NULL;
|
2016-09-29 21:27:44 -04:00
|
|
|
|
|
|
|
if (ag && prev_sector != Graph::UNKNOWN_SECTOR)
|
2016-09-15 03:47:17 -04:00
|
|
|
{
|
2016-09-29 21:27:44 -04:00
|
|
|
// For ArenaGraph, only test nodes around current node
|
2016-09-29 20:15:36 -04:00
|
|
|
test_nodes = ag->getNode(prev_sector)->getNearbyNodes();
|
2016-09-15 03:47:17 -04:00
|
|
|
}
|
|
|
|
|
2016-09-29 21:27:44 -04:00
|
|
|
// Don't only test nodes around if it was not on road
|
|
|
|
Graph::get()->findRoadSector(xyz, &m_current_graph_node,
|
|
|
|
m_on_road ? test_nodes : NULL, ignore_vertical);
|
2016-09-15 03:47:17 -04:00
|
|
|
m_on_road = m_current_graph_node != Graph::UNKNOWN_SECTOR;
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2011-08-04 19:33:41 -04:00
|
|
|
// If m_track_sector == UNKNOWN_SECTOR, then the kart is not on top of
|
|
|
|
// the road, so we have to use search for the closest graph node.
|
2018-03-31 21:03:56 -04:00
|
|
|
if (m_current_graph_node == Graph::UNKNOWN_SECTOR)
|
2011-08-04 19:33:41 -04:00
|
|
|
{
|
2016-09-15 03:47:17 -04:00
|
|
|
m_current_graph_node = Graph::get()->findOutOfRoadSector(xyz,
|
|
|
|
prev_sector, test_nodes, ignore_vertical);
|
2018-03-31 21:03:56 -04:00
|
|
|
}
|
2018-03-31 20:49:45 -04:00
|
|
|
|
2018-07-19 13:36:02 -04:00
|
|
|
// Keep the last valid graph node for arena mode
|
|
|
|
if (ag)
|
|
|
|
{
|
|
|
|
if (prev_sector != Graph::UNKNOWN_SECTOR)
|
|
|
|
m_last_valid_graph_node = prev_sector;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-31 20:49:45 -04:00
|
|
|
|
2018-03-31 21:03:56 -04:00
|
|
|
// keep the current quad as the latest valid one IF the player has one
|
2018-09-27 14:40:40 -04:00
|
|
|
// of the required checklines AND is on road
|
|
|
|
// The on-road condition isn't required for the estimated valid node
|
|
|
|
// used for distances.
|
2018-03-31 21:03:56 -04:00
|
|
|
const DriveNode* dn = DriveGraph::get()->getNode(m_current_graph_node);
|
|
|
|
const std::vector<int>& checkline_requirements = dn->getChecklineRequirements();
|
2018-03-31 20:38:09 -04:00
|
|
|
|
2018-03-31 21:03:56 -04:00
|
|
|
if (checkline_requirements.size() == 0)
|
|
|
|
{
|
2018-09-27 14:40:40 -04:00
|
|
|
m_estimated_valid_graph_node = m_current_graph_node;
|
2018-03-31 21:03:56 -04:00
|
|
|
if (m_on_road)
|
|
|
|
m_last_valid_graph_node = m_current_graph_node;
|
2011-08-04 19:33:41 -04:00
|
|
|
}
|
2011-09-11 20:07:41 -04:00
|
|
|
else
|
2011-09-11 19:59:49 -04:00
|
|
|
{
|
2018-03-31 21:03:56 -04:00
|
|
|
for (unsigned int i=0; i<checkline_requirements.size(); i++)
|
2011-09-11 19:59:49 -04:00
|
|
|
{
|
2018-10-11 23:01:53 -04:00
|
|
|
// If a checkline is validated while off-road and rescue is then
|
|
|
|
// used ; checking for > is required to have the rescue position
|
|
|
|
// correctly updating until the checkline is crossed again.
|
|
|
|
// This requires an ordering of checklines such that
|
|
|
|
// if checkline N is validated, all checklines for n<N are too.
|
2018-09-27 14:40:40 -04:00
|
|
|
if (m_last_triggered_checkline >= checkline_requirements[i])
|
2013-05-29 18:04:35 -04:00
|
|
|
{
|
2018-03-31 21:03:56 -04:00
|
|
|
//has_prerequisite = true;
|
2018-09-27 14:40:40 -04:00
|
|
|
m_estimated_valid_graph_node = m_current_graph_node;
|
2018-03-31 21:03:56 -04:00
|
|
|
if (m_on_road)
|
2011-09-11 19:59:49 -04:00
|
|
|
m_last_valid_graph_node = m_current_graph_node;
|
2018-03-31 21:03:56 -04:00
|
|
|
break;
|
2011-09-11 19:59:49 -04:00
|
|
|
}
|
2018-03-31 21:03:56 -04:00
|
|
|
}
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2018-09-27 14:40:40 -04:00
|
|
|
// TODO: show a message when we detect a user missed a checkline.
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2011-09-11 19:59:49 -04:00
|
|
|
}
|
2013-05-29 18:04:35 -04:00
|
|
|
|
|
|
|
// Now determine the 'track' coords, i.e. ow far from the start of the
|
2011-08-04 19:33:41 -04:00
|
|
|
// track, and how far to the left or right of the center driveline.
|
2018-04-08 20:47:24 -04:00
|
|
|
DriveGraph::get()->spatialToTrack(&m_current_track_coords, xyz,
|
|
|
|
m_current_graph_node);
|
|
|
|
|
|
|
|
if (m_last_valid_graph_node != Graph::UNKNOWN_SECTOR)
|
2018-03-31 19:42:08 -04:00
|
|
|
{
|
2018-04-08 20:47:24 -04:00
|
|
|
DriveGraph::get()->spatialToTrack(&m_latest_valid_track_coords, xyz,
|
2018-03-31 19:42:08 -04:00
|
|
|
m_last_valid_graph_node);
|
|
|
|
}
|
2018-09-27 14:40:40 -04:00
|
|
|
|
|
|
|
if (m_estimated_valid_graph_node != Graph::UNKNOWN_SECTOR)
|
|
|
|
{
|
|
|
|
DriveGraph::get()->spatialToTrack(&m_estimated_valid_track_coords, xyz,
|
|
|
|
m_estimated_valid_graph_node);
|
|
|
|
}
|
2011-08-04 19:33:41 -04:00
|
|
|
} // update
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2013-06-28 19:45:48 -04:00
|
|
|
/** Sets current and last valid graph node to the rescue location.
|
|
|
|
*/
|
2011-08-04 19:33:41 -04:00
|
|
|
void TrackSector::rescue()
|
|
|
|
{
|
2016-09-17 02:30:28 -04:00
|
|
|
if (m_last_valid_graph_node != Graph::UNKNOWN_SECTOR)
|
2011-08-04 19:33:41 -04:00
|
|
|
m_current_graph_node = m_last_valid_graph_node;
|
|
|
|
|
|
|
|
// Using the predecessor has the additional advantage (besides punishing
|
2013-05-29 18:04:35 -04:00
|
|
|
// the player a bit more) that it makes it less likely to fall in a
|
2012-07-11 02:48:31 -04:00
|
|
|
// rescue loop since the kart moves back on each attempt. At this stage
|
|
|
|
// STK does not keep track of where the kart is coming from, so always
|
|
|
|
// use the first predecessor, which is the one on the main driveline.
|
2016-09-17 02:30:28 -04:00
|
|
|
m_current_graph_node = DriveGraph::get()->getNode(m_current_graph_node)
|
|
|
|
->getPredecessor(0);
|
|
|
|
m_last_valid_graph_node = DriveGraph::get()->getNode(m_current_graph_node)
|
|
|
|
->getPredecessor(0);
|
2018-09-27 14:40:40 -04:00
|
|
|
m_estimated_valid_graph_node = m_current_graph_node;
|
2011-09-08 20:40:08 -04:00
|
|
|
} // rescue
|
2011-10-18 16:14:03 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Returns the relative distance of the corresponding kart from the center,
|
|
|
|
* i.e. a value between -1 and 1 inclusive.
|
|
|
|
* \return THe relative distance between -1.0f and +1.0f;
|
|
|
|
*/
|
|
|
|
float TrackSector::getRelativeDistanceToCenter() const
|
|
|
|
{
|
2016-09-17 02:30:28 -04:00
|
|
|
float w = DriveGraph::get()->getNode(m_current_graph_node)->getPathWidth();
|
2011-10-18 16:14:03 -04:00
|
|
|
// w * 0.5 is the distance from center of the quad to the left or right
|
|
|
|
// This way we get a value between -1 and 1.
|
|
|
|
float ratio = getDistanceToCenter()/(w*0.5f);
|
|
|
|
if(ratio>1.0f)
|
|
|
|
ratio=1.0f;
|
|
|
|
else if(ratio<-1.0f)
|
|
|
|
ratio=-1.0f;
|
|
|
|
return ratio;
|
|
|
|
} // getRelativeDistanceToCenter
|
2018-07-29 03:59:34 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-24 23:08:28 -05:00
|
|
|
/** Only basket ball is used for rewind for TrackSector so save the minimum.
|
|
|
|
*/
|
2018-07-29 03:59:34 -04:00
|
|
|
void TrackSector::saveState(BareNetworkString* buffer) const
|
|
|
|
{
|
2018-12-24 23:08:28 -05:00
|
|
|
buffer->addUInt16((int16_t)m_current_graph_node);
|
|
|
|
buffer->addFloat(m_current_track_coords.getZ());
|
2018-07-29 03:59:34 -04:00
|
|
|
} // saveState
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void TrackSector::rewindTo(BareNetworkString* buffer)
|
|
|
|
{
|
2018-12-24 23:08:28 -05:00
|
|
|
int16_t node = buffer->getUInt16();
|
|
|
|
m_current_graph_node = node;
|
|
|
|
m_current_track_coords.setZ(buffer->getFloat());
|
2018-07-29 03:59:34 -04:00
|
|
|
} // rewindTo
|
2019-01-06 00:43:19 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Save completely for spectating in linear race
|
|
|
|
*/
|
|
|
|
void TrackSector::saveCompleteState(BareNetworkString* bns)
|
|
|
|
{
|
|
|
|
bns->addUInt32(m_current_graph_node);
|
|
|
|
bns->addUInt32(m_estimated_valid_graph_node);
|
|
|
|
bns->addUInt32(m_last_valid_graph_node);
|
|
|
|
bns->add(m_current_track_coords);
|
|
|
|
bns->add(m_estimated_valid_track_coords);
|
|
|
|
bns->add(m_latest_valid_track_coords);
|
2019-01-06 22:44:22 -05:00
|
|
|
bns->addUInt8(m_on_road ? 1 : 0);
|
2019-01-06 00:43:19 -05:00
|
|
|
bns->addUInt32(m_last_triggered_checkline);
|
|
|
|
} // saveCompleteState
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void TrackSector::restoreCompleteState(const BareNetworkString& b)
|
|
|
|
{
|
2019-04-09 22:16:23 -04:00
|
|
|
const int max_node = Graph::get()->getNumNodes();
|
2019-01-06 00:43:19 -05:00
|
|
|
m_current_graph_node = b.getUInt32();
|
2019-04-09 22:16:23 -04:00
|
|
|
if (m_current_graph_node >= max_node)
|
|
|
|
{
|
|
|
|
Log::warn("TrackSector", "Server has different graph node list.");
|
|
|
|
// 0 so that if any function is called before update track sector
|
|
|
|
// again it will have at least a valid node
|
|
|
|
m_current_graph_node = 0;
|
|
|
|
}
|
2019-01-06 00:43:19 -05:00
|
|
|
m_estimated_valid_graph_node = b.getUInt32();
|
2019-04-09 22:16:23 -04:00
|
|
|
if (m_estimated_valid_graph_node >= max_node)
|
|
|
|
{
|
|
|
|
Log::warn("TrackSector", "Server has different graph node list.");
|
|
|
|
m_estimated_valid_graph_node = 0;
|
|
|
|
}
|
2019-01-06 00:43:19 -05:00
|
|
|
m_last_valid_graph_node = b.getUInt32();
|
2019-04-09 22:16:23 -04:00
|
|
|
if (m_last_valid_graph_node >= max_node)
|
|
|
|
{
|
|
|
|
Log::warn("TrackSector", "Server has different graph node list.");
|
|
|
|
m_last_valid_graph_node = 0;
|
|
|
|
}
|
2019-01-06 00:43:19 -05:00
|
|
|
m_current_track_coords = b.getVec3();
|
|
|
|
m_estimated_valid_track_coords = b.getVec3();
|
|
|
|
m_latest_valid_track_coords = b.getVec3();
|
|
|
|
m_on_road = b.getUInt8() == 1;
|
|
|
|
m_last_triggered_checkline = b.getUInt32();
|
|
|
|
} // restoreCompleteState
|