Search in sources :

Example 31 with Entity

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

the class UnitPrintQueueDialog method refresh.

private void refresh() {
    unitList.removeAll();
    Vector<String> unitNameList = new Vector<String>();
    for (Entity ent : units) {
        unitNameList.add(String.format("%1$s %2$s", ent.getChassis(), ent.getModel()).trim());
    }
    unitList.setListData(unitNameList);
    unitList.repaint();
}
Also used : Entity(megamek.common.Entity) Vector(java.util.Vector)

Example 32 with Entity

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

the class TestBot method calculateMoveTurn.

public MovePath calculateMoveTurn() {
    long enter = System.currentTimeMillis();
    int initiative = 0;
    MoveOption min = null;
    System.out.println("beginning movement calculations...");
    // first check and that someone else has moved so we don't replan
    Object[] enemy_array = this.getEnemyEntities().toArray();
    for (int j = 0; j < enemy_array.length; j++) {
        if (!((Entity) enemy_array[j]).isSelectableThisTurn()) {
            initiative++;
        }
    }
    // if nobody's moved and we have a valid move waiting, use that
    if (initiative == enemies_moved && old_moves != null) {
        min = this.old_moves.getResult();
        if (min == null || !min.isMoveLegal() || (min.isPhysical && centities.get(min.getPhysicalTargetId()).isPhysicalTarget)) {
            this.old_moves = null;
            System.out.println("recalculating moves since the old move was invalid");
            return calculateMoveTurn();
        }
    } else {
        enemies_moved = initiative;
        Vector possible = new Vector();
        Enumeration e = game.getEntities();
        while (e.hasMoreElements()) {
            Entity entity = (Entity) e.nextElement();
            // ignore loaded units
            if (entity.getPosition() == null) {
                continue;
            }
            CEntity cen = centities.get(entity);
            cen.refresh();
            firstPass(cen);
        }
        Iterator i = this.getEntitiesOwned().iterator();
        boolean short_circuit = false;
        while (i.hasNext() && !short_circuit) {
            Entity entity = (Entity) i.next();
            // (not really necessary unless bot manages to load units)
            if (entity.getPosition() == null) {
                continue;
            }
            // if we can't move this entity right now, ignore it
            if (!game.getTurn().isValidEntity(entity, game)) {
                continue;
            }
            CEntity cen = centities.get(entity);
            System.out.println("Contemplating movement of " + entity.getShortName() + " " + entity.getId());
            MoveOption[] result = calculateMove(entity);
            if (game.getOptions().booleanOption("skip_ineligable_movement") && cen.getEntity().isImmobile()) {
                cen.moved = true;
            } else if (!cen.moved) {
                if (result.length < 6) {
                    min = result.length > 0 ? (MoveOption) result[0] : null;
                    short_circuit = true;
                }
                possible.add(result);
            }
        }
        // and only do the below when there are 2 or mechs left to move
        if (!short_circuit) {
            if (this.getEntitiesOwned().size() > 1) {
                GALance lance = new GALance(this, possible, 50, 80);
                lance.evolve();
                min = lance.getResult();
                this.old_moves = lance;
            } else if (((MoveOption[]) possible.elementAt(0)) != null && ((MoveOption[]) possible.elementAt(0)).length > 0) {
                min = ((MoveOption[]) possible.elementAt(0))[0];
            }
        }
    }
    if (min == null) {
        min = new MoveOption(game, centities.get(getFirstEntityNum()));
    }
    for (int d = 0; d < enemy_array.length; d++) {
        Entity en = (Entity) enemy_array[d];
        // ignore loaded units
        if (en.getPosition() == null) {
            continue;
        }
        CEntity enemy = centities.get(en);
        int enemy_hit_arc = CEntity.getThreatHitArc(enemy.current.getFinalCoords(), enemy.current.getFinalFacing(), min.getFinalCoords());
        MoveOption.DamageInfo di = (MoveOption.DamageInfo) min.damageInfos.get(enemy);
        if (di != null) {
            enemy.expected_damage[enemy_hit_arc] += di.min_damage;
        }
        if (enemy.expected_damage[enemy_hit_arc] > 0) {
            enemy.hasTakenDamage = true;
        }
    }
    if (min.isPhysical) {
        centities.get(min.getPhysicalTargetId()).isPhysicalTarget = true;
    }
    System.out.println(min);
    min.getCEntity().current = min;
    min.getCEntity().last = min;
    this.my_mechs_moved++;
    min.getCEntity().moved = true;
    long exit = System.currentTimeMillis();
    System.out.println("move turn took " + (exit - enter) + " ms");
    return min;
}
Also used : Entity(megamek.common.Entity) Enumeration(java.util.Enumeration) Iterator(com.sun.java.util.collections.Iterator) Vector(com.sun.java.util.collections.Vector)

Example 33 with Entity

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

the class TestBot method bestAttack.

public GAAttack bestAttack(MoveOption es, CEntity target, int search_level) {
    Entity en = es.getEntity();
    int[] attacks = new int[3];
    Vector front = new Vector();
    Vector left = new Vector();
    Vector right = new Vector();
    GAAttack result = null;
    int o_facing = en.getFacing();
    for (Enumeration i = en.getWeapons(); i.hasMoreElements(); ) {
        Mounted mw = (Mounted) i.nextElement();
        Vector c = this.calculateWeaponAttacks(en, mw, true);
        if (c.size() > 0) {
            front.add(c);
            attacks[0] = Math.max(attacks[0], c.size());
        }
        if (!es.getFinalProne() && en.canChangeSecondaryFacing()) {
            en.setSecondaryFacing((o_facing + 5) % 6);
            c = this.calculateWeaponAttacks(en, mw, true);
            if (c.size() > 0) {
                left.add(c);
                attacks[1] = Math.max(attacks[1], c.size());
            }
            en.setSecondaryFacing((o_facing + 1) % 6);
            c = this.calculateWeaponAttacks(en, mw, true);
            if (c.size() > 0) {
                right.add(c);
                attacks[2] = Math.max(attacks[2], c.size());
            }
        } else {
            attacks[1] = 0;
            attacks[2] = 0;
        }
        en.setSecondaryFacing(o_facing);
    }
    Vector arcs = new Vector();
    arcs.add(front);
    if (!es.getFinalProne() && en.canChangeSecondaryFacing()) {
        arcs.add(left);
        arcs.add(right);
    }
    for (int i = 0; i < arcs.size(); i++) {
        Vector v = (Vector) arcs.elementAt(i);
        if (v.size() > 0) {
            GAAttack test = new GAAttack(this, centities.get(en), v, Math.max((v.size() + attacks[i]) * search_level, 20 * search_level), 30 * search_level, en.isEnemyOf((Entity) getEntitiesOwned().get(0)));
            test.setFiringArc(i);
            test.evolve();
            if (target != null) {
                if (result == null || test.getDamageUtility(target) > result.getDamageUtility(target)) {
                    result = test;
                }
            } else if (result == null || test.getFittestChromosomesFitness() > result.getFittestChromosomesFitness()) {
                result = test;
            }
        }
    }
    return result;
}
Also used : Entity(megamek.common.Entity) Enumeration(java.util.Enumeration) Mounted(megamek.common.Mounted) Vector(com.sun.java.util.collections.Vector)

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