HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Building a 'Debt System'

07-25-2008, 04:14 PM#1
Fulla
I'm looking towards making a debt system & need some advice. Technically speaking its works quite similar to assistance systems.

Debt System

Gold = Gold (Just as normal)
Gold is given as a reward for killing enemy units.

Lumber = Debt
Debt is incurred when teamkilling. The teammate's bounty value will need to be paid off, before gold can be accumulated once again.

So example:
  • I teamkill an ally
  • Teammates bounty value is 100 gold
  • I must pay him 100 gold
  • I only have 50 gold, so I pay him 50 but gain a debt (lumber) of 50. (If I can pay it off immediately, no debt is incurred).
  • Whenever I gain gold, instead my debt decreases by that amount & the teammate I owe, gains that amount in gold.

This is pretty simple, but gets a bit complicated if a player teamkills multiple ppl, or multiple players owe each other gold.[/list]So I basically just wanted to ask how this could be done or how you would handle this, more specifically how to keep track of multiple debts?
I have to rush off to work atm, but what I've planned so far (not much yet):
  • A custom function to handle all gold generation for players, which can check if a ppl is in debt before granting gold, e.g.:
    - call GenerateGold(player p, integer amount)
  • Some method of tracking debts, as 12 players, means alot of possiblities. I'm currently thinking of using a string array. When a debt is incurred a stack added to the string array. Whenever the GenerateGold function is used, it will loop through the strings checking if the player is in debt to either grant gold or pay towards the debt
  • How the string array could be used:
    - Player's in debt player number - "XX," +
    - Player's owed player number - "XX," +
    - Amount owed - "XXX"
    So e.g. "01,05,034" = Player 2, owes Player 5, 34 gold

Would this be the most efficient method?
thx for any advice
07-25-2008, 05:20 PM#2
Alexander244
The main alternative is a linked list of structs, which is probably better than strings.
07-25-2008, 05:21 PM#3
Ammorth
Make a struct for each player.
Collapse JASS:
globals
    Debt array PlayerDebts
endglobals

struct Debt
    integer array owe[11]
    integer id
    integer total

    method addDebt takes integer ID, real amount returns nothing
        local Debt d = PlayerDebts[ID] // the debts the other player owes
        if ID = .id then // player can't owe himself money
            return
        endif
        if d.owe[.id] != 0 then // the there other player owes this player money then refund as much as you can with this debt
            if d.owe[.id] < amount // covers it all, so player will now be in debt of remainder
                set amount = amount - d.owe[.id]
                set d.total = d.total - d.owe[.id]
                set d.owe[.id] = 0
                set .owe[ID] = amount
                set .total = .total + amount
            else // other player will still owe this player money
                set d.owe[.id] = d.owe[.id] - amount
                set d.total = d.total - amount
                // do nothing to the other player as all his potential debt was used paying off the other guys debt
            endif
        else // other player doesn't owe any money, so give it all as debt
            set .owe[ID] = .owe[ID] + amount
            set .total = .total + amount
        endif
    endmethod
    
endstruct

You would then need to check when a player gains money and see if he has any debts to pay off. I would make it a last, out style, so he pays off the latest debts first, and then proceeds upwards.

edit: linked list of structs would be better than my idea, now that I think about it. It would help with the last out method.
07-25-2008, 10:45 PM#4
Fulla
Thx for the sample Ammorth.

Sorry but what is/do you mean by a linked list of structs?
07-25-2008, 11:38 PM#5
Here-b-Trollz
Expand JASS:

If you use a linked list, you don't have to compile all the debts to a single player together - rather, you would just add a link to your list with the debt info, and periodically go through your list, solving the links and erasing them, starting of course from the first one. Then, debt is payed off in exactly the order it is incurred. Once you are out of gold, you would loop through the rest of the links and add up the values to find the remaining debt (which you would assumedly want to display.)
07-26-2008, 04:35 AM#6
Ammorth
After posting, I started working on a debt system (planning on doing something like this in a map I'm currently thinking about). I'm almost finished, but I need to clean up, test and write brief instructions on how to use it. I'll post it here and then in the resource section when I'm done.
07-26-2008, 09:22 AM#7
Fulla
thx for the sample Here-b-Trollz

@Ammorth, yea that would be awesome.
07-26-2008, 11:47 PM#8
Ammorth
I haven't forgotten, I've just been really busy. Hopefully have it polished by tomorrow.