-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
first crack at bot minefield avoidance #2898
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
megamek/src/megamek/client/bot/princess/MinefieldUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* MegaMek - | ||
* Copyright (C) 2021 The MegaMek Team | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 2 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
*/ | ||
|
||
package megamek.client.bot.princess; | ||
|
||
import megamek.common.Compute; | ||
import megamek.common.Entity; | ||
import megamek.common.EntityMovementMode; | ||
import megamek.common.Mech; | ||
import megamek.common.Minefield; | ||
import megamek.common.MovePath; | ||
import megamek.common.MoveStep; | ||
import megamek.common.annotations.Nullable; | ||
|
||
/** | ||
* This class contains logic to evaluate the damage a unit could sustain from | ||
* moving a long a move path containing minefields | ||
* @author NickAragua | ||
*/ | ||
public class MinefieldUtil { | ||
/** | ||
* Calculate how much damage we'll take from stepping on mines over a particular path | ||
*/ | ||
public static double checkPathForMinefieldHazards(MovePath path) { | ||
double hazardAccumulator = 0; | ||
|
||
for (MoveStep step : path.getStepVector()) { | ||
hazardAccumulator += calcMinefieldHazardForHex(step, path.getEntity(), | ||
path.isJumping(), step.equals(path.getLastStep())); | ||
} | ||
|
||
//TODO: Teach bot to activate minesweepers | ||
|
||
return hazardAccumulator; | ||
} | ||
|
||
/** | ||
* Calculate how much damage we'll take from stepping on mines in a particular hex | ||
*/ | ||
public static double calcMinefieldHazardForHex(@Nullable MoveStep step, Entity movingUnit, | ||
boolean isJumping, boolean lastStep) { | ||
// if we're not actually taking a step, no minefield hazard | ||
if ((step == null) || !step.getType().entersNewHex()) { | ||
return 0; | ||
} | ||
|
||
// if our movement mode does not result in minefield detonation, no minefield hazard | ||
if (!movingUnit.getMovementMode().detonatesGroundMinefields()) { | ||
return 0; | ||
} | ||
|
||
double hazardAccumulator = 0; | ||
// hovercraft and WIGEs grinding along the ground detonate minefields on a 12 | ||
boolean hoverMovement = movingUnit.getMovementMode() == EntityMovementMode.HOVER || | ||
((movingUnit.getMovementMode() == EntityMovementMode.WIGE) && (movingUnit.getElevation() == 0)); | ||
double hoverMovementMultiplier = hoverMovement ? | ||
Compute.oddsAbove(Minefield.HOVER_WIGE_DETONATION_TARGET) : 1; | ||
|
||
// only mechs interact with vibrabombs | ||
boolean unitIsMech = movingUnit instanceof Mech; | ||
|
||
for (Minefield minefield : movingUnit.getGame().getMinefields(step.getPosition())) { | ||
sixlettervariables marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch (minefield.getType()) { | ||
case Minefield.TYPE_CONVENTIONAL: | ||
case Minefield.TYPE_INFERNO: | ||
// if we're either not jumping or it's the last step | ||
if (!isJumping || lastStep) { | ||
hazardAccumulator += minefield.getDensity() * hoverMovementMultiplier; | ||
} | ||
break; | ||
case Minefield.TYPE_ACTIVE: | ||
hazardAccumulator += minefield.getDensity() * hoverMovementMultiplier; | ||
break; | ||
case Minefield.TYPE_VIBRABOMB: | ||
// mechs >10 tons the "setting" will set off vibrabombs before they | ||
// get to them so we don't particularly care | ||
if (unitIsMech && (!isJumping || lastStep) && | ||
(movingUnit.getWeight() >= minefield.getSetting()) && | ||
(movingUnit.getWeight() <= minefield.getSetting() + 10)) { | ||
hazardAccumulator += minefield.getDensity(); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return hazardAccumulator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import java.util.Deque; | ||
import java.util.List; | ||
|
||
import megamek.client.bot.princess.MinefieldUtil; | ||
import megamek.common.Coords; | ||
import megamek.common.IGame; | ||
import megamek.common.Infantry; | ||
|
@@ -108,6 +109,30 @@ public int compare(MovePath first, MovePath second) { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Comparator that sorts MovePaths based on, in order, the following criteria: | ||
* Minefield hazard (stepping on less mines is better) | ||
* Least MP used | ||
* Most distance moved | ||
*/ | ||
public static class MovePathMinefieldAvoidanceMinMPMaxDistanceComparator extends MovePathMinMPMaxDistanceComparator { | ||
@Override | ||
public int compare(MovePath first, MovePath second) { | ||
Double firstMinefieldScore = MinefieldUtil.calcMinefieldHazardForHex(first.getLastStep(), | ||
first.getEntity(), first.isJumping(), false); | ||
Double secondMinefieldScore = MinefieldUtil.calcMinefieldHazardForHex(second.getLastStep(), | ||
second.getEntity(), second.isJumping(), false); | ||
|
||
int s = secondMinefieldScore.compareTo(firstMinefieldScore); | ||
|
||
if (s == 0) { | ||
return super.compare(first, second); | ||
} else { | ||
return s; | ||
Comment on lines
+129
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ternary return candidate |
||
} | ||
} | ||
} | ||
|
||
/** | ||
* Relaxer for longest path movement. Current implementation needs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brackets