Skip to content

Commit

Permalink
Merge pull request #79 from JJJHolscher/develop
Browse files Browse the repository at this point in the history
Build for Arch and an info property for the Observation protobuf class
  • Loading branch information
AI-WAIFU committed Aug 25, 2023
2 parents b0254fd + 8c9fde5 commit 315cb46
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
end_of_line = lf

[*.{cpp,h,lua,txt,glsl,md,c,cmake,java,gradle}]
charset = utf8
charset = utf-8
indent_size = 4
indent_style = tab
insert_final_newline = true
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/.venv
/tags
/tags.lock
/tags.temp
/*-*-*-*-*.conf
/*-*-*-*-*/

## Editors and development environments
*~
*.swp
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()
project(minetest)
set(PROJECT_NAME_CAPITALIZED "Minetest")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(GCC_MINIMUM_VERSION "5.1")
set(CLANG_MINIMUM_VERSION "3.5")
Expand Down
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ MINETESTER_VERSION := 0.0.1
SDL2_CMAKE_FILE := lib/SDL/build/lib/cmake/SDL2/sdl2-config.cmake
ZMQPP_LIB_FILE := lib/zmqpp/build/max-g++/libzmqpp.a
MINETEST_BINARY := bin/minetest
DEBUG_BINARY := bin/debug
MINETESTER_WHEEL := build/package/wheel/minetester-$(MINETESTER_VERSION)-py3-none-manylinux_2_35_x86_64.whl

default: minetest

deb_deps:
linux_deps:
# Install debian dependencies
util/minetester/install_deps.sh

python_build_deps:
# Install python build dependencies
pip install --upgrade pip
pip install -r build_requirements.txt

repos:
Expand Down Expand Up @@ -43,6 +45,11 @@ $(MINETEST_BINARY):

minetest: $(MINETEST_BINARY)

$(DEBUG_BINARY):
util/minetester/build_debuggable_minetest.sh

debug: $(DEBUG_BINARY)

$(MINETESTER_WHEEL):
#build minetester python library
util/minetester/build_minetester.sh
Expand All @@ -57,6 +64,9 @@ demo:
#install run demo script
python -m minetester.scripts.test_loop

ctags:
ctags --extras=+f -R --links=no .

clean:
#clean up repo
util/minetester/clean.sh
Expand Down
6 changes: 3 additions & 3 deletions clientmods/mods.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load_mod_random_v0 = true
load_mod_rewards = true
load_mod_treechop_v1 = false
load_mod_treechop_shaped_v0 = false
load_mod_random_v1 = false
load_mod_treechop_v0 = false
load_mod_random_v0 = false
load_mod_preview = false
load_mod_treechop_shaped_v0 = false
load_mod_treechop_v0 = false
3 changes: 2 additions & 1 deletion clientmods/random_v0/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
minetest.register_globalstep(function(dtime)
REWARD = math.random()
TERMINAL = math.floor(math.random() + 0.5) == 1
end)
INFO = "hello world"
end)
3 changes: 2 additions & 1 deletion clientmods/rewards/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- define global reward and terminal variables
-- define global reward, terminal and info variables
REWARD = 0.0
TERMINAL = false
INFO = ""
14 changes: 5 additions & 9 deletions minetester/minetest_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
import matplotlib.pyplot as plt
import numpy as np
import zmq
from minetester.utils import (
KEY_MAP,
pack_pb_action,
start_minetest_client,
start_minetest_server,
unpack_pb_obs,
start_xserver,
)

from minetester.utils import (KEY_MAP, pack_pb_action, start_minetest_client,
start_minetest_server, start_xserver,
unpack_pb_obs)

import pkg_resources

Expand Down Expand Up @@ -410,7 +406,7 @@ def step(self, action: Dict[str, Any]):
assert action == last_action

self.last_obs = next_obs
logging.debug(f"Received obs - {next_obs.shape}; reward - {rew}")
logging.debug(f"Received obs - {next_obs.shape}; reward - {rew}; info - {info}")
return next_obs, rew, done, info

def render(self, render_mode: str = "human"):
Expand Down
7 changes: 5 additions & 2 deletions minetester/scripts/test_loop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
from minetester import Minetest
import os

from minetester import Minetest

env = Minetest(
seed=42,
start_minetest=True,
Expand All @@ -10,15 +11,17 @@
headless=True,
start_xvfb=True,
clientmods=["random_v0"],
servermods=["info"]
)

render = True
render = True
obs = env.reset()
done = False
while not done:
try:
action = env.action_space.sample()
obs, rew, done, info = env.step(action)
print(rew, done, info)
if render:
env.render()
except KeyboardInterrupt:
Expand Down
3 changes: 1 addition & 2 deletions minetester/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def unpack_pb_obs(received_obs: str):
last_action = unpack_pb_action(pb_obs.action) if pb_obs.action else None
rew = pb_obs.reward
done = pb_obs.terminal
# TODO receive extra infos
info = {}
info = pb_obs.info
return obs, rew, done, info, last_action


Expand Down
14 changes: 14 additions & 0 deletions mods/info/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
minetest.register_globalstep(function(dtime)
-- set a random reward and terminal value
local players = minetest.get_connected_players()
for i = 1, #players do
local player = players[i]
local playername = player:get_player_name()
minetest.debug("Reward before: " .. tostring(REWARD[playername]))
local reward = math.random()
REWARD[playername] = reward
TERMINAL[playername] = math.floor(reward + 0.05) == 1
INFO[playername] = playername .. " is at " ..
tostring(player:get_pos())
end
end)
1 change: 1 addition & 0 deletions mods/info/mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = info
5 changes: 3 additions & 2 deletions mods/random_v2/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
minetest.register_globalstep(function(dtime)
-- set a random reward and terminal value
local players = minetest.get_connected_players()
local players = minetest.get_connected_players()
for i = 1, #players do
local playername = players[i]:get_player_name()
minetest.debug("Reward before: " .. tostring(REWARD[playername]))
REWARD[playername] = math.random()
TERMINAL[playername] = math.floor(math.random() + 0.5) == 1
OUT[playername] = "Hello World"
end
end)
end)
3 changes: 2 additions & 1 deletion mods/rewards/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- define global reward and terminal tables
-- define global reward, terminal and info tables
REWARD = {}
TERMINAL = {}
INFO = {}
1 change: 1 addition & 0 deletions proto/objects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@ message Observation {
float reward = 2;
bool terminal = 3;
Action action = 4;
string info = 5;
}
50 changes: 47 additions & 3 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,7 @@ void Client::makeScreenshot()
{
irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
irr::video::IImage* raw_image;

if(m_rendering_engine->headless) {
raw_image = m_rendering_engine->get_screenshot();
} else {
Expand Down Expand Up @@ -1920,6 +1920,50 @@ void Client::makeScreenshot()
raw_image->drop();
}

std::string Client::getInfo() {
try {
ClientScripting *scr = getScript();
if (scr) {
lua_State *L = scr->getStack();
// read out global INFO variable
lua_getglobal(L, "INFO"); // push global OUT value to stack
// check if it is a string
if (!lua_isstring(L, lua_gettop(L)))
errorstream << "`INFO' should be a string!" << std::endl;
// convert to string
size_t str_len = lua_objlen(L, lua_gettop(L));
std::string info(lua_tolstring(L, lua_gettop(L), &str_len));

// Get current time
auto now = std::chrono::high_resolution_clock::now();

// Convert to a time_t object
auto now_c = std::chrono::high_resolution_clock::to_time_t(now);

// Convert to local time
std::tm* now_tm = std::localtime(&now_c);

// Get the remaining fractional seconds (nanoseconds)
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch() % std::chrono::seconds(1)).count();

// Format the time as a string
std::stringstream ss;
ss << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S") << "." << std::setfill('0') << std::setw(9) << nanos;
std::string timestamp = ss.str();
warningstream << "[Client] Reading out INFO = " << info << "| " << timestamp << std::endl;
lua_pop(L, 1); // remove INFO value from stack
// reset global INFO to nil
lua_pushstring(L, ""); // push an empty string to the stack
lua_setglobal(L, "INFO"); // pop nil and assign to OUT
return info;
}
} catch(LuaError &e) {
errorstream << "No reward mod active!" << std::endl;
setFatalError(e);
}
return "";
}

float Client::getReward() {
float reward = 0.0;
try {
Expand Down Expand Up @@ -1988,7 +2032,7 @@ bool Client::getTerminal() {

pb_objects::Image Client::getPixelData(core::position2di cursorPosition, bool isMenuActive, irr::video::IImage* cursorImage) {
irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();

irr::video::IImage* raw_image;
if(m_rendering_engine->headless) {
raw_image = m_rendering_engine->get_screenshot();
Expand All @@ -2009,7 +2053,7 @@ pb_objects::Image Client::getPixelData(core::position2di cursorPosition, bool is
const irr::video::SColor color = irr::video::SColor(255, 255, 255, 255);
cursorImage->copyToWithAlpha(image, cursorPosition, sourceRect, color, nullptr, true);
}

auto dim = image->getDimension();
std::string imageData = std::string((char*)image->getData(), image->getImageDataSizeInBytes());
pb_objects::Image pb_img;
Expand Down
1 change: 1 addition & 0 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef

// Added methods
float getReward();
std::string getInfo();
bool getTerminal();
pb_objects::Image getPixelData(core::position2di cursorPosition, bool isMenuActive, irr::video::IImage* cursorImage);
RenderingEngine* getRenderingEngine();
Expand Down
20 changes: 12 additions & 8 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ bool Game::startup(bool *kill,
errorstream << "ZeroMQ error: " << e.what() << " (address: " << start_data.client_address << ")\n";
throw e;
};

if (start_data.sync_port != "") {
// the dumb client is synchronized with the server (and other clients)
// via a REQ/REP pattern
Expand Down Expand Up @@ -1219,7 +1219,7 @@ bool Game::startup(bool *kill,
};
}
}

// pass socket to dumb handler and recorder
if (start_data.isDumbClient()) {
dynamic_cast<DumbClientInputHandler*>(input)->socket = data_socket;
Expand Down Expand Up @@ -1272,10 +1272,11 @@ void Game::run()
while (m_rendering_engine->run()
&& !(*kill || g_gamecallback->shutdown_requested
|| (server && server->isShutdownRequested()))) {


float reward;
bool terminal;
std::string info;
if (sync_socket != nullptr) {
// send client is done signal to server
zmqpp::message syncDoneMsg;
Expand All @@ -1294,17 +1295,20 @@ void Game::run()
dtime = serverSyncMsg.get<f32>(0);
reward = serverSyncMsg.get<float>(1);
terminal = serverSyncMsg.get<bool>(2);

info = serverSyncMsg.get<std::string>(3);

//warningstream << "Received dtime = " << std::to_string(dtime) << std::endl;
} else {
info = client->getInfo();
reward = client->getReward();
terminal = client->getTerminal();
}

// send data out
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
if(recorder && !firstIter) {
pb_objects::Image pb_img = client->getPixelData(input->getMousePos(), isMenuActive(), cursorImage);
recorder->setInfo(info);
recorder->setImage(pb_img);
recorder->setReward(reward);
recorder->setTerminal(terminal);
Expand All @@ -1313,7 +1317,7 @@ void Game::run()
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
//warningstream << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;


const irr::core::dimension2d<u32> &current_screen_size =
m_rendering_engine->get_video_driver()->getScreenSize();
// Verify if window size has changed and save it if it's the case
Expand Down Expand Up @@ -1379,7 +1383,7 @@ void Game::run()
resumeAnimation();
}


if (!m_is_paused)
step(dtime);
processClientEvents(&cam_view_target);
Expand Down
5 changes: 5 additions & 0 deletions src/client/recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void Recorder::setReward(float & reward) {
rewardToSend = reward;
}

void Recorder::setInfo(std::string & info) {
infoToSend = info;
}

void Recorder::setTerminal(bool & terminal) {
terminalToSend = terminal;
}
Expand All @@ -40,6 +44,7 @@ void Recorder::setTerminal(bool & terminal) {
void Recorder::sendObservation() {
pb_objects::Observation obsToSend;
obsToSend.set_reward(rewardToSend);
obsToSend.set_info(infoToSend);
obsToSend.set_terminal(terminalToSend);
obsToSend.set_allocated_image(&imgToSend);
obsToSend.set_allocated_action(&actionToSend);
Expand Down
Loading

0 comments on commit 315cb46

Please sign in to comment.