Search in sources :

Example 16 with Entity

use of megamek.common.Entity in project spoon by INRIA.

the class TestBot method initMovement.

/**
 * consider how to put more pre-turn logic here
 */
protected void initMovement() {
    this.my_mechs_moved = 0;
    this.old_moves = null;
    this.enemies_moved = 0;
    double max_modifier = 1.4;
    java.util.Vector entities = game.getEntitiesVector();
    double num_entities = Math.sqrt(entities.size()) / 100;
    Vector friends = new Vector();
    Vector foes = new Vector();
    double friend_sum = 0;
    double foe_sum = 0;
    double max_foe_bv = 0;
    CEntity max_foe = null;
    for (int i = 0; i < entities.size(); i++) {
        Entity entity = (Entity) entities.elementAt(i);
        CEntity centity = centities.get(entity);
        centity.enemy_num = i;
        double old_value = centity.bv * (centity.overall_armor_percent + 1);
        // should get fresh values
        centity.reset();
        double new_value = centity.bv * (centity.overall_armor_percent + 1);
        double percent = 1 + (new_value - old_value) / old_value;
        if (entity.getOwner().equals(getLocalPlayer())) {
            friends.add(centity);
            friend_sum += new_value;
            if (percent < .85) {
                // small retreat
                centity.strategy.attack = .85;
            } else if (percent < .95) {
                centity.strategy.attack = 1;
            } else if (percent <= 1 && centity.strategy.attack < max_modifier) {
                if (percent == 1) {
                    if (centity.strategy.attack < 1) {
                        centity.strategy.attack = Math.min(1.4 * centity.strategy.attack, 1);
                    } else {
                        centity.strategy.attack *= (1.0 + num_entities);
                    }
                } else {
                    centity.strategy.attack *= (1.0 + 2 * num_entities);
                }
            }
        } else if (!entity.getOwner().isEnemyOf(getLocalPlayer())) {
            friend_sum += new_value;
        } else {
            foes.add(centity);
            foe_sum += new_value;
            if (new_value > max_foe_bv) {
                max_foe_bv = new_value;
                max_foe = centity;
            }
            if (this.getEntitiesOwned().size() > 2) {
                if (centity.strategy.target > 2) {
                    centity.strategy.target = 1 + .5 * (centity.strategy.target - 2);
                }
                if (percent < .85 && centity.strategy.target < max_modifier) {
                    centity.strategy.target *= (1.0 + 6 * num_entities);
                } else if (percent < .95 && centity.strategy.target < max_modifier) {
                    centity.strategy.target *= (1.0 + 4 * num_entities);
                } else if (percent <= 1) {
                    if (percent == 1) {
                        centity.strategy.target /= (1.0 + 2 * num_entities);
                    } else {
                        centity.strategy.target /= (1.0 + num_entities);
                    }
                }
                // don't go below one
                if (centity.strategy.target < 1)
                    centity.strategy.target = 1;
            }
        }
    }
    System.out.println("Us " + friend_sum + " Them " + foe_sum);
    // do some more reasoning...
    if (this.unit_values.size() == 0) {
        this.unit_values.add(new Double(friend_sum));
        this.enemy_values.add(new Double(foe_sum));
        return;
    }
    Iterator i = foes.iterator();
    if (friends.size() > 1) {
        if (Strategy.MainTarget == null || null == game.getEntity(Strategy.MainTarget.getEntity().getId())) {
            Strategy.MainTarget = max_foe;
        }
        // TODO : Handle this better.
        if (null == Strategy.MainTarget)
            System.err.println("TestBot#initMovement() - no main target for bot");
        else if (null == Strategy.MainTarget.strategy)
            System.err.println("TestBot#initMovement() - no strategy for main target");
        else {
            Strategy.MainTarget.strategy.target += .2;
            while (i.hasNext()) {
                CEntity centity = (CEntity) i.next();
                // predictability
                if (friend_sum - foe_sum >= .9 * (((Double) this.unit_values.getLast()).doubleValue() - ((Double) this.enemy_values.getLast()).doubleValue())) {
                    if (Compute.randomInt(2) == 1) {
                        centity.strategy.target += .3;
                    }
                // lost that turn, but still in the fight, just get a
                // little more aggressive
                } else if (friend_sum > .9 * foe_sum) {
                    centity.strategy.target += .15;
                // lost that turn and loosing
                } else if (centity.strategy.target < 2) {
                    // go for the gusto
                    centity.strategy.target += .3;
                }
                System.out.println(centity.getEntity().getShortName() + " " + centity.strategy.target);
            }
        }
    }
    double ratio = friend_sum / foe_sum;
    double mod = 1;
    if (ratio < .9) {
        mod = .95;
    } else if (ratio < 1) {
    // no change
    } else {
        // attack
        mod = (1.0 + num_entities);
    }
    i = friends.iterator();
    while (i.hasNext()) {
        CEntity centity = (CEntity) i.next();
        if (!(mod < 1 && centity.strategy.attack < .6) && !(mod > 1 && centity.strategy.attack >= max_modifier))
            centity.strategy.attack *= mod;
    }
    // just to make sure
    System.gc();
}
Also used : Entity(megamek.common.Entity) Iterator(com.sun.java.util.collections.Iterator) Vector(com.sun.java.util.collections.Vector)

Example 17 with Entity

use of megamek.common.Entity in project spoon by INRIA.

the class TestBot method calculateDeployment.

protected void calculateDeployment() {
    // Use the old clumping algorithm until someone puts AI in here
    int entNum = game.getFirstDeployableEntityNum();
    Coords cStart = getStartingCoords();
    Coords cDeploy = getCoordsAround(cStart);
    Coords cCenter = new Coords(game.getBoard().getWidth() / 2, game.getBoard().getHeight() / 2);
    int nDir;
    if (getLocalPlayer().getStartingPos() != 0) {
        // face towards center if you aren't already there
        nDir = cDeploy.direction(cCenter);
    } else {
        // otherwise, face away
        nDir = cCenter.direction(cDeploy);
    }
    Entity ce = game.getEntity(entNum);
    megamek.debug.Assert.assertTrue(!ce.isHexProhibited(game.getBoard().getHex(cDeploy)));
    deploy(entNum, cDeploy, nDir);
}
Also used : Entity(megamek.common.Entity) Coords(megamek.common.Coords)

Example 18 with Entity

use of megamek.common.Entity in project spoon by INRIA.

the class TestBot method firstPass.

/**
 *************************************************************************
 * first pass, filter moves based upon present case
 *************************************************************************
 */
public void firstPass(CEntity self) {
    ArrayList enemies = getEnemyEntities();
    Object[] move_array;
    if (self.getEntity().isSelectableThisTurn() && !self.moved) {
        move_array = self.getAllMoves().values().toArray();
    } else {
        move_array = new Object[] { self.current };
    }
    for (int i = 0; i < move_array.length; i++) {
        MoveOption option = (MoveOption) move_array[i];
        option.setState();
        for (int e = 0; e < enemies.size(); e++) {
            // for each enemy
            Entity en = (Entity) enemies.get(e);
            // ignore loaded units
            if (en.getPosition() == null) {
                continue;
            }
            CEntity enemy = centities.get(en);
            int[] modifiers = option.getModifiers(enemy.getEntity());
            if (modifiers[MoveOption.DEFENCE_MOD] == ToHitData.IMPOSSIBLE && modifiers[MoveOption.ATTACK_MOD] == ToHitData.IMPOSSIBLE) {
                continue;
            }
            int enemy_hit_arc = CEntity.getThreatHitArc(enemy.current.getFinalCoords(), enemy.current.getFinalFacing(), option.getFinalCoords());
            int self_hit_arc = CEntity.getThreatHitArc(option.getFinalCoords(), option.getFinalFacing(), enemy.current.getFinalCoords());
            if (!enemy.getEntity().isImmobile() && modifiers[MoveOption.DEFENCE_MOD] != ToHitData.IMPOSSIBLE) {
                self.engaged = true;
                int mod = modifiers[MoveOption.DEFENCE_MOD];
                double max = option.getMaxModifiedDamage(enemy.current, mod, modifiers[MoveOption.DEFENCE_PC]);
                if (en.isSelectableThisTurn()) {
                    enemy.current.addStep(MovePath.STEP_TURN_RIGHT);
                    max = Math.max(option.getMaxModifiedDamage(enemy.current, mod + 1, modifiers[MoveOption.DEFENCE_PC]), max);
                    enemy.current.removeLastStep();
                    enemy.current.addStep(MovePath.STEP_TURN_LEFT);
                    max = Math.max(option.getMaxModifiedDamage(enemy.current, mod + 1, modifiers[MoveOption.DEFENCE_PC]), max);
                    // return to original facing
                    enemy.current.removeLastStep();
                }
                max = self.getThreatUtility(max, self_hit_arc);
                if (enemy.getEntity().isProne())
                    max *= enemy.base_psr_odds;
                MoveOption.DamageInfo di = option.getDamageInfo(enemy, true);
                di.threat = max;
                di.max_threat = max;
                option.threat += max;
                option.tv.add(max + " Threat " + e + "\n");
            }
            /*
                 * As a first approximation, take the maximum to a single
                 * target
                 */
            if (!option.isPhysical) {
                if (modifiers[MoveOption.ATTACK_MOD] != ToHitData.IMPOSSIBLE) {
                    self.engaged = true;
                    double max = enemy.current.getMaxModifiedDamage(option, modifiers[0], modifiers[MoveOption.ATTACK_PC]);
                    max = enemy.getThreatUtility(max, enemy_hit_arc);
                    MoveOption.DamageInfo di = option.getDamageInfo(enemy, true);
                    di.damage = max;
                    di.min_damage = max;
                    option.tv.add(max + " Damage " + e + "\n");
                    option.damage = Math.max(max, option.damage);
                }
            } else {
                CEntity target = centities.get(option.getPhysicalTargetId());
                try {
                    if (target.getEntity().getId() == enemy.getEntity().getId()) {
                        if (!target.isPhysicalTarget) {
                            ToHitData toHit = null;
                            double self_threat = 0;
                            double damage = 0;
                            if (option.isJumping()) {
                                self.current.setState();
                                toHit = DfaAttackAction.toHit(game, option.getEntity().getId(), target.getEntity(), option);
                                damage = 2 * DfaAttackAction.getDamageFor(option.getEntity());
                                self_threat = option.getCEntity().getThreatUtility(DfaAttackAction.getDamageTakenBy(option.getEntity()), ToHitData.SIDE_REAR) * Compute.oddsAbove(toHit.getValue()) / 100;
                                self_threat += option.getCEntity().getThreatUtility(.1 * self.getEntity().getWeight(), ToHitData.SIDE_REAR);
                                self_threat *= 100 / option.getCEntity().getEntity().getWeight();
                            } else {
                                self.current.setState();
                                toHit = new ChargeAttackAction(option.getEntity(), target.getEntity()).toHit(game, option);
                                damage = ChargeAttackAction.getDamageFor(option.getEntity(), option.getHexesMoved());
                                self_threat = option.getCEntity().getThreatUtility(ChargeAttackAction.getDamageTakenBy(option.getEntity(), target.getEntity()), ToHitData.SIDE_FRONT) * (Compute.oddsAbove(toHit.getValue()) / 100);
                                option.setState();
                            }
                            damage = target.getThreatUtility(damage, toHit.getSideTable()) * Compute.oddsAbove(toHit.getValue()) / 100;
                            // mechs
                            if (!option.isJumping())
                                damage *= Math.sqrt((double) enemy.bv / (double) self.bv);
                            // 12
                            if (toHit.getValue() > 10)
                                damage = 0;
                            // 7 or less is good
                            if (toHit.getValue() < 8)
                                damage *= 1.5;
                            // this is all you are good for
                            if (self.range_damages[CEntity.RANGE_SHORT] < 5)
                                damage *= 2;
                            MoveOption.DamageInfo di = option.getDamageInfo(enemy, true);
                            di.damage = damage;
                            di.min_damage = damage;
                            option.damage = damage;
                            option.movement_threat += self_threat;
                        } else {
                            option.threat += Integer.MAX_VALUE;
                        }
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    option.threat += Integer.MAX_VALUE;
                }
            }
        }
        // -- end while of each enemy
        self.current.setState();
    }
    // -- end while of first pass
    // top balanced
    filterMoves(move_array, self.pass, new MoveOption.WeightedComparator(1, 1), 100);
    // top damage
    filterMoves(move_array, self.pass, new MoveOption.WeightedComparator(.5, 1), 100);
}
Also used : Entity(megamek.common.Entity) ToHitData(megamek.common.ToHitData) ArrayList(com.sun.java.util.collections.ArrayList) ChargeAttackAction(megamek.common.actions.ChargeAttackAction)

Example 19 with Entity

use of megamek.common.Entity in project spoon by INRIA.

the class TestBot method calculateWeaponAttacks.

protected Vector calculateWeaponAttacks(Entity en, Mounted mw, boolean best_only) {
    int from = en.getId();
    int weaponID = en.getEquipmentNum(mw);
    Vector result = new Vector();
    Enumeration ents = game.getValidTargets(en).elements();
    AttackOption a = null;
    AttackOption max = new AttackOption(null, null, 0, null);
    while (ents.hasMoreElements()) {
        Entity e = (Entity) ents.nextElement();
        CEntity enemy = centities.get(e);
        ToHitData th = WeaponAttackAction.toHit(game, from, e, weaponID);
        if (th.getValue() != ToHitData.IMPOSSIBLE && !(th.getValue() >= 13)) {
            double expectedDmg;
            // Are we an Infantry platoon?
            if (en instanceof Infantry) {
                // Get the expected damage, given our current
                // manpower level.
                Infantry inf = (Infantry) en;
                expectedDmg = inf.getDamage(inf.getShootingStrength());
            } else {
                // Get the expected damage of the weapon.
                expectedDmg = CEntity.getExpectedDamage((WeaponType) mw.getType());
            }
            // Infantry in the open suffer double damage.
            if (e instanceof Infantry) {
                IHex e_hex = game.getBoard().getHex(e.getPosition());
                if (!e_hex.containsTerrain(Terrains.WOODS) && !e_hex.containsTerrain(Terrains.BUILDING)) {
                    expectedDmg *= 2;
                }
            }
            a = new AttackOption(enemy, mw, expectedDmg, th);
            if (a.value > max.value) {
                if (best_only) {
                    max = a;
                } else {
                    result.add(0, a);
                }
            } else {
                result.add(a);
            }
        }
    }
    if (best_only && max.target != null) {
        result.add(max);
    }
    if (result.size() > 0) {
        result.add(new AttackOption(null, mw, 0, null));
    }
    return result;
}
Also used : Entity(megamek.common.Entity) Infantry(megamek.common.Infantry) Enumeration(java.util.Enumeration) ToHitData(megamek.common.ToHitData) WeaponType(megamek.common.WeaponType) Vector(com.sun.java.util.collections.Vector)

Example 20 with Entity

use of megamek.common.Entity in project megameklab by MegaMek.

the class MenuBarCreator method jMenuGetUnitValidationFromCache_actionPerformed.

private void jMenuGetUnitValidationFromCache_actionPerformed() {
    UnitLoadingDialog unitLoadingDialog = new UnitLoadingDialog(parentFrame);
    unitLoadingDialog.setVisible(true);
    UnitSelectorDialog viewer = new UnitSelectorDialog(parentFrame, unitLoadingDialog, true);
    Entity tempEntity = viewer.getChosenEntity();
    if (null == tempEntity) {
        return;
    }
    UnitUtil.showValidation(tempEntity, parentFrame);
}
Also used : Entity(megamek.common.Entity) UnitLoadingDialog(megamek.client.ui.swing.UnitLoadingDialog) UnitSelectorDialog(megamek.client.ui.swing.UnitSelectorDialog)

Aggregations

Entity (megamek.common.Entity)33 File (java.io.File)10 JFileChooser (javax.swing.JFileChooser)10 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)10 UnitLoadingDialog (megamek.client.ui.swing.UnitLoadingDialog)10 UnitSelectorDialog (megamek.client.ui.swing.UnitSelectorDialog)10 MechFileParser (megamek.common.MechFileParser)10 BattleArmor (megamek.common.BattleArmor)7 Vector (com.sun.java.util.collections.Vector)6 Aero (megamek.common.Aero)6 Infantry (megamek.common.Infantry)6 Mech (megamek.common.Mech)6 Tank (megamek.common.Tank)6 BLKFile (megamek.common.loaders.BLKFile)6 Iterator (com.sun.java.util.collections.Iterator)5 Enumeration (java.util.Enumeration)4 Vector (java.util.Vector)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 List (java.util.List)3