/*
 * Support for selling food and drinks
 *
 * Made by Nick
 *
 * Sierpien 96: Generalne spolszczenie /Alvin
 */

#pragma strict_types

#include <stdproperties.h>
#include <macros.h>
#include <money.h>
#include <language.h>

static	mixed	drinks;	/* Array holding all drink data */
static	mixed	dr_id;	/* Array holding all names the drinks can be identified
			 * with when bought */
static	mixed	food;
static	mixed	fo_id;

int order(string str);

/*
 * Function name: init_pub
 * Description:   This function adds the buy and order command to this_player()
 *		  call it from your init()
 */
void
init_pub()
{
    add_action(order, "kup");
    add_action(order, "zamow");
}

/*
 * Function name: fix_drink
 * Description:   Set up a drink and return the object
 * Arguments:	  data - The drink data from drinks arrayen
 * Returns:	  The configured drink object
 */
object
fix_drink(mixed data)
{
    int i;
    object ob;

    seteuid(getuid(this_object()));
    ob = clone_object("/std/drink");
    ob->ustaw_nazwe(data[0], data[1], data[7]);
    if (sizeof(data[2]) == 2)
    { 
        i = sizeof(data[2][0]);
        while (--i >= 0)
            ob->dodaj_przym(data[2][0], data[2][1]); 
    }

    ob->set_soft_amount(data[3]);
    ob->set_alco_amount(data[4]);
    ob->set_long(data[6]);
//    ob->set_rodzaj(data[7]);
    ob->ustaw_shorty(data[8], data[9], data[7]);
    ob->add_prop(HEAP_I_UNIT_VOLUME, data[3] + data[4]);
    ob->add_prop(HEAP_I_UNIT_WEIGHT, data[3] + data[4]);
    ob->add_prop(HEAP_I_UNIT_VALUE, data[5]);

    return ob;
}

/*
 * Function name: fix_food
 * Description:   Configure the food object and return it
 * Arguments:	  data - The data for the food
 * Returns:	  The food object
 */
object
fix_food(mixed data)
{
    int i;
    object ob;

    seteuid(getuid(this_object()));
    ob = clone_object("/std/food");
    ob->ustaw_nazwe(data[0], data[1], data[6]);
    if (sizeof(data[2]) == 2)
    { 
        i = sizeof(data[2][0]);
        while (--i >= 0)
            ob->dodaj_przym(data[2][0], data[2][1]); 
    }
    ob->set_amount(data[3]);
//    ob->set_rodzaj(data[6]);
    ob->ustaw_shorty(data[7], data[8], data[6]);
    ob->set_long(data[5]);
    ob->add_prop(HEAP_I_UNIT_VOLUME, data[3]);
    ob->add_prop(HEAP_I_UNIT_WEIGHT, data[3]);
    ob->add_prop(HEAP_I_UNIT_VALUE, data[4]);

    return ob;
}

/*
 * Function name: pub_hook_cant_pay
 * Description:   A hook to redefine if you want own message when player can't 
 *		  pay the price.
 * Arguments:     price - The price he should had payed
 */
void
pub_hook_cant_pay(int price)
{
    write("Nie masz wystarczajacej ilosci pieniedzy, zeby zaplacic.\n");
}

/*
 * Function name: pub_hook_cant_carry
 * Description:   A hook to redefine if you want own message when player can't
 *		  carry what he ordered.
 * Arguments:     ob - The object he couldn't carry
 */
void
pub_hook_cant_carry(object ob)
{
    write(capitalize(ob->short()) + " is too heavy for you. You drop it " +
	"to the ground.\n");
    say(capitalize(ob->short()) + " is too heavy for " + QTNAME(this_player()) +
	" and falls to the ground.\n");
}

/*
 * Function name: pub_hook_player_buys
 * Description:   A hook to redefine if you want own message when player
 *		  gets his/hers order.
 * Arguments:     ob - The object player ordered
 *		  price - The price the player payed for the object
 */
void
pub_hook_player_buys(object ob, int price)
{
    write("Zaplacil" + this_player()->koncowka("es", "as") +
        " " + MONEY_TEXT( ({ price, 0, 0, 0 }), PL_BIE) + " za " + 
        ob->short(PL_BIE) +".\n");
    say(QCIMIE(this_player(), PL_MIA) + " zamawia " + 
        ob->short(PL_BIE) + ".\n");
}

/*
 * Function name: order
 * Description:   The player has ordered something, let's see if we can satisfy
 *		  him.
 * Arguments:	  str - The order from the player
 * Returns:	  1 or 0
 */
int
order(string str)
{
    string *words;
    int num, tmp, i, price;
    object ob;

    if (!str)
    {
	notify_fail(capitalize(query_verb()) + " co?\n");
	return 0;
    }

    words = explode(str, " ");
    num = 1;
    if (sizeof(words) > 1)
    {
	tmp = LANG_NUMS(words[0]);
	if (!tmp)
	    sscanf(words[0], "%d", tmp);
	if (tmp > 0)
	{
	    num = tmp;
	    str = implode(words[1 .. sizeof(words)], " ");
	}
    }

    for (i = 0; i < sizeof(dr_id); i++)
	if (member_array(str, dr_id[i]) >= 0)
	{
	    ob = fix_drink(drinks[i]);
	    price = num * drinks[i][5];
	    ob->set_heap_size(num);
	    break;
	}

    if (!ob)
	for (i = 0; i < sizeof(fo_id); i++)
	    if (member_array(str, fo_id[i]) >= 0)
	    {
		ob = fix_food(food[i]);
		price = num * food[i][4];
		ob->set_heap_size(num);
		break;
	    }

    if (!ob)
    {
	notify_fail("Nie ma na skladzie niczego takiego.\n");
	return 0;
    }

    if (!MONEY_ADD(this_player(), -price))
    {
	pub_hook_cant_pay(price);
	ob->remove_object();
	return 1;
    }

    pub_hook_player_buys(ob, price);

    ob->move(this_player(), 1);
/*
 * I can't get this to work so let the players be loaded down with food if
 * they want to. Anyone willing to fix this bug, please send us a patch.
 * /Nick
 *
    tmp = num - ob->num_heap();
    if (tmp > 0)
    {
	ob->set_heap_size(tmp);
	ob->remove_remove_object_call();
	ob->move(environment(this_player()));
	pub_hook_cant_carry(ob);
    }
 */

    return 1;
}

/*
 * Function name: add_drink
 * Description:   Dodaje jakis napoj do menu
 * Arguments:	  nazwy     - Odmiana nazwy napoju w lp
 * 		  pnazwy    - Odmiana nazwy napoju w lmn
 *		  adj	    - Tablica z parami przymiotnikow
 * 		  soft	    - Ilosc cieczy (bez alk) (integer)
 *		  alco	    - Ilosc alkoholu (integer)
 * 		  cena	    - Cena napoju (koszt nabycia)
 *		  long	    - Dlugi opis napoju
 *                rodzaj    - Rodzaj gramatyczny nazwy napoju
 *		  short	    - Short napoju
 *		  pshort    - Short napoju w liczbie mnogiej
 */
varargs void
add_drink(string *nazwy, string *pnazwy, mixed *adj, int soft, 
          int alco, int cena, string long, int rodzaj, string *short, 
          string *pshort)
{
    if (sizeof(nazwy)%6 != 0) return;
    if (sizeof(pnazwy)%6 != 0) return;
    if (!drinks)
	drinks = ({ });
    if (!dr_id)
	dr_id = ({ });

    dr_id += ({ ({ nazwy[3], pnazwy[3] }) });
    drinks += ({ ({ nazwy, pnazwy, adj, soft, alco, cena, long, rodzaj,
    		    short, pshort }) });
}

/*
 * Function name: query_drinks
 * Description:   Query the drink array
 * Returns:	  The drink array
 */
mixed
query_drinks() { return drinks; }

/*
 * Function name: query_drink_id
 * Description:   Query the drink id:s
 * Returns:	  The array holding all drink id:s
 */
mixed
query_drink_id() { return dr_id; }

/*
 * Function name: remove_drink
 * Description:   Remove a special drink, identified with id
 * Arguments:	  id - A identifying string
 * Returns:	  1 if removed
 */
int
remove_drink(string id)
{
    int i;

    for (i = 0; i < sizeof(dr_id); i++)
	if (member_array(id, dr_id[i]) >= 0)
	{
	    dr_id = exclude_array(dr_id, i, i);
	    drinks = exclude_array(drinks, i, i);
	    return 1;
	}

    return 0;
}

/*
 * Function name: add_food
 * Description:   Add an item of food  to the menu list
 * Arguments:	  nazwy     - Odmiana nazw jedzenia w lp
 *		  pnazwy    - Odmiana nazw jedzenia w lmn
 *		  adj	    - Tablica z parami przymiotnikow
 *                ilosc     - Ilosc (integer)
 *                cena      - Cena zarcia (koszt kupna)
 *		  long      - Dlugi opis
 *		  rodzaj    - Rodzaj gramatyczny nazwy jedzenia
 *                short     - Odmiana krotkiego opisu w lp (opcjonalne)
 *		  pshort    - Odmiana krotkiego opisu w lmn (opcjonalne)
 */
varargs void
add_food(string *nazwy, string *pnazwy, mixed *adj,
         int ilosc, int cena, string long, int rodzaj, string *short,
         string *pshort)
{
    if (sizeof(nazwy)%6 != 0) return;
    if (sizeof(pnazwy)%6 != 0) return;
    
    if (!food)
        food = ({ });
    if (!fo_id)
        fo_id = ({ });

    fo_id += ({ ({ nazwy[3], pnazwy[3] }) });
    food += ({ ({ nazwy, pnazwy, adj, ilosc, cena, long, rodzaj,
        short, pshort }) });
}

/*
 * Function name: query_food
 * Description:   Query the food array
 * Returns:       The food array
 */
mixed
query_food() { return food; }

/*
 * Function name: query_food_id
 * Description:   Query the food id:s
 * Returns:       The array holding all food id:s
 */
mixed
query_food_id() { return fo_id; }

/*
 * Function name: remove_food
 * Description:   Remove a special food, identified with id
 * Arguments:     id - A identifying string
 * Returns:       1 if removed
 */
int
remove_food(string id)
{
    int i;

    for (i = 0; i < sizeof(fo_id); i++)
        if (member_array(id, fo_id[i]) >= 0)
        {
            fo_id = exclude_array(fo_id, i, i);
            food = exclude_array(food, i, i);
            return 1;
        }

    return 0;
}

