obj_id GameInstSet::add_instance(GameInst* inst, int id) { if (tset_should_resize(unit_amnt, unit_capacity)) this->reallocate_internal_data(); Pos c(inst->last_x, inst->last_y); //Will be set to the current state object in 'add' InstanceState* state = &unit_set[0]; if (id == 0 && inst->id != 0) { fprintf(stderr, "Adding instance with id of %d; not 0!\n", inst->id); LANARTS_ASSERT(false); } inst->id = id ? id : (next_id++); //Add an object with the assumption that this object does not currently exist (_noequal) if (tset_add_noequal<GameInstSetFunctions>(inst, state, unit_capacity)) unit_amnt++; if (true || inst->solid) { LANARTS_ASSERT(within_bounds_check(c)); InstanceLinkedList& unit_list = unit_grid[get_xyind(c, grid_w)]; add_to_collisionlist(state, unit_list); } add_to_depthlist(state, depthlist_map[inst->depth]); inst->retain_reference(); return inst->id; }
void GameInstSet::update_instance_for_step(InstanceState* state, GameInst* inst) { if (inst->destroyed) return; if (true || inst->solid) { Pos last_pos(inst->last_x, inst->last_y), new_pos(inst->x, inst->y); LANARTS_ASSERT(within_bounds_check(last_pos)); LANARTS_ASSERT(within_bounds_check(new_pos)); __update_collision_position(state, last_pos, new_pos); } inst->last_x = inst->x, inst->last_y = inst->y; }
void net_recv_game_init_data(SerializeBuffer& sb, int sender, GameStateInitData& init_data, PlayerData& pd) { //Write seed sb.read(init_data); init_data.received_init_data = true; //Read player data int localidx; sb.read_int(localidx); int playern; sb.read_int(playern); LANARTS_ASSERT(pd.all_players().empty()); for (int i = 0; i < playern; i++) { std::string name; std::string classtype; int net_id; sb.read(name); sb.read(classtype); sb.read_int(net_id); pd.register_player(name, NULL, classtype, LuaValue(), (i == localidx), net_id); } printf( "Received init packet: seed = 0x%X, localid = %d, nplayers = %d, " "frame_action_repeat = %d, regen_level_on_death = %d, network_debug_mode = %d, time_per_step = %f\n", init_data.seed, localidx, playern, init_data.frame_action_repeat, (int) init_data.regen_on_death, (int) init_data.network_debug_mode, init_data.time_per_step); }
void net_recv_game_init_data(SerializeBuffer& sb, int sender, GameStateInitData& init_data, PlayerData& pd) { //Write seed sb.read_int(init_data.seed); init_data.seed_set_by_network_message = true; //Read player data int localidx; sb.read_int(localidx); pd.set_local_player_idx(localidx); int playern; sb.read_int(playern); LANARTS_ASSERT(pd.all_players().empty()); for (int i = 0; i < playern; i++) { std::string name; class_id classtype; int net_id; sb.read(name); sb.read_int(classtype); sb.read_int(net_id); pd.register_player(name, NULL, classtype, net_id); } printf("Received init packet: seed = 0x%X, localid = %d, nplayers = %d\n", init_data.seed, localidx, playern); }
void PlayerInst::pickup_item(GameState* gs, const GameAction& action) { const int PICKUP_RATE = 10; GameInst* inst = gs->get_instance(action.use_id); if (!inst) { return; } ItemInst* iteminst = dynamic_cast<ItemInst*>(inst); LANARTS_ASSERT(iteminst); const Item& type = iteminst->item_type(); int amnt = iteminst->item_quantity(); bool inventory_full = false; if (type.id == get_item_by_name("Gold")) { gold() += amnt; } else { itemslot_t slot = inventory().add(type); if (slot == -1) { inventory_full = true; } else if (projectile_should_autowield(equipment(), type, this->last_chosen_weaponclass)) { projectile_smart_equip(inventory(), slot); } } if (!inventory_full) { cooldowns().reset_pickup_cooldown(PICKUP_RATE); gs->remove_instance(iteminst); } }
void GameInstSet::deserialize(GameState* gs, SerializeBuffer& serializer) { serializer.read_int(grid_w); serializer.read_int(grid_h); //Resize and clear unit_grid.resize(grid_w * grid_h); clear(); int amnt; serializer.read_int(amnt); serializer.read_int(next_id); for (int i = 0; i < amnt; i++) { InstType type; int id; serializer.read_int(type); serializer.read_int(id); printf("Deserializing id=%d\n", id); GameInst* inst = get_instance(id); bool has_inst = inst != NULL; if (!has_inst) { inst = from_inst_type(type); inst->deserialize(gs, serializer); inst->last_x = inst->x; inst->last_y = inst->y; inst->id = id; add_instance(inst, inst->id); } else { safe_deserialize(inst, gs, serializer); } LANARTS_ASSERT( serializer_equals_read(serializer, inst->integrity_hash())); } }
GLimage::GLimage(const GLimage& img) : filename(img.filename), height(img.height), width(img.width), texw( img.texw), texh(img.texh), texture(0) { if (img.texture != 0){ LANARTS_ASSERT(img.texture == 0); } }
void PlayerInst::enqueue_actions(const ActionQueue& queue) { if (!actions_set_for_turn) { LANARTS_ASSERT(queued_actions.empty()); queued_actions = queue; actions_set_for_turn = true; } }
weapon_id get_weapon_by_name(const char* name) { if (strcmp(name, "Unarmed") == 0) { return NO_ITEM; } item_id id = get_item_by_name(name); LANARTS_ASSERT(dynamic_cast<WeaponEntry*>(game_item_data.get(id))); return (weapon_id)id; }
PlayerDataEntry& GameScreenSet::local_player_data(GameState* gs) { if (current_screen == -1) { LANARTS_ASSERT(false); return gs->player_data().get(screens.at(0).focus_player_id); } else { return gs->player_data().get(screen().focus_player_id); } }
static void write_or_assert_hash(SerializeBuffer& sb, unsigned int hash, bool is_writing) { if (is_writing) { sb.write_int(hash); } else { LANARTS_ASSERT(serializer_equals_read(sb, hash)); } }
equipment_id get_equipment_by_name(const char* name) { if (strcmp(name, "Nothing") == 0) { return NO_ITEM; } item_id id = get_item_by_name(name); LANARTS_ASSERT(dynamic_cast<EquipmentEntry*>(game_item_data.at(id))); return (equipment_id) id; }
void GameInstSet::update_statepointer_for_reallocate_( InstanceState** stateptr) { if (*stateptr) { *stateptr = tset_find<GameInstSetFunctions>((*stateptr)->inst->id, &unit_set[0], unit_capacity); LANARTS_ASSERT(*stateptr != NULL); } }
static E& get_X_ref_by_name(std::vector<E>& t, const char* name) { for (int i = 0; i < t.size(); i++) { if (name == t.at(i).name) { return t.at(i); } } /*Error if resource not found*/ fprintf(stderr, "Failed to load resource!\nname: %s, of type %s\n", name, typeid(t[0]).name()); fflush(stderr); LANARTS_ASSERT(false /*resource not found*/); return t.at(-1); /* throws */ }
static void write_or_assert_hash(SerializeBuffer& sb, unsigned int hash, bool is_writing) { if (is_writing) { sb.write_int(hash); } else { unsigned int val; sb.read(val); if (val != hash) { printf("Values 0x%X and 0x%X do not match!\n", val, hash); LANARTS_ASSERT(false); } } }
void PlayerInst::purchase_from_store(GameState* gs, const GameAction& action) { StoreInst* store = (StoreInst*)gs->get_instance(action.use_id); if (!store) { return; } LANARTS_ASSERT(dynamic_cast<StoreInst*>(gs->get_instance(action.use_id))); StoreInventory& inv = store->inventory(); StoreItemSlot& slot = inv.get(action.use_id2); if (gold() >= slot.cost) { inventory().add(slot.item); gold() -= slot.cost; slot.item.clear(); } }
void WeaponEntry::parse_lua_table(const LuaValue& table) { EquipmentEntry::parse_lua_table(table); weapon_class = table["type"].to_str(); attack = parse_attack(table); attack.attack_sprite = item_sprite; if (!table["spr_attack"].isnil()) { attack.attack_sprite = res::sprite_id(table["spr_attack"].to_str()); LANARTS_ASSERT(attack.attack_sprite >= 0); } uses_projectile = luawrap::defaulted(table, "uses_projectile", false); attack_stat_func = table["attack_stat_func"]; }
void GameScreenSet::deserialize(GameState *gs, SerializeBuffer &serializer) { serializer.read_int(current_screen); simulation_map = read_gamemap(gs, serializer); serializer.read_container(screens, [&](GameScreen& screen) { screen.deserialize(gs, serializer); }); GameSettings& settings = gs->game_settings(); int n_local_players = 0; for (PlayerDataEntry &player: gs->player_data().all_players()) { if (player.is_local_player) { n_local_players++; } } // Number of split-screens tiled together int n_x = 1, n_y = 1; if (n_local_players <= 2) { n_x = n_local_players; } else if (n_local_players <= 4) { // More than 2, less than 4? Try 2x2 tiling n_x = 2, n_y = 2; } else if (n_local_players <= 6) { n_x = 3, n_y = 2; } else { LANARTS_ASSERT(n_local_players <= 9); // Last resort, Try 3x3 tiling n_x = 3, n_y = 3; } const int WIDTH = settings.view_width / n_x; const int HEIGHT = settings.view_height / n_y; // / N_PLAYERS; std::vector<BBox> bounding_boxes; for (GameScreen& screen : screens) { const int x1 = (screen.index % n_x) * WIDTH, y1 = (screen.index / n_x) * HEIGHT; bounding_boxes.push_back(BBox {x1, y1, x1 + WIDTH, y1 + HEIGHT}); } if (bounding_boxes.size() == 3) { bounding_boxes[1] = {WIDTH, 0, settings.view_width, settings.view_height}; } for (GameScreen& screen : screens) { BBox b = bounding_boxes[screen.index]; screen.window_region = b; screen.hud = GameHud { BBox(b.x2 - GAME_SIDEBAR_WIDTH, b.y1, b.x2, b.y2), BBox(b.x1, b.y1, b.x2 - GAME_SIDEBAR_WIDTH, b.y2) }; screen.view = GameView {0, 0, b.width() - GAME_SIDEBAR_WIDTH, b.height()}; } }
static int get_X_by_name(const T& t, const char* name, bool error_if_not_found = true) { for (int i = 0; i < t.size(); i++) { if (name == t.at(i).name) { return i; } } if (error_if_not_found) { /*Error if resource not found*/ fprintf(stderr, "Failed to load resource!\nname: %s, of type %s\n", name, typeid(t[0]).name()); fflush(stderr); LANARTS_ASSERT(false /*resource not found*/); } return -1; }
static EquipmentEntry::equip_type name2type(const char* name) { if (strcmp(name, "armour") == 0) { return EquipmentEntry::BODY_ARMOUR; } else if (strcmp(name, "ring") == 0) { return EquipmentEntry::RING; } else if (strcmp(name, "boots") == 0) { return EquipmentEntry::BOOTS; } else if (strcmp(name, "helmet") == 0) { return EquipmentEntry::HEADGEAR; } else if (strcmp(name, "gloves") == 0) { return EquipmentEntry::GLOVES; } else { LANARTS_ASSERT(false); return EquipmentEntry::NONE; } }
SidebarNavigator::NavigationOption& SidebarNavigator::current_option() { switch (view) { case INVENTORY: return inventory; case SPELLS: return spells; case ENEMIES: return enemies; case EQUIPMENT: return equipment; case CONFIG: return config; } /* shouldn't happen */ LANARTS_ASSERT(false); return inventory; }
void GameInstSet::copy_to(GameInstSet& inst_set) const { DepthMap::const_iterator it = depthlist_map.end(); //Synch live objects for (int ind = 0; it != depthlist_map.begin();) { --it; InstanceState* state = it->second.start_of_list; while (state) { GameInst* inst = state->inst; obj_id id = inst->id; GameInst* oinst = inst_set.get_instance(id); if (oinst == NULL || typeid(inst) != typeid(oinst)) { inst_set.add_instance(inst->clone(), id); if (oinst) oinst->destroyed = true; } state = state->next_same_depth; } } //Remove dead objects for (int i = 0; i < inst_set.unit_capacity; i++) { InstanceState* state = &inst_set.unit_set[i]; GameInst* oinst = state->inst; if (valid_inst(oinst)) { if (!oinst->destroyed) { GameInst* inst = get_instance(oinst->id); if (inst != NULL) { inst_set.__update_collision_position(state, Pos(oinst->x, oinst->y), Pos(inst->x, inst->y)); inst->copy_to(oinst); } else inst_set.__remove_instance(state); inst->free_reference(); } } } inst_set.next_id = this->next_id; LANARTS_ASSERT(check_copy_integrity(inst_set)); }
void GameNetConnection::initialize_as_server(const conn_callback &callback, int port) { LANARTS_ASSERT(!_connection); _connection = create_server_connection(port); _connection->initialize_connection(callback, 1); //1ms timeout for connection attempts }
void GameNetConnection::initialize_as_server(int port) { LANARTS_ASSERT(!_connection); _connection = create_server_connection(port); _connection->initialize_connection(); }
void PlayerInst::enqueue_io_actions(GameState* gs) { LANARTS_ASSERT(is_local_player() && gs->local_player() == this); if (actions_set_for_turn) { return; } bool single_player = (gs->player_data().all_players().size() <= 1); actions_set_for_turn = true; GameSettings& settings = gs->game_settings(); GameView& view = gs->view(); PlayerDataEntry& pde = gs->player_data().local_player_data(); if (pde.action_queue.has_actions_for_frame(gs->frame())) { pde.action_queue.extract_actions_for_frame(queued_actions, gs->frame()); event_log("Player %d has %d actions", player_entry(gs).net_id, (int) queued_actions.size()); return; } if (!single_player) { gs->set_repeat_actions_counter(settings.frame_action_repeat); } int dx = 0, dy = 0; bool mouse_within = gs->mouse_x() < gs->view().width; int rmx = view.x + gs->mouse_x(), rmy = view.y + gs->mouse_y(); bool was_moving = moving, do_stopaction = false; IOController& io = gs->io_controller(); if (!settings.loadreplay_file.empty()) { load_actions(gs, queued_actions); } enqueue_io_movement_actions(gs, dx, dy); if (was_moving && !moving && cooldowns().can_do_stopaction()) { do_stopaction = true; } //Shifting target if (gs->key_press_state(SDLK_k)) { shift_autotarget(gs); } if (gs->key_press_state(SDLK_m)) spellselect = -1; bool attack_used = false; if (!gs->game_hud().handle_io(gs, queued_actions)) { attack_used = enqueue_io_spell_and_attack_actions(gs, dx, dy); enqueue_io_equipment_actions(gs, do_stopaction); } bool action_usage = io.query_event(IOEvent::ACTIVATE_SPELL_N) || io.query_event(IOEvent::USE_WEAPON) || io.query_event(IOEvent::AUTOTARGET_CURRENT_ACTION) || io.query_event(IOEvent::MOUSETARGET_CURRENT_ACTION); if ((do_stopaction && !action_usage) || gs->key_down_state(SDLK_PERIOD) || gs->mouse_downwheel()) { queue_portal_use(gs, this, queued_actions); } // If we haven't done anything, rest if (queued_actions.empty()) { queued_actions.push_back(game_action(gs, this, GameAction::USE_REST)); } ActionQueue only_passive_actions; for (int i = 0; i < queued_actions.size(); i++) { GameAction::action_t act = queued_actions[i].act; if (act == GameAction::MOVE || act == GameAction::USE_REST) { only_passive_actions.push_back(queued_actions[i]); } } GameNetConnection& net = gs->net_connection(); if (net.is_connected()) { net_send_player_actions(net, gs->frame(), player_get_playernumber(gs, this), queued_actions); } int repeat = single_player ? 0 : settings.frame_action_repeat; for (int i = 1; i <= repeat; i++) { for (int j = 0; j < only_passive_actions.size(); j++) { only_passive_actions[j].frame = gs->frame() + i; } pde.action_queue.queue_actions_for_frame(only_passive_actions, gs->frame() + i); if (net.is_connected()) { net_send_player_actions(net, gs->frame() + i, player_get_playernumber(gs, this), only_passive_actions); } } }
static bool same_item_colfilter(GameInst* self, GameInst* other) { LANARTS_ASSERT(dynamic_cast<ItemInst*>(self)); ItemInst* other_item = dynamic_cast<ItemInst*>(other); return other_item && ((ItemInst*)self)->item_type() == other_item->item_type(); }
void GameNetConnection::initialize_as_client(const char* host, int port) { LANARTS_ASSERT(!_connection); _connection = create_client_connection(host, port); _connection->initialize_connection(); }
void AnimatedInst::copy_to(GameInst *inst) const { LANARTS_ASSERT(typeid(*this) == typeid(*inst)); *(AnimatedInst*)inst = *this; }
void ItemInst::copy_to(GameInst *inst) const { LANARTS_ASSERT(typeid(*this) == typeid(*inst)); *(ItemInst*)inst = *this; }
void GameNetConnection::initialize_as_client(const conn_callback &callback, const char* host, int port) { LANARTS_ASSERT(!_connection); _connection = create_client_connection(host, port); _connection->initialize_connection(callback, 1); //1ms timeout for connection attempts }