List of usage examples for com.badlogic.gdx.utils ObjectIntMap ObjectIntMap
public ObjectIntMap()
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 w w w . jav a2s. co 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); }// ww w .ja v a 2s . c om } } } 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 ww w. j av a 2 s . com*/ } } } return new IceStormResult(owner, creature, cell, targets); }
From source file:com.vlaaad.dice.game.actions.imp.Summon.java
License:Open Source License
private IActionResult calcResult(Creature creature, Grid2D.Coordinate coordinate) { ProfessionDescription profession = Config.professions.get(professionName); Array<Ability> abilities = new Array<Ability>(); for (String abilityName : abilityNames) { abilities.add(Config.abilities.get(abilityName)); }/*from w ww.j a v a 2s .c o m*/ Die die = new Die(profession, summonedName, 10000000, abilities, new ObjectIntMap<Ability>()); Creature summoned = new Creature(die, creature.player, creature.player.fraction + "@s-" + creature.player.nextSummonedId()); summoned.set(Attribute.canBeResurrected, Boolean.FALSE); return new SummonResult(owner, creature, coordinate, summoned); }
From source file:com.vlaaad.dice.game.config.abilities.Ability.java
License:Open Source License
public static Comparator<? super Ability> countComparator(UserData userData) { final ObjectIntMap<Ability> counts = new ObjectIntMap<Ability>(); for (Die die : userData.dice()) { for (ObjectIntMap.Entry<Ability> e : die.inventory) counts.getAndIncrement(e.key, e.value, e.value); for (Ability ability : die.abilities) if (ability != null) counts.getAndIncrement(ability, 0, 1); }/*from w w w. j a v a 2s . co m*/ return new Comparator<Ability>() { @Override public int compare(Ability o1, Ability o2) { return counts.get(o1, 0) - counts.get(o2, 0); } }; }
From source file:com.vlaaad.dice.game.config.abilities.Ability.java
License:Open Source License
public Ability(Map info) { description = info;//from ww w .j ava 2 s . c om name = MapHelper.get(info, "name"); soundName = MapHelper.get(info, "sound-name", "ability-" + name); String typeName = MapHelper.get(info, "type"); if (typeName == null) throw new IllegalStateException("type not defined in " + name); type = Type.valueOf(typeName); cost = MapHelper.get(info, "cost", Numbers.ZERO).intValue(); sellCost = MapHelper.get(info, "sell", (Number) (MathUtils.ceil(cost / 2))).intValue(); initiative = MapHelper.get(info, "initiative", Numbers.ZERO).intValue(); skill = MapHelper.get(info, "skill", Numbers.ONE).intValue(); String id = MapHelper.get(info, "id"); if (id == null) throw new IllegalStateException("ability " + name + " does not have any id!"); this.id = id; HashMap<String, Object> req = MapHelper.get(info, "requirement"); this.ingredients = new ObjectIntMap<Item>(); Map<String, Integer> ing = MapHelper.get(info, "ingredients"); if (ing != null) { for (String itemName : ing.keySet()) { Item item = Config.items.get(itemName); if (item.type != Item.Type.ingredient && item.type != Item.Type.anyIngredient) throw new IllegalStateException(item + " in " + name + " ability is not an ingredient"); ingredients.put(item, ing.get(itemName)); } } requirement = CreatureRequirementFactory.parse(req); HashMap<String, Object> ac = MapHelper.get(info, "action"); if (ac != null) { if (ac.size() != 1) throw new IllegalStateException(ac + " should contain 1 element"); for (String name : ac.keySet()) { action = CreatureActionFactory.create(name, ac.get(name), this); } } else { action = CreatureAction.doNothing(this); } }
From source file:com.vlaaad.dice.game.config.items.drop.Drop.java
License:Open Source License
public ObjectIntMap<Item> roll() { ObjectIntMap<Item> res = new ObjectIntMap<Item>(); for (Roll roll : rolls) { for (RangeItemCount rangeItemCount : roll.roll()) { res.getAndIncrement(rangeItemCount.item, 0, rangeItemCount.count); }// w w w . j a v a2s. c o m } return res; }
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 ww w .jav a 2 s . c o m 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.user.Die.java
License:Open Source License
public Die() { abilities = new Array<Ability>(); inventory = new ObjectIntMap<Ability>(); }
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 a v a 2 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); }