From 84e78e0d4093d965209dc15df7e4b32d770b9d15 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Tue, 14 Jul 2015 19:00:00 -0400 Subject: [PATCH] Some cleanup to check structures, start adding new cylinder check structure (WIP) --- src/tracks/check_cylinder.cpp | 67 ++++++++++++++++++++++++ src/tracks/check_cylinder.hpp | 66 +++++++++++++++++++++++ src/tracks/check_lap.cpp | 31 +++++++---- src/tracks/check_line.cpp | 18 +++++-- src/tracks/check_structure.cpp | 7 --- src/tracks/track_object_presentation.cpp | 4 +- 6 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 src/tracks/check_cylinder.cpp create mode 100644 src/tracks/check_cylinder.hpp diff --git a/src/tracks/check_cylinder.cpp b/src/tracks/check_cylinder.cpp new file mode 100644 index 000000000..dfe1111ff --- /dev/null +++ b/src/tracks/check_cylinder.cpp @@ -0,0 +1,67 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2009-2015 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 "tracks/check_cylinder.hpp" + +#include +#include + +#include "io/xml_node.hpp" +#include "modes/world.hpp" +#include "race/race_manager.hpp" + +CheckCylinder::CheckCylinder(const XMLNode &node, unsigned int index) + : CheckStructure(node, index) +{ + m_radius2 = 1; + m_height = 0; + node.get("height", &m_height); + node.get("radius", &m_radius2); + m_radius2 *= m_radius2; + node.get("xyz", &m_center_point); + unsigned int num_karts = race_manager->getNumberOfKarts(); + m_is_inside.resize(num_karts); + m_distance2.resize(num_karts); + for (unsigned int i=0; i=m_radius2 && new_dist2 < m_radius2) || + (old_dist2< m_radius2 && new_dist2 >=m_radius2); +} // isTriggered diff --git a/src/tracks/check_cylinder.hpp b/src/tracks/check_cylinder.hpp new file mode 100644 index 000000000..6dede2312 --- /dev/null +++ b/src/tracks/check_cylinder.hpp @@ -0,0 +1,66 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2009-2015 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_CHECK_CYLINDER_HPP +#define HEADER_CHECK_CYLINDER_HPP + +#include "tracks/check_structure.hpp" + +class XMLNode; +class CheckManager; + +/** This class implements a check sphere that is used to change the ambient + * light if a kart is inside this sphere. Besides a normal radius this + * sphere also has a 2nd 'inner' radius: player karts inside the inner + * radius will have the full new ambient light, karts outside the default + * light, and karts in between will mix the light dependent on distance. + * + * \ingroup tracks + */ +class CheckCylinder : public CheckStructure +{ +private: + /** Center of the sphere. */ + Vec3 m_center_point; + /** Squared radius of the cylinder. */ + float m_radius2; + float m_height; + /** A flag for each kart to indicate if it's inside of the sphere. */ + std::vector m_is_inside; + /** Stores the distance of each kart from the center of this sphere. + * This saves some computations. */ + std::vector m_distance2; +public: + CheckCylinder(const XMLNode &node, unsigned int index); + virtual ~CheckCylinder() {}; + virtual bool isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, + unsigned int kart_id); + // ------------------------------------------------------------------------ + /** Returns if kart indx is currently inside of the sphere. */ + bool isInside(int index) const { return m_is_inside[index]; } + // ------------------------------------------------------------------------- + /** Returns the squared distance of kart index from the enter of + * this sphere. */ + float getDistance2ForKart(int index) const { return m_distance2[index];} + // ------------------------------------------------------------------------- + /** Returns the square of the radius of this sphere. */ + float getRadius2() const { return m_radius2; } +}; // CheckCylinder + +#endif + diff --git a/src/tracks/check_lap.cpp b/src/tracks/check_lap.cpp index 016c987d5..dad3c1a9a 100644 --- a/src/tracks/check_lap.cpp +++ b/src/tracks/check_lap.cpp @@ -55,27 +55,36 @@ void CheckLap::reset(const Track &track) * 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 + * \param kart_index Index of the kart, can be used to store kart specific * additional data. */ bool CheckLap::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, - unsigned int indx) + unsigned int kart_index) { - float track_length = World::getWorld()->getTrack()->getTrackLength(); - LinearWorld *lin_world = dynamic_cast(World::getWorld()); + World* w = World::getWorld(); + LinearWorld* lin_world = dynamic_cast(w); + if (lin_world != NULL) + { + lin_world->getTrackSector(kart_index).setLastTriggeredCheckline(m_index); + } + + float track_length = w->getTrack()->getTrackLength(); // Can happen if a non-lap based race mode is used with a scene file that // has check defined. if(!lin_world) return false; - float current_distance = lin_world->getDistanceDownTrackForKart(indx); - bool result =(m_previous_distance[indx]>0.95f*track_length && + float current_distance = lin_world->getDistanceDownTrackForKart(kart_index); + bool result = (m_previous_distance[kart_index]>0.95f*track_length && current_distance<7.0f); - if(UserConfigParams::m_check_debug && result) - Log::info("CheckLap", "Kart %s crossed start line from %f to %f.", - World::getWorld()->getKart(indx)->getIdent().c_str(), - m_previous_distance[indx], current_distance); - m_previous_distance[indx] = current_distance; + if (UserConfigParams::m_check_debug && result) + { + Log::info("CheckLap", "Kart %s crossed start line from %f to %f.", + World::getWorld()->getKart(kart_index)->getIdent().c_str(), + m_previous_distance[kart_index], current_distance); + } + + m_previous_distance[kart_index] = current_distance; return result; } // isTriggered diff --git a/src/tracks/check_line.cpp b/src/tracks/check_line.cpp index 46d06bed3..b04e9a298 100644 --- a/src/tracks/check_line.cpp +++ b/src/tracks/check_line.cpp @@ -22,6 +22,7 @@ #include "graphics/irr_driver.hpp" #include "io/xml_node.hpp" #include "karts/abstract_kart.hpp" +#include "modes/linear_world.hpp" #include "modes/world.hpp" #include "race/race_manager.hpp" @@ -151,14 +152,21 @@ void CheckLine::changeDebugColor(bool is_active) * additional data. */ bool CheckLine::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, - unsigned int indx) + unsigned int kart_index) { + World* w = World::getWorld(); + LinearWorld* lw = dynamic_cast(w); + if (lw != NULL) + { + lw->getTrackSector(kart_index).setLastTriggeredCheckline(m_index); + } + core::vector2df p=new_pos.toIrrVector2d(); bool sign = m_line.getPointOrientation(p)>=0; bool result; // If the sign has changed, i.e. the infinite line was crossed somewhere, // check if the finite line was actually crossed: - if(sign!=m_previous_sign[indx] && + if (sign != m_previous_sign[kart_index] && m_line.intersectWith(core::line2df(old_pos.toIrrVector2d(), new_pos.toIrrVector2d()), m_cross_point) ) @@ -175,17 +183,17 @@ bool CheckLine::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, if(World::getWorld()->getNumKarts()>0) Log::info("CheckLine", "Kart %s crosses line, but wrong height " "(%f vs %f).", - World::getWorld()->getKart(indx)->getIdent().c_str(), + World::getWorld()->getKart(kart_index)->getIdent().c_str(), new_pos.getY(), m_min_height); else Log::info("CheckLine", "Kart %d crosses line, but wrong height " "(%f vs %f).", - indx, new_pos.getY(), m_min_height); + kart_index, new_pos.getY(), m_min_height); } } else result = false; - m_previous_sign[indx] = sign; + m_previous_sign[kart_index] = sign; return result; } // isTriggered diff --git a/src/tracks/check_structure.cpp b/src/tracks/check_structure.cpp index c72421e27..9f2c81788 100644 --- a/src/tracks/check_structure.cpp +++ b/src/tracks/check_structure.cpp @@ -198,13 +198,6 @@ void CheckStructure::changeStatus(const std::vector &indices, */ void CheckStructure::trigger(unsigned int kart_index) { - World* w = World::getWorld(); - LinearWorld* lw = dynamic_cast(w); - if (lw != NULL) - { - lw->getTrackSector(kart_index).setLastTriggeredCheckline(m_index); - } - switch(m_check_type) { case CT_NEW_LAP : diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index 9336008c3..a69909dbd 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -40,6 +40,7 @@ #include "modes/world.hpp" #include "scriptengine/script_engine.hpp" #include "states_screens/dialogs/tutorial_message_dialog.hpp" +#include "tracks/check_cylinder.hpp" #include "tracks/check_manager.hpp" #include "tracks/check_sphere.hpp" #include "tracks/model_definition_loader.hpp" @@ -961,11 +962,12 @@ TrackObjectPresentationActionTrigger::TrackObjectPresentationActionTrigger( { // TODO: rewrite as a sphere check structure? ItemManager::get()->newItem(m_init_xyz, trigger_distance, this); + // CheckManager::get()->add(new CheckSphere(xml_node, 0 /* TODO what is this? */)); } else if (m_type == TRIGGER_TYPE_CYLINDER) { // TODO: create the right check structure - CheckManager::get()->add(new CheckSphere(xml_node, 0 /* TODO what is this? */)); + CheckManager::get()->add(new CheckCylinder(xml_node, 0 /* TODO what is this? */)); } else {