use of ww.Dir in project cg by nmahoude.
the class Oracle method checkForPushed.
/**
* as there was a push, there is no move, all opp agents are at the same place
*/
void checkForPushed(int myAgentId, GameState currentState) {
// pushed ! by who ?
Cell in = currentState.agents[myAgentId].cell;
Cell from = currentState.grid.get(formerSimulatedState.agents[myAgentId].position);
Dir dir = in.dirTo(from);
Set<Point> pushFilter = new HashSet<>();
for (Dir pushedDir : dir.pushDirections()) {
Cell origin = from.get(pushedDir);
if (!formerSimulatedState.grid.get(origin.position).isValid())
continue;
// watch for this condition : the inverse is not true : if we saw an entity, it doesn't mean it did it
if (canSeeCell(formerSimulatedState.agents[theOtherId(myAgentId)], origin) && formerSimulatedState.grid.get(origin.position).agent == null)
continue;
pushFilter.add(origin.position);
}
// here pushFilter contains only cell where the push can originate
if (pushFilter.size() == 0) {
if (!PROD) {
throw new RuntimeException("Cant find the origin of the push ? ");
} else {
fillAllPossiblePositions(currentState, possiblePositions[2]);
fillAllPossiblePositions(currentState, possiblePositions[3]);
}
}
boolean agent2InFilter = isAgentInFilter(currentState, pushFilter, 2);
boolean agent3InFilter = isAgentInFilter(currentState, pushFilter, 3);
int count = (agent2InFilter ? 1 : 0) + (agent3InFilter ? 1 : 0);
if (count == 0) {
System.err.println("Push locations : " + pushFilter);
System.err.println("Simulated state :");
formerSimulatedState.toTDD();
System.err.println("Current state: ");
currentState.toTDD();
if (!PROD) {
throw new RuntimeException("Nobody can be in the origin of the push ? ");
} else {
fillAllPossiblePositions(currentState, possiblePositions[2]);
fillAllPossiblePositions(currentState, possiblePositions[3]);
}
} else if (count == 1) {
// only on agent can be here !
int id = agent2InFilter ? 2 : 3;
if (debugMode)
System.err.println("Pushed by " + id);
possiblePositions[id].addAll(getFilteredPositions(currentState, formerPossiblePositions[id], pushFilter));
possiblePositions[theOtherId(id)].addAll(formerPossiblePositions[theOtherId(id)]);
if (debugMode) {
System.err.println("Copying former position of the other : " + possiblePositions[theOtherId(id)]);
}
} else if (count == 2) {
// if not, choose the one with the least moves
if (pushFilter.size() == 1) {
// ok, we d'ont know which one, but we are sure one was here !
// TODO infer further ?
int id;
if (formerPossiblePositions[2].size() == 1) {
id = 2;
} else if (formerPossiblePositions[3].size() == 1) {
id = 3;
} else {
id = formerPossiblePositions[3].size() >= formerPossiblePositions[2].size() ? 2 : 3;
}
possiblePositions[id].addAll(pushFilter);
possiblePositions[theOtherId(id)].addAll(formerPossiblePositions[theOtherId(id)]);
} else {
possiblePositions[2].addAll(formerPossiblePositions[2]);
possiblePositions[3].addAll(formerPossiblePositions[3]);
}
}
}
use of ww.Dir in project cg by nmahoude.
the class Simulation method getPossibleActionsCount.
public static int getPossibleActionsCount(GameState state, Agent agent) {
int count = 0;
for (Dir dir1 : Dir.getValues()) {
Cell dir1Cell = agent.cell.get(dir1);
if (!dir1Cell.isValid())
continue;
if (dir1Cell.isFriendly(agent))
continue;
if (dir1Cell.agent != null) {
// push
for (Dir dir2 : dir1.pushDirections()) {
Cell pushCell = dir1Cell.get(dir2);
if (pushCell.isValid() && !pushCell.isOccupied()) {
count++;
}
}
} else {
// move
for (Dir dir2 : Dir.getValues()) {
Cell buildCell = dir1Cell.get(dir2);
if (!buildCell.isValid())
continue;
if (buildCell.isOccupiedButNotBy(agent))
continue;
count++;
}
}
}
return count;
}
use of ww.Dir in project cg by nmahoude.
the class Simulation method computePush.
private void computePush(GameState state, int depth, long[] transposition) {
Dir[] validDirs = move.dir1.pushDirections();
boolean validPushDirection = (move.dir2 == validDirs[0]) || (move.dir2 == validDirs[1]) || (move.dir2 == validDirs[2]);
if (!validPushDirection) {
// dir2 is invalid
move.dir2Invalid();
return;
}
Cell pushFrom = agent.cell.get(move.dir1);
Agent pushed = pushFrom.agent;
if (pushed.isFriendly(agent)) {
move.dir1Invalid();
return;
}
Cell pushTo = pushFrom.get(move.dir2);
if ((!pushTo.isValid()) || (pushTo.isOccupied())) {
move.dir2Invalid();
return;
}
if (pushTo.height >= Cell.FINAL_HEIGHT || pushTo.height > pushFrom.height + 1) {
move.dir2Invalid();
return;
}
// move ok, update agent position & grid
move.isPush = true;
pushed.pushTo(pushTo);
pushFrom.elevate();
updateTransposition(state, transposition, pushFrom.height, pushFrom.position.mask);
move.allValid();
return;
}
use of ww.Dir in project cg by nmahoude.
the class Simulation method computePush.
private void computePush() {
Dir[] validDirs = move.dir1.pushDirections();
boolean validPushDirection = (move.dir2 == validDirs[0]) || (move.dir2 == validDirs[1]) || (move.dir2 == validDirs[2]);
if (!validPushDirection) {
// dir2 is invalid
move.dir2Invalid();
return;
}
int targetX = agent.x + move.dir1.dx;
int targetY = agent.y + move.dir1.dy;
Agent pushed = state.agents[state.occupiedBy(targetX, targetY)];
int pushToX = targetX + move.dir2.dx;
int pushToY = targetY + move.dir2.dy;
if ((!state.isValid(pushToX, pushToY)) || (state.isOccupied(move.id, pushToX, pushToY))) {
move.dir2Invalid();
return;
}
int pushToHeight = state.getHeight(pushToX, pushToY);
int pushFromHeight = state.getHeight(targetX, targetY);
if (pushToHeight >= FINAL_HEIGHT || pushToHeight > pushFromHeight + 1) {
move.dir2Invalid();
return;
}
// TODO check if we know a agent is at pushTo and would block the action ?
// move ok, update agent position & grid
agent.x = agent.x;
agent.y = agent.y;
state.setHeight(targetX, targetY, pushFromHeight + 1);
pushed.x = pushToX;
pushed.y = pushToY;
move.dir1X = targetX;
move.dir1Y = targetY;
move.dir1Height = pushFromHeight;
move.dir2X = pushToX;
move.dir2Y = pushToY;
move.dir2Height = pushToHeight;
move.allValid();
return;
}
use of ww.Dir in project cg by nmahoude.
the class Perf method perf.
@Test
public void perf() {
state.size = 6;
state.readInit(new Scanner("" + state.size + " 2"));
TU.setHeights(state, "344444", "333434", "..34..", ".3..3.", ".0101.", "000002");
TU.setAgent(state, 0, 1, 3);
TU.setAgent(state, 1, 1, 1);
TU.setAgent(state, 2, -1, -1);
TU.setAgent(state, 3, -1, -1);
Player.state = state;
GameState.startTime = System.currentTimeMillis() + 1_000_000;
state.legalActionDepth0NodeCache.clear();
for (int i = 0; i < 2; i++) {
for (Dir dir1 : Dir.getValues()) {
for (Dir dir2 : Dir.getValues()) {
Move move = new Move(state.agents[i]);
move.dir1 = dir1;
move.dir2 = dir2;
NodePOC node = new NodePOC(1);
node.move = move;
state.legalActionDepth0NodeCache.add(node);
}
}
}
for (int i = 0; i < 500; i++) {
new Think(state).think(3);
}
}
Aggregations