inherit "/lib/shop";
inherit "/d/Imperium/std/monster"; 

#include <ss_types.h>
#include <money.h>
#include <cmdparse.h>
#include <language.h>
#include <composite.h>
#include "szturmix.h"

#define MAGAZYN  LOK + "skorymagazyn"

void
create_monster()
{
    ustaw_nazwe(({"dirk", "dirka", "dirkowi", "dirka", "dirkiem", "dirku"}),
        allocate(6),PL_MESKI_OS);
    dodaj_przym("niesympatyczny","niesympatyczni");
    dodaj_przym("wielkooki", "wielkoocy");    
    dodaj_nazwy(({ "handlarz", "handlarza","handlarzowi","handlarza",
               "handlarzem", "handlarzu"}), ({"handlarze", "handlarzy",
               "handlarzom", "handlarzy", "handlarzami", "handlarzach"}),

                    PL_MESKI_OS);
    
    set_long("Handlarz skor.\n");
        
    ustaw_odmiane_rasy(PL_CZLOWIEK);

    set_gender(G_MALE);
    set_living_name("handlarz_skor");
    

 
    add_prop(CONT_I_WEIGHT, 70000);
    add_prop(CONT_I_HEIGHT, 175);

    set_skill(SS_DEFENCE, 15);
    set_skill(SS_UNARM_COMBAT, 15);

    set_stats(({ 30, 20, 60, 10, 10, 5}));


        
    set_cchat_time(6);
    add_cchat("Pomocy!");
    
    set_act_time(12);
    add_act("emote podrzuca monety.");
    add_act("emote mowi: Moglbym w czyms sluzyc moj drogi kolego przybyszu?");

    config_default_trade();
    set_store_room(MAGAZYN);

    dodaj_miedz(10+random(100));
    dodaj_srebro(10+random(15));
}

string
sk_answer(string co)
{
    return "Handlarz mowi: "+co;
}

void
init_living()
{
    init_shop();
    ::init_living();
}

/* ********************************************************************** */
//    HANDEL
/* ********************************************************************** */
int
query_buy_price(object ob)
{
    int seed;

    sscanf(OB_NUM(ob), "%d", seed);
    return 2 * ob->query_skora_value() * (query_money_greed_buy() +
        15 - this_player()->query_skill(SS_TRADING) / 4 +
        random(15, seed)) / 100;
}

int
query_sell_price(object ob)
{
    int seed;

    sscanf(OB_NUM(ob), "%d", seed);
    return ob->query_skora_value() * 100 / (query_money_greed_sell() +
        15 - this_player()->query_skill(SS_TRADING) / 3 + 
        random(15, seed + 1)); /* Use another seed than on buying */
}

int
shop_hook_allow_sell(object ob)
{
    if (ob->query_skora_value())
        return 1;
    else 
    {
        notify_fail(sk_answer("Skupuje tylko skory.\n"));
        return 0;
    }
}

int
shop_hook_buy_no_match(string str)
{
    notify_fail(sk_answer("Nie mam niczego takiego.\n"));
}

void
shop_hook_list_empty_store(string str)
{
    notify_fail(sk_answer("Nie mam w tej chwili zadnych skor na sprzedaz.\n"));
}

int 
shop_hook_list_no_match(string str)
{
    notify_fail(sk_answer("Sprzedaje tylko skory.\n"));
}


object *
sell_it(object *ob, string str, int check) 
{
    int price, i, j, k, *tmp_arr, *null, *value_arr, *null_arr, err;
    object *sold;
    mixed tmp;

    call_other(store_room, "load_me"); /* Make sure store room is loaded */

    value_arr = allocate(sizeof(query_money_types()));
    null_arr = value_arr + ({});
    sold = allocate(sizeof(ob));

    for (i = 0; i < sizeof(ob); i++)
    {
        if (!shop_hook_allow_sell(ob[i]))
            continue;

        if (ob[i]->query_skora_value() == 0)
        {
            shop_hook_sell_no_value(ob[i]);
            continue;
        }

        if (check && (ob[i]->query_worn() ||
                      ob[i]->query_wielded()))
        {
            shop_hook_sell_worn_or_wielded(ob[i]);
            continue;
        }
    
        if (tmp = ob[i]->query_prop(OBJ_M_NO_SELL))
        {
            shop_hook_sell_no_sell(ob[i], tmp);
            continue;
        }

        /* Save price if ob destructed in move */
        price = query_sell_price(ob[i]);

        if (price <= 0)
        {
            shop_hook_sell_no_value(ob[i]);
            continue;
        }

        if (err = ob[i]->move(store_room))
        {
            shop_hook_sell_object_stuck(ob[i], err);
            continue;
        }

        if (price > 0)
        {
            tmp_arr = calc_change(price, null, str);
            for (k = 0; k < sizeof(value_arr); k++)
                value_arr[k] += tmp_arr[k];

            sold[j] = ob[i];
            j++;
            if (j >= 20)
                break;
    /*
     * Only let people sell 20 objects at once and hopefully we wont get
     * those too long evaluation problems.
     */
        }
    }

    sold = sold - ({ 0 });

    if (sizeof(sold) > 0)
    {
        change_money(null_arr + value_arr, this_player());
        shop_hook_sell_get_money(text(value_arr, 0));
    }

    return sold;
}


int
do_value(string str)
{
    object *item, store;
    int *arr, price, i, j, num, no_inv;

    if (!str || str =="")
    {
        notify_fail("Wycen co?");
        return 0;
    }

    num = sizeof(query_money_types());

    item = FIND_STR_IN_OBJECT(str, this_player(), PL_BIE);
    if (!sizeof(item))
        no_inv = 1;

    for (i = 0; i < sizeof(item); i++)
    {
        if (!shop_hook_allow_sell(item[i]) ||
                !item[i]->query_skora_value() ||
                item[i]->query_prop(OBJ_M_NO_SELL)) 
        {
            shop_hook_value_not_interesting(item[i]);
            continue;
        }

        price = query_sell_price(item[i]);
        arr = give(price, this_player(), "", 1);
        shop_hook_value_held(item[i], text(arr[num .. 2 * num - 1], 0));
        j++;
    }

    call_other(store_room, "load_me");
    store = find_object(store_room);
    item = FIND_STR_IN_OBJECT(str, store, PL_BIE);
    if (!sizeof(item) && no_inv)
        return shop_hook_value_no_match(str);

    for (i = 0; i < sizeof(item); i++)
    {
        price = query_buy_price(item[i]);
        arr = split_values(price); /* A price with few coins possible */
        shop_hook_value_store(item[i], text(arr, 0));
        j++;
    }   

    shop_hook_value_asking(str);
    if (j > 0)
        return 1;

    return 0;
}

void
add_introduced(string imie)
{
    set_alarm(2.0,0.0,"return_intro", imie);
}

varargs void 
return_intro(string kto)
{
    command("przedstaw sie " + find_player(kto)->query_real_name(PL_CEL));
}


string
query_presentation()
{
    return"Dirk Garbarz z Middenheim, czlowiek";
}    
