int sort_coins(string s1, string s2) { return (coinvalue(s1)<coinvalue(s2)) ; }
int withdraw (string str) { int number, i ; int oldval, newval, newnum ; string type, word, newtype ; object card, coins ; string *types ; if (!str) { notify_fail ("Usage: withdraw <number> <type>\n") ; return 0 ; } if (sscanf(str,"%d %s",number,type)!=2) { notify_fail ("Usage: withdraw <number> <type>\n") ; return 0 ; } if (number<1) { notify_fail ("You can only withdraw positive numbers of coins.\n") ; return 0 ; } card = present("bank card", this_player()) ; if (!card) { notify_fail ("You don't have an account!\n") ; return 0 ; } if ((int)card->query_val(type)>=number) { coins = clone_object(COINS) ; coins->set_number(number) ; coins->set_type(type) ; if (coins->move(this_player())==MOVE_OK) { if (number==1) word = "coin" ; else word="coins" ; write ("The banker hands you "+number+" "+type+" "+word+".\n") ; say (this_player()->query("cap_name")+" withdraws some "+type+".\n") ; card -> add_val (type,-number) ; return 1 ; } notify_fail ("You cannot carry that many more coins.\n") ; return 0 ; } // If the following lines (to the ending comment) are in place, if the player // can't withdraw what he requests, the bank will give him the same amount // of money in some other coinage if it can. For example, in the default // coin system, if he requests 20 gold but doesn't have that, the bank will // give him 2 platinum or 200 silver instead. This enables clever players // to change coinage for free. If you have a coin exchange which charges // a fee, you need to either rip this out, institute a fee on withdrawals, // or allow smart players to get a free ride... types = cointypes() ; i = member_array(type,types) ; if (i==-1) { notify_fail ("You don't have that many "+type+" and the bank cannot convert them.\n") ; return 0 ; } oldval = coinvalue(type) ; for (i=0;i<sizeof(types);i++) { newtype = types[i] ; newval = coinvalue(newtype) ; newnum = number*oldval/newval ; // Check to see if it's exact change. if ((number*oldval)-(newval*newnum)>0) continue ; // Check to see if he's got that many of the new type. if ((int)card->query_val(newtype)>=newnum) { coins = clone_object(COINS) ; coins->set_number(number) ; coins->set_type(type) ; if (coins->move(this_player())==MOVE_OK) { if (number==1) word = "coin" ; else word="coins" ; write ("You don't have that many "+type+" but you do have "+newnum+" "+newtype+".\n") ; write ("The banker withdraws "+newnum+" "+newtype+" from your account and hands you "+number+" "+type+" "+word+".\n") ; say (this_player()->query("cap_name")+" withdraws some "+type+".\n") ; card -> add_val (newtype,-newnum) ; return 1 ; } } } // Ending comment. If we get here, it means he didn't have enough of any // kind of coin to make his requested withdrawal. notify_fail ("You don't have enough coins of any type to withdraw that amount of money.\n") ; return 0 ; }