Example usage for com.badlogic.gdx.utils ObjectIntMap put

List of usage examples for com.badlogic.gdx.utils ObjectIntMap put

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils ObjectIntMap put.

Prototype

public void put(K key, int value) 

Source Link

Usage

From source file:com.vlaaad.dice.game.actions.imp.ChainLightning.java

License:Open Source License

private IActionResult calcResult(Ability ability, Creature caster, Creature target, World world) {
    ObjectSet<Creature> affected = tmpSet;
    Array<Creature> chain = new Array<Creature>();
    chain.add(target);//from   ww  w  .j a  va 2s .c  o  m
    affected.add(target);

    // i == 1 because we already have initial target
    RandomController random = world.getController(RandomController.class);

    for (int i = 1; i < targets; i++) {
        Creature last = chain.peek();
        tmpArray2.clear();
        Array<Creature> neighbours = getNeighbourCreatures(world, last.getX(), last.getY(), tmpArray2);
        Iterator<Creature> it = neighbours.iterator();
        while (it.hasNext()) {
            Creature toCheck = it.next();
            if (affected.contains(toCheck))
                it.remove();
        }
        if (neighbours.size == 0) {
            break;
        } else if (neighbours.size == 1) {
            addToChain(affected, chain, neighbours.first());
        } else {
            Array<Creature> betterTargets = tmpArray3;
            betterTargets.clear();
            for (Creature toCheck : neighbours) {
                if (toCheck.get(Attribute.defenceFor(attackType)) < attackLevel) {
                    betterTargets.add(toCheck);
                }
            }
            if (betterTargets.size > 0) {
                addToChain(affected, chain, random.random(betterTargets));
            } else {
                addToChain(affected, chain, random.random(neighbours));
            }
        }
    }

    ObjectIntMap<Creature> expResults = new ObjectIntMap<Creature>();
    Array<Creature> killed = new Array<Creature>();

    for (Creature creature : chain) {
        if (creature.get(Attribute.defenceFor(attackType)) < attackLevel) { // killed
            if (creature.player != caster.player) {
                expResults.getAndIncrement(caster, 0, ExpHelper.expForKill(caster, creature));
            }
            killed.add(creature);
        } else { // survived
            expResults.put(creature, ExpHelper.expForDefence(caster, creature));
        }
    }

    tmpSet.clear();
    tmpArray2.clear();
    tmpArray3.clear();

    return new ChainLightningResult(caster, ability, chain, killed, expResults);
}

From source file:com.vlaaad.dice.game.actions.imp.Firestorm.java

License:Open Source License

private IActionResult calcResult(Creature creature, Grid2D.Coordinate cell) {
    Vector2 position = tmp.set(cell.x(), cell.y());
    Array<Creature> underAttack = new Array<Creature>();
    Array<Creature> killed = new Array<Creature>();
    ObjectIntMap<Creature> expResults = new ObjectIntMap<Creature>();
    for (int i = cell.x() - MathUtils.ceil(radius); i <= cell.x() + radius; i++) {
        for (int j = cell.y() - MathUtils.ceil(radius); j <= cell.y() + radius; j++) {
            if (position.dst(i, j) <= radius) {
                WorldObject object = creature.world.get(i, j);
                if (object instanceof Creature && ((Creature) object).get(Attribute.canBeSelected)) {
                    underAttack.add((Creature) object);
                }/*  w ww  .ja v a 2  s .c o m*/
            }
        }
    }
    for (Creature c : underAttack) {
        int attackLevel = (c.getX() == cell.x() && c.getY() == cell.y()) ? this.epicenterAttackLevel
                : this.attackLevel;
        int defenceLevel = c.get(Attribute.defenceFor(attackType));
        if (attackLevel > defenceLevel) {
            killed.add(c);
            if (creature.inRelation(Creature.CreatureRelation.enemy, c)) {
                expResults.getAndIncrement(creature, 0, ExpHelper.expForKill(creature, c));
            }
        } else {
            if (creature.inRelation(Creature.CreatureRelation.enemy, c)) {
                expResults.put(c, ExpHelper.expForDefence(creature, c));
            } else {
                expResults.put(c, ExpHelper.MIN_EXP);
            }
        }
    }
    return new FirestormResult(creature, owner, underAttack, killed, expResults, cell);
}

From source file:com.vlaaad.dice.game.actions.imp.IceStorm.java

License:Open Source License

private IActionResult calcResult(Creature creature, Grid2D.Coordinate cell) {
    Vector2 position = tmp.set(cell.x(), cell.y());
    ObjectIntMap<Creature> targets = new ObjectIntMap<Creature>();
    for (int i = cell.x() - MathUtils.ceil(radius); i <= cell.x() + radius; i++) {
        for (int j = cell.y() - MathUtils.ceil(radius); j <= cell.y() + radius; j++) {
            if (position.dst(i, j) <= radius) {
                WorldObject object = creature.world.get(i, j);
                if (object instanceof Creature && ((Creature) object).get(Attribute.canBeSelected)
                        && !((Creature) object).get(Attribute.frozen)) {
                    targets.put((Creature) object, i == cell.x() && j == cell.y() ? epicenterTurns : turns);
                }/*from w ww .  j  a v a  2s  .co m*/
            }
        }
    }

    return new IceStormResult(owner, creature, cell, targets);
}

From source file:com.vlaaad.dice.game.user.Die.java

License:Open Source License

@SuppressWarnings("unchecked")
public static Die fromMap(Map map) {
    if (map.get("exp") == null) {
        map.put("exp", 1000000);
    }/*from  w  w  w .  j a  v a  2  s.  c om*/
    String professionName = MapHelper.get(map, "profession", "warrior");
    ProfessionDescription profession = Config.professions.get(professionName);
    int level = MapHelper.get(map, "level", Numbers.MINUS_ONE).intValue();
    if (level != -1) {
        map.put("exp", profession.getExpForLevel(level));
    }

    Array<Ability> abilities = new Array<Ability>();
    Iterable<String> abilitiesRaw = MapHelper.get(map, "abilities");
    if (abilitiesRaw != null) {
        for (String abilityName : abilitiesRaw) {
            if (abilityName == null) {
                abilities.add(null);
            } else {
                abilities.add(Config.abilities.get(abilityName));
            }
        }
    }
    ObjectIntMap<Ability> inventory = new ObjectIntMap<Ability>();
    Object o = map.get("inventory");
    if (o instanceof Map) {
        Map<String, Integer> inventoryRaw = MapHelper.get(map, "inventory");
        if (inventoryRaw != null) {
            for (String abilityName : inventoryRaw.keySet()) {
                inventory.put(Config.abilities.get(abilityName), inventoryRaw.get(abilityName));
            }
        }
    }
    return new Die(profession, MapHelper.get(map, "name", "Null"),
            MapHelper.get(map, "exp", Numbers.ZERO).intValue(), abilities, inventory,
            MapHelper.get(map, "renames", Numbers.THREE).intValue());
}

From source file:com.vlaaad.dice.game.world.behaviours.Behaviour.java

License:Open Source License

@SuppressWarnings("unchecked")
public <R, P> IFuture<R> request(BehaviourRequest<R, P> request, P params) {
    Array<RequestProcessor> list = processors.get(request);
    if (list == null)
        throw new IllegalStateException("Can't process request " + request + ": nothing registered");

    final ObjectIntMap<RequestProcessor> results = tmpMap;
    Array<RequestProcessor> fitting = tmpList;
    for (RequestProcessor processor : list) {
        int result = processor.preProcess(params);
        if (result >= 0) {
            results.put(processor, result);
            fitting.add(processor);/*from  www. jav a 2s.c o  m*/
        }
    }
    if (fitting.size == 0)
        throw new IllegalStateException(
                "Can't process request " + request + ", " + params + ": all processors failed: " + list);

    fitting.sort(REQUEST_COMPARATOR);
    IFuture<R> r = null;
    for (RequestProcessor p : fitting) {
        r = p.process(params);
        if (r != null)
            break;
    }
    if (r == null)
        throw new IllegalStateException(
                "no processors returned result: " + request + ", " + params + ", " + list);

    tmpList.clear();
    tmpMap.clear();
    return r;
}

From source file:com.vlaaad.dice.game.world.controllers.PveLoadLevelController.java

License:Open Source License

@Override
protected void start() {
    LevelDescription description = world.level;
    for (Map.Entry<Grid2D.Coordinate, String> entry : description.getElements(LevelElementType.tile)) {
        world.dispatcher.dispatch(LOAD_TILE,
                new TileInfo(entry.getKey().x(), entry.getKey().y(), entry.getValue()));
    }//from w w w  . j  av  a2 s .c  o m
    ObjectMap<Grid2D.Coordinate, Creature> creatures = new ObjectMap<Grid2D.Coordinate, Creature>();
    Array<Creature> dropCreatures = new Array<Creature>();
    ObjectMap<Creature, ObjectIntMap<Item>> drop = new ObjectMap<Creature, ObjectIntMap<Item>>();
    Player antagonist = world.players.get(PlayerHelper.antagonist);
    for (Map.Entry<Grid2D.Coordinate, Die> entry : description.getElements(LevelElementType.enemy)) {
        Creature creature = antagonist.addCreature(entry.getValue());
        creatures.put(entry.getKey(), creature);
    }
    for (Creature creature : creatures.values()) {
        dropCreatures.add(creature);
    }
    final ObjectIntMap<Creature> weights = new ObjectIntMap<Creature>();
    int total = 0;
    for (Creature creature : dropCreatures) {
        int cost = ExpHelper.getTotalCost(creature);
        weights.put(creature, cost);
        total += cost;
    }
    dropCreatures.sort(new Comparator<Creature>() {
        @Override
        public int compare(Creature o1, Creature o2) {
            return weights.get(o2, 0) - weights.get(o1, 0);
        }
    });
    if (dropCreatures.size > 1) {
        dropCreatures.truncate(Math.max(1, dropCreatures.size * 3 / 5));
    }

    for (Creature creature : dropCreatures) {
        drop.put(creature, new ObjectIntMap<Item>());
    }
    //        Logger.debug("drop: " + world.level.drop);
    ObjectIntMap<Item> droppedItems = world.level.drop.roll();
    //        Logger.debug("rolled: " + droppedItems);
    for (Item item : droppedItems.keys()) {
        int count = droppedItems.get(item, 1);
        float factor = count / (float) total;
        int distributedCount = 0;
        for (Creature creature : dropCreatures) {
            int creatureItemCount = (int) (weights.get(creature, 0) * factor);
            drop.get(creature).put(item, creatureItemCount);
            distributedCount += creatureItemCount;
        }
        if (distributedCount > count)
            throw new IllegalStateException("OMG! distributed " + item + " more than should! drop: " + drop
                    + ", to distribute: " + droppedItems);
        if (distributedCount < count) {
            while (distributedCount < count) {
                Creature random = dropCreatures.random();
                drop.get(random).getAndIncrement(item, 0, 1);
                //                    Logger.debug("added lost 1 " + item + " to " + random + "!");
                distributedCount++;
            }
        }
    }
    for (Creature creature : drop.keys()) {
        creature.setDrop(drop.get(creature));
        //            Logger.debug("set drop: " + creature + " => " + creature.drop);
    }
    for (Grid2D.Coordinate coordinate : creatures.keys()) {
        world.add(coordinate.x(), coordinate.y(), creatures.get(coordinate));
    }
    for (Map.Entry<Grid2D.Coordinate, Obstacle> entry : description.getElements(LevelElementType.obstacle)) {
        world.add(entry.getKey().x(), entry.getKey().y(), entry.getValue());
    }
    for (Map.Entry<Grid2D.Coordinate, StepDetector> entry : description
            .getElements(LevelElementType.stepDetector)) {
        world.add(entry.getKey().x(), entry.getKey().y(), entry.getValue());
    }
    for (Map.Entry<Grid2D.Coordinate, Fraction> entry : description.getElements(LevelElementType.spawn)) {
        world.dispatcher.dispatch(ADD_SPAWN_POINT,
                new SpawnPoint(entry.getKey().x(), entry.getKey().y(), entry.getValue()));
    }
    world.dispatcher.dispatch(LEVEL_LOADED, null);
}

From source file:com.vlaaad.dice.pvp.ClientMessageListener.java

License:Open Source License

private void awaitSpawns(final PvpPlayState state) {
    Logger.debug("client: await of other players spawns");
    register(Spawned.class, new IMessageProcessor<Spawned>() {
        int toReceiveCount = session.getOthers().size;

        @Override/*www.  j  a  v a 2s.c o  m*/
        public void receive(IParticipant from, Spawned message) {
            toReceiveCount--;
            Player player = state.world.players.get(message.fraction);
            ObjectIntMap<Ability> potions = new ObjectIntMap<Ability>();
            for (ObjectMap.Entry<String, Integer> p : message.potions.entries()) {
                potions.put(Config.abilities.getById(p.key), p.value);
            }
            player.setPotions(potions);
            for (PlacedCreature c : message.dice) {
                Array<Ability> abilities = new Array<Ability>(c.abilities.size);
                for (String n : c.abilities)
                    abilities.add(Config.abilities.getById(n));
                Die die = new Die(Config.professions.getById(c.profession), c.name, c.exp, abilities,
                        new ObjectIntMap<Ability>());
                Creature creature = new Creature(die, player, c.id);
                creature.setPosition(c.x, c.y);
                player.addCreature(creature);

                ObjectIntMap<Item> drop = new ObjectIntMap<Item>();
                for (ObjectMap.Entry<String, Integer> e : c.drop.entries())
                    drop.put(Config.items.getById(e.key), e.value);
                creature.setDrop(drop);

                state.world.add(creature.getX(), creature.getY(), creature);
            }
            Logger.debug("client: received player with dice: " + player.creatures);
            if (toReceiveCount <= 0) {
                Logger.debug("client: all spawned received");
                unregister(Spawned.class);
                awaitStart(state);
            }
        }
    });
}

From source file:com.vlaaad.dice.pvp.ServerMessageListener.java

License:Open Source License

private void applyDrop(ObjectIntMap<Item> droppedItems, Array<PlacedCreature> dice) {
    for (PlacedCreature c : dice) {
        c.drop.clear();//from  w w w  .j  a va2s. c om
    }
    final ObjectIntMap<PlacedCreature> weights = new ObjectIntMap<PlacedCreature>();
    int total = 0;
    for (PlacedCreature creature : dice) {
        int cost = creature.exp;
        weights.put(creature, cost);
        total += cost;
    }
    dice.sort(new Comparator<PlacedCreature>() {
        @Override
        public int compare(PlacedCreature o1, PlacedCreature o2) {
            return weights.get(o2, 0) - weights.get(o1, 0);
        }
    });

    ObjectMap<PlacedCreature, ObjectIntMap<Item>> drop = new ObjectMap<PlacedCreature, ObjectIntMap<Item>>();
    for (PlacedCreature creature : dice) {
        drop.put(creature, new ObjectIntMap<Item>());
    }

    for (Item item : droppedItems.keys()) {
        int count = droppedItems.get(item, 1);
        float factor = count / (float) total;
        int distributedCount = 0;
        for (PlacedCreature creature : dice) {
            int creatureItemCount = (int) (weights.get(creature, 0) * factor);
            drop.get(creature).put(item, creatureItemCount);
            distributedCount += creatureItemCount;
        }
        if (distributedCount > count)
            throw new IllegalStateException("OMG! distributed " + item + " more than should! drop: " + drop
                    + ", to distribute: " + droppedItems);
        if (distributedCount < count) {
            while (distributedCount < count) {
                PlacedCreature random = dice.random();
                drop.get(random).getAndIncrement(item, 0, 1);
                distributedCount++;
            }
        }
    }
    for (PlacedCreature creature : drop.keys()) {
        for (ObjectIntMap.Entry<Item> e : drop.get(creature).entries()) {
            creature.drop.put(e.key.id, e.value);
        }
    }
}