#include "gold.h"

#include "constants.h"

int rand_gold_pile(RNG *rng) {
        const int denominator = 8;
        const int normal = 5;
        const int dragon = 6;

        int tmp = rng->rand_under(denominator);

        if (tmp < normal)
                return GOLD_NORMAL;
        else if (tmp < dragon)
                return GOLD_DRAGON;
        else
                return GOLD_SMALL;

        return 0;
}

gold get_gold_at(const position &pos, gold_list &glist) {
        gold ret({0, 0}, 0);

        for (size_t i = 0; i < glist.size(); ++i)
                if (!glist[i].is_locked() && glist[i].get_pos() == pos) {
                        ret = glist[i];
                        glist.erase(i + glist.begin());

                        return ret;
                }

        return ret;
}

int rand_gold_drop(RNG *rng) {
        if (rng->coin_flip())
                return GOLD_NORMAL;

        return GOLD_SMALL;
}

gold::gold(const position &pos, const int amount): item{pos}, amount{amount},
        locked{amount == GOLD_DRAGON ? true : false} {}

void gold::print(output *out) const {
        out->print_char(pos, 'G', COLOR_PAIR(COLOR_YELLOW));
}

int gold::get_amount() const {
        return amount;
}

void gold::set_amount(const int new_amount) {
        amount = new_amount;
}

bool gold::is_locked() const {
        return locked;
}

void gold::unlock() {
        locked = false;
}
