const int AIMachinegunnerPlayer::getWeaponAmount(const int idx) const{ switch(idx) { case 0: case 1: return -1; default: throw_ex(("weapon %d doesnt supported", idx)); } }
const std::string AIMachinegunnerPlayer::getWeapon(const int idx) const { switch(idx) { case 0: return "bullets:machinegunner"; case 1: return std::string(); default: throw_ex(("weapon %d doesnt supported", idx)); } }
void Chooser::set(const std::string &name) { for(int i = 0; i < _n; ++i) { if (strcasecmp(name.c_str(), _options[i].c_str()) == 0) { _i = i; invalidate(); return; } } throw_ex(("chooser doesnt contain option '%s'", name.c_str())); }
void PlayerNameControl::set(const std::string &name) { std::string profile; Config->get("engine.profile", profile, std::string()); if (profile.empty()) throw_ex(("empty profile")); Config->set("profile." + profile + "." + _config_key, name); _name->set(name); _edit_flag = false; invalidate(true); }
void UpperBox::update(const GameType game_type) { switch(game_type) { case GameTypeDeathMatch: value = "deathmatch"; break; case GameTypeCooperative: value = "cooperative"; break; case GameTypeRacing: value = "racing"; break; default: throw_ex(("invalid game_type value! (%d)", (int)game_type)); } }
KeyPlayer::KeyPlayer(const std::string &variant) { on_key_slot.assign(this, &KeyPlayer::on_key, Window->key_signal); int up, down, left, right, fire, alt_fire, leave, hint_control; #include "controls/default_keys.cpp" int i = 0; if (variant == "keys") i = 0; else if (variant == "keys-1") i = 1; else if (variant == "keys-2") i = 2; else throw_ex(("unknown keyboard variant used (%s)", variant.c_str())); std::string profile; Config->get("engine.profile", profile, std::string()); if (profile.empty()) throw_ex(("empty profile")); std::string base = "profile." + profile + ".controls." + variant; Config->get(base + ".up", up, keys[i][0]); Config->get(base + ".down", down, keys[i][1]); Config->get(base + ".left", left, keys[i][2]); Config->get(base + ".right", right, keys[i][3]); Config->get(base + ".fire", fire, keys[i][4]); Config->get(base + ".alt-fire", alt_fire, keys[i][5]); Config->get(base + ".disembark", leave, keys[i][6]); Config->get(base + ".hint-control", hint_control, keys[i][7]); _up = (SDLKey)up; _down = (SDLKey)down; _left = (SDLKey)left; _right = (SDLKey)right; _fire = (SDLKey)fire; _alt_fire = (SDLKey)alt_fire; this->leave = (SDLKey)leave; _hint_control = (SDLKey)hint_control; }
void DestructableLayer::init(const int w, const int h, const mrt::Chunk & data) { if (hp <= 0) throw_ex(("destructable layer cannot have hp %d (hp must be > 0)", hp)); Layer::init(w, h, data); int size = _w * _h; delete[] _hp_data; _hp_data = new int[size]; for(int i = 0; i < size; ++i) { _hp_data[i] = (Layer::_get(i) != 0) ? hp : 0; } }
void calculate(const float dt) { if (!_reaction.tick(dt)) return; if (_parent == NULL) throw_ex(("turret is only operable attached to shilka ")); if (_parent->disable_ai && PlayerManager->get_slot_by_id(_parent->get_id()) == NULL) { Object::calculate(dt); return; } v2<float> pos, vel; std::set<const Object *> objects; _parent->enumerate_objects(objects, getWeaponRange("shilka-bullet"), &ai::Targets->troops); int dirs = get_directions_number(); //int parent_dir = _parent->get_direction(); //(_parent->get_direction() - _parent->get_directions_number() / 2) * get_directions_number() / _parent->get_directions_number(); const Object *target = NULL; v2<float> target_pos; for(std::set<const Object *>::iterator i = objects.begin(); i != objects.end(); ++i) { const Object *o = *i; if (o->get_id() == _parent->get_id() || o->impassability == 0 || o->hp <= 0 || PIERCEABLE_PAIR(_parent, o) || !ZBox::sameBox(_parent->get_z(), o->get_z()) || _parent->has_same_owner(o) || o->has_effect("invulnerability") ) continue; pos = get_relative_position(o); if (target == NULL || pos.quick_length() < target_pos.quick_length()) { target = o; target_pos = pos; } //LOG_DEBUG(("%s <- dir: %d, parent_dir: %d (%g, %g)", o->animation.c_str(), dir, parent_dir, pos.x, pos.y)); } target_pos.normalize(); int dir = target_pos.get_direction(dirs) - 1; if (target == NULL || dir < 0) { Object::calculate(dt); return; } _direction = target_pos; set_direction(dir); }
const bool Container::in(const Control *c, const int x, const int y) const { assert(c != NULL); ControlList::const_reverse_iterator i; for(i = _controls.rbegin(); i != _controls.rend(); ++i) { if ((*i) == c) break; } if (i == _controls.rend()) throw_ex(("no control %p in container %p", (const void *)c, (const void *)this)); int bw, bh, base_x, base_y; c->get_size(bw, bh); c->get_base(base_x, base_y); const sdlx::Rect dst(base_x, base_y, bw, bh); return dst.in(x, y); }
void forward_function(v8::FunctionCallbackInfo<v8::Value> const& args) { static_assert(detail::is_function_pointer<F>::value || std::is_member_function_pointer<F>::value, "required pointer to a free or member function"); v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope scope(isolate); try { forward_ret<F>(args); } catch (std::exception const& ex) { args.GetReturnValue().Set(throw_ex(isolate, ex.what())); } }
PlayerNameControl::PlayerNameControl(const std::string &label, const std::string &config_key, const int w) : _font(ResourceManager->loadFont("small", true)), _config_key(config_key), _edit_flag(false), width(w) { _dice = ResourceManager->load_surface("menu/dice.png"); _edit = ResourceManager->load_surface("menu/edit.png"); std::string name; std::string profile; Config->get("engine.profile", profile, std::string()); if (profile.empty()) throw_ex(("empty profile")); Config->get("profile." + profile + "." + config_key, name, Nickname::generate()); mrt::utf8_resize(name, 32); _label = new Label(_font, label); _name = new Label(_font, name); int sw, sh; _label->get_size(sw, sh); add(-sw, 0, _label); int label_w = width - ( _dice->get_width() + _edit->get_width() + 10); if (label_w < 0) label_w = 4; //lol :) _name->set_size(label_w, sh); add(0, 0, _name); Container::get_size(sw, sh); if (w > 0) { sw = w - _edit->get_width() - _dice->get_width() - 10; } _dice_area.x = sw + 4; _dice_area.y = (sh - _edit->get_height()) / 2; _dice_area.w = _dice->get_width(); _dice_area.h = _dice->get_height(); _edit_area.x = _dice_area.x + _dice_area.w + 6; _edit_area.y = (sh - _edit->get_height()) / 2; _edit_area.w = _edit->get_width(); _edit_area.h = _edit->get_height(); }
void context::run_file(v8::FunctionCallbackInfo<v8::Value> const& args) { v8::Isolate* isolate = args.GetIsolate(); v8::EscapableHandleScope scope(isolate); v8::Local<v8::Value> result; try { std::string const filename = from_v8<std::string>(isolate, args[0], ""); if (filename.empty()) { throw std::runtime_error("run_file: require filename string argument"); } context* ctx = detail::get_external_data<context*>(args.Data()); result = to_v8(isolate, ctx->run_file(filename)); } catch (std::exception const& ex) { result = throw_ex(isolate, ex.what()); } args.GetReturnValue().Set(scope.Escape(result)); }
virtual optional<expr> expand(expr const &, abstract_type_context &) const { throw_ex(); }
virtual expr check_type(expr const &, abstract_type_context &, bool) const { throw_ex(); }
unsigned Hrtf::process( unsigned sample_rate, clunk::Buffer &dst_buf, unsigned dst_ch, const clunk::Buffer &src_buf, unsigned src_ch, const v3f &delta_position, float fx_volume) { s16 * const dst = static_cast<s16*>(dst_buf.get_ptr()); const unsigned dst_n = (unsigned)dst_buf.get_size() / dst_ch / 2; const s16 * const src = static_cast<const s16 *>(src_buf.get_ptr()); const unsigned src_n = (unsigned)src_buf.get_size() / src_ch / 2; assert(dst_n <= src_n); kemar_ptr kemar_data; int angles; get_kemar_data(kemar_data, angles, delta_position); if (delta_position.is0() || kemar_data == NULL) { //2d stereo sound! if (src_ch == dst_ch) { memcpy(dst_buf.get_ptr(), src_buf.get_ptr(), dst_buf.get_size()); return dst_n; } else throw_ex(("unsupported sample conversion")); } assert(dst_ch == 2); //LOG_DEBUG(("data: %p, angles: %d", (void *) kemar_data, angles)); float t_idt, angle_gr, left_to_right_amp; idt_iit(delta_position, t_idt, angle_gr, left_to_right_amp); const int kemar_sector_size = 360 / angles; const int kemar_idx[2] = { ((360 - (int)angle_gr + kemar_sector_size / 2) / kemar_sector_size) % angles, ((int)angle_gr + kemar_sector_size / 2) / kemar_sector_size }; float amp[2] = { left_to_right_amp > 1? 1: 1 / left_to_right_amp, left_to_right_amp > 1? left_to_right_amp: 1 }; //LOG_DEBUG(("%g (of %d)-> left: %d, right: %d", angle_gr, angles, kemar_idx_left, kemar_idx_right)); int idt_offset = (int)(t_idt * sample_rate); int window = 0; while(sample3d[0].get_size() < dst_n * 2 || sample3d[1].get_size() < dst_n * 2) { size_t offset = window * WINDOW_SIZE / 2; assert(offset + WINDOW_SIZE / 2 <= src_n); for(unsigned c = 0; c < dst_ch; ++c) { sample3d[c].reserve(WINDOW_SIZE); s16 *dst = static_cast<s16 *>(static_cast<void *>((static_cast<u8 *>(sample3d[c].get_ptr()) + sample3d[c].get_size() - WINDOW_SIZE))); hrtf(c, dst, src + offset * src_ch, src_ch, src_n - offset, idt_offset, kemar_data, kemar_idx[c], amp[c]); } ++window; } assert(sample3d[0].get_size() >= dst_n * 2 && sample3d[1].get_size() >= dst_n * 2); //LOG_DEBUG(("angle: %g", angle_gr)); //LOG_DEBUG(("idt offset %d samples", idt_offset)); s16 * src_3d[2] = { static_cast<s16 *>(sample3d[0].get_ptr()), static_cast<s16 *>(sample3d[1].get_ptr()) }; //LOG_DEBUG(("size1: %u, %u, needed: %u\n%s", (unsigned)sample3d[0].get_size(), (unsigned)sample3d[1].get_size(), dst_n, sample3d[0].dump().c_str())); for(unsigned i = 0; i < dst_n; ++i) { for(unsigned c = 0; c < dst_ch; ++c) { dst[i * dst_ch + c] = src_3d[c][i]; } } skip(dst_n); return window * WINDOW_SIZE / 2; }
void SpecialZone::onExit(const int slot_id) { if (type == "z-warp") { onWarp(slot_id, false); } else if (_live) throw_ex(("unhandled exit for type '%s'", type.c_str())); }
virtual optional<expr> expand(expr const &, extension_context &) const { throw_ex(); }
inline v8::Handle<v8::Value> throw_ex(v8::Isolate* isolate, std::string const& str, v8::Local<v8::Value> (*exception_ctor)(v8::Handle<v8::String>) = v8::Exception::Error) { return throw_ex(isolate, str.c_str(), exception_ctor); }
void Chooser::set(const int i) { if (i < 0 || i >= _n) throw_ex(("set(%d) is greater than available options (%d)", i, _n)); _i = i; invalidate(); }
void context::load_module(v8::FunctionCallbackInfo<v8::Value> const& args) { v8::Isolate* isolate = args.GetIsolate(); v8::EscapableHandleScope scope(isolate); v8::Local<v8::Value> result; try { std::string const name = from_v8<std::string>(isolate, args[0], ""); if (name.empty()) { throw std::runtime_error("load_module: require module name string argument"); } context* ctx = detail::get_external_data<context*>(args.Data()); context::dynamic_modules::iterator it = ctx->modules_.find(name); // check if module is already loaded if (it != ctx->modules_.end()) { result = v8::Local<v8::Value>::New(isolate, it->second.exports); } else { std::string filename = name; if (!ctx->lib_path_.empty()) { filename = ctx->lib_path_ + path_sep + name; } std::string const suffix = V8PP_PLUGIN_SUFFIX; if (filename.size() >= suffix.size() && filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) != 0) { filename += suffix; } dynamic_module module; #if defined(WIN32) UINT const prev_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX); module.handle = LoadLibraryA(filename.c_str()); ::SetErrorMode(prev_error_mode); #else module.handle = dlopen(filename.c_str(), RTLD_LAZY); #endif if (!module.handle) { throw std::runtime_error("load_module(" + name + "): could not load shared library " + filename); } #if defined(WIN32) void *sym = ::GetProcAddress((HMODULE)module.handle, STRINGIZE(V8PP_PLUGIN_INIT_PROC_NAME)); #else void *sym = dlsym(module.handle, STRINGIZE(V8PP_PLUGIN_INIT_PROC_NAME)); #endif if (!sym) { throw std::runtime_error("load_module(" + name + "): initialization function " STRINGIZE(V8PP_PLUGIN_INIT_PROC_NAME) " not found in " + filename); } using module_init_proc = v8::Handle<v8::Value>(*)(v8::Isolate*); module_init_proc init_proc = reinterpret_cast<module_init_proc>(sym); result = init_proc(isolate); module.exports.Reset(isolate, result); ctx->modules_.emplace(name, std::move(module)); } } catch (std::exception const& ex) { result = throw_ex(isolate, ex.what()); } args.GetReturnValue().Set(scope.Escape(result)); }
const std::string &Message::get(const std::string &key) const { AttrMap::const_iterator i = _attrs.find(key); if (i == _attrs.end()) throw_ex(("no attribute '%s' found", key.c_str())); return i->second; }
void tick(const float dt) { Object::tick(dt); if (_parent == NULL) throw_ex(("turret is only operable attached to shilka ")); bool play_fire = false; const bool fire_possible = _fire.tick(dt); const bool alt_fire_possible = _special_fire.tick(dt); if (_state.alt_fire && alt_fire_possible) { _special_fire.reset(); if (_parent->has_effect("dirt")) { if (get_state().substr(0,4) == "fire") cancel(); static const std::string left_fire = "shilka-bullet-left"; static const std::string right_fire = "shilka-bullet-right"; std::string animation = "shilka-dirt-bullet-"; animation += (_left_fire)?"left":"right"; _parent->spawn("dirt-bullet", animation, v2<float>(), _direction); play_fire = true; } } if (_state.fire && fire_possible) { _fire.reset(); if (_parent->has_effect("ricochet")) { _parent->spawn("ricochet-bullet(auto-aim)", "ricochet-bullet", v2<float>(), _direction); play_fire = true; _left_fire = ! _left_fire; } else if (_parent->has_effect("dispersion")) { if (alt_fire_possible) { _special_fire.reset(); _parent->spawn("dispersion-bullet", "dispersion-bullet", v2<float>(), _direction); play_fire = true; _left_fire = ! _left_fire; }; } else { static const std::string left_fire = "shilka-bullet-left"; static const std::string right_fire = "shilka-bullet-right"; std::string animation = "shilka-bullet-"; animation += (_left_fire)?"left":"right"; _parent->spawn("shilka-bullet", animation, v2<float>(), _direction); play_fire = true; _left_fire = ! _left_fire; } } if (play_fire) { if (get_state().substr(0,4) == "fire") cancel(); play_now(_left_fire?"fire-left":"fire-right"); } } //end of tick()
const std::string& Chooser::getValue() const { if (_options.empty()) throw_ex(("getValue() on non-text Chooser is invalid")); return _options[_i]; }
void IResourceManager::start(const std::string &name, Attrs &attr) { if (name == "resources") { _tw = atol(attr["tile_width"].c_str()); if (_tw == 0) throw_ex(("resources tag must contain `tile_width' attribute (default tile width)")); _th = atol(attr["tile_height"].c_str()); if (_th == 0) throw_ex(("resources tag must contain `tile_height' attribute (default tile height)")); if (attr["version"].empty()) throw_ex(("resources tag must contain `version' attribute")); LOG_DEBUG(("file version: %s", attr["version"].c_str())); } else if (name == "animation") { status = "animation"; const std::string &id = attr["id"]; if (id.empty()) throw_ex(("animation.id was not set")); const std::string &model = attr["model"]; if (model.empty()) throw_ex(("animation.model was not set")); long tw = atol(attr["tile_width"].c_str()); long th = atol(attr["tile_height"].c_str()); long sz = atol(attr["size"].c_str()); if (tw == 0) tw = _tw; if (th == 0) th = _th; if (sz != 0) tw = th = sz; sdlx::Surface *s = NULL; sdlx::CollisionMap *cmap = NULL; bool real_load = !attr["persistent"].empty(); GET_CONFIG_VALUE("engine.preload", bool , preload_all, false); real_load |= preload_all; std::string &tile = attr["tile"]; if (_base_dir.empty()) throw_ex(("base directory was not defined (multiply resources tag ? invalid resource structure?)")); if (_surfaces.find(tile) == _surfaces.end()) { TRY { if (real_load) { mrt::Chunk data; std::string tname = "tiles/" + tile; Finder->load(data, tname); s = new sdlx::Surface; s->load_image(data); s->display_format_alpha(); cmap = create_cmap(s, tname); LOG_DEBUG(("loaded animation '%s'", id.c_str())); } _surfaces[tile] = s; s = NULL; _cmaps[tile] = cmap; cmap = NULL; } CATCH(mrt::format_string("loading animation \"%s\"", tile.c_str()).c_str(), { delete s; s = NULL; delete cmap; cmap = NULL; throw; });
virtual pair<expr, constraint_seq> check_type(expr const &, extension_context &, bool) const { throw_ex(); }