1) Removed unused TrackInfo objects for the AI.

2) More code cleanup to use the new quad structure
   instead of driveliens.
3) Updates VS project file to work with latest
   GUI additions.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3515 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2009-05-18 04:08:00 +00:00
parent 5d47618ddb
commit d40f096127
12 changed files with 67 additions and 194 deletions

View File

@@ -222,8 +222,6 @@ supertuxkart_SOURCES = \
physics/triangle_mesh.hpp \
robots/default_robot.cpp \
robots/default_robot.hpp \
robots/track_info.cpp \
robots/track_info.hpp \
tracks/quad_graph.hpp \
tracks/quad_graph.cpp \
tracks/quad_set.hpp \

View File

@@ -252,6 +252,10 @@
<Filter
Name="gui"
>
<File
RelativePath="..\..\gui\credits.cpp"
>
</File>
<File
RelativePath="..\..\gui\engine.cpp"
>
@@ -316,10 +320,6 @@
RelativePath="../../../src\robots\default_robot.cpp"
>
</File>
<File
RelativePath="..\..\robots\track_info.cpp"
>
</File>
</Filter>
<Filter
Name="challenges"
@@ -782,6 +782,10 @@
<Filter
Name="gui"
>
<File
RelativePath="..\..\gui\credits.hpp"
>
</File>
<File
RelativePath="..\..\gui\font.hpp"
>
@@ -842,10 +846,6 @@
RelativePath="../../../src\robots\default_robot.hpp"
>
</File>
<File
RelativePath="..\..\robots\track_info.hpp"
>
</File>
</Filter>
<Filter
Name="challenges"

View File

@@ -610,8 +610,8 @@ void Kart::update(float dt)
if (getHoT()==Track::NOHIT) // kart falling off the track
{
// let kart fall a bit before rescuing
if( RaceManager::getTrack()->m_left_driveline.size() > 0 &&
fabs( getXYZ().getZ() - RaceManager::getTrack()->m_left_driveline[0].getZ() ) > 17)
if(fabs( getXYZ().getZ()
-RaceManager::getTrack()->getQuadGraph().getCenterOfQuad(0).getZ() ) > 17)
forceRescue();
}
else if(material)

View File

@@ -227,9 +227,6 @@ void LinearWorld::doLapCounting ( KartInfo& kart_info, Kart* kart )
bool newLap = kart_info.m_last_track_coords.getY() > track_length-delta &&
kart_info.m_curr_track_coords.getY() < delta;
// This fails if a kart skips a sector (or comes from the outside of the drivelines)
//const bool newLap = kart_info.m_last_valid_sector == (int)m_track->m_distance_from_start.size()-1 &&
// kart_info.m_track_sector == 0;
if ( newLap )
{
// Only increase the lap counter and set the new time if the
@@ -520,7 +517,7 @@ void LinearWorld::moveKartAfterRescue(Kart* kart, btRigidBody* body)
kart->setXYZ( m_track->trackToSpatial(info.m_track_sector) );
btQuaternion heading(btVector3(0.0f, 0.0f, 1.0f),
m_track->m_angle[info.m_track_sector] );
m_track->getAngle(info.m_track_sector) );
kart->setRotation(heading);
// A certain epsilon is added here to the Z coordinate (0.1), in case
@@ -530,7 +527,7 @@ void LinearWorld::moveKartAfterRescue(Kart* kart, btRigidBody* body)
btTransform pos;
pos.setOrigin(kart->getXYZ()+btVector3(0, 0, 0.5f*kart->getKartHeight()+0.1f));
pos.setRotation(btQuaternion(btVector3(0.0f, 0.0f, 1.0f),
m_track->m_angle[info.m_track_sector]));
m_track->getAngle(info.m_track_sector)));
body->setCenterOfMassTransform(pos);
@@ -608,9 +605,15 @@ void LinearWorld::checkForWrongDirection(unsigned int i)
if(!m) return;
const Kart *kart=m_kart[i];
// 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;
// check if the player is going in the wrong direction
float angle_diff = kart->getHPR().getHeading() -
m_track->m_angle[m_kart_info[i].m_track_sector];
m_track->getAngle(m_kart_info[i].m_track_sector);
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

View File

@@ -42,24 +42,15 @@
#include "graphics/scene.hpp"
#include "modes/linear_world.hpp"
#include "network/network_manager.hpp"
#include "robots/track_info.hpp"
#include "tracks/quad_graph.hpp"
#include "tracks/track.hpp"
#include "utils/constants.hpp"
const TrackInfo *DefaultRobot::m_track_info = NULL;
int DefaultRobot::m_num_of_track_info_instances = 0;
DefaultRobot::DefaultRobot(const std::string& kart_name,
int position, const btTransform& init_pos,
const Track *track ) :
AutoKart( kart_name, position, init_pos )
{
if(m_num_of_track_info_instances==0)
{
m_track_info = new TrackInfo(track);
m_num_of_track_info_instances++;
}
reset();
m_kart_length = m_kart_properties->getKartModel()->getLength();
m_kart_width = m_kart_properties->getKartModel()->getWidth();
@@ -130,12 +121,6 @@ DefaultRobot::DefaultRobot(const std::string& kart_name,
*/
DefaultRobot::~DefaultRobot()
{
m_num_of_track_info_instances--;
if(m_num_of_track_info_instances==0)
{
delete m_track_info;
}
} // ~DefaultRobot
//-----------------------------------------------------------------------------
@@ -267,8 +252,10 @@ void DefaultRobot::handleBraking()
//limit.
if ( m_crashes.m_road && kart_info.m_on_road && getVelocityLC().getY() > MIN_SPEED)
{
float kart_ang_diff = m_track->m_angle[m_track_sector] -
RAD_TO_DEGREE(getHPR().getHeading());
float kart_ang_diff =
m_quad_graph->getAngleToNext(m_track_sector,
m_successor_index[m_track_sector])
- getHPR().getHeading();
kart_ang_diff = normalizeAngle(kart_ang_diff);
kart_ang_diff = fabsf(kart_ang_diff);
@@ -729,7 +716,8 @@ void DefaultRobot::handleNitroAndZipper()
//-----------------------------------------------------------------------------
float DefaultRobot::steerToAngle(const size_t SECTOR, const float ANGLE)
{
float angle = m_track->m_angle[SECTOR];
float angle = m_quad_graph->getAngleToNext(SECTOR,
m_successor_index[SECTOR]);
//Desired angle minus current angle equals how many angles to turn
float steer_angle = angle - getHPR().getHeading();
@@ -1109,7 +1097,10 @@ void DefaultRobot::findCurve()
}
m_curve_angle = normalizeAngle(m_track->m_angle[i] - m_track->m_angle[m_track_sector]);
m_curve_angle =
normalizeAngle(m_quad_graph->getAngleToNext(i, m_successor_index[i])
-m_quad_graph->getAngleToNext(m_track_sector,
m_successor_index[m_track_sector]) );
m_inner_curve = m_curve_angle > 0.0 ? -1 : 1;
m_curve_target_speed = getMaxSpeedOnTerrain();

View File

@@ -26,7 +26,6 @@
class Track;
class LinearWorld;
class TrackInfo;
class QuadGraph;
class DefaultRobot : public AutoKart
@@ -143,13 +142,6 @@ private:
/** Cache width of kart. */
float m_kart_width;
/** All AIs share the track info object, so that its information needs
* only to be computed once. */
static const TrackInfo *m_track_info;
/** This counts how many AIs have a pointer to the TrackInfo object. If
* this number reaches zero, the shared TrackInfo object is
* deallocated. */
static int m_num_of_track_info_instances;
/** The minimum steering angle at which the AI adds skidding. Lower values
* tend to improve the line the AI is driving. This is used to adjust for

View File

@@ -1,74 +0,0 @@
// $Id: track_info.cpp 2547 2008-12-02 13:01:39Z hikerstk $
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2008 Joerg Henrichs
//
// 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 "robots/track_info.hpp"
#include "tracks/quad_graph.hpp"
#include "tracks/track.hpp"
#include "utils/constants.hpp"
TrackInfo::TrackInfo(const Track *track)
{
m_track = track;
setupSteerInfo();
} // TrackInfo
// ----------------------------------------------------------------------------
/** Creates the steer-info array.
*/
void TrackInfo::setupSteerInfo()
{
// First find the beginning of a new section, i.e. a place where the track
// direction changes from either straight to curve or the other way round.
int i = 0;
int num_drivelines = m_track->getQuadGraph().getNumNodes();
// not ready yet float current_angle = m_track->m_angle[i];
while(i<num_drivelines)
{
// not ready yetDirectionType dir = computeDirection(i);
i++;
}
} // setupSteerInfo
// ----------------------------------------------------------------------------
TrackInfo::DirectionType TrackInfo::computeDirection(int i)
{
int i_prev = i-1;
if(i_prev<0) i_prev = m_track->m_angle.size()-1;
float prev_angle = m_track->m_angle[i_prev];
float angle = m_track->m_angle[i];
float diff = prev_angle - angle;
while( diff> 2*M_PI ) diff -= 2*M_PI;
while( diff < -2*M_PI ) diff += 2*M_PI;
if( diff > M_PI ) diff -= 2*M_PI;
else if( diff < -M_PI ) diff+= 2*M_PI;
// Consider a difference of up to 5 degrees as 'straight'.
const float curve_degree = 5*M_PI/180.0f;
DirectionType t = DIR_STRAIGHT;
if (diff <-curve_degree)
t = DIR_LEFT;
else if (diff > curve_degree)
t = DIR_RIGHT;
return t;
} // computeDirection
// ----------------------------------------------------------------------------

View File

@@ -1,61 +0,0 @@
// $Id: track_info.hpp 2547 2008-12-02 13:01:39Z hikerstk $
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2008 Joerg Henrichs
//
// 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.
#ifndef HEADER_TRACK_INFO_HPP
#define HEADER_TRACK_INFO_HPP
#include <vector>
class Track;
/** This class is used to pre-compute some track information used by the AI,
* e.g. length of straight sections, turn radius etc.
* Each AI share this information, so it's only done once per track.
*/
class TrackInfo
{
/** The direction of the drivelines: straight ahead, or a turn. */
enum DirectionType {DIR_STRAIGHT, DIR_LEFT, DIR_RIGHT};
/** This class stores information for a set of consecutive driveline
* points. E.g points that form a straight line, or a left/right turn.
*/
class SteerInfo
{
public:
DirectionType m_type;
}; // SteerInfo
/** The list of all steer info objects. */
std::vector<SteerInfo> m_steer_info;
/** The mapping of driveline points to steer infos. Several driveline
* points will have the same steer info object. */
std::vector<int> m_driveline_2_steer_info;
/** Pointer to the track. */
const Track *m_track;
void setupSteerInfo();
DirectionType computeDirection(int i);
public:
TrackInfo(const Track *track);
}; // TrackInfo
#endif

View File

@@ -118,7 +118,11 @@ void QuadGraph::getSuccessors(int node_number, std::vector<unsigned int>& succ)
void QuadGraph::GraphNode::addSuccessor(int to, const QuadSet &quad_set)
{
m_vertices.push_back(to);
Vec3 diff = quad_set.getCenterOfQuad(to)
- quad_set.getCenterOfQuad(m_index);
Vec3 this_xyz = quad_set.getCenterOfQuad(m_index);
Vec3 next_xyz = quad_set.getCenterOfQuad(to);
Vec3 diff = next_xyz - this_xyz;
m_distance_to_next.push_back(diff.length());
float theta = -atan2(diff.getX(), diff.getY());
m_angle_to_next.push_back(theta);
} // addSuccessor

View File

@@ -37,6 +37,8 @@ class QuadGraph {
std::vector<int> m_vertices;
/** The distance to each of the successors. */
std::vector<float> m_distance_to_next;
/** The angle of the line from this node to each neighbour. */
std::vector<float> m_angle_to_next;
public:
/** Constructor, stores the index.
* \param index Index of this node in m_all_quads. */
@@ -50,6 +52,9 @@ class QuadGraph {
/** Returns the distance to the j-th. successor. */
float getDistanceToSuccessor(int j) const
{ return m_distance_to_next[j]; }
/** Returns the angle from this node to the j-th. successor. */
float getAngleToSuccessor(int j) const
{ return m_angle_to_next[j]; }
};
// ========================================================================
@@ -78,7 +83,13 @@ public:
/** Return the distance to the j-th successor of node n. */
float getDistanceToNext(int n, int j) const
{ return m_all_nodes[n]->getDistanceToSuccessor(j);}
/** Returns the angle of the line between node n and its j-th.
* successor. */
float getAngleToNext(int n, int j) const
{ return m_all_nodes[n]->getAngleToSuccessor(j); }
/** Returns the number of successors of a node n. */
int getNumberOfSuccessors(int n) const
{ return m_all_nodes[n]->getNumberOfSuccessors(); }
}; // QuadGraph
#endif

View File

@@ -952,6 +952,12 @@ void Track::loadDriveline()
{
m_quad_graph = new QuadGraph(file_manager->getTrackFile(m_ident+".quads"),
file_manager->getTrackFile(m_ident+".graph"));
if(m_quad_graph->getNumNodes()==0)
{
fprintf(stderr, "No graph nodes defined for track '%s'\n",
m_filename);
exit(-1);
}
readDrivelineFromFile(m_left_driveline, ".drvl");
const unsigned int DRIVELINE_SIZE = (unsigned int)m_left_driveline.size();
@@ -966,7 +972,6 @@ void Track::loadDriveline()
m_driveline.reserve(DRIVELINE_SIZE);
m_path_width.reserve(DRIVELINE_SIZE);
m_angle.reserve(DRIVELINE_SIZE);
for(unsigned int i = 0; i < DRIVELINE_SIZE; ++i)
{
Vec3 center_point = (m_left_driveline[i]+m_right_driveline[i])*0.5;
@@ -983,7 +988,6 @@ void Track::loadDriveline()
float dy = m_driveline[next].getY() - m_driveline[i].getY();
float theta = -atan2(dx, dy);
m_angle.push_back(theta);
}
m_driveline_min = Vec3( SG_MAX/2.0f);

View File

@@ -36,13 +36,13 @@ using namespace irr;
#include "material.hpp"
#include "audio/music_information.hpp"
#include "items/item.hpp"
#include "tracks/quad_graph.hpp"
#include "utils/vec3.hpp"
class TriangleMesh;
class MovingTexture;
class XMLNode;
class PhysicalObject;
class QuadGraph;
class Track
{
@@ -137,20 +137,16 @@ public:
//FIXME: should the driveline be set as a sgVec2?
private:
std::vector<Vec3> m_driveline;
public:
/** Same as drivelines, but with stk_config->m_offroad_tolerance applied
* to widen the road (to make shortcut detection less severe). */
std::vector<float> m_distance_from_start;
std::vector<float> m_path_width;
std::vector<float> m_angle;
//Left and Right drivelines for overhead map rendering.
std::vector<Vec3> m_left_driveline;
std::vector<Vec3> m_right_driveline;
public:
/** Start positions for arenas (unused in linear races) */
std::vector<Vec3> m_start_positions;
//Left and Right drivelines for overhead map rendering.
//(Should probably be private as they are only used internally right now)
std::vector<Vec3> m_left_driveline;
std::vector<Vec3> m_right_driveline;
Vec3 m_driveline_min;
Vec3 m_driveline_max;
@@ -226,6 +222,15 @@ public:
/** Returns the graph of quads, mainly for the AI. */
const QuadGraph& getQuadGraph() const { return *m_quad_graph; }
/** Returns 'a' angle for quad n. This angle is used to position a kart
* after a rescue, and to detect wrong directions. This function will
* always return the angle towards the first successor, i.e. the angle
* in the direction of the default way on the track.
* \param n Number of the quad for which the angle is asked.
*/
float getAngle(int n) const
{ return m_quad_graph->getAngleToNext(n, 0); }
/*
void glVtx (sgVec2 v, float x_offset, float y_offset) const
{