Removed code duplication by using macros.

This commit is contained in:
hiker 2017-05-22 12:38:21 +10:00
parent fb5a600787
commit 742be06c21
8 changed files with 118 additions and 170 deletions

View File

@ -85,20 +85,23 @@ public:
AIBaseController(AbstractKart *kart); AIBaseController(AbstractKart *kart);
virtual ~AIBaseController() {}; virtual ~AIBaseController() {};
virtual void reset(); virtual void reset();
virtual bool disableSlipstreamBonus() const; virtual bool disableSlipstreamBonus() const OVERRIDE;
virtual void crashed(const Material *m); virtual void crashed(const Material *m) OVERRIDE;
static void enableDebug() {m_ai_debug = true; } static void enableDebug() {m_ai_debug = true; }
static void setTestAI(int n) {m_test_ai = n; } static void setTestAI(int n) {m_test_ai = n; }
static int getTestAI() { return m_test_ai; } static int getTestAI() { return m_test_ai; }
virtual void crashed(const AbstractKart *k) {}; virtual void crashed(const AbstractKart *k) OVERRIDE {};
virtual void handleZipper(bool play_sound) {}; virtual void handleZipper(bool play_sound) OVERRIDE {};
virtual void finishedRace(float time) {}; virtual void finishedRace(float time) OVERRIDE {};
virtual void collectedItem(const Item &item, int add_info=-1, virtual void collectedItem(const Item &item, int add_info=-1,
float previous_energy=0) {}; float previous_energy=0) OVERRIDE {};
virtual void setPosition(int p) {}; virtual void setPosition(int p) OVERRIDE {};
virtual bool isPlayerController() const { return false; } virtual bool isPlayerController() const OVERRIDE { return false; }
virtual bool isLocalPlayerController() const { return false; } virtual bool isLocalPlayerController() const OVERRIDE { return false; }
virtual void action(PlayerAction action, int value) {}; virtual bool action(PlayerAction action, int value, bool dry_run=false) OVERRIDE
{
return true;
};
virtual void skidBonusTriggered() {}; virtual void skidBonusTriggered() {};
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Not used for AIs. */ /** Not used for AIs. */

View File

@ -89,7 +89,7 @@ public:
const std::string &getControllerName() const { return m_controller_name; } const std::string &getControllerName() const { return m_controller_name; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Default: ignore actions. Only PlayerController get them. */ /** Default: ignore actions. Only PlayerController get them. */
virtual void action(PlayerAction action, int value) = 0; virtual bool action(PlayerAction action, int value, bool dry_run=false) = 0;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Callback whenever a new lap is triggered. Used by the AI /** Callback whenever a new lap is triggered. Used by the AI
* to trigger a recomputation of the way to use. */ * to trigger a recomputation of the way to use. */

View File

@ -81,9 +81,10 @@ void GhostController::addReplayTime(float time)
} // addReplayTime } // addReplayTime
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void GhostController::action(PlayerAction action, int value) bool GhostController::action(PlayerAction action, int value, bool dry_run)
{ {
// Watching replay use only // Watching replay use only
if (action == PA_LOOK_BACK) if (action == PA_LOOK_BACK)
m_controls->setLookBack(value!=0); m_controls->setLookBack(value!=0);
return true;
} // action } // action

View File

@ -55,7 +55,8 @@ public:
virtual void setPosition(int p) OVERRIDE {} virtual void setPosition(int p) OVERRIDE {}
virtual bool isPlayerController() const OVERRIDE { return false; } virtual bool isPlayerController() const OVERRIDE { return false; }
virtual bool isLocalPlayerController() const OVERRIDE { return false; } virtual bool isLocalPlayerController() const OVERRIDE { return false; }
virtual void action(PlayerAction action, int value) OVERRIDE; virtual bool action(PlayerAction action, int value,
bool dry_run=false) OVERRIDE;
virtual void skidBonusTriggered() OVERRIDE {} virtual void skidBonusTriggered() OVERRIDE {}
virtual void newLap(int lap) OVERRIDE {} virtual void newLap(int lap) OVERRIDE {}
virtual void saveState(BareNetworkString *buffer) const {}; virtual void saveState(BareNetworkString *buffer) const {};

View File

@ -139,14 +139,18 @@ void LocalPlayerController::resetInputState()
* if between 1 and 32767, it indicates an analog value, * if between 1 and 32767, it indicates an analog value,
* and if it's 0 it indicates that the corresponding button * and if it's 0 it indicates that the corresponding button
* was released. * was released.
* \param dry_run If set it will return if this action will trigger a
* state change or not.
* \return True if dry_run==true and a state change would be triggered.
* If dry_run==false, it returns true.
*/ */
void LocalPlayerController::action(PlayerAction action, int value) bool LocalPlayerController::action(PlayerAction action, int value,
bool dry_run)
{ {
// If this event does not change the control state (e.g. // If this event does not change the control state (e.g.
// it's a (auto) repeat event), do nothing. This especially // it's a (auto) repeat event), do nothing. This especially
// optimises traffic to the server and other clients. // optimises traffic to the server and other clients.
if (!actionChangesState(action, value)) if (!PlayerController::action(action, value, /*dry_run*/true)) return false;
return;
// If this is a client, send the action to networking layer // If this is a client, send the action to networking layer
if (World::getWorld()->isNetworkWorld() && if (World::getWorld()->isNetworkWorld() &&
@ -158,7 +162,7 @@ void LocalPlayerController::action(PlayerAction action, int value)
action, value, action, value,
m_steer_val_l, m_steer_val_r); m_steer_val_l, m_steer_val_r);
} }
PlayerController::action(action, value); return PlayerController::action(action, value, /*dry_run*/false);
} // action } // action
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -60,7 +60,8 @@ public:
StateManager::ActivePlayer *player); StateManager::ActivePlayer *player);
~LocalPlayerController(); ~LocalPlayerController();
void update (float) OVERRIDE; void update (float) OVERRIDE;
void action (PlayerAction action, int value) OVERRIDE; bool action (PlayerAction action, int value,
bool dry_run=false) OVERRIDE;
virtual void handleZipper (bool play_sound) OVERRIDE; virtual void handleZipper (bool play_sound) OVERRIDE;
void collectedItem (const Item &item, int add_info=-1, void collectedItem (const Item &item, int add_info=-1,
float previous_energy=0) OVERRIDE; float previous_energy=0) OVERRIDE;

View File

@ -78,115 +78,6 @@ void PlayerController::resetInputState()
m_controls->reset(); m_controls->reset();
} // resetInputState } // resetInputState
// ----------------------------------------------------------------------------
/** Returns true if the specified action and value will trigger a state
* change. If a new action from the player does not cause a state change,
* there is no need to forward that action to the server (or other clients).
* NOTE: this function must close mirror action(), make sure both functions
* are changed in synch.
* \param action The action to be executed.
* \param value If 32768, it indicates a digital value of 'fully set'
* if between 1 and 32767, it indicates an analog value,
* and if it's 0 it indicates that the corresponding button
* was released.
* \return
*/
bool PlayerController::actionChangesState(PlayerAction action, int value)
{
switch (action)
{
case PA_STEER_LEFT:
if (m_steer_val_l != value) return true;
if (value)
{
if (m_steer_val != value) return true;
return (m_controls->getSkidControl() == KartControl::SC_NO_DIRECTION);
}
else
return m_steer_val != m_steer_val_r;
break;
case PA_STEER_RIGHT:
if (m_steer_val_r != -value) return true;
if (value)
{
if(m_steer_val != -value) return true;
return m_controls->getSkidControl() == KartControl::SC_NO_DIRECTION;
}
else
return m_steer_val != m_steer_val_l;
break;
case PA_ACCEL:
if (m_prev_accel != value) return true;
if (value && !(m_penalty_time > 0.0f))
{
if (m_controls->getAccel() != value / 32768.f) return true;
if (m_controls->getBrake()) return true;
return m_controls->getNitro() != m_prev_nitro;
}
else
{
if (m_controls->getAccel() != 0 ) return true;
if (m_controls->getBrake() != m_prev_brake) return true;
return m_controls->getNitro();
}
break;
case PA_BRAKE:
if (m_prev_brake != (value != 0) ) return true;
// let's consider below that to be a deadzone
if (value > 32768 / 2)
{
if (!m_controls->getBrake() ) return true;
if (m_controls->getAccel() != 0) return true;
return m_controls->getNitro();
}
else
{
if (m_controls->getBrake() ) return true;
if (m_controls->getAccel() != m_prev_accel/32768.0f) return true;
// Nitro still depends on whether we're accelerating
return m_controls->getNitro() != m_prev_nitro && m_prev_accel;
}
break;
case PA_NITRO:
// This basically keeps track whether the button still is being pressed
if (m_prev_nitro != (value != 0)) return true;
// Enable nitro only when also accelerating
return m_controls->getNitro() != ( (value != 0) &&
m_controls->getAccel() );
break;
case PA_RESCUE:
return m_controls->getRescue() != (value != 0);
break;
case PA_FIRE:
return m_controls->getFire() != (value != 0);
break;
case PA_LOOK_BACK:
return m_controls->getLookBack() != (value != 0);
break;
case PA_DRIFT:
if (value == 0)
return m_controls->getSkidControl() != KartControl::SC_NONE;
else
{
if (m_steer_val == 0)
return m_controls->getSkidControl() != KartControl::SC_NO_DIRECTION;
else
return m_controls->getSkidControl() != (m_steer_val<0
? KartControl::SC_RIGHT
: KartControl::SC_LEFT );
}
break;
case PA_PAUSE_RACE:
if (value != 0) StateManager::get()->escapePressed();
break;
default:
break;
}
return true;
} // actionChangesState
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** This function interprets a kart action and value, and set the corresponding /** This function interprets a kart action and value, and set the corresponding
* entries in the kart control data structure. This function handles esp. * entries in the kart control data structure. This function handles esp.
@ -194,103 +85,147 @@ void PlayerController::resetInputState()
* releasing right, the steering must switch to left again. Similarly it * releasing right, the steering must switch to left again. Similarly it
* handles 'press left, press right, release left' (in which case still * handles 'press left, press right, release left' (in which case still
* right must be selected). Similarly for braking and acceleration. * right must be selected). Similarly for braking and acceleration.
* NOTE: this function must close mirror action(), make sure both functions * This function can be run in two modes: first, if 'dry_run' is set,
* are changed in synch. * it will return true if this action will cause a state change. This
* is sued in networking to avoid sending events to the server (and then
* to other clients) if they are just (e.g. auto) repeated events/
* \param action The action to be executed. * \param action The action to be executed.
* \param value If 32768, it indicates a digital value of 'fully set' * \param value If 32768, it indicates a digital value of 'fully set'
* if between 1 and 32767, it indicates an analog value, * if between 1 and 32767, it indicates an analog value,
* and if it's 0 it indicates that the corresponding button * and if it's 0 it indicates that the corresponding button
* was released. * was released.
* \param dry_run If set, it will only test if the parameter will trigger
* a state change. If not set, the appropriate actions
* (i.e. input state change) will be done.
* \return If dry_run is set, will return true if this action will
* cause a state change. If dry_run is not set, will return
* false.
*/ */
void PlayerController::action(PlayerAction action, int value) bool PlayerController::action(PlayerAction action, int value, bool dry_run)
{ {
Log::info("action", "t %f action %d value %d val_l %d val_r %d val %d", Log::info("action", "t %f action %d value %d val_l %d val_r %d val %d",
World::getWorld()->getTime(), action, value, World::getWorld()->getTime(), action, value,
m_steer_val_l, m_steer_val_r, m_steer_val); m_steer_val_l, m_steer_val_r, m_steer_val);
/** If dry_run (parameter) is true, this macro tests if this action would
* trigger a state change in the specified variable (without actually
* doing it). If it will trigger a state change, the marco will trigger
* immediatley a return to the caller. If dry_run is false, it will only
* assign the new value to the variable (and not return to the user
* early). The do-while(0) helps using this macro e.g. in the 'then'
* clause of an if statement. */
#define SET_OR_TEST(var, value) \
do \
{ \
if(dry_run) \
{ \
if (var != (value) ) return true; \
} \
else \
{ \
var = value; \
} \
} while(0)
/** Basically the same as the above macro, but is uses getter/setter
* funcitons. The name of the setter/getter is set'name'(value) and
* get'name'(). */
#define SET_OR_TEST_GETTER(name, value) \
do \
{ \
if(dry_run) \
{ \
if (m_controls->get##name() != (value) ) return true; \
} \
else \
{ \
m_controls->set##name(value); \
} \
} while(0)
switch (action) switch (action)
{ {
case PA_STEER_LEFT: case PA_STEER_LEFT:
m_steer_val_l = value; SET_OR_TEST(m_steer_val_l, value);
if (value) if (value)
{ {
m_steer_val = value; SET_OR_TEST(m_steer_val, value);
if(m_controls->getSkidControl()==KartControl::SC_NO_DIRECTION) if (m_controls->getSkidControl() == KartControl::SC_NO_DIRECTION)
m_controls->setSkidControl(KartControl::SC_LEFT); SET_OR_TEST_GETTER(SkidControl, KartControl::SC_LEFT);
} }
else else
m_steer_val = m_steer_val_r; SET_OR_TEST(m_steer_val, m_steer_val_r);
break; break;
case PA_STEER_RIGHT: case PA_STEER_RIGHT:
m_steer_val_r = -value; SET_OR_TEST(m_steer_val_r, -value);
if (value) if (value)
{ {
m_steer_val = -value; SET_OR_TEST(m_steer_val, -value);
if(m_controls->getSkidControl()==KartControl::SC_NO_DIRECTION) if (m_controls->getSkidControl() == KartControl::SC_NO_DIRECTION)
m_controls->setSkidControl(KartControl::SC_RIGHT); SET_OR_TEST_GETTER(SkidControl, KartControl::SC_RIGHT);
} }
else else
m_steer_val = m_steer_val_l; SET_OR_TEST(m_steer_val, m_steer_val_l);
break; break;
case PA_ACCEL: case PA_ACCEL:
m_prev_accel = value; SET_OR_TEST(m_prev_accel, value);
if (value && !(m_penalty_time > 0.0f)) if (value && !(m_penalty_time > 0.0f))
{ {
m_controls->setAccel(value/32768.0f); SET_OR_TEST_GETTER(Accel, value/32768.0f);
m_controls->setBrake(false); SET_OR_TEST_GETTER(Brake, false);
m_controls->setNitro(m_prev_nitro); SET_OR_TEST_GETTER(Nitro, m_prev_nitro);
} }
else else
{ {
m_controls->setAccel(0.0f); SET_OR_TEST_GETTER(Accel, 0.0f);
m_controls->setBrake(m_prev_brake); SET_OR_TEST_GETTER(Brake, m_prev_brake);
m_controls->setNitro(false); SET_OR_TEST_GETTER(Nitro, false);
} }
break; break;
case PA_BRAKE: case PA_BRAKE:
m_prev_brake = value!=0; SET_OR_TEST(m_prev_brake, value!=0);
// let's consider below that to be a deadzone // let's consider below that to be a deadzone
if(value > 32768/2) if(value > 32768/2)
{ {
m_controls->setBrake(true); SET_OR_TEST_GETTER(Brake, true);
m_controls->setAccel(0.0f); SET_OR_TEST_GETTER(Accel, 0.0f);
m_controls->setNitro(false); SET_OR_TEST_GETTER(Nitro, false);
} }
else else
{ {
m_controls->setBrake(false); SET_OR_TEST_GETTER(Brake, false);
m_controls->setAccel(m_prev_accel/32768.0f); SET_OR_TEST_GETTER(Accel, m_prev_accel/32768.0f);
// Nitro still depends on whether we're accelerating // Nitro still depends on whether we're accelerating
m_controls->setNitro(m_prev_nitro && m_prev_accel); SET_OR_TEST_GETTER(Nitro, m_prev_nitro && m_prev_accel);
} }
break; break;
case PA_NITRO: case PA_NITRO:
// This basically keeps track whether the button still is being pressed // This basically keeps track whether the button still is being pressed
m_prev_nitro = (value != 0); SET_OR_TEST(m_prev_nitro, value != 0 );
// Enable nitro only when also accelerating // Enable nitro only when also accelerating
m_controls->setNitro( ((value!=0) && m_controls->getAccel()) ); SET_OR_TEST_GETTER(Nitro, ((value!=0) && m_controls->getAccel()) );
break; break;
case PA_RESCUE: case PA_RESCUE:
m_controls->setRescue(value!=0); SET_OR_TEST_GETTER(Rescue, value!=0);
break; break;
case PA_FIRE: case PA_FIRE:
m_controls->setFire(value!=0); SET_OR_TEST_GETTER(Fire, value!=0);
break; break;
case PA_LOOK_BACK: case PA_LOOK_BACK:
m_controls->setLookBack(value!=0); SET_OR_TEST_GETTER(LookBack, value!=0);
break; break;
case PA_DRIFT: case PA_DRIFT:
if(value==0) if (value == 0)
m_controls->setSkidControl(KartControl::SC_NONE); SET_OR_TEST_GETTER(SkidControl, KartControl::SC_NONE);
else else
{ {
if(m_steer_val==0) if (m_steer_val == 0)
m_controls->setSkidControl(KartControl::SC_NO_DIRECTION); SET_OR_TEST_GETTER(SkidControl, KartControl::SC_NO_DIRECTION);
else else
m_controls->setSkidControl(m_steer_val<0 SET_OR_TEST_GETTER(SkidControl, m_steer_val<0
? KartControl::SC_RIGHT ? KartControl::SC_RIGHT
: KartControl::SC_LEFT ); : KartControl::SC_LEFT );
} }
break; break;
case PA_PAUSE_RACE: case PA_PAUSE_RACE:
@ -299,7 +234,10 @@ void PlayerController::action(PlayerAction action, int value)
default: default:
break; break;
} }
if (dry_run) return false;
return true;
#undef SET_OR_TEST
#undef SET_OR_TEST_GETTER
} // action } // action
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -44,8 +44,8 @@ public:
PlayerController(AbstractKart *kart); PlayerController(AbstractKart *kart);
virtual ~PlayerController (); virtual ~PlayerController ();
virtual void update (float) OVERRIDE; virtual void update (float) OVERRIDE;
virtual void action (PlayerAction action, int value) OVERRIDE; virtual bool action (PlayerAction action, int value,
virtual bool actionChangesState(PlayerAction action, int value); bool dry_run = false ) OVERRIDE;
virtual void actionFromNetwork(PlayerAction action, int value, virtual void actionFromNetwork(PlayerAction action, int value,
int value_l, int value_r); int value_l, int value_r);
virtual void skidBonusTriggered() OVERRIDE; virtual void skidBonusTriggered() OVERRIDE;