summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/ObjectiveSet.java
blob: 61feba6b5293302f5723ae2f1c39c3c61f1ff0a0 (plain)
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
package ch.asynk.rustanddust.engine;

import java.util.HashMap;

public class ObjectiveSet extends HashMap<Tile, Objective>
{
    public interface ObjectiveCb
    {
        public void showObjective(Tile tile, Faction faction);
    }

    private final Board board;
    private final HashMap<Objective, Tile> modified;

    public ObjectiveSet(Board board, int n)
    {
        super(n);
        this.board = board;
        this.modified = new HashMap<Objective, Tile>(10);
    }

    public void add(Tile tile, Faction faction, boolean persistent)
    {
        put(tile, new Objective(faction, persistent));
    }

    public int count(Faction faction)
    {
        int n = 0;
        for (Objective objective : values()) {
            if (objective.is(faction))
                n += 1;
        }
        return n;
    }

    public boolean isObjectiveFor(Tile tile, Pawn pawn)
    {
        Objective objective = get(tile);
        if (objective == null)
            return false;

        if (objective.faction() == pawn.getFaction())
            return false;

        return (objective.persistent());
    }

    public Faction claim(Tile tile, Faction faction)
    {
        Objective objective = get(tile);
        if (objective == null)
            return null;

        if (objective.set(faction))
            modified.put(objective, tile);
        return objective.faction();
    }

    public Faction unclaim(Tile tile)
    {
        Objective objective = get(tile);
        if (objective == null)
            return null;

        if (objective.unset())
            modified.remove(objective);
        return objective.faction();
    }

    public void forget()
    {
        modified.clear();
    }

    public int modifiedCount()
    {
        return modified.size();
    }

    public void revert(ObjectiveCb cb)
    {
        for (Objective objective : modified.keySet()) {
            objective.revert();
            cb.showObjective(modified.get(objective), objective.faction());
        }
        modified.clear();
    }
}