/* * Copyright (C) 2002-2007 The Warp Rogue Team * Part of the Warp Rogue Project * * This software is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License. * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY. * * See the license.txt file for more details. */ /* * Module Name: Economy * Description: - */ #define Uses_Ui #define Uses_Object #define Uses_Inventory #define Uses_Perks #define Uses_Stats #define Uses_Util #define Uses_ProgramManager #include "mheader.h" #include "economy.h" /* * shop type */ typedef struct { LIST * object_list; MENU * menu; PRICE_LEVEL price_level; } SHOP; static void shop_exchange(CHARACTER *, OBJECT *); static void shop_menu_create(void); static MONEY shop_object_price(const OBJECT *); static bool customer_can_acquire(const CHARACTER *, const OBJECT *, MONEY ); /* * the current shop */ static SHOP Shop = { /* OBJECT_LIST */ NULL, /* MENU */ NULL, /* PRICE_LEVEL */ 100 }; /* * the shop */ void shop(CHARACTER *customer) { shop_menu_create(); do { OBJECT *object; command_bar_set(5, CM_UP, CM_DOWN, CM_OK, CM_INFO, CM_EXIT ); render_shop_screen(customer); shop_screen_menu(Shop.menu); object = (OBJECT *)list_data_at(Shop.object_list, Shop.menu->highlighted ); if (Shop.menu->entered_command == CM_OK) { shop_exchange(customer, object); } else if (Shop.menu->entered_command == CM_INFO) { object_screen(object); } } while (Shop.menu->entered_command != CM_EXIT); menu_free_with(Shop.menu, free); list_free_with(Shop.object_list, object_free); Shop.object_list = NULL; } /* * adds an object to the current shop */ void shop_add(const char *object_name) { if (Shop.object_list == NULL) { Shop.object_list = list_new(); } list_add(Shop.object_list, object_create(object_name)); } /* * sets the price level of the current shop */ void shop_set_price_level(PRICE_LEVEL price_level) { Shop.price_level = price_level; } /* * the manufacture screen */ void manufacture_screen(CHARACTER *character) { Shop.object_list = object_list_drugs(NULL); Shop.price_level = 50; shop(character); } /* * shop exchange: customer receives object, shop receives money */ static void shop_exchange(CHARACTER *customer, OBJECT *object) { MONEY price; price = shop_object_price(object); if (!customer_can_acquire(customer, object, price)) { return; } message(SID_DIALOGUE, "%s received", object_static_data(object)->name ); inventory_money_reduce(customer, price); inventory_add(customer, object_clone(object)); } /* * creates the shop menu */ static void shop_menu_create(void) { LIST *menu_items; LIST_NODE *node; menu_items = list_new(); for (node = Shop.object_list->head; node != NULL; node = node->next) { OBJECT *object; char *entry; object = (OBJECT *)node->data; entry = checked_malloc(STRING_BUFFER_SIZE); sprintf(entry, "%s (%dc) ", object_static_data(object)->name, shop_object_price(object) ); list_add(menu_items, entry); } Shop.menu = menu_create(menu_items); } /* * returns the price of an object */ static MONEY shop_object_price(const OBJECT *object) { MONEY value, price; value = object_static_data(object)->value; if (value == VALUE_NIL) return value; price = percent(Shop.price_level, value); return price; } /* * returns true if the customer can acquire the passed object */ static bool customer_can_acquire(const CHARACTER *customer, const OBJECT *object, MONEY price ) { if (price > inventory_money(customer)) { message(SID_DIALOGUE, "Not enough credits"); return false; } if (inventory_is_full(customer)) { message(SID_DIALOGUE, "Inventory is full"); return false; } if (object_static_data(object)->weight + inventory_weight(customer) > ENCUMBRANCE_MAX(customer)) { message(SID_DIALOGUE, "You cannot carry any more"); return false; } return true; }