Skip to content
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

Added pickup and drop actions #38

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/envs/doorkey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mutable struct DoorKey{W<:GridWorldBase} <: AbstractGridWorld
world::W
agent_pos::CartesianIndex{2}
agent::Agent
has_key::Bool
end

function DoorKey(;n=8, agent_start_pos=CartesianIndex(2,2), rng=Random.GLOBAL_RNG)
Expand All @@ -33,21 +32,22 @@ function DoorKey(;n=8, agent_start_pos=CartesianIndex(2,2), rng=Random.GLOBAL_RN
world[EMPTY, key_pos] = false
world[Key(:yellow), key_pos] = true

DoorKey(world, agent_start_pos, Agent(dir=RIGHT),false)
DoorKey(world, agent_start_pos, Agent(dir=RIGHT))
end

function (w::DoorKey)(::MoveForward)
dir = get_dir(w.agent)
dest = dir(w.agent_pos)

if w.world[Key(:yellow), dest]
w.has_key = true
w.world[Key(:yellow), dest] = false
w.world[EMPTY, dest] = true
if PICKUP(w.agent, Key(:yellow))
w.world[Key(:yellow), dest] = false
w.world[EMPTY, dest] = true
end
w.agent_pos = dest
elseif w.world[Door(:yellow), dest] && w.has_key
elseif w.world[Door(:yellow), dest] && w.agent.inv == Key(:yellow)
w.agent_pos = dest
elseif w.world[Door(:yellow), dest] && !w.has_key
elseif w.world[Door(:yellow), dest] && w.agent.inv != Key(:yellow)
nothing
elseif dest ∈ CartesianIndices((size(w.world, 2), size(w.world, 3))) && !w.world[WALL,dest]
w.agent_pos = dest
Expand Down
39 changes: 39 additions & 0 deletions src/objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ get_color(::Gem) = :magenta
Base.@kwdef mutable struct Agent <: AbstractObject
color::Symbol=:red
dir::LRUD
inv::Union{AbstractObject, Nothing}=nothing
findmyway marked this conversation as resolved.
Show resolved Hide resolved
end
function Base.convert(::Type{Char}, a::Agent)
if a.dir === UP
Expand All @@ -102,3 +103,41 @@ end
get_color(a::Agent) = a.color
get_dir(a::Agent) = a.dir
set_dir!(a::Agent, d) = a.dir = d

#####
# Pick Up and Drop
#####

struct Item end
landrumb marked this conversation as resolved.
Show resolved Hide resolved
struct Nonitem end
const ITEM = Item()
const NONITEM = Nonitem()
isitem(::Type{Key{T}}) where T = ITEM
landrumb marked this conversation as resolved.
Show resolved Hide resolved
isitem(::Type{Gem}) = ITEM
isitem() = NONITEM
landrumb marked this conversation as resolved.
Show resolved Hide resolved

struct Pickup end
const PICKUP = Pickup()

(::Pickup)(a::Agent, o::T) where T = pickup(isitem(T), a, o)
function pickup(::Item, a::Agent, o::AbstractObject)
if a.inv == nothing
a.inv = o
return true
end
return false
end
pickup(a::Agent, ::Nonitem, o::AbstractObject) = nothing
landrumb marked this conversation as resolved.
Show resolved Hide resolved

struct Drop end
const DROP = Drop()
landrumb marked this conversation as resolved.
Show resolved Hide resolved

function (::Drop)(a::Agent)
if a.inv != nothing
x = a.inv
a.inv = nothing
return x
end
return nothing
end