Skip to content

Commit

Permalink
Make C++98 compatible again
Browse files Browse the repository at this point in the history
The main reason is Python 2 (also 3.5).
  • Loading branch information
Kojoley committed Jun 27, 2019
1 parent 4c832aa commit 6763312
Show file tree
Hide file tree
Showing 98 changed files with 478 additions and 750 deletions.
32 changes: 16 additions & 16 deletions atari_py/ale_interface/src/ale_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void ALEInterface::disableBufferedIO() {
std::cout.sync_with_stdio();
}

void ALEInterface::createOSystem(std::unique_ptr<OSystem> &theOSystem,
std::unique_ptr<Settings> &theSettings) {
void ALEInterface::createOSystem(scoped_ptr<OSystem>& theOSystem,
scoped_ptr<Settings>& theSettings) {
#if (defined(_WIN32) || defined(__MINGW32__))
theOSystem.reset(new OSystemWin32());
theSettings.reset(new SettingsWin32(theOSystem.get()));
Expand All @@ -81,8 +81,8 @@ void ALEInterface::createOSystem(std::unique_ptr<OSystem> &theOSystem,
theOSystem->settings().loadConfig();
}

void ALEInterface::checkForUnsupportedRom(std::unique_ptr<OSystem>& theOSystem) {
const Properties properties = theOSystem->console().properties();
void ALEInterface::checkForUnsupportedRom(OSystem& theOSystem) {
const Properties properties = theOSystem.console().properties();
const std::string md5 = properties.get(Cartridge_MD5);
bool found = false;
std::ifstream ss("md5.txt");
Expand All @@ -105,27 +105,27 @@ void ALEInterface::checkForUnsupportedRom(std::unique_ptr<OSystem>& theOSystem)
}

void ALEInterface::loadSettings(const std::string& rom, const std::string& name,
std::unique_ptr<OSystem> &theOSystem) {
OSystem& theOSystem) {
// Load the configuration from a config file (passed on the command
// line), if provided
std::string configFile = theOSystem->settings().getString("config", false);
std::string configFile = theOSystem.settings().getString("config", false);

if (!configFile.empty()) {
theOSystem->settings().loadConfig(configFile.c_str());
theOSystem.settings().loadConfig(configFile.c_str());
}

theOSystem->settings().validate();
theOSystem->create();
theOSystem.settings().validate();
theOSystem.create();

// Attempt to load the ROM
if (rom.empty()) {
Logger::Error << "Empty ROM File specified"
<< std::endl;
exit(1);
} else if (theOSystem->createConsole(rom, name)) {
} else if (theOSystem.createConsole(rom, name)) {
checkForUnsupportedRom(theOSystem);
Logger::Info << "Running ROM " << name << "..." << std::endl;
theOSystem->settings().setString("rom_name", name);
theOSystem.settings().setString("rom_name", name);
} else {
Logger::Error << "Unable to create console for " << name << std::endl;
exit(1);
Expand All @@ -134,11 +134,11 @@ void ALEInterface::loadSettings(const std::string& rom, const std::string& name,
// Must force the resetting of the OSystem's random seed, which is set before we change
// choose our random seed.
Logger::Info << "Random seed is "
<< theOSystem->settings().getInt("random_seed") << std::endl;
theOSystem->resetRNGSeed();
<< theOSystem.settings().getInt("random_seed") << std::endl;
theOSystem.resetRNGSeed();

std::string currentDisplayFormat = theOSystem->console().getFormat();
theOSystem->colourPalette().setPalette("standard", currentDisplayFormat);
std::string currentDisplayFormat = theOSystem.console().getFormat();
theOSystem.colourPalette().setPalette("standard", currentDisplayFormat);
}

ALEInterface::ALEInterface() {
Expand All @@ -163,7 +163,7 @@ ALEInterface::~ALEInterface() {
// load.
bool ALEInterface::loadROM(std::string rom_file, std::string name) {
assert(theOSystem.get());
loadSettings(rom_file, name, theOSystem);
loadSettings(rom_file, name, *theOSystem);
romSettings.reset(buildRomRLWrapper(name));
if (romSettings.get() == NULL) return false;
environment.reset(new StellaEnvironment(theOSystem.get(), romSettings.get()));
Expand Down
18 changes: 9 additions & 9 deletions atari_py/ale_interface/src/ale_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include "common/Log.hpp"

#include <string>
#include <memory>
#include "common/scoped_ptr.hpp"

static const std::string Version = "0.6.0";

Expand Down Expand Up @@ -180,24 +180,24 @@ class ALEInterface {
//Kojoley ScreenExporter *createScreenExporter(const std::string &path) const;

public:
std::unique_ptr<OSystem> theOSystem;
std::unique_ptr<Settings> theSettings;
std::unique_ptr<RomSettings> romSettings;
std::unique_ptr<StellaEnvironment> environment;
scoped_ptr<OSystem> theOSystem;
scoped_ptr<Settings> theSettings;
scoped_ptr<RomSettings> romSettings;
scoped_ptr<StellaEnvironment> environment;
int max_num_frames; // Maximum number of frames for each episode

public:
// Display ALE welcome message
static std::string welcomeMessage();
static void disableBufferedIO();
static void createOSystem(std::unique_ptr<OSystem> &theOSystem,
std::unique_ptr<Settings> &theSettings);
static void createOSystem(scoped_ptr<OSystem>& theOSystem,
scoped_ptr<Settings>& theSettings);
static void loadSettings(const std::string& romfile,
const std::string& name,
std::unique_ptr<OSystem> &theOSystem);
OSystem& theOSystem);

private:
static void checkForUnsupportedRom(std::unique_ptr<OSystem>& theOSystem);
static void checkForUnsupportedRom(OSystem& theOSystem);
};

#endif
117 changes: 117 additions & 0 deletions atari_py/ale_interface/src/common/mt19937.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* Copyright (C) 2019 by Nikita Kniazev
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef NK_MT19937
#define NK_MT19937

#include <stddef.h>

#if defined(_MSC_VER) && (_MSC_VER < 1600)
typedef unsigned __int32 uint32_t;
#else
# include <stdint.h>
# include <inttypes.h>
#endif

class mt19937
{
public:
typedef uint32_t result_type;

static const size_t word_size = 32;
static const size_t state_size = 624;
static const size_t shift_size = 397;
static const size_t mask_bits = 31;
static const uint32_t xor_mask = 0x9908b0df;
static const size_t tempering_u = 11;
static const uint32_t tempering_d = 0xffffffff;
static const size_t tempering_s = 7;
static const uint32_t tempering_b = 0x9d2c5680;
static const size_t tempering_t = 15;
static const uint32_t tempering_c = 0xefc60000;
static const size_t tempering_l = 18;
static const uint32_t initialization_multiplier = 1812433253;
static const uint32_t default_seed = 5489u;


explicit mt19937(uint32_t value = default_seed) { seed(value); }

void seed(uint32_t value = default_seed)
{
state[0] = value;
for (uint32_t i = 1; i < state_size; ++i)
state[i] = i + initialization_multiplier * (state[i - 1] ^ (state[i - 1] >> 30));
index = 0;
}

result_type operator()()
{
uint32_t next = (index + 1) % state_size;

uint32_t y = (0x80000000 & state[index]) | (0x7fffffff & state[next]);
uint32_t r = state[index] = state[(index + shift_size) % state_size] ^ (y >> 1) ^ ((-(y & 1)) & xor_mask);
r ^= r >> tempering_u & tempering_d;
r ^= r << tempering_s & tempering_b;
r ^= r << tempering_t & tempering_c;
r ^= r >> tempering_l;

index = next;

return r;
}

result_type (min)() const { return 0; }
result_type (max)() const { return 0xffffffffUL; }

template <typename OStream>
friend OStream& operator<<(OStream& os, mt19937 const& g)
{
typename OStream::fmtflags saved_flags = os.flags(os.dec);

for (uint32_t i = g.index; i < state_size; ++i)
os << g.state[i] << ' ';
for (uint32_t i = 0; i < g.index; ++i)
os << g.state[i] << ' ';

os.flags(saved_flags);

return os;
}

template <typename IStream>
friend IStream& operator>>(IStream& is, mt19937& g)
{
typename IStream::fmtflags saved_flags = is.flags(is.dec | is.skipws);

uint32_t tmp[state_size];
for (uint32_t i = 0; i < state_size; ++i)
is >> tmp[i];

is.flags(saved_flags);

if (is.good()) {
for (uint32_t i = 0; i < state_size; ++i)
g.state[i] = tmp[i];
g.index = 0;
}

return is;
}

private:
uint32_t state[state_size];
uint32_t index;
};

#endif // NK_MT19937
63 changes: 63 additions & 0 deletions atari_py/ale_interface/src/common/scoped_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (C) 2019 by Nikita Kniazev
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef NK_SCOPED_PTR
#define NK_SCOPED_PTR

namespace utils {

struct bool_conversion { int padding; int truth; };
typedef int bool_conversion::* explicit_bool;

}

template<class T>
class scoped_ptr {
public:
typedef T* pointer;
typedef T element_type;

scoped_ptr() throw() : p_() {}
explicit scoped_ptr(pointer p) throw() : p_(p) {}
~scoped_ptr() { destroy(); }

element_type& operator*() const { return *get(); }
pointer operator->() const throw() { return get(); }
pointer get() const throw() { return p_; }
operator bool() const throw() { return get() != pointer(); }
operator utils::explicit_bool() const
{
return get() != pointer() ? &utils::bool_conversion::truth
: utils::explicit_bool(0);
}

pointer release() throw() { pointer p = p_; p_ = pointer(); return p; }
void reset(pointer p = pointer()) throw() { destroy(); p_ = p; }
void swap(scoped_ptr& u) throw()
{
pointer p = release();
p_ = u.release();
u.p_ = p;
}

private:
scoped_ptr(const scoped_ptr&);
scoped_ptr& operator=(const scoped_ptr&);

void destroy() { delete get(); }

pointer p_;
};

#endif // ALE_SCOPED_PTR
5 changes: 2 additions & 3 deletions atari_py/ale_interface/src/controllers/ale_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include "../emucore/OSystem.hxx"
#include "../emucore/m6502/src/System.hxx"
#include "../environment/stella_environment.hpp"

#include <memory>
#include "../common/scoped_ptr.hpp"

class ALEController {
public:
Expand All @@ -42,7 +41,7 @@ class ALEController {

protected:
OSystem* m_osystem;
std::unique_ptr<RomSettings> m_settings;
scoped_ptr<RomSettings> m_settings;
StellaEnvironment m_environment;
};

Expand Down
5 changes: 2 additions & 3 deletions atari_py/ale_interface/src/emucore/Random.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include "Serializer.hxx"
#include "Deserializer.hxx"

// This uses C++11.
#include <random>
#include "../common/mt19937.hpp"
#include <sstream>

// A static Random object for compatibility purposes. Don't use this.
Expand All @@ -31,7 +30,7 @@ Random Random::s_random;
// Implementation of Random's random number generator wrapper.
class Random::Impl {

typedef std::mt19937 randgen_t;
typedef mt19937 randgen_t;

public:

Expand Down
10 changes: 4 additions & 6 deletions atari_py/ale_interface/src/environment/stella_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ void StellaEnvironment::reset() {

// reset the rom (after emulating, in case the NOOPs led to reward)
m_settings->reset();


StellaEnvironmentWrapper wrapper(*this);

// Apply mode that was previously defined, then soft reset with this mode
m_settings->setMode(m_state.getCurrentMode(), m_osystem->console().system(), getWrapper());
m_settings->setMode(m_state.getCurrentMode(), m_osystem->console().system(), wrapper);
softReset();

// Apply necessary actions specified by the rom itself
Expand Down Expand Up @@ -267,10 +269,6 @@ const ALEState& StellaEnvironment::getState() const {
return m_state;
}

std::unique_ptr<StellaEnvironmentWrapper> StellaEnvironment::getWrapper() {
return std::unique_ptr<StellaEnvironmentWrapper>(new StellaEnvironmentWrapper(*this));
}

void StellaEnvironment::processScreen() {
if (m_colour_averaging) {
// Perform phosphor averaging; the blender stores its result in the given screen
Expand Down
4 changes: 0 additions & 4 deletions atari_py/ale_interface/src/environment/stella_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
//Kojoley #include "../common/ScreenExporter.hpp"

#include <stack>
#include <memory>

class StellaEnvironment {
public:
Expand Down Expand Up @@ -96,9 +95,6 @@ class StellaEnvironment {
int getFrameNumber() const { return m_state.getFrameNumber(); }
int getEpisodeFrameNumber() const { return m_state.getEpisodeFrameNumber(); }

/** Returns a wrapper providing #include-free access to our methods. */
std::unique_ptr<StellaEnvironmentWrapper> getWrapper();

private:
/** This applies an action exactly one time step. Helper function to act(). */
reward_t oneStepAct(Action player_a_action, Action player_b_action);
Expand Down
Loading

0 comments on commit 6763312

Please sign in to comment.