Currency and Banking for NakedMud

I’ve been coding in NakedMud lately for Athens. A very simplistic approach to money handling and banking. This code will create values in charAuxData for gold and for bank accounts and allows for interaction between players with money and saving money into banks. It also gives a few simple instructions on a couple minor changes that need to be made to the NakedMud src files.

Feel free to use the code as you like. If you distribute it on a website, I would appreciate acknowledgement.

-Hera of Athens (athensmud.com)

in cmd_manip.c:

add:

#include “money/money.h”

in COMMAND(cmd_drop) {

under bool multiple = FALSE;

add:

if(!strcasecmp(arg, “drachma”) || !strcasecmp(arg, “drachmas”))

{

int char_cash;

int cash_change;

char_cash = (charGetMoney(ch));

cash_change = (char_cash -1);

OBJ_DATA *obj = newObj();

objSetName (obj, “a gold coin”);

objSetKeywords (obj, “coin”);

objSetRdesc (obj, “A gold coin is laying here.”);

objSetMultiName (obj, “%d gold coins”);

objSetMultiRdesc (obj, “%d gold coins lie here in a pile.”);

objSetDesc (obj, ” This simple, gold slug bears no mark or seal.”);

send_to_char(ch, “You drop a gold coin.\r\n”);

obj_to_game(obj);

obj_to_room(obj, charGetRoom(ch));

charSetMoney(ch, cash_change);

}else{

Make sure to add an extra } at the end of cmd_drop

in handler.c

change void do_get to:

void do_get(CHAR_DATA *ch, OBJ_DATA *obj, OBJ_DATA *container) {

if(bitIsOneSet(objGetBits(obj), “notake”))

send_to_char(ch, “You cannot take %s.\r\n”, objGetName(obj));

else if(container) {

send_to_char(ch, “You get %s from %s.\r\n”,

objGetName(obj), objGetName(container));

message(ch, NULL, obj, container, TRUE, TO_ROOM,

“$n gets $o from $O.”);

obj_from_obj(obj);

obj_to_char(obj, ch);

}else if(objGetKeywords(obj), “coin”){

do_get_money(ch);

extract_obj(obj);

}else {

send_to_char(ch, “You get %s.\r\n”, objGetName(obj));

message(ch, NULL, obj, NULL, TRUE, TO_ROOM,

“$n gets $o.”);

obj_from_room(obj);

obj_to_char(obj, ch);

hookRun(“get”, ch, obj, NULL);

}

}

In a DIR named: banks

banks.c

// ==> This file will deal with banking…

#include “../mud.h”

#include “../character.h”

#include “../account.h”

#include “../storage.h”

#include “../auxiliary.h”

#include “../handler.h”

#include “../socket.h”

#include “../utils.h”

#include “../save.h”

#include “../set_val/set_val.h”

#include “../bitvector.h”

#include “../room.h”

#include “../money/money.h”

#include “../world.h”

#include “banks.h”

/************************************************************************

* auxiliary data for bank *

************************************************************************/

typedef struct bank_data {

int bank;

} BANK_DATA;

BANK_DATA *newBankData() {

BANK_DATA *data = malloc(sizeof(BANK_DATA));

data->bank = 0;

return data;

}

void deleteBankData(BANK_DATA *data) {

free(data);

}

void bankDataCopyTo(BANK_DATA *from, BANK_DATA *to) {

to->bank = from->bank;

}

BANK_DATA *bankDataCopy(BANK_DATA *data) {

BANK_DATA *new_data = newBankData();

bankDataCopyTo(data, new_data);

return new_data;

}

STORAGE_SET *bankDataStore(BANK_DATA *data) {

STORAGE_SET *set = new_storage_set();

store_int(set, “bank”, data->bank);

return set;

}

BANK_DATA *bankDataRead(STORAGE_SET *set) {

BANK_DATA *data = newBankData();

data->bank = read_int(set, “bank”);

return data;

}

int charGetBank(CHAR_DATA *ch)

{

BANK_DATA *data = charGetAuxiliaryData(ch, “bank_aux_data”);

return data->bank;

}

void charSetBank(CHAR_DATA *ch, int new_bank)

{

BANK_DATA *bankdata = charGetAuxiliaryData(ch, “bank_aux_data”);

bankdata->bank = new_bank;

return;

}

void getbalance(CHAR_DATA *ch, ROOM_DATA *room)

{

if(bitIsOneSet(roomGetBits(room), “bank”))

{

if(charGetBank == 0 || charGetBank == NULL){

send_to_char(ch, “{gA banker informs you that you do not currently have an account.\r\n”);

}else{

send_to_char(ch, “{gA banker informs you of your balance:\r\n”);

send_to_char(ch, “You have saved {c%d {ggold coin%s.\r\n”, charGetBank(ch), (charGetBank(ch) == 1 ? “” : “s”));}

}else{

send_to_char(ch, “{gYou must stand within a bank to find out about your balance.\r\n”);

}

}

COMMAND(cmd_balance)

{

getbalance(ch, charGetRoom(ch));

}

COMMAND(cmd_withdraw)

{

int bank_withdrawl;

int newbank;

int newmoney;

ROOM_DATA *room = charGetRoom(ch);

if(!parse_args(ch, TRUE, cmd, arg, “int”, &bank_withdrawl))

return;

int bank_balance;

bank_balance = (charGetBank(ch));

int char_money;

char_money = (charGetMoney(ch));

if(!bitIsOneSet(roomGetBits(room), “bank”))

{

send_to_char(ch, “You must stand within a bank to make a withdrawl.”);

return;

}

if(bank_balance == 0)

{

send_to_char(ch, “{gA banker informs you that you do not currently have an account.\r\n”);

return;

}else{

if(bank_balance < bank_withdrawl) { send_to_char(ch, “{gA banker states that you do not have that much in your account.\r\n”); }else{ if(bank_balance == bank_withdrawl || bank_balance > bank_withdrawl)

{

newbank = (bank_balance – bank_withdrawl);

charSetBank(ch, newbank);

newmoney = (char_money + bank_withdrawl);

charSetMoney(ch, newmoney);

send_to_char(ch, “{gA banker hands you %d gold coin%s.\r\n”, bank_withdrawl, (bank_withdrawl == 1 ? “” : “s”));

}

}

}

}

COMMAND(cmd_deposit)

{

int bank_deposits;

int newbalance;

int newcash;

ROOM_DATA *room = charGetRoom(ch);

if(!parse_args(ch, TRUE, cmd, arg, “int”, &bank_deposits))

return;

int bank_balances;

bank_balances = (charGetBank(ch));

int char_cash;

if(!bitIsOneSet(roomGetBits(room), “bank”))

{

send_to_char(ch, “You must stand within a bank to make a deposit.”);

return;

}

char_cash = (charGetMoney(ch));

if(char_cash == 0)

{

send_to_char(ch, “{gYou must have cash to make a deposit.\r\n”);

}else{

if(char_cash < bank_deposits) { send_to_char(ch, “{gYou don’t have enough cash on hand to make that deposit.\r\n”); return; }else{ if(char_cash == bank_deposits || char_cash > bank_deposits)

{

newbalance = (bank_balances + bank_deposits);

charSetBank(ch, newbalance);

newcash = (char_cash – bank_deposits);

charSetMoney(ch, newcash);

send_to_char(ch, “{gYou hand %d gold coin%s to a banker for safe keeping.\r\n”, bank_deposits, (bank_deposits == 1 ? “” : “s”));

}

}

}

}

/************************************************************************

Below here are all of the init_ functions for this file.

*************************************************************************/

void init_bank(void)

{

add_set(“bank”, SET_CHAR, SET_TYPE_INT, charSetBank, NULL);

auxiliariesInstall(“bank_aux_data”, newAuxiliaryFuncs(AUXILIARY_TYPE_CHAR, newBankData,

deleteBankData, bankDataCopyTo, bankDataCopy, bankDataStore, bankDataRead));

add_cmd(“balance”, “bal”, cmd_balance, POS_UNCONCIOUS, POS_FLYING, “player”, TRUE, FALSE);

bitvectorAddBit(“room_bits”, “bank”);

add_cmd(“withdraw”, “with”, cmd_withdraw, POS_UNCONCIOUS, POS_FLYING, “player”, TRUE, FALSE);

add_cmd(“deposit”, “depo”, cmd_deposit, POS_UNCONCIOUS, POS_FLYING, “player”, TRUE, FALSE);

}

banks.h

#ifndef BANK_H

#define BANK_H

// this function should be called when the MUD first boots up.

// calling it will initialize the module for use.

int charGetBank(CHAR_DATA *ch);

void charSetBank(CHAR_DATA *ch, int new_bank);

void init_bank(void);

#endif //Bank_H

module.mk

# include all of the source files contained in this module

SRC += banks/banks.c

In a DIR named: money

money.c

// ==> This file will deal with money…

#include “../mud.h”

#include “../character.h”

#include “../account.h”

#include “../storage.h”

#include “../auxiliary.h”

#include “../handler.h”

#include “../socket.h”

#include “../utils.h”

#include “../save.h”

#include “../object.h”

#include “../set_val/set_val.h”

#include “money.h”

/************************************************************************

* auxiliary data for money *

************************************************************************/

typedef struct money_data {

int money;

} MONEY_DATA;

MONEY_DATA *newMoneyData() {

MONEY_DATA *data = malloc(sizeof(MONEY_DATA));

data->money = 0;

return data;

}

void deleteMoneyData(MONEY_DATA *data) {

free(data);

}

void moneyDataCopyTo(MONEY_DATA *from, MONEY_DATA *to) {

to->money = from->money;

}

MONEY_DATA *moneyDataCopy(MONEY_DATA *data) {

MONEY_DATA *new_data = newMoneyData();

moneyDataCopyTo(data, new_data);

return new_data;

}

STORAGE_SET *moneyDataStore(MONEY_DATA *data) {

STORAGE_SET *set = new_storage_set();

store_int(set, “money”, data->money);

return set;

}

MONEY_DATA *moneyDataRead(STORAGE_SET *set) {

MONEY_DATA *data = newMoneyData();

data->money = read_int(set, “money”);

return data;

}

int charGetMoney(CHAR_DATA *ch)

{

MONEY_DATA *data = charGetAuxiliaryData(ch, “money_aux_data”);

return data->money;

}

void charSetMoney(CHAR_DATA *ch, int new_money)

{

MONEY_DATA *moneydata = charGetAuxiliaryData(ch, “money_aux_data”);

moneydata->money = new_money;

return;

}

void do_get_money(CHAR_DATA *ch)

{

int char_cash;

int cash_change;

char_cash = (charGetMoney(ch));

cash_change = (char_cash +1);

send_to_char(ch, “You get a gold coin.\r\n”);

charSetMoney(ch, cash_change);

}

COMMAND(cmd_pay)

{

int payment;

int char_money;

int char_cash;

int recv_cash;

CHAR_DATA *recv = NULL; // the person we’re paying

char_money = charGetMoney(ch);

if(!parse_args(ch, TRUE, cmd, arg, “ch.room.noself int”, &recv, &payment))

return;

if(charGetMoney == 0 || charGetMoney == NULL){

send_to_char(ch, “You have no money to pay %s with.\r\n”, charGetName(recv));

}else if(char_money < payment){

send_to_char(ch, “You only have %d gold coin%s.\r\n”, charGetMoney(ch), (charGetMoney(ch) == 1 ? “” : “s”));

}else if(payment < 0){

send_to_char(ch, “You can’t pay a negative amount.\r\n”);

}else{

char_cash = (charGetMoney(ch) – payment);

recv_cash = (charGetMoney(recv) + payment);

charSetMoney(ch, char_cash);

charSetMoney(recv, recv_cash);

send_to_char(ch, “You pay %s %d gold coin%s.\r\n”, charGetName(recv), payment, (payment == 1 ? “” : “s”));

send_to_char(recv, “%s pays you %d gold coin%s.\r\n”, charGetName(ch), payment, (payment == 1 ? “” : “s”));

}

}

/************************************************************************

Below here are all of the init_ functions for this file.

*************************************************************************/

void init_money(void)

{

add_set(“money”, SET_CHAR, SET_TYPE_INT, charSetMoney, NULL);

auxiliariesInstall(“money_aux_data”, newAuxiliaryFuncs(AUXILIARY_TYPE_CHAR, newMoneyData,

deleteMoneyData, moneyDataCopyTo, moneyDataCopy, moneyDataStore, moneyDataRead));

add_cmd(“pay”, “pay”, cmd_pay, POS_UNCONCIOUS, POS_FLYING, “player”, TRUE, FALSE);

}

money.h

#ifndef MONEY_H

#define MONEY_H

// this function should be called when the MUD first boots up.

// calling it will initialize the module for use.

int charGetMoney(CHAR_DATA *ch);

void charSetMoney(CHAR_DATA *ch, int new_experience);

void init_money(void);

void do_get_money(CHAR_DATA *ch);

#endif //MONEY_H

module.mk

# include all of the source files contained in this module

SRC += money/money.c