mana
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
let MiningAI = extend(AIController, {
    // author: xeloboyo
    mining: true,
    targetItem: null,
    ore: null,
    unitS(u) {
        if (this.unit == u) return;
        this.unit = u;
        this.init();
    },
    updateMovement() {
        let unit = this.unit;

        var core = unit.closestCore(); //core is a Building

        if (!(unit.canMine()) || core == null) return;

        if (unit.mineTile != null && !unit.mineTile.within(unit, unit.type.range)) {
            unit.mineTile = null;
        }

        if (this.mining) {
            if (this.timer.get(1, 240) || this.targetItem == null) {
                // let mineItems = unit.team.data().mineItems;
                let mineItems = Seq.with(Items.copper, Items.lead, Items.coal, Items.titanium, Items.thorium);

                this.targetItem = mineItems.min(boolf(i => Vars.indexer.hasOre(i) && unit.canMine(i)), floatf(i => core.items.get(i) - orePriority(i)));


            }

            //core full of the target item, do nothing
            if (this.targetItem != null && core.acceptStack(this.targetItem, 1, unit) == 0) {
                unit.clearItem();
                unit.mineTile = null;
                return;
            }
            //custom player mining ai: todo
            //if inventory is full, drop it off.
            if (unit.stack.amount >= unit.type.itemCapacity || (this.targetItem != null && !unit.acceptsItem(this.targetItem))) {
                this.mining = false;
            } else {
                if (this.targetItem != null) {
                    this.ore = Vars.indexer.findClosestOre(core.x, core.y, this.targetItem);
                }

                if (this.ore != null) {
                    this.moveTo(this.ore, unit.type.range / 5, 20);

                    if (unit.within(this.ore, unit.type.range * 0.5)) {
                        unit.mineTile = this.ore;
                    }

                    if (this.ore.block() != Blocks.air) {
                        this.mining = false;
                    }
                }
            }
        } else {
            unit.mineTile = null;

            if (unit.stack.amount == 0) {
                this.mining = true;
                this.return;
            }

            if (unit.within(core, unit.type.range)) {
                if (core.acceptStack(unit.stack.item, unit.stack.amount, unit) > 0) {
                    Call.transferInventory(Vars.player, core);
                    //Call.transferItemTo(unit, unit.stack.item, unit.stack.amount, unit.x, unit.y, core);
                }

                //unit.clearItem();
                this.mining = true;
            }
            // 
            this.circle(core, unit.type.range / 1.8);
        }
    }
});

function orePriority(item) {
    if (item == Items.copper) {
        return 900;
    }
    if (item == Items.lead) {
        return 850;
    }
    if (item == Items.sand) {
        return 600;
    }
    if (item == Items.coal) {
        return 200;
    }
    if (item == Items.titanium) {
        return 100;
    }
    if (item == Items.thorium) {
        return 0;
    }
    return 0;
}

module.exports = {
    playerMiningAI: MiningAI
}