2009-07-06 09:35:33 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2009-2015 Joerg Henrichs
|
2009-07-06 09:35:33 -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.
|
|
|
|
|
2009-09-16 22:54:48 -04:00
|
|
|
#include "tracks/check_line.hpp"
|
2009-07-06 09:35:33 -04:00
|
|
|
|
2014-02-25 20:52:16 -05:00
|
|
|
#include "config/user_config.hpp"
|
2012-05-08 03:07:15 -04:00
|
|
|
#include "graphics/irr_driver.hpp"
|
2017-01-06 02:04:15 -05:00
|
|
|
#include "graphics/stk_tex_manager.hpp"
|
2009-07-06 09:35:33 -04:00
|
|
|
#include "io/xml_node.hpp"
|
2012-03-19 16:21:11 -04:00
|
|
|
#include "karts/abstract_kart.hpp"
|
2015-07-14 19:00:00 -04:00
|
|
|
#include "modes/linear_world.hpp"
|
2010-02-10 06:40:33 -05:00
|
|
|
#include "modes/world.hpp"
|
2009-07-06 09:35:33 -04:00
|
|
|
#include "race/race_manager.hpp"
|
|
|
|
|
2012-04-18 18:07:45 -04:00
|
|
|
#include "irrlicht.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-08-24 10:45:27 -04:00
|
|
|
#include <cstdint>
|
2012-04-18 18:07:45 -04:00
|
|
|
#include <string>
|
|
|
|
|
2013-05-29 18:04:35 -04:00
|
|
|
/** Constructor for a checkline.
|
2009-07-08 08:33:22 -04:00
|
|
|
* \param node XML node containing the parameters for this checkline.
|
2012-02-12 16:25:06 -05:00
|
|
|
* \param index Index of this check structure in the check manager.
|
2009-07-08 08:33:22 -04:00
|
|
|
*/
|
2013-05-29 18:04:35 -04:00
|
|
|
CheckLine::CheckLine(const XMLNode &node, unsigned int index)
|
2012-02-12 16:25:06 -05:00
|
|
|
: CheckStructure(node, index)
|
2009-07-06 09:35:33 -04:00
|
|
|
{
|
2010-02-10 06:40:33 -05:00
|
|
|
// Note that when this is called the karts have not been allocated
|
|
|
|
// in world, so we can't call world->getNumKarts()
|
|
|
|
m_previous_sign.resize(race_manager->getNumberOfKarts());
|
2012-04-29 19:08:50 -04:00
|
|
|
std::string p1_string("p1");
|
|
|
|
std::string p2_string("p2");
|
|
|
|
|
2013-05-29 18:04:35 -04:00
|
|
|
// In case of a cannon in a reverse track, we have to use the target line
|
2012-04-29 19:08:50 -04:00
|
|
|
// as check line
|
|
|
|
if(getType()==CT_CANNON && race_manager->getReverseTrack())
|
|
|
|
{
|
|
|
|
p1_string = "target-p1";
|
|
|
|
p2_string = "target-p2";
|
|
|
|
}
|
2009-07-06 09:35:33 -04:00
|
|
|
core::vector2df p1, p2;
|
2012-04-29 19:08:50 -04:00
|
|
|
if(node.get(p1_string, &p1) &&
|
|
|
|
node.get(p2_string, &p2) &&
|
2012-04-18 18:07:45 -04:00
|
|
|
node.get("min-height", &m_min_height))
|
|
|
|
{
|
|
|
|
m_left_point = Vec3(p1.X, m_min_height, p1.Y);
|
|
|
|
m_right_point = Vec3(p2.X, m_min_height, p2.Y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-29 19:08:50 -04:00
|
|
|
node.get(p1_string, &m_left_point);
|
2012-04-18 18:07:45 -04:00
|
|
|
p1 = core::vector2df(m_left_point.getX(), m_left_point.getZ());
|
2012-04-29 19:08:50 -04:00
|
|
|
node.get(p2_string, &m_right_point);
|
2012-04-18 18:07:45 -04:00
|
|
|
p2 = core::vector2df(m_right_point.getX(), m_right_point.getZ());
|
|
|
|
m_min_height = std::min(m_left_point.getY(), m_right_point.getY());
|
|
|
|
}
|
2009-07-06 09:35:33 -04:00
|
|
|
m_line.setLine(p1, p2);
|
2010-11-28 17:08:02 -05:00
|
|
|
if(UserConfigParams::m_check_debug)
|
|
|
|
{
|
2016-04-13 10:49:18 -04:00
|
|
|
#ifndef SERVER_ONLY
|
2010-11-28 17:08:02 -05:00
|
|
|
video::SMaterial material;
|
|
|
|
material.setFlag(video::EMF_BACK_FACE_CULLING, false);
|
2010-11-29 00:26:45 -05:00
|
|
|
material.setFlag(video::EMF_LIGHTING, false);
|
2010-11-28 17:08:02 -05:00
|
|
|
material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
|
2013-05-29 18:04:35 -04:00
|
|
|
scene::IMesh *mesh = irr_driver->createQuadMesh(&material,
|
2010-11-28 17:08:02 -05:00
|
|
|
/*create mesh*/true);
|
|
|
|
scene::IMeshBuffer *buffer = mesh->getMeshBuffer(0);
|
2015-08-14 09:14:19 -04:00
|
|
|
|
2010-12-15 17:31:53 -05:00
|
|
|
assert(buffer->getVertexType()==video::EVT_STANDARD);
|
2013-05-29 18:04:35 -04:00
|
|
|
irr::video::S3DVertex* vertices
|
2010-12-15 17:31:53 -05:00
|
|
|
= (video::S3DVertex*)buffer->getVertices();
|
2013-05-29 18:04:35 -04:00
|
|
|
vertices[0].Pos = core::vector3df(p1.X,
|
|
|
|
m_min_height-m_under_min_height,
|
2010-12-15 17:31:53 -05:00
|
|
|
p1.Y);
|
2013-05-29 18:04:35 -04:00
|
|
|
vertices[1].Pos = core::vector3df(p2.X,
|
|
|
|
m_min_height-m_under_min_height,
|
2010-12-15 17:31:53 -05:00
|
|
|
p2.Y);
|
2013-05-29 18:04:35 -04:00
|
|
|
vertices[2].Pos = core::vector3df(p2.X,
|
|
|
|
m_min_height+m_over_min_height,
|
2010-12-15 17:31:53 -05:00
|
|
|
p2.Y);
|
2013-05-29 18:04:35 -04:00
|
|
|
vertices[3].Pos = core::vector3df(p1.X,
|
|
|
|
m_min_height+m_over_min_height,
|
2010-12-15 17:31:53 -05:00
|
|
|
p1.Y);
|
|
|
|
for(unsigned int i=0; i<4; i++)
|
|
|
|
{
|
2013-05-29 18:04:35 -04:00
|
|
|
vertices[i].Color = m_active_at_reset
|
2015-08-14 09:14:19 -04:00
|
|
|
? video::SColor(128, 255, 0, 0)
|
|
|
|
: video::SColor(128, 128, 128, 128);
|
2010-12-15 17:31:53 -05:00
|
|
|
}
|
|
|
|
buffer->recalculateBoundingBox();
|
2017-01-06 02:04:15 -05:00
|
|
|
buffer->getMaterial().setTexture(0, STKTexManager::getInstance()->getUnicolorTexture(video::SColor(128, 255, 105, 180)));
|
|
|
|
buffer->getMaterial().setTexture(1, STKTexManager::getInstance()->getUnicolorTexture(video::SColor(0, 0, 0, 0)));
|
|
|
|
buffer->getMaterial().setTexture(2, STKTexManager::getInstance()->getUnicolorTexture(video::SColor(0, 0, 0, 0)));
|
2015-08-14 09:14:19 -04:00
|
|
|
buffer->getMaterial().BackfaceCulling = false;
|
|
|
|
//mesh->setBoundingBox(buffer->getBoundingBox());
|
2014-10-24 19:25:24 -04:00
|
|
|
m_debug_node = irr_driver->addMesh(mesh, "checkdebug");
|
2010-11-28 17:08:02 -05:00
|
|
|
mesh->drop();
|
2016-04-13 10:49:18 -04:00
|
|
|
#endif
|
2010-11-28 17:08:02 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_debug_node = NULL;
|
|
|
|
}
|
2009-09-16 22:54:48 -04:00
|
|
|
} // CheckLine
|
2009-07-06 09:35:33 -04:00
|
|
|
|
2010-11-28 17:08:02 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
CheckLine::~CheckLine()
|
|
|
|
{
|
|
|
|
if(m_debug_node)
|
2010-11-29 06:47:15 -05:00
|
|
|
irr_driver->removeNode(m_debug_node);
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2010-11-28 17:08:02 -05:00
|
|
|
} // CheckLine
|
2009-07-06 09:35:33 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-09-16 22:54:48 -04:00
|
|
|
void CheckLine::reset(const Track &track)
|
2009-07-06 09:35:33 -04:00
|
|
|
{
|
|
|
|
CheckStructure::reset(track);
|
2015-08-06 20:24:20 -04:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i<m_previous_sign.size(); i++)
|
2009-07-06 09:35:33 -04:00
|
|
|
{
|
|
|
|
core::vector2df p = m_previous_position[i].toIrrVector2d();
|
2015-08-06 20:24:20 -04:00
|
|
|
m_previous_sign[i] = m_line.getPointOrientation(p) >= 0;
|
2009-07-06 09:35:33 -04:00
|
|
|
}
|
|
|
|
} // reset
|
|
|
|
|
2017-01-19 22:13:12 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void CheckLine::resetAfterKartMove(unsigned int kart_index)
|
|
|
|
{
|
|
|
|
AbstractKart *kart = World::getWorld()->getKart(kart_index);
|
|
|
|
m_previous_position[kart_index] = kart->getXYZ();
|
|
|
|
} // resetAfterKartMove
|
|
|
|
|
2010-11-29 00:26:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void CheckLine::changeDebugColor(bool is_active)
|
|
|
|
{
|
|
|
|
assert(m_debug_node);
|
|
|
|
|
|
|
|
scene::IMesh *mesh = m_debug_node->getMesh();
|
|
|
|
scene::IMeshBuffer *buffer = mesh->getMeshBuffer(0);
|
2013-05-29 18:04:35 -04:00
|
|
|
irr::video::S3DVertex* vertices
|
2010-11-29 00:26:45 -05:00
|
|
|
= (video::S3DVertex*)buffer->getVertices();
|
2015-08-14 09:14:19 -04:00
|
|
|
video::SColor color = is_active ? video::SColor(192, 255, 0, 0)
|
|
|
|
: video::SColor(192, 128, 128, 128);
|
2010-11-29 00:26:45 -05:00
|
|
|
for(unsigned int i=0; i<4; i++)
|
|
|
|
{
|
2015-08-14 09:14:19 -04:00
|
|
|
vertices[i].Color = color;
|
2010-11-29 00:26:45 -05:00
|
|
|
}
|
2016-04-13 10:49:18 -04:00
|
|
|
#ifndef SERVER_ONLY
|
2017-01-06 02:04:15 -05:00
|
|
|
buffer->getMaterial().setTexture(0, STKTexManager::getInstance()->getUnicolorTexture(color));
|
2016-04-13 10:49:18 -04:00
|
|
|
#endif
|
2015-08-14 09:14:19 -04:00
|
|
|
|
2010-11-29 00:26:45 -05:00
|
|
|
} // changeDebugColor
|
|
|
|
|
2009-07-06 09:35:33 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** True if going from old_pos to new_pos crosses this checkline. This function
|
|
|
|
* is called from update (of the checkline structure).
|
|
|
|
* \param old_pos Position in previous frame.
|
|
|
|
* \param new_pos Position in current frame.
|
|
|
|
* \param indx Index of the kart, can be used to store kart specific
|
|
|
|
* additional data.
|
|
|
|
*/
|
2013-05-29 18:04:35 -04:00
|
|
|
bool CheckLine::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
2017-01-19 22:13:12 -05:00
|
|
|
unsigned int kart_index)
|
2009-07-06 09:35:33 -04:00
|
|
|
{
|
2015-07-14 19:00:00 -04:00
|
|
|
World* w = World::getWorld();
|
2009-07-06 09:35:33 -04:00
|
|
|
core::vector2df p=new_pos.toIrrVector2d();
|
|
|
|
bool sign = m_line.getPointOrientation(p)>=0;
|
2013-04-17 08:09:04 -04:00
|
|
|
bool result;
|
2015-08-06 20:24:20 -04:00
|
|
|
|
|
|
|
bool previous_sign;
|
|
|
|
|
2016-08-24 10:45:27 -04:00
|
|
|
if (kart_index == UINT_MAX)
|
2015-08-06 20:24:20 -04:00
|
|
|
{
|
|
|
|
core::vector2df p = old_pos.toIrrVector2d();
|
|
|
|
previous_sign = (m_line.getPointOrientation(p) >= 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
previous_sign = m_previous_sign[kart_index];
|
|
|
|
}
|
|
|
|
|
2009-07-06 09:35:33 -04:00
|
|
|
// If the sign has changed, i.e. the infinite line was crossed somewhere,
|
|
|
|
// check if the finite line was actually crossed:
|
2015-08-06 20:24:20 -04:00
|
|
|
if (sign != previous_sign &&
|
2013-05-29 18:04:35 -04:00
|
|
|
m_line.intersectWith(core::line2df(old_pos.toIrrVector2d(),
|
|
|
|
new_pos.toIrrVector2d()),
|
2012-04-18 09:14:18 -04:00
|
|
|
m_cross_point) )
|
2009-07-06 09:35:33 -04:00
|
|
|
{
|
|
|
|
// Now check the minimum height: the kart position must be within a
|
|
|
|
// reasonable distance in the Z axis - 'reasonable' for now to be
|
|
|
|
// between -1 and 4 units (negative numbers are unlikely, but help
|
2012-04-18 09:14:18 -04:00
|
|
|
// in case that the kart is 'somewhat' inside of the track, or the
|
2009-07-06 09:35:33 -04:00
|
|
|
// checklines are a bit off in Z direction.
|
2013-05-29 18:04:35 -04:00
|
|
|
result = new_pos.getY()-m_min_height<m_over_min_height &&
|
2010-11-28 17:08:02 -05:00
|
|
|
new_pos.getY()-m_min_height>-m_under_min_height;
|
2010-09-12 19:42:28 -04:00
|
|
|
if(UserConfigParams::m_check_debug && !result)
|
2010-08-02 19:25:35 -04:00
|
|
|
{
|
2012-04-03 18:27:11 -04:00
|
|
|
if(World::getWorld()->getNumKarts()>0)
|
2014-07-19 14:43:19 -04:00
|
|
|
Log::info("CheckLine", "Kart %s crosses line, but wrong height "
|
|
|
|
"(%f vs %f).",
|
2015-07-14 19:00:00 -04:00
|
|
|
World::getWorld()->getKart(kart_index)->getIdent().c_str(),
|
2014-07-19 14:43:19 -04:00
|
|
|
new_pos.getY(), m_min_height);
|
2012-04-03 18:27:11 -04:00
|
|
|
else
|
2014-07-19 14:43:19 -04:00
|
|
|
Log::info("CheckLine", "Kart %d crosses line, but wrong height "
|
|
|
|
"(%f vs %f).",
|
2015-07-14 19:00:00 -04:00
|
|
|
kart_index, new_pos.getY(), m_min_height);
|
2012-04-03 18:27:11 -04:00
|
|
|
|
2010-08-02 19:25:35 -04:00
|
|
|
}
|
2009-07-06 09:35:33 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = false;
|
2015-07-16 19:38:34 -04:00
|
|
|
|
2016-08-24 10:45:27 -04:00
|
|
|
if (kart_index != UINT_MAX)
|
2015-08-06 20:24:20 -04:00
|
|
|
m_previous_sign[kart_index] = sign;
|
|
|
|
|
2016-08-24 10:45:27 -04:00
|
|
|
if (result && kart_index != UINT_MAX)
|
2015-07-16 19:38:34 -04:00
|
|
|
{
|
|
|
|
LinearWorld* lw = dynamic_cast<LinearWorld*>(w);
|
|
|
|
if (lw != NULL)
|
|
|
|
lw->setLastTriggeredCheckline(kart_index, m_index);
|
|
|
|
}
|
2009-07-06 09:35:33 -04:00
|
|
|
return result;
|
2009-07-08 08:33:22 -04:00
|
|
|
} // isTriggered
|