void G_Formation::remove_soldier(const G_SoldierInfo *info) noexcept { assert(info->is_hero()); if (remove_object(info->id())) { the_data()->_formation_opts.emplace_back(); to_opt(the_data()->_formation_opts.back()); } }
void G_Formation::change_soldier(const G_SoldierInfo *old_info, const G_SoldierInfo *new_info) noexcept { assert(old_info->is_hero() == new_info->is_hero()); if (old_info->is_hero()) { ptr<G_FormationItem> old_item = remove_object(old_info->id()); if (!old_item) { return; } G_FormationItem *new_item = probe_object(new_info->id(), new_info); if (new_item) { new_item->_soldier = old_item->_soldier; new_item->_x = old_item->_x; new_item->_y = old_item->_y; } } else { bool found = false; for (G_FormationItem *item : objects()) { if (item->_soldier == old_info) { item->_soldier = new_info; found = true; } } if (!found) { return; } } the_data()->_formation_opts.emplace_back(); to_opt(the_data()->_formation_opts.back()); }
timeval_t G_Cooldown::set(unsigned id, timeval_t time) noexcept { assert(id < G_CD_UNKNOWN); the_data()->_cooldown_opts.emplace_back(); auto &opt = the_data()->_cooldown_opts.back(); opt.id = id; if (time) { object<G_CooldownItem> item(id); the_player()->timer_mgr()->schedule(item, time + G_CooldownMgr::instance()->get(id)); _cds[id] = item; opt.expire = item->expire(); } else { _cds[id] = nullptr; opt.expire = 0; } return opt.expire; }
const Sparse<T> sparse_binop(const Sparse<T> &m1, const Sparse<T> &m2, binop op) { size_t rows = m1.rows(); size_t cols = m1.columns(); assert(rows == m2.rows() && cols == m2.columns()); if (rows == 0 || cols == 0) return m1; index max_size = m1.priv_data().size() + m2.priv_data().size(); Vector<T> data(max_size); Indices column(max_size); Indices row_start(rows + 1); typename Vector<T>::iterator out_data = data.begin(); typename Indices::iterator out_column = column.begin(); typename Indices::iterator out_row_start = row_start.begin(); typename Vector<T>::iterator out_begin = out_data; typename Vector<T>::const_iterator m1_data = m1.priv_data().begin(); typename Indices::const_iterator m1_row_start = m1.priv_row_start().begin(); typename Indices::const_iterator m1_column = m1.priv_column().begin(); typename Vector<T>::const_iterator m2_data = m2.priv_data().begin(); typename Indices::const_iterator m2_row_start = m2.priv_row_start().begin(); typename Indices::const_iterator m2_column = m2.priv_column().begin(); index j1 = *(m1_row_start++); // data start for this row in M1 index l1 = (*m1_row_start) - j1; // # elements in this row in M1 index j2 = *(m2_row_start++); // data start for this row in M2 index l2 = (*m2_row_start) - j2; // # elements in this row in M2 *out_row_start = 0; while (1) { // We look for the next unprocessed matrix element on this row, // for both matrices. c1 and c2 are the columns associated to // each element on each matrix. index c1 = l1 ? *m1_column : cols; index c2 = l2 ? *m2_column : cols; T value; index c; if (c1 < c2) { // There is an element a column c1 on matrix m1, but the // same element at m2 is zero value = op(*m1_data, number_zero<T>()); c = c1; l1--; m1_column++; m1_data++; } else if (c2 < c1) { // There is an element a column c2 on matrix m2, but the // same element at m1 is zero value = op(number_zero<T>(), *m2_data); c = c2; l2--; m2_column++; m2_data++; } else if (c2 < cols) { // Both elements in m1 and m2 are nonzero. value = op(*m1_data, *m2_data); c = c1; l1--; l2--; m1_column++; m1_data++; m2_column++; m2_data++; } else { // We have processed all elements in this row. out_row_start++; *out_row_start = out_data - out_begin; if (--rows == 0) { break; } j1 = *m1_row_start; m1_row_start++; l1 = (*m1_row_start) - j1; j2 = *m2_row_start; m2_row_start++; l2 = (*m2_row_start) - j2; continue; } if (!(value == number_zero<T>())) { *(out_data++) = value; *(out_column++) = c; } } index j = out_data - out_begin; Indices the_column(j); std::copy(column.begin(), column.begin() + j, the_column.begin()); Vector<T> the_data(j); std::copy(data.begin(), data.begin() + j, the_data.begin()); return Sparse<T>(m1.dimensions(), row_start, the_column, the_data); }