#include "DoubleDunk.hpp"
#include "../RomUtils.hpp"
namespace ale {
DoubleDunkSettings::DoubleDunkSettings() { reset(); }
RomSettings* DoubleDunkSettings::clone() const {
RomSettings* rval = new DoubleDunkSettings();
*rval = *this;
return rval;
}
void DoubleDunkSettings::step(const System& system) {
int my_score = getDecimalScore(0xF6, &system);
int oppt_score = getDecimalScore(0xF7, &system);
int score = my_score - oppt_score;
m_reward = score - m_score;
m_score = score;
int some_value = readRam(&system, 0xFE);
m_terminal = (my_score >= 24 || oppt_score >= 24) && some_value == 0xE7;
}
bool DoubleDunkSettings::isTerminal() const { return m_terminal; };
reward_t DoubleDunkSettings::getReward() const { return m_reward; }
bool DoubleDunkSettings::isMinimal(const Action& a) const {
switch (a) {
case PLAYER_A_NOOP:
case PLAYER_A_FIRE:
case PLAYER_A_UP:
case PLAYER_A_RIGHT:
case PLAYER_A_LEFT:
case PLAYER_A_DOWN:
case PLAYER_A_UPRIGHT:
case PLAYER_A_UPLEFT:
case PLAYER_A_DOWNRIGHT:
case PLAYER_A_DOWNLEFT:
case PLAYER_A_UPFIRE:
case PLAYER_A_RIGHTFIRE:
case PLAYER_A_LEFTFIRE:
case PLAYER_A_DOWNFIRE:
case PLAYER_A_UPRIGHTFIRE:
case PLAYER_A_UPLEFTFIRE:
case PLAYER_A_DOWNRIGHTFIRE:
case PLAYER_A_DOWNLEFTFIRE:
return true;
default:
return false;
}
}
void DoubleDunkSettings::reset() {
m_reward = 0;
m_score = 0;
m_terminal = false;
}
void DoubleDunkSettings::saveState(Serializer& ser) {
ser.putInt(m_reward);
ser.putInt(m_score);
ser.putBool(m_terminal);
}
void DoubleDunkSettings::loadState(Deserializer& ser) {
m_reward = ser.getInt();
m_score = ser.getInt();
m_terminal = ser.getBool();
}
ActionVect DoubleDunkSettings::getStartingActions() {
ActionVect startingActions;
startingActions.push_back(PLAYER_A_UPFIRE);
return startingActions;
}
ModeVect DoubleDunkSettings::getAvailableModes() {
ModeVect modes(getNumModes());
for (unsigned int i = 0; i < modes.size(); i++) {
modes[i] = i;
}
return modes;
}
void DoubleDunkSettings::goDown(
System& system, std::unique_ptr<StellaEnvironmentWrapper>& environment) {
unsigned int previousSelection = readRam(&system, 0xB0);
while (previousSelection == readRam(&system, 0xB0)) {
environment->act(PLAYER_A_DOWN, PLAYER_B_NOOP);
environment->act(PLAYER_A_NOOP, PLAYER_B_NOOP);
}
}
void DoubleDunkSettings::activateOption(
System& system, unsigned int bitOfInterest,
std::unique_ptr<StellaEnvironmentWrapper>& environment) {
while ((readRam(&system, 0x80) & bitOfInterest) != bitOfInterest) {
environment->act(PLAYER_A_RIGHT, PLAYER_B_NOOP);
environment->act(PLAYER_A_NOOP, PLAYER_B_NOOP);
}
}
void DoubleDunkSettings::deactivateOption(
System& system, unsigned int bitOfInterest,
std::unique_ptr<StellaEnvironmentWrapper>& environment) {
while ((readRam(&system, 0x80) & bitOfInterest) == bitOfInterest) {
environment->act(PLAYER_A_LEFT, PLAYER_B_NOOP);
environment->act(PLAYER_A_NOOP, PLAYER_B_NOOP);
}
}
void DoubleDunkSettings::setMode(
game_mode_t m, System& system,
std::unique_ptr<StellaEnvironmentWrapper> environment) {
if (m < getNumModes()) {
environment->pressSelect();
goDown(system, environment);
goDown(system, environment);
if (m & 1) {
activateOption(system, 0x08, environment);
} else {
deactivateOption(system, 0x08, environment);
}
goDown(system, environment);
if (m & 2) {
activateOption(system, 0x10, environment);
} else {
deactivateOption(system, 0x10, environment);
}
goDown(system, environment);
if (m & 4) {
activateOption(system, 0x04, environment);
} else {
deactivateOption(system, 0x04, environment);
}
goDown(system, environment);
if (m & 8) {
activateOption(system, 0x20, environment);
} else {
deactivateOption(system, 0x20, environment);
}
environment->softReset();
environment->act(PLAYER_A_UPFIRE, PLAYER_B_NOOP);
environment->act(PLAYER_A_NOOP, PLAYER_B_NOOP);
} else {
throw std::runtime_error("This mode doesn't currently exist for this game");
}
}
}