+/*
+ * Combine and reorder items in the home
+ */
+bool combine_and_reorder_home(int store_num)
+{
+ int i, j, k;
+ s32b o_value;
+ object_type forge, *o_ptr, *j_ptr;
+ bool flag = FALSE, combined;
+ store_type *old_st_ptr = st_ptr;
+ bool old_stack_force_notes = stack_force_notes;
+ bool old_stack_force_costs = stack_force_costs;
+
+ st_ptr = &town[1].store[store_num];
+ if (store_num != STORE_HOME)
+ {
+ stack_force_notes = FALSE;
+ stack_force_costs = FALSE;
+ }
+
+ do
+ {
+ combined = FALSE;
+
+ /* Combine the items in the home (backwards) */
+ for (i = st_ptr->stock_num - 1; i > 0; i--)
+ {
+ /* Get the item */
+ o_ptr = &st_ptr->stock[i];
+
+ /* Skip empty items */
+ if (!o_ptr->k_idx) continue;
+
+ /* Scan the items above that item */
+ for (j = 0; j < i; j++)
+ {
+ int max_num;
+
+ /* Get the item */
+ j_ptr = &st_ptr->stock[j];
+
+ /* Skip empty items */
+ if (!j_ptr->k_idx) continue;
+
+ /*
+ * Get maximum number of the stack if these
+ * are similar, get zero otherwise.
+ */
+ max_num = object_similar_part(j_ptr, o_ptr);
+
+ /* Can we (partialy) drop "o_ptr" onto "j_ptr"? */
+ if (max_num && j_ptr->number < max_num)
+ {
+ if (o_ptr->number + j_ptr->number <= max_num)
+ {
+ /* Add together the item counts */
+ object_absorb(j_ptr, o_ptr);
+
+ /* One object is gone */
+ st_ptr->stock_num--;
+
+ /* Slide everything down */
+ for (k = i; k < st_ptr->stock_num; k++)
+ {
+ /* Structure copy */
+ st_ptr->stock[k] = st_ptr->stock[k + 1];
+ }
+
+ /* Erase the "final" slot */
+ object_wipe(&st_ptr->stock[k]);
+ }
+ else
+ {
+ int old_num = o_ptr->number;
+ int remain = j_ptr->number + o_ptr->number - max_num;
+
+ /* Add together the item counts */
+ object_absorb(j_ptr, o_ptr);
+
+ o_ptr->number = remain;
+
+ /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
+ if (o_ptr->tval == TV_ROD)
+ {
+ o_ptr->pval = o_ptr->pval * remain / old_num;
+ o_ptr->timeout = o_ptr->timeout * remain / old_num;
+ }
+
+ /* Hack -- if wands are stacking, combine the charges. -LM- */
+ else if (o_ptr->tval == TV_WAND)
+ {
+ o_ptr->pval = o_ptr->pval * remain / old_num;
+ }
+ }
+
+ /* Take note */
+ combined = TRUE;
+
+ /* Done */
+ break;
+ }
+ }
+ }
+
+ flag |= combined;
+ }
+ while (combined);
+
+ /* Re-order the items in the home (forwards) */
+ for (i = 0; i < st_ptr->stock_num; i++)
+ {
+ /* Get the item */
+ o_ptr = &st_ptr->stock[i];
+
+ /* Skip empty slots */
+ if (!o_ptr->k_idx) continue;
+
+ /* Get the "value" of the item */
+ o_value = object_value(o_ptr);
+
+ /* Scan every occupied slot */
+ for (j = 0; j < st_ptr->stock_num; j++)
+ {
+ if (object_sort_comp(o_ptr, o_value, &st_ptr->stock[j])) break;
+ }
+
+ /* Never move down */
+ if (j >= i) continue;
+
+ /* Take note */
+ flag = TRUE;
+
+ /* Get local object */
+ j_ptr = &forge;
+
+ /* Save a copy of the moving item */
+ object_copy(j_ptr, &st_ptr->stock[i]);
+
+ /* Slide the objects */
+ for (k = i; k > j; k--)
+ {
+ /* Slide the item */
+ object_copy(&st_ptr->stock[k], &st_ptr->stock[k - 1]);
+ }
+
+ /* Insert the moving item */
+ object_copy(&st_ptr->stock[j], j_ptr);
+ }
+
+ st_ptr = old_st_ptr;
+ if (store_num != STORE_HOME)
+ {
+ stack_force_notes = old_stack_force_notes;
+ stack_force_costs = old_stack_force_costs;
+ }
+
+ return flag;
+}
+