Commit ca6a704e authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

re-implement color setting

parent 646da987
local instruction_counter = 0
colors = { 0x0000FF }
function set_quota(max_num_events, max_time_secs)
local events_per_check = 1000
......
......@@ -20,7 +20,6 @@ LuaBot::LuaBot(Bot &bot, std::string script)
m_luaSegmentInfoTable.reserve(1000);
LuaSelfInfo::Register(m_lua_state);
m_self.colors.push_back(0x0000FF);
}
bool LuaBot::init(std::string& initErrorMessage)
......@@ -32,6 +31,8 @@ bool LuaBot::init(std::string& initErrorMessage)
m_setQuotaFunc = m_lua_state["set_quota"];
m_clearQuotaFunc = m_lua_state["clear_quota"];
m_self.setColorTable(m_lua_state["colors"]);
m_lua_safe_env = createEnvironment();
std::string chunkName = "bot.lua";
......@@ -99,6 +100,11 @@ bool LuaBot::step(float &directionChange, bool &boost)
return retval;
}
std::vector<uint32_t>& LuaBot::getColors()
{
return m_self.getCachedColors();
}
void LuaBot::setQuota(uint32_t num_instructions, double seconds)
{
m_setQuotaFunc(num_instructions, seconds);
......
......@@ -13,7 +13,7 @@ class LuaBot
LuaBot(Bot &bot, std::string script);
bool init(std::string &initErrorMessage);
bool step(float &directionChange, bool &boost);
std::vector<uint32_t> getColors() { return m_self.colors; }
std::vector<uint32_t> &getColors();
uint32_t getFace() { return m_self.getFace(); }
uint32_t getDogTag() { return m_self.getDogTag(); }
......
......@@ -8,6 +8,26 @@
LuaSelfInfo::LuaSelfInfo(Bot &bot, guid_t aId)
: id(aId), colorsLocked(false), m_bot(bot)
{
m_cachedColors.reserve(config::MAX_COLORS);
}
void LuaSelfInfo::setColorTable(sol::table v)
{
m_colorTable = v;
m_cachedColors.clear();
for (auto kvp: v)
{
auto index = kvp.first.as<size_t>();
auto value = kvp.second.as<uint32_t>();
if ((index>=1) && (index<=config::MAX_COLORS))
{
while (m_cachedColors.size() < index)
{
m_cachedColors.push_back(0x808080);
}
m_cachedColors[index-1] = value;
}
}
}
void LuaSelfInfo::Register(sol::state &lua)
......@@ -27,42 +47,12 @@ void LuaSelfInfo::Register(sol::state &lua)
"food_consumed_natural", sol::property(&LuaSelfInfo::getConsumedNaturalFood),
"food_consumed_hunted_self", sol::property(&LuaSelfInfo::getConsumedFoodHuntedBySelf),
"food_consumed_hunted_by_others", sol::property(&LuaSelfInfo::getConsumedFoodHuntedByOthers),
"colors", sol::property(&LuaSelfInfo::getColors, &LuaSelfInfo::setColors),
"colors", sol::property(&LuaSelfInfo::getColorTable, &LuaSelfInfo::setColorTable),
"face", sol::property(&LuaSelfInfo::getFace, &LuaSelfInfo::setFace),
"logo", sol::property(&LuaSelfInfo::getDogTag, &LuaSelfInfo::setDogTag)
);
}
void LuaSelfInfo::setColors(sol::table v)
{
if (colorsLocked)
{
throw std::runtime_error("snake colors can only be changed in the init() function");
}
std::map<size_t,uint32_t> colorMap;
v.for_each([&colorMap](sol::object key, sol::object value)
{
auto index = key.as<size_t>();
if (index>0) { index--; } // lua tables tend to start by index 1
if (index>config::MAX_COLORS) { return; }
colorMap[index] = value.as<uint32_t>();
});
colors.clear();
for (auto kvp: colorMap)
{
while (colors.size() < kvp.first)
{
colors.push_back(colors.empty() ? 0x808080 : colors.back());
}
colors.push_back(kvp.second);
}
if (colors.empty())
{
colors.push_back(0x0000FF00);
}
}
void LuaSelfInfo::setFace()
{
if (colorsLocked)
......@@ -133,3 +123,8 @@ real_t LuaSelfInfo::getMaxStepAngle()
{
return m_bot.getSnake()->maxRotationPerStep();
}
void LuaSelfInfo::registerColorTable(sol::table colorTable)
{
m_colorTable = colorTable;
}
#pragma once
#include <types.h>
#include <vector>
#include <sol_forward.hpp>
#include <sol.hpp>
class Bot;
......@@ -11,15 +11,14 @@ class LuaSelfInfo
{
public:
guid_t id;
std::vector<uint32_t> colors;
bool colorsLocked;
uint32_t face = 0;
uint32_t dogTag = 0;
LuaSelfInfo(Bot& bot, guid_t aId);
std::vector<uint32_t>& getColors() { return colors; }
void setColors(sol::table v);
sol::table getColorTable() { return m_colorTable; }
void setColorTable(sol::table v);
uint32_t getFace() { return face; }
void setFace();
......@@ -39,9 +38,15 @@ class LuaSelfInfo
real_t getConsumedFoodHuntedBySelf();
real_t getMaxStepAngle();
void registerColorTable(sol::table colorTable);
static void Register(sol::state& lua);
std::vector<uint32_t>& getCachedColors() { return m_cachedColors; }
private:
Bot &m_bot;
sol::table m_colorTable;
std::vector<uint32_t> m_cachedColors;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment