use of eidolons.game.battlecraft.ai.UnitAI in project Eidolons by IDemiurge.
the class AiScriptExecutor method executeCommand.
private void executeCommand(Unit unit, COMBAT_SCRIPT_FUNCTION function, String arg, boolean free, boolean immediate, String... args) {
ActionSequence sequence = null;
GOAL_TYPE goal = getGoalType(function);
Task task = new Task(true, unit.getAI(), goal, arg);
UnitAI ai = unit.getAI();
switch(function) {
case MOVE_TO:
// via a path!
ActionPath path = getPathSequenceConstructor().getOptimalPathSequence(unit.getAI(), new Coordinates(arg.toString()));
sequence = new ActionSequence(path.getActions(), task, unit.getAI());
break;
case TURN_TO:
// cell id
sequence = new ActionSequence(getTurnSequenceConstructor().getTurnSequence(FACING_SINGLE.IN_FRONT, unit, new Coordinates(arg.toString())), task, unit.getAI());
break;
case ACTION:
Action action = AiActionFactory.newAction(arg.toString(), ai);
// new ActionSequence();
sequence = getActionSequenceConstructor().constructSingleActionSequence(action, new Task(ai, goal, args[0]));
break;
case ATTACK:
break;
case FREEZE:
break;
case UNFREEZE:
break;
case ORDER:
// OrderFactory.getOrder();
Order a = new Order(arg.toString());
unit.getAI().setCurrentOrder(a);
return;
}
if (immediate) {
unit.getAI().setStandingOrders(sequence);
unit.getAI().setFree(free);
} else
sequence.getActions().forEach(// TODO wait?
action -> getExecutor().execute(action, free));
}
use of eidolons.game.battlecraft.ai.UnitAI in project Eidolons by IDemiurge.
the class ActionSequenceConstructor method constructSingleActionSequence.
public ActionSequence constructSingleActionSequence(Action targetAction, Task task) {
List<Action> actions = new ArrayList<>();
UnitAI ai = task.getAI();
targetAction.getRef().setID(KEYS.ACTIVE, targetAction.getActive().getId());
switch(task.getType()) {
case ATTACK:
{
// only facing!
actions = constructSingleAttackSequence(targetAction, task);
break;
}
case DEBUFF:
case BUFF:
actions.addAll(getTurnSequenceConstructor().getTurnSequence(targetAction));
actions.add(targetAction);
break;
// break;
default:
actions.add(targetAction);
break;
}
if (actions.isEmpty()) {
return null;
}
// not very good
Action action = actions.get(0);
if (!action.canBeActivated()) {
LogMaster.log(LOG_CHANNEL.AI_DEBUG2, "No sequence for " + actions.get(actions.size() - 1) + " - " + action.getActive().getName() + ": " + action.getActive().getCosts().getReason());
return null;
}
return new ActionSequence(actions, task, ai);
}
use of eidolons.game.battlecraft.ai.UnitAI in project Eidolons by IDemiurge.
the class ExplorationAiMaster method checkAiActs.
public void checkAiActs() {
// master.getGame().getAiManager().getBehaviorMaster().
activeUnitAIs.forEach(ai -> ai.setExplorationTimePassed(master.getTimeMaster().getTime() - ai.getExplorationTimeOfLastAction()));
// if (isAiActs())
// return;
boolean isAiActs = false;
for (UnitAI ai : activeUnitAIs) {
if (!ExplorationMaster.isExplorationOn())
return;
if (ai.getExplorationTimePassed() <= ExplorationTimeMaster.secondsPerAP)
continue;
double distance = PositionMaster.getExactDistance(ai.getUnit().getCoordinates(), Eidolons.getMainHero().getCoordinates());
if (distance > getMaxDistance(ai))
continue;
try {
if (tryMoveAi(ai))
isAiActs = true;
} catch (Exception e) {
main.system.ExceptionMaster.printStackTrace(e);
}
}
if (isAiActs) {
aiActs = true;
if (ExplorationMaster.isExplorationOn())
master.getLoop().signal();
}
}
use of eidolons.game.battlecraft.ai.UnitAI in project Eidolons by IDemiurge.
the class AtomicAi method checkAtomicActionApproach.
public boolean checkAtomicActionApproach(UnitAI ai) {
if (ai.getCurrentOrder() != null)
if (ai.getCurrentOrder().getStrictPriority() != ORDER_PRIORITY_MODS.ATTACK && ai.getCurrentOrder().getStrictPriority() != ORDER_PRIORITY_MODS.APPROACH)
return false;
double maxDistance = getDistanceForAtomicApproach(ai);
double minDistance = -1;
for (Unit sub : Analyzer.getVisibleEnemies(ai)) {
if (game.getVisionMaster().getSightMaster().getClearShotCondition().check(getUnit(), sub)) {
double distance = PositionMaster.getExactDistance(getUnit(), sub);
if (distance < minDistance)
minDistance = distance;
}
}
if (minDistance < 0) {
return false;
}
minDistance -= Analyzer.getVisibleEnemies(ai).size();
minDistance += ai.getGroup().getMembers().size();
Double average = ai.getGroup().getMembers().stream().collect(Collectors.averagingInt((t) -> t.getIntParam(PARAMS.POWER)));
Integer p = ai.getUnit().getIntParam(PARAMS.POWER);
minDistance -= p / average;
minDistance += average / p;
return minDistance > maxDistance;
}
use of eidolons.game.battlecraft.ai.UnitAI in project Eidolons by IDemiurge.
the class ActionManager method chooseAction.
public Action chooseAction() {
UnitAI ai = getMaster().getUnitAI();
if (ai.checkStandingOrders()) {
return ai.getStandingOrders().get(0);
}
// TODO try not to? :)
getPathSequenceConstructor().clearCache();
if (getUnit() != ai.getUnit()) {
getCellPrioritizer().reset();
} else {
}
checkDeactivate();
if (ListMaster.isNotEmpty(ai.getForcedActions())) {
Action action = ai.getForcedActions().get(0);
ai.getForcedActions().remove(0);
return action;
}
// if (!ai.isEngaged()) {
// TODO return behaviorMaster.getBehaviorAction(ai);
// }
FACING_DIRECTION originalFacing = getUnit().getFacing();
Coordinates originalCoordinates = getUnit().getCoordinates();
Action action = null;
ActionSequence chosenSequence = null;
boolean atomic = false;
if (isAtomicAiOn())
try {
atomic = getAtomicAi().checkAtomicActionRequired(ai);
} catch (Exception e) {
main.system.ExceptionMaster.printStackTrace(e);
}
if (atomic)
if (isAtomicAiOn())
try {
action = getAtomicAi().getAtomicAction(ai);
} catch (Exception e) {
main.system.ExceptionMaster.printStackTrace(e);
action = getAtomicAi().getAtomicWait(ai.getUnit());
}
if (action == null) {
List<ActionSequence> actions = new ArrayList<>();
try {
List<ActionSequence> sequences = getActionSequenceConstructor().createActionSequences(ai);
for (ActionSequence a : sequences) {
if (a.get(0).canBeActivated()) {
// if (a.getOrCreate(0).canBeTargeted())
actions.add(a);
}
}
if (ListMaster.isNotEmpty(actions)) {
chosenSequence = DC_PriorityManager.chooseByPriority(actions);
}
} catch (Exception e) {
main.system.ExceptionMaster.printStackTrace(e);
} finally {
getUnit().setCoordinates(originalCoordinates);
getUnit().setFacing(originalFacing);
}
}
if (chosenSequence == null) {
if (action == null) {
action = getForcedAction(ai);
}
return action;
} else {
if (chosenSequence.getType() == GOAL_TYPE.DEFEND)
return chosenSequence.nextAction();
}
if (getUnit().getUnitAI().getLogLevel() > UnitAI.LOG_LEVEL_NONE) {
if (Launcher.DEV_MODE)
game.getLogManager().log(LOG.GAME_INFO, ai.getUnit().getName() + " chooses task: " + chosenSequence.getTask().toShortString());
String message = getUnit() + " has chosen: " + chosenSequence + " with priority of " + StringMaster.wrapInParenthesis(chosenSequence.getPriority() + "");
LogMaster.log(LOG_CHANNEL.AI_DEBUG, message);
SpecialLogger.getInstance().appendSpecialLog(SPECIAL_LOG.AI, message);
}
// TODO for behaviors? ai-issued-orders?
ai.checkSetOrders(chosenSequence);
return chosenSequence.nextAction();
}
Aggregations