variant operator - ( const variant& a, const variant& b ) { if( a.is_array() && b.is_array() ) { const variants& aa = a.get_array(); const variants& ba = b.get_array(); variants result; result.reserve( std::max(aa.size(),ba.size()) ); auto num = std::max(aa.size(),ba.size()); for( unsigned i = 0; i < num; --i ) { if( aa.size() > i && ba.size() > i ) result[i] = aa[i] - ba[i]; else if( aa.size() > i ) result[i] = aa[i]; else result[i] = ba[i]; } return result; } if( a.is_string() || b.is_string() ) return a.as_string() - b.as_string(); if( a.is_double() || b.is_double() ) return a.as_double() - b.as_double(); if( a.is_int64() || b.is_int64() ) return a.as_int64() - b.as_int64(); if( a.is_uint64() || b.is_uint64() ) return a.as_uint64() - b.as_uint64(); FC_ASSERT( false, "invalid operation ${a} + ${b}", ("a",a)("b",b) ); }
variant operator != ( const variant& a, const variant& b ) { if( a.is_string() || b.is_string() ) return a.as_string() != b.as_string(); if( a.is_double() || b.is_double() ) return a.as_double() != b.as_double(); if( a.is_int64() || b.is_int64() ) return a.as_int64() != b.as_int64(); if( a.is_uint64() || b.is_uint64() ) return a.as_uint64() != b.as_uint64(); return false; }
variant operator <= ( const variant& a, const variant& b ) { if( a.is_string() || b.is_string() ) return a.as_string() <= b.as_string(); if( a.is_double() || b.is_double() ) return a.as_double() <= b.as_double(); if( a.is_int64() || b.is_int64() ) return a.as_int64() <= b.as_int64(); if( a.is_uint64() || b.is_uint64() ) return a.as_uint64() <= b.as_uint64(); FC_ASSERT( false, "Invalid operation" ); }
int from_variant(variant node) { if(node.is_string()) { return from_string(node.as_string()); } return node.as_int(-1); }
BlendEquation::BlendEquation(const variant& node) : rgb_(BlendEquationConstants::BE_ADD), alpha_(BlendEquationConstants::BE_ADD) { if(node.is_map()) { if(node.has_key("rgba")) { rgb_ = alpha_ = convert_string_to_equation(node["rgba"].as_string()); } if(node.has_key("rgb")) { rgb_ = convert_string_to_equation(node["rgb"].as_string()); } if(node.has_key("alpha")) { alpha_ = convert_string_to_equation(node["alpha"].as_string()); } if(node.has_key("a")) { alpha_ = convert_string_to_equation(node["a"].as_string()); } } else if(node.is_list()) { ASSERT_LOG(node.num_elements() > 0, "When using a list for blend equation must give at least one element"); if(node.num_elements() == 1) { rgb_ = alpha_ = convert_string_to_equation(node[0].as_string()); } else { rgb_ = convert_string_to_equation(node[0].as_string()); alpha_ = convert_string_to_equation(node[1].as_string()); } } else if(node.is_string()) { // simply setting the rgb/alpha values that same, from string rgb_ = alpha_ = convert_string_to_equation(node.as_string()); } else { ASSERT_LOG(false, "Unrecognised type for blend equation: " << node.to_debug_string()); } }
void BlendMode::set(const variant& node) { if(node.is_string()) { const std::string& blend = node.as_string(); if(blend == "add") { set(BlendModeConstants::BM_ONE, BlendModeConstants::BM_ONE); } else if(blend == "alpha_blend") { set(BlendModeConstants::BM_SRC_ALPHA, BlendModeConstants::BM_ONE_MINUS_SRC_ALPHA); } else if(blend == "colour_blend" || blend == "color_blend") { set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_ONE_MINUS_SRC_COLOR); } else if(blend == "modulate") { set(BlendModeConstants::BM_DST_COLOR, BlendModeConstants::BM_ZERO); } else if(blend == "src_colour one" || blend == "src_color one") { set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_ONE); } else if(blend == "src_colour zero" || blend == "src_color zero") { set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_ZERO); } else if(blend == "src_colour dest_colour" || blend == "src_color dest_color") { set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_DST_COLOR); } else if(blend == "dest_colour one" || blend == "dest_color one") { set(BlendModeConstants::BM_DST_COLOR, BlendModeConstants::BM_ONE); } else if(blend == "dest_colour src_colour" || blend == "dest_color src_color") { set(BlendModeConstants::BM_DST_COLOR, BlendModeConstants::BM_SRC_COLOR); } else { ASSERT_LOG(false, "BlendMode: Unrecognised scene_blend mode " << blend); } } else if(node.is_list() && node.num_elements() >= 2) { ASSERT_LOG(node[0].is_string() && node[1].is_string(), "BlendMode: Blend mode must be specified by a list of two strings."); set(parse_blend_string(node[0].as_string()), parse_blend_string(node[1].as_string())); } else { ASSERT_LOG(false, "BlendMode: Setting blend requires either a string or a list of greater than two elements." << node.to_debug_string()); } }
void luaW_pushfaivariant(lua_State* L, variant val) { if(val.is_int()) { lua_pushinteger(L, val.as_int()); } else if(val.is_decimal()) { lua_pushnumber(L, val.as_decimal() / 1000.0); } else if(val.is_string()) { const std::string result_string = val.as_string(); lua_pushlstring(L, result_string.c_str(), result_string.size()); } else if(val.is_list()) { lua_newtable(L); for(const variant& v : val.as_list()) { lua_pushinteger(L, lua_rawlen(L, -1) + 1); luaW_pushfaivariant(L, v); lua_settable(L, -3); } } else if(val.is_map()) { typedef std::map<variant,variant>::value_type kv_type; lua_newtable(L); for(const kv_type& v : val.as_map()) { luaW_pushfaivariant(L, v.first); luaW_pushfaivariant(L, v.second); lua_settable(L, -3); } } else if(val.is_callable()) { // First try a few special cases if(unit_callable* u_ref = val.try_convert<unit_callable>()) { const unit& u = u_ref->get_unit(); unit_map::iterator un_it = resources::gameboard->units().find(u.get_location()); if(&*un_it == &u) { luaW_pushunit(L, u.underlying_id()); } else { luaW_pushunit(L, u.side(), u.underlying_id()); } } else if(location_callable* loc_ref = val.try_convert<location_callable>()) { luaW_pushlocation(L, loc_ref->loc()); } else { // If those fail, convert generically to a map const formula_callable* obj = val.as_callable(); std::vector<formula_input> inputs; obj->get_inputs(&inputs); lua_newtable(L); for(const formula_input& attr : inputs) { if(attr.access == FORMULA_WRITE_ONLY) { continue; } lua_pushstring(L, attr.name.c_str()); luaW_pushfaivariant(L, obj->query_value(attr.name)); lua_settable(L, -3); } } } else if(val.is_null()) { lua_pushnil(L); } }
float convert_numeric(const variant& node) { if(node.is_int()) { return clamp<int>(node.as_int32(), 0, 255) / 255.0f; } else if(node.is_float()) { return clamp<float>(node.as_float(), 0.0f, 1.0f); } else if(node.is_string()) { return convert_string_to_number(node.as_string()); } ASSERT_LOG(false, "attribute of Color value was expected to be numeric type."); return 1.0f; }
bool lua_context::execute(const variant& value, game_logic::formula_callable* callable) { bool res = false; if(callable) { set_self_callable(*callable); } if(value.is_string()) { res = dostring("", value.as_string()); } else { lua_compiled_ptr compiled = value.try_convert<lua_compiled>(); ASSERT_LOG(compiled != NULL, "FATAL: object given couldn't be converted to type 'lua_compiled'"); res = compiled->run(context_ptr()); } return res; }
void Material::init(const variant& node) { blend_.set(BlendModeConstants::BM_SRC_ALPHA, BlendModeConstants::BM_ONE_MINUS_SRC_ALPHA); if(node.is_string()) { name_ = node.as_string(); tex_.emplace_back(DisplayDevice::createTexture(name_)); } else if(node.is_map()) { name_ = node["name"].as_string(); // XXX: technically a material could have multiple technique's and passes -- ignoring for now. ASSERT_LOG(node.has_key("technique"), "PSYSTEM2: 'material' must have 'technique' attribute."); ASSERT_LOG(node["technique"].has_key("pass"), "PSYSTEM2: 'material' must have 'pass' attribute."); const variant& pass = node["technique"]["pass"]; use_lighting_ = pass["lighting"].as_bool(false); use_fog_ = pass["fog_override"].as_bool(false); do_depth_write_ = pass["depth_write"].as_bool(true); do_depth_check_ = pass["depth_check"].as_bool(true); if(pass.has_key("scene_blend")) { blend_.set(pass["scene_blend"]); } if(pass.has_key("texture_unit")) { if(pass["texture_unit"].is_map()) { tex_.emplace_back(createTexture(pass["texture_unit"])); } else if(pass["texture_unit"].is_list()) { for(size_t n = 0; n != pass["texture_unit"].num_elements(); ++n) { tex_.emplace_back(createTexture(pass["texture_unit"][n])); } } else { ASSERT_LOG(false, "'texture_unit' attribute must be map or list "); } } if(pass.has_key("rect")) { draw_rect_ = rectf(pass["rect"]); } } else { ASSERT_LOG(false, "Materials(Textures) must be either a single string filename or a map."); } }
std::size_t operator() ( const variant& var ) const noexcept { static const std::hash<std::string> str_hash ; static const std::hash<int> int_hash ; return var.is_string() ? str_hash(var.str) : int_hash(var.i) ; }
bool operator() ( const variant& a, const variant& b ) const noexcept { if( a.is_string() ) return b.is_string() && a.str == b.str ; else return b.is_int() && a.i == b.i ; }