Skip to content

Instantly share code, notes, and snippets.

@josh04
Created May 20, 2019 19:54
Show Gist options
  • Save josh04/2a05b251562fa1b990490cae2a060778 to your computer and use it in GitHub Desktop.
Save josh04/2a05b251562fa1b990490cae2a060778 to your computer and use it in GitHub Desktop.
_space_train simple codegen item functions
import codegen
def item_display_specifiers(o, display_specifiers, default):
first = display_specifiers.pop(0)
o.write(" if (display_specifier == "+str(first['display_specifier'])+") { \n")
o.write(" return {"+str(first['tile']['x'])+", "+str(first['tile']['y'])+"};\n")
o.write(" }")
for pair in display_specifiers:
o.write(" else if (display_specifier == "+str(pair['display_specifier'])+") { \n")
o.write(" return {"+str(pair['tile']['x'])+", "+str(pair['tile']['y'])+"};\n")
o.write(" }")
o.write(" else { \n")
o.write(" return {"+str(default['x'])+", "+str(default['y'])+"};\n")
o.write(" }")
o.write("\n")
def item_tile_case(o, item):
o.write(" case list::"+item['type']+":\n")
if len(item['display_specifiers']) > 0:
item_display_specifiers(o, item['display_specifiers'], item['tile'])
else:
o.write(" return {"+str(item['tile']['x'])+", "+str(item['tile']['y'])+"};\n")
o.write(" break;\n")
def gen():
filename = "item_codegen"
database = "items.cdb"
table = "items"
item_db_items, item_db_types = codegen.get_sheet(database, table)
lua_flag_types = [ty for ty in item_db_types if ty['name'] == "lua_flags"]
lua_flag_type_string = lua_flag_types[0]['typeStr']
flags = lua_flag_type_string.split(':', 1)
flag_types = flags[1].split(',')
output_h = codegen.open_header(filename, database, ['string'], [])
output_h.write("""
namespace game {
namespace items {
enum class lua_flags : uint32_t {
""")
i = 0
for flag in flag_types:
output_h.write(" "+flag+" = "+str(pow(2, i))+",\n")
i = i+1
output_h.write("""
};
enum class list {
""")
for item in item_db_items:
output_h.write(" "+item['type']+",\n")
output_h.write("""
};
list name_to_type(const std::string& name);
std::string type_to_name(list type);
const char * get_fixed_item_description(list name);
struct tile_loc {
int x;
int y;
};
tile_loc get_item_tile(list name, uint32_t display_specifier);
bool check_lua_flag(list name, lua_flags flag);
uint32_t get_lua_flags(list name);
std::string get_item_hover_text(list name, uint32_t display_specifier);
}
}
#endif
""")
####################################################################################################
output_w = codegen.open_cpp(filename, database, ['string_view'], ['item_codegen.hpp'])
output_w.write("""
namespace game {
namespace items {
using item_pair = std::pair<list, std::string_view>;
constexpr item_pair map_items[] = {
""")
for item in item_db_items:
output_w.write(" { list::"+item['type']+", \""+item['type']+"\" },\n")
output_w.write("""
};
constexpr auto map_size = sizeof map_items / sizeof map_items[0];
static constexpr std::string_view _type_to_name(list key, int range = map_size) {
return
(range == 0) ? throw "Key not present" :
(map_items[range - 1].first == key) ? map_items[range - 1].second :
_type_to_name(key, range - 1);
};
static constexpr list _name_to_type(std::string_view value, int range = map_size) {
return
(range == 0) ? throw "Value not present" :
(map_items[range - 1].second == value) ? map_items[range - 1].first :
_name_to_type(value, range - 1);
};
static_assert(_name_to_type(_type_to_name(list::none)) == list::none, "should be inverse");
const char * get_fixed_item_description(list name) {
switch(name) {
""")
for item in item_db_items:
output_w.write(" case list::"+item['type']+":\n")
output_w.write(" return \""+item['description']+"\";\n")
output_w.write(" break;\n")
output_w.write("""
default:
return "unidentified item";
}
}
list name_to_type(const std::string& name) {
return _name_to_type(name);
}
std::string type_to_name(list type) {
return std::string(_type_to_name(type));
}
tile_loc get_item_tile(list name, uint32_t display_specifier) {
switch (name) {
""")
for item in item_db_items:
item_tile_case(output_w, item)
output_w.write("""
}
}
bool check_lua_flag(list name, lua_flags flag) {
return get_lua_flags(name) & (uint32_t)flag;
}
uint32_t get_lua_flags(list name) {
switch (name) {
""")
for item in item_db_items:
output_w.write(" case list::"+item['type']+":\n")
output_w.write(" return "+str(item['lua_flags'])+";\n")
output_w.write(" break;\n")
output_w.write("""
}
}
std::string get_item_hover_text(list name, uint32_t display_specifier) {
switch(name) {
""")
for item in item_db_items:
output_w.write(" case list::"+item['type']+":\n")
output_w.write(" return \""+item['name']+"\";\n")
output_w.write(" break;\n")
output_w.write("""
default:
return "unidentified item";
}
}
}
}
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment