Skip to content

Commit

Permalink
simplified fakePlace
Browse files Browse the repository at this point in the history
Instead of tracking the agent's position and orientation, fakePlace now places one of the desired item in the agent's inventory and the agent actually places it.
  • Loading branch information
v-chyou authored Feb 26, 2020
1 parent 2f98849 commit 7009ab9
Showing 1 changed file with 8 additions and 115 deletions.
123 changes: 8 additions & 115 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,126 +261,19 @@ namespace hourOfCode {
}
}

let NORTH = -180
let EAST = -90
let SOUTH = 0
let WEST = 90
let UNIT_NORTH = positions.create(0, 0, -1)
let UNIT_EAST = positions.create(1, 0, 0)
let UNIT_SOUTH = positions.create(0, 0, 1)
let UNIT_WEST = positions.create(-1, 0, 0)
let UNIT_UP = positions.create(0, 1, 0)
let UNIT_DOWN = positions.create(0, -1, 0)

/**
* Places an item or block in the world
* Causes the agent to place the specified block in the specified direction
*/
//% block="agent place %b %dir"
//% weight = 99
export function fakePlace(b: Block, dir: SixDirection) {
let agentOrientation = agent.getOrientation()
let agentPosition = agent.getPosition()
agent.attack(SixDirection.Up)
if (agent.inspect(AgentInspection.Block, dir) == Block.Air) {
if (dir == SixDirection.Up) {
blocks.place(b, positions.add(
agentPosition,
UNIT_UP
))
} else if (dir == SixDirection.Down) {
blocks.place(b, positions.add(
agentPosition,
UNIT_DOWN
))
} else if (agentOrientation == NORTH) {
if (dir == SixDirection.Forward) {
blocks.place(b, positions.add(
agentPosition,
UNIT_NORTH
))
} else if (dir == SixDirection.Right) {
blocks.place(b, positions.add(
agentPosition,
UNIT_EAST
))
} else if (dir == SixDirection.Back) {
blocks.place(b, positions.add(
agentPosition,
UNIT_SOUTH
))
} else if (dir == SixDirection.Left) {
blocks.place(b, positions.add(
agentPosition,
UNIT_WEST
))
}
} else if (agentOrientation == EAST) {
if (dir == SixDirection.Forward) {
blocks.place(b, positions.add(
agentPosition,
UNIT_EAST
))
} else if (dir == SixDirection.Right) {
blocks.place(b, positions.add(
agentPosition,
UNIT_SOUTH
))
} else if (dir == SixDirection.Back) {
blocks.place(b, positions.add(
agentPosition,
UNIT_WEST
))
} else if (dir == SixDirection.Left) {
blocks.place(b, positions.add(
agentPosition,
UNIT_NORTH
))
}
} else if (agentOrientation == SOUTH) {
if (dir == SixDirection.Forward) {
blocks.place(b, positions.add(
agentPosition,
UNIT_SOUTH
))
} else if (dir == SixDirection.Right) {
blocks.place(b, positions.add(
agentPosition,
UNIT_WEST
))
} else if (dir == SixDirection.Back) {
blocks.place(b, positions.add(
agentPosition,
UNIT_NORTH
))
} else if (dir == SixDirection.Left) {
blocks.place(b, positions.add(
agentPosition,
UNIT_EAST
))
}
} else if (agentOrientation == WEST) {
if (dir == SixDirection.Forward) {
blocks.place(b, positions.add(
agentPosition,
UNIT_WEST
))
} else if (dir == SixDirection.Right) {
blocks.place(b, positions.add(
agentPosition,
UNIT_NORTH
))
} else if (dir == SixDirection.Back) {
blocks.place(b, positions.add(
agentPosition,
UNIT_EAST
))
} else if (dir == SixDirection.Left) {
blocks.place(b, positions.add(
agentPosition,
UNIT_SOUTH
))
}
}
if (!(b == AIR)) {
let MIN_SLOT = 1
let MAX_SLOT = 27
let randomSlot = Math.randomRange(MIN_SLOT, MAX_SLOT)
agent.setItem(b, 1, randomSlot)
agent.setSlot(randomSlot)
agent.place(dir)
}
}
}

0 comments on commit 7009ab9

Please sign in to comment.