Attached Files |
Shopping+sorting2.patch [^] (4,888 bytes) 2012-01-04 06:42 [Show Content] [Hide Content]From d1a8602a15a6a13bd4ef5a6a94f31b982a0669d3 Wed, 4 Jan 2012 02:40:04 -0200
From: Kek <tuk kek at gmail com>
Date: Wed, 4 Jan 2012 02:22:55 -0200
Subject: [PATCH] Shopping sorting
diff --git a/crawl-ref/source/shopping.cc b/crawl-ref/source/shopping.cc
index f7afff9..f202fed 100644
--- a/crawl-ref/source/shopping.cc
+++ b/crawl-ref/source/shopping.cc
@@ -43,6 +43,16 @@
static bool _in_shop_now = false;
+
+enum ordering_modes
+{
+ ORDER_MODE_DEFAULT=0,
+ ORDER_MODE_PRICE,
+ ORDER_MODE_ALPHABETICAL,
+ ORDER_MODE_TYPE,
+ NUM_ORDER_MODES
+} order = ORDER_MODE_DEFAULT;
+
static bool _purchase(int shop, int item_got, int cost, bool id);
static void _shop_print(const char *shoppy, int line)
@@ -167,14 +177,22 @@
pkeys = "[" + pkeys + "] select item to "
+ (viewing ? "examine" : "buy");
}
+ std::string sort_mode;
+ switch (order){
+ case ORDER_MODE_DEFAULT: sort_mode="default";break;
+ case ORDER_MODE_PRICE: sort_mode="price";break;
+ case ORDER_MODE_ALPHABETICAL: sort_mode="name";break;
+ case ORDER_MODE_TYPE: sort_mode="type";break;
+ default: sort_mode="unknown";break;
+ }
fs = formatted_string::parse_string(make_stringf(
"[<w>x</w>/<w>Esc</w>"
#ifdef USE_TILE
"/<w>R-Click</w>"
#endif
- "] exit [<w>!</w>] %s %s",
+ "] exit [<w>!</w>] %s %s [<w>/</w>] sort (%s)",
(viewing ? "buy items " : "examine items "),
- pkeys.c_str()));
+ pkeys.c_str(),sort_mode.c_str()));
fs.cprintf("%*s", get_number_of_cols() - fs.width() - 1, "");
fs.display();
@@ -228,13 +246,66 @@
ShopInfo &si = StashTrack.get_shop(shop.pos);
const bool id = shoptype_identifies_stock(shop.type);
std::string purchasable;
- for (unsigned int i = 0; i < stock.size(); ++i)
+
+ //order using bubblesort
+ std::vector<int> stockOrder(stock.size(),0);
+ for (unsigned int i = 0; i < stockOrder.size(); ++i)
{
+ stockOrder[i]=i;
+ }
+ bool swapped;
+ do
+ {
+ swapped=false;
+ for (unsigned int i = 1;i<stockOrder.size();i++)
+ {
+ int previous = i-1;
+ item_def& a = mitm[stock[stockOrder[previous]]];
+ item_def& b = mitm[stock[stockOrder[i]]];
+ bool swap;
+ switch (order) {
+ case ORDER_MODE_PRICE:
+ swap = (
+ _shop_get_item_value(a, shop.greed, id)<
+ _shop_get_item_value(b, shop.greed, id)
+ );
+ break;
+ case ORDER_MODE_ALPHABETICAL:
+ swap = (
+ a.name(DESC_A, false, id).compare(
+ b.name(DESC_A, false, id).c_str()
+ )>0
+ );
+ break;
+ case ORDER_MODE_TYPE:
+ if (a.base_type==b.base_type) {
+ swap = (a.sub_type<b.sub_type);
+ } else {
+ swap = (a.base_type<b.base_type);
+ }
+ break;
+ case ORDER_MODE_DEFAULT:
+ default:
+ swap = false;
+ }
+ if (swap) {
+ int temp=stockOrder[previous];
+ stockOrder[previous]=stockOrder[i];
+ stockOrder[i]=temp;
+ swapped=true;
+ }
+ }
+ } while (swapped);
+
+ for (unsigned int index = 0; index < stockOrder.size(); ++index)
+ {
+ unsigned int i = stockOrder[index];
const item_def& item = mitm[stock[i]];
const int gp_value = _shop_get_item_value(item, shop.greed, id);
const bool can_afford = (you.gold >= gp_value);
- cgotoxy(1, i+1, GOTO_CRT);
+ cgotoxy(1, index+1, GOTO_CRT);
const char c = i + 'a';
if (can_afford)
purchasable += c;
@@ -282,7 +353,7 @@
textcolor(col != -1 ? col : LIGHTGREY);
}
else
- textcolor(i % 2 ? LIGHTGREY : WHITE);
+ textcolor(index % 2 ? LIGHTGREY : WHITE);
std::string item_name = item.name(DESC_A, false, id);
if (unknown)
@@ -595,6 +666,20 @@
//_shop_more();
continue;
}
+ else if (key == '/')
+ {
+ // Toggle between order mode.
+ for (int mode = ORDER_MODE_DEFAULT; ; ++mode)
+ {
+ if (mode==NUM_ORDER_MODES) {
+ order=ORDER_MODE_DEFAULT;
+ break;
+ } else if (mode>order) {
+ order = static_cast<ordering_modes>(mode);
+ break;
+ }
+ }
+ }
else if (key == '!')
{
// Toggle between browsing and shopping.
0001-Shopping-sorting.patch [^] (4,870 bytes) 2013-03-11 21:34 [Show Content] [Hide Content]From 28919b411546fc766bef51bf27f5773283090887 Mon Sep 17 00:00:00 2001
From: Kek <tuk kek at gmail com>
Date: Wed, 4 Jan 2012 02:22:55 -0200
Subject: [PATCH 1/2] Shopping sorting
---
crawl-ref/source/shopping.cc | 92 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 88 insertions(+), 4 deletions(-)
diff --git a/crawl-ref/source/shopping.cc b/crawl-ref/source/shopping.cc
index 8f2afbb..a1b8a5b 100644
--- a/crawl-ref/source/shopping.cc
+++ b/crawl-ref/source/shopping.cc
@@ -48,6 +48,16 @@ ShoppingList shopping_list;
static bool _in_shop_now = false;
+
+enum ordering_modes
+{
+ ORDER_MODE_DEFAULT=0,
+ ORDER_MODE_PRICE,
+ ORDER_MODE_ALPHABETICAL,
+ ORDER_MODE_TYPE,
+ NUM_ORDER_MODES
+} order = ORDER_MODE_DEFAULT;
+
static bool _purchase(int shop, int item_got, int cost, bool id);
static void _shop_print(const char *shoppy, int line)
@@ -388,14 +398,22 @@ static void _list_shop_keys(const string &purchasable, bool viewing,
pkeys = "[" + pkeys + "] select item to "
+ (viewing ? "examine" : "buy");
}
+ std::string sort_mode;
+ switch (order){
+ case ORDER_MODE_DEFAULT: sort_mode="default";break;
+ case ORDER_MODE_PRICE: sort_mode="price";break;
+ case ORDER_MODE_ALPHABETICAL: sort_mode="name";break;
+ case ORDER_MODE_TYPE: sort_mode="type";break;
+ default: sort_mode="unknown";break;
+ }
fs = formatted_string::parse_string(make_stringf(
"[<w>x</w>/<w>Esc</w>"
#ifdef USE_TILE
"/<w>R-Click</w>"
#endif
- "] exit [<w>!</w>] %s %s",
+ "] exit [<w>!</w>] %s %s [<w>/</w>] sort (%s)",
(viewing ? "buy items " : "examine items "),
- pkeys.c_str()));
+ pkeys.c_str(),sort_mode.c_str()));
fs.cprintf("%*s", get_number_of_cols() - fs.width() - 1, "");
fs.display();
@@ -458,13 +476,65 @@ static string _shop_print_stock(const vector<int>& stock,
#ifdef USE_TILE_LOCAL
TextItem* tmp = NULL;
#endif
- for (unsigned int i = 0; i < stock.size(); ++i)
+
+ //order using bubblesort
+ std::vector<int> stockOrder(stock.size(),0);
+ for (unsigned int i = 0; i < stockOrder.size(); ++i)
+ {
+ stockOrder[i]=i;
+ }
+ bool swapped;
+ do
{
+ swapped=false;
+ for (unsigned int i = 1;i<stockOrder.size();i++)
+ {
+ int previous = i-1;
+ item_def& a = mitm[stock[stockOrder[previous]]];
+ item_def& b = mitm[stock[stockOrder[i]]];
+ bool swap;
+ switch (order) {
+ case ORDER_MODE_PRICE:
+ swap = (
+ _shop_get_item_value(a, shop.greed, id)<
+ _shop_get_item_value(b, shop.greed, id)
+ );
+ break;
+ case ORDER_MODE_ALPHABETICAL:
+ swap = (
+ a.name(DESC_A, false, id).compare(
+ b.name(DESC_A, false, id).c_str()
+ )>0
+ );
+ break;
+ case ORDER_MODE_TYPE:
+ if (a.base_type==b.base_type) {
+ swap = (a.sub_type<b.sub_type);
+ } else {
+ swap = (a.base_type<b.base_type);
+ }
+ break;
+ case ORDER_MODE_DEFAULT:
+ default:
+ swap = false;
+ }
+ if (swap) {
+ int temp=stockOrder[previous];
+ stockOrder[previous]=stockOrder[i];
+ stockOrder[i]=temp;
+ swapped=true;
+ }
+ }
+ } while (swapped);
+
+ for (unsigned int index = 0; index < stockOrder.size(); ++index)
+ {
+ unsigned int i = stockOrder[index];
const item_def& item = mitm[stock[i]];
const int gp_value = _shop_get_item_value(item, shop.greed, id);
const bool can_afford = (you.gold >= gp_value);
- cgotoxy(1, i+1, GOTO_CRT);
+ cgotoxy(1, index+1, GOTO_CRT);
const char c = i + 'a';
if (can_afford)
purchasable += c;
@@ -870,6 +940,20 @@ static bool _in_a_shop(int shopidx, int &num_in_list)
//_shop_more();
continue;
}
+ else if (key == '/')
+ {
+ // Toggle between order mode.
+ for (int mode = ORDER_MODE_DEFAULT; ; ++mode)
+ {
+ if (mode==NUM_ORDER_MODES) {
+ order=ORDER_MODE_DEFAULT;
+ break;
+ } else if (mode>order) {
+ order = static_cast<ordering_modes>(mode);
+ break;
+ }
+ }
+ }
else if (key == '!')
{
// Toggle between browsing and shopping.
--
1.7.10.4
0002-Various-cleanups-for-shopping-list-ordering.patch [^] (6,369 bytes) 2013-03-11 21:34 [Show Content] [Hide Content]From 28eba056c57d73344a008299ae28a24475a3cde5 Mon Sep 17 00:00:00 2001
From: Neil Moore <neil@s-z.org>
Date: Mon, 11 Mar 2013 16:33:20 -0400
Subject: [PATCH 2/2] Various cleanups for shopping list ordering.
---
crawl-ref/source/shopping.cc | 129 ++++++++++++++++++++----------------------
1 file changed, 60 insertions(+), 69 deletions(-)
diff --git a/crawl-ref/source/shopping.cc b/crawl-ref/source/shopping.cc
index a1b8a5b..df09751 100644
--- a/crawl-ref/source/shopping.cc
+++ b/crawl-ref/source/shopping.cc
@@ -49,14 +49,20 @@ ShoppingList shopping_list;
static bool _in_shop_now = false;
-enum ordering_modes
+enum ordering_mode
{
ORDER_MODE_DEFAULT=0,
ORDER_MODE_PRICE,
ORDER_MODE_ALPHABETICAL,
ORDER_MODE_TYPE,
NUM_ORDER_MODES
-} order = ORDER_MODE_DEFAULT;
+};
+
+static ordering_mode shopping_order = ORDER_MODE_DEFAULT;
+
+static const char * const shopping_order_names[NUM_ORDER_MODES] = {
+ "default", "price", "name", "type"
+};
static bool _purchase(int shop, int item_got, int cost, bool id);
@@ -398,14 +404,9 @@ static void _list_shop_keys(const string &purchasable, bool viewing,
pkeys = "[" + pkeys + "] select item to "
+ (viewing ? "examine" : "buy");
}
- std::string sort_mode;
- switch (order){
- case ORDER_MODE_DEFAULT: sort_mode="default";break;
- case ORDER_MODE_PRICE: sort_mode="price";break;
- case ORDER_MODE_ALPHABETICAL: sort_mode="name";break;
- case ORDER_MODE_TYPE: sort_mode="type";break;
- default: sort_mode="unknown";break;
- }
+ ASSERT(shopping_order >= ORDER_MODE_DEFAULT
+ && shopping_order < NUM_ORDER_MODES);
+ std::string sort_mode = shopping_order_names[shopping_order];
fs = formatted_string::parse_string(make_stringf(
"[<w>x</w>/<w>Esc</w>"
#ifdef USE_TILE
@@ -460,6 +461,46 @@ static int _shop_get_item_value(const item_def& item, int greed, bool id,
return result;
}
+// Comparator for sorting a permutation list according to the shop, the
+// original list of item IDs, and the current ordering mode.
+class ShopSorter {
+public:
+ ShopSorter(const shop_struct &shop, const vector<int>& stocklist)
+ : stock(stocklist), id(shoptype_identifies_stock(shop.type)),
+ greed(shop.greed)
+ {}
+
+ bool operator()(int a_index, int b_index) const
+ {
+ item_def& a = mitm[stock[a_index]];
+ item_def& b = mitm[stock[b_index]];
+
+ switch(shopping_order)
+ {
+ case ORDER_MODE_PRICE:
+ // Greed will affect all items equally (other than rounding
+ // error), so don't bother taking the shop's actual greed level.
+ return (_shop_get_item_value(a, greed, id)
+ < _shop_get_item_value(b, greed, id));
+ case ORDER_MODE_ALPHABETICAL:
+ return (a.name(DESC_PLAIN, false, id)
+ < b.name(DESC_PLAIN, false, id));
+ case ORDER_MODE_TYPE:
+ if (a.base_type == b.base_type)
+ return (a.sub_type < b.sub_type);
+ else
+ return (a.base_type < b.base_type);
+ case ORDER_MODE_DEFAULT:
+ default:
+ return a_index < b_index;
+ }
+ }
+private:
+ const vector<int>& stock;
+ const bool id;
+ const int greed;
+};
+
static string _shop_print_stock(const vector<int>& stock,
const vector<bool>& selected,
const vector<bool>& in_list,
@@ -477,55 +518,11 @@ static string _shop_print_stock(const vector<int>& stock,
TextItem* tmp = NULL;
#endif
- //order using bubblesort
- std::vector<int> stockOrder(stock.size(),0);
- for (unsigned int i = 0; i < stockOrder.size(); ++i)
- {
+ // Permutation list.
+ vector<int> stockOrder(stock.size(),0);
+ for (unsigned int i = 0; i < stock.size(); ++i)
stockOrder[i]=i;
- }
- bool swapped;
- do
- {
- swapped=false;
- for (unsigned int i = 1;i<stockOrder.size();i++)
- {
- int previous = i-1;
- item_def& a = mitm[stock[stockOrder[previous]]];
- item_def& b = mitm[stock[stockOrder[i]]];
- bool swap;
- switch (order) {
- case ORDER_MODE_PRICE:
- swap = (
- _shop_get_item_value(a, shop.greed, id)<
- _shop_get_item_value(b, shop.greed, id)
- );
- break;
- case ORDER_MODE_ALPHABETICAL:
- swap = (
- a.name(DESC_A, false, id).compare(
- b.name(DESC_A, false, id).c_str()
- )>0
- );
- break;
- case ORDER_MODE_TYPE:
- if (a.base_type==b.base_type) {
- swap = (a.sub_type<b.sub_type);
- } else {
- swap = (a.base_type<b.base_type);
- }
- break;
- case ORDER_MODE_DEFAULT:
- default:
- swap = false;
- }
- if (swap) {
- int temp=stockOrder[previous];
- stockOrder[previous]=stockOrder[i];
- stockOrder[i]=temp;
- swapped=true;
- }
- }
- } while (swapped);
+ stable_sort(stockOrder.begin(), stockOrder.end(), ShopSorter(shop, stock));
for (unsigned int index = 0; index < stockOrder.size(); ++index)
{
@@ -942,17 +939,11 @@ static bool _in_a_shop(int shopidx, int &num_in_list)
}
else if (key == '/')
{
- // Toggle between order mode.
- for (int mode = ORDER_MODE_DEFAULT; ; ++mode)
- {
- if (mode==NUM_ORDER_MODES) {
- order=ORDER_MODE_DEFAULT;
- break;
- } else if (mode>order) {
- order = static_cast<ordering_modes>(mode);
- break;
- }
- }
+ // Toggle between order modes.
+ int mode = shopping_order;
+ if (++mode >= NUM_ORDER_MODES)
+ mode = ORDER_MODE_DEFAULT;
+ shopping_order = static_cast<ordering_mode>(mode);
}
else if (key == '!')
{
--
1.7.10.4
0001-Shopping-sorting201310.patch [^] (6,372 bytes) 2013-10-15 20:36 [Show Content] [Hide Content]From 20b91dab048be348f999bcdead6d9c544a090ed6 Mon Sep 17 00:00:00 2001
From: Kek <tuk kek at gmail com>
Date: Wed, 4 Jan 2012 02:22:55 -0200
Subject: [PATCH] Shopping sorting
---
crawl-ref/source/shopping.cc | 98 +++++++++++++++++++++++++++++++++++++++---
1 files changed, 91 insertions(+), 7 deletions(-)
diff --git a/crawl-ref/source/shopping.cc b/crawl-ref/source/shopping.cc
index b0faa0e..adb1f24 100644
--- a/crawl-ref/source/shopping.cc
+++ b/crawl-ref/source/shopping.cc
@@ -49,6 +49,22 @@ ShoppingList shopping_list;
static bool _in_shop_now = false;
+
+enum ordering_mode
+{
+ ORDER_MODE_DEFAULT=0,
+ ORDER_MODE_PRICE,
+ ORDER_MODE_ALPHABETICAL,
+ ORDER_MODE_TYPE,
+ NUM_ORDER_MODES
+};
+
+static ordering_mode shopping_order = ORDER_MODE_DEFAULT;
+
+static const char * const shopping_order_names[NUM_ORDER_MODES] = {
+ "default", "price", "name", "type"
+};
+
static bool _purchase(int shop, int item_got, int cost, bool id);
static void _shop_print(const char *shoppy, int line)
@@ -204,8 +220,7 @@ static void _list_shop_keys(const string &purchasable, bool viewing,
tmp->set_visible(true);
// ///////// BUY/EXAMINE ITEMS //////////
- // set cursor [20 chars of text + 11 spaces + start at 1]
- cgotoxy(32, numlines - 1, GOTO_CRT);
+ cgotoxy(22, numlines - 1, GOTO_CRT);
// print formatted string for BUY/EXAMINE ITEMS
fs = formatted_string::parse_string(make_stringf(
"[<w>!</w>] %s",
@@ -227,9 +242,16 @@ static void _list_shop_keys(const string &purchasable, bool viewing,
tmp->set_visible(true);
+ // ///////// SORT MODE //////////
+ cgotoxy(40, numlines - 1, GOTO_CRT);
+ string sortmodename=shopping_order_names[shopping_order];
+ string sortmode = make_stringf("[<w>/</w>] sort (%s)",
+ sortmodename.c_str());
+ fs = formatted_string::parse_string(sortmode.c_str());
+ fs.display();
+
// ///////// SELECT ITEM TO BUY/EXAMINE //////////
- // set cursor [32 + 16 chars + 5 whitespace]
- cgotoxy(53, numlines - 1, GOTO_CRT);
+ cgotoxy(59, numlines - 1, GOTO_CRT);
// calculate and draw formatted text
string pkeys = "";
if (viewing)
@@ -389,12 +411,16 @@ static void _list_shop_keys(const string &purchasable, bool viewing,
pkeys = "[" + pkeys + "] select item to "
+ (viewing ? "examine" : "buy");
}
+ ASSERT(shopping_order >= ORDER_MODE_DEFAULT
+ && shopping_order < NUM_ORDER_MODES);
+ std::string sort_mode = shopping_order_names[shopping_order];
fs = formatted_string::parse_string(make_stringf(
"[<w>x</w>/<w>Esc</w>"
#ifdef USE_TILE
"/<w>R-Click</w>"
#endif
- "] exit [<w>!</w>] %s %s",
+ "] exit [<w>/</w>] sort (%s) [<w>!</w>] %s %s",
+ sort_mode.c_str(),
(viewing ? "buy items " : "examine items "),
pkeys.c_str()));
@@ -447,6 +473,47 @@ static int _shop_get_item_value(const item_def& item, int greed, bool id,
return max(result, 1);
}
+// Comparator for sorting a permutation list according to the shop, the
+// original list of item IDs, and the current ordering mode.
+class ShopSorter {
+public:
+ ShopSorter(const shop_struct &shop, const vector<int>& stocklist)
+ : stock(stocklist), id(shoptype_identifies_stock(shop.type)),
+ greed(shop.greed)
+ {}
+
+ bool operator()(int a_index, int b_index) const
+ {
+ item_def& a = mitm[stock[a_index]];
+ item_def& b = mitm[stock[b_index]];
+
+ switch(shopping_order)
+ {
+ case ORDER_MODE_PRICE:
+ // Greed will affect all items equally (other than rounding
+ // error), so don't bother taking the shop's actual greed level.
+ return (_shop_get_item_value(a, greed, id)
+ < _shop_get_item_value(b, greed, id));
+ case ORDER_MODE_ALPHABETICAL:
+ return (a.name(DESC_PLAIN, false, id)
+ < b.name(DESC_PLAIN, false, id));
+ case ORDER_MODE_TYPE:
+ if (a.base_type == b.base_type)
+ return (a.sub_type < b.sub_type);
+ else
+ return (a.base_type < b.base_type);
+ case ORDER_MODE_DEFAULT:
+ default:
+ return a_index < b_index;
+ }
+ }
+private:
+ const vector<int>& stock;
+ const bool id;
+ const int greed;
+};
+vector<int> stockOrderInput;
+
static string _shop_print_stock(const vector<int>& stock,
const vector<bool>& selected,
const vector<bool>& in_list,
@@ -463,14 +530,22 @@ static string _shop_print_stock(const vector<int>& stock,
#ifdef USE_TILE_LOCAL
TextItem* tmp = NULL;
#endif
+
+ // Permutation list.
+ vector<int> stockOrder(stock.size(),0);
for (unsigned int i = 0; i < stock.size(); ++i)
+ stockOrder[i]=i;
+ stable_sort(stockOrder.begin(), stockOrder.end(), ShopSorter(shop, stock));
+ stockOrderInput=stockOrder;
+ for (unsigned int index = 0; index < stockOrder.size(); ++index)
{
+ unsigned int i = stockOrder[index];
const item_def& item = mitm[stock[i]];
const int gp_value = _shop_get_item_value(item, shop.greed, id);
const bool can_afford = (you.gold >= gp_value);
- cgotoxy(1, i+1, GOTO_CRT);
- const char c = i + 'a';
+ cgotoxy(1, index+1, GOTO_CRT);
+ const char c = index + 'a';
if (can_afford)
purchasable += c;
@@ -872,6 +947,14 @@ static bool _in_a_shop(int shopidx, int &num_in_list)
//_shop_more();
continue;
}
+ else if (key == '/')
+ {
+ // Toggle between order modes.
+ int mode = shopping_order;
+ if (++mode >= NUM_ORDER_MODES)
+ mode = ORDER_MODE_DEFAULT;
+ shopping_order = static_cast<ordering_mode>(mode);
+ }
else if (key == '!')
{
// Toggle between browsing and shopping.
@@ -960,6 +1043,7 @@ static bool _in_a_shop(int shopidx, int &num_in_list)
_shop_more();
continue;
}
+ key=stockOrderInput[key];
item_def& item = mitm[stock[key]];
if (viewing)
--
1.7.2.5
|