void draw_caravan_categories(WINDOW *w, int category_selected, int total_price, int cash) { // Clear the window for (int i = 1; i <= 10; i++) mvwprintz(w, i, 1, c_black, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); mvwprintz(w, 1, 1, c_white, "Your Cash:%s%d", (cash >= 10000 ? " " : (cash >= 1000 ? " " : (cash >= 100 ? " " : (cash >= 10 ? " " : " ")))), cash); wprintz(w, c_ltgray, " -> "); wprintz(w, (total_price > cash ? c_red : c_green), "%d", cash - total_price); for (int i = 0; i < NUM_CARAVAN_CATEGORIES; i++) mvwprintz(w, i + 3, 1, (i == category_selected ? h_white : c_white), caravan_category_name( caravan_category(i) ).c_str()); wrefresh(w); }
void defense_game::caravan(game *g) { std::vector<itype_id> items[NUM_CARAVAN_CATEGORIES]; std::vector<int> item_count[NUM_CARAVAN_CATEGORIES]; // Init the items for each category for (int i = 0; i < NUM_CARAVAN_CATEGORIES; i++) { items[i] = caravan_items( caravan_category(i) ); for (int j = 0; j < items[i].size(); j++) { if (current_wave == 0 || !one_in(4)) item_count[i].push_back(0); // Init counts to 0 for each item else { // Remove the item items[i].erase( items[i].begin() + j); j--; } } } int total_price = 0; WINDOW *w = newwin(25, 80, 0, 0); int offset = 0, item_selected = 0, category_selected = 0; int current_window = 0; draw_caravan_borders(w, current_window); draw_caravan_categories(w, category_selected, total_price, g->u.cash); bool done = false; bool cancel = false; while (!done) { char ch = input(); switch (ch) { case '?': popup_top("\ CARAVAN:\n\ Start by selecting a category using your favorite up/down keys.\n\ Switch between category selection and item selecting by pressing Tab.\n\ Pick an item with the up/down keys, press + to buy 1 more, - to buy 1 less.\n\ Press Enter to buy everything in your cart, Esc to buy nothing."); draw_caravan_categories(w, category_selected, total_price, g->u.cash); draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); break; case 'j': if (current_window == 0) { // Categories category_selected++; if (category_selected == NUM_CARAVAN_CATEGORIES) category_selected = CARAVAN_CART; draw_caravan_categories(w, category_selected, total_price, g->u.cash); offset = 0; item_selected = 0; draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } else if (items[category_selected].size() > 0) { // Items if (item_selected < items[category_selected].size() - 1) item_selected++; else { item_selected = 0; offset = 0; } if (item_selected > offset + 22) offset++; draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } break; case 'k': if (current_window == 0) { // Categories if (category_selected == 0) category_selected = NUM_CARAVAN_CATEGORIES - 1; else category_selected--; if (category_selected == NUM_CARAVAN_CATEGORIES) category_selected = CARAVAN_CART; draw_caravan_categories(w, category_selected, total_price, g->u.cash); offset = 0; item_selected = 0; draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } else if (items[category_selected].size() > 0) { // Items if (item_selected > 0) item_selected--; else { item_selected = items[category_selected].size() - 1; offset = item_selected - 22; if (offset < 0) offset = 0; } if (item_selected < offset) offset--; draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } break; case '+': case 'l': if (current_window == 1 && items[category_selected].size() > 0) { item_count[category_selected][item_selected]++; itype_id tmp_itm = items[category_selected][item_selected]; total_price += caravan_price(g->u, g->itypes[tmp_itm]->price); if (category_selected == CARAVAN_CART) { // Find the item in its category for (int i = 1; i < NUM_CARAVAN_CATEGORIES; i++) { for (int j = 0; j < items[i].size(); j++) { if (items[i][j] == tmp_itm) item_count[i][j]++; } } } else { // Add / increase the item in the shopping cart bool found_item = false; for (int i = 0; i < items[0].size() && !found_item; i++) { if (items[0][i] == tmp_itm) { found_item = true; item_count[0][i]++; } } if (!found_item) { items[0].push_back(items[category_selected][item_selected]); item_count[0].push_back(1); } } draw_caravan_categories(w, category_selected, total_price, g->u.cash); draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } break; case '-': case 'h': if (current_window == 1 && items[category_selected].size() > 0 && item_count[category_selected][item_selected] > 0) { item_count[category_selected][item_selected]--; itype_id tmp_itm = items[category_selected][item_selected]; total_price -= caravan_price(g->u, g->itypes[tmp_itm]->price); if (category_selected == CARAVAN_CART) { // Find the item in its category for (int i = 1; i < NUM_CARAVAN_CATEGORIES; i++) { for (int j = 0; j < items[i].size(); j++) { if (items[i][j] == tmp_itm) item_count[i][j]--; } } } else { // Decrease / remove the item in the shopping cart bool found_item = false; for (int i = 0; i < items[0].size() && !found_item; i++) { if (items[0][i] == tmp_itm) { found_item = true; item_count[0][i]--; if (item_count[0][i] == 0) { item_count[0].erase(item_count[0].begin() + i); items[0].erase(items[0].begin() + i); } } } } draw_caravan_categories(w, category_selected, total_price, g->u.cash); draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } break; case '\t': current_window = (current_window + 1) % 2; draw_caravan_borders(w, current_window); break; case KEY_ESCAPE: if (query_yn("Really buy nothing?")) { cancel = true; done = true; } else { draw_caravan_categories(w, category_selected, total_price, g->u.cash); draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } break; case '\n': if (total_price > g->u.cash) popup("You can't afford those items!"); else if ((items[0].empty() && query_yn("Really buy nothing?")) || (!items[0].empty() && query_yn("Buy %d items, leaving you with $%d?", items[0].size(), g->u.cash - total_price))) done = true; if (!done) { // We canceled, so redraw everything draw_caravan_categories(w, category_selected, total_price, g->u.cash); draw_caravan_items(w, g, &(items[category_selected]), &(item_count[category_selected]), offset, item_selected); draw_caravan_borders(w, current_window); } break; } // switch (ch) } // while (!done) if (!cancel) { g->u.cash -= total_price; bool dropped_some = false; for (int i = 0; i < items[0].size(); i++) { item tmp(g->itypes[ items[0][i] ], g->turn); tmp = tmp.in_its_container(&(g->itypes)); for (int j = 0; j < item_count[0][i]; j++) { if (g->u.volume_carried() + tmp.volume() <= g->u.volume_capacity() && g->u.weight_carried() + tmp.weight() <= g->u.weight_capacity() && g->u.inv.size() < inv_chars.size()) g->u.i_add(tmp); else { // Could fit it in the inventory! dropped_some = true; g->m.add_item(g->u.posx, g->u.posy, tmp); } } } if (dropped_some) g->add_msg("You drop some items."); } }