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 19 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
79 changes: 74 additions & 5 deletions src/objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,26 @@ const GEM = Gem()
Base.convert(::Type{Char}, ::Gem) = '♦'
get_color(::Gem) = :magenta

Base.@kwdef mutable struct Agent <: AbstractObject
abstract type AbstractAgent <: AbstractObject end

Base.@kwdef mutable struct Agent <: AbstractAgent
color::Symbol=:red
dir::LRUD
inv::Union{AbstractObject, Nothing}=nothing
findmyway marked this conversation as resolved.
Show resolved Hide resolved
end

Base.@kwdef mutable struct Array_agent <: AbstractAgent
color::Symbol=:red
dir::LRUD
inv::Vector{Union{AbstractObject, Nothing}}=[]
end

function Array_agent(dir::LRUD, len::Integer; color::Symbol=:red)
Array_agent(color, dir, Vector{Union{AbstractObject, Nothing}}(nothing, len))
end

function Base.convert(::Type{Char}, a::Agent)
function Base.convert(::Type{Char}, a::AbstractAgent)

if a.dir === UP
'↑'
elseif a.dir === DOWN
Expand All @@ -62,6 +76,61 @@ function Base.convert(::Type{Char}, a::Agent)
end
end

get_color(a::Agent) = a.color
get_dir(a::Agent) = a.dir
set_dir!(a::Agent, d) = a.dir = d
get_color(a::AbstractAgent) = a.color
get_dir(a::AbstractAgent) = a.dir
set_dir!(a::AbstractAgent, d) = a.dir = d

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

struct Transportable end
struct Nontransportable end
const TRANSPORTABLE = Transportable()
const NONTRANSPORTABLE = Nontransportable()
istransportable(::Type{<:Key}) = TRANSPORTABLE
istransportable(::Type{Gem}) = TRANSPORTABLE
istransportable(x::AbstractObject) = istransportable(typeof(x))

struct Pickup end
const PICKUP = Pickup()

struct Drop end
const DROP = Drop()

(::Pickup)(a::AbstractAgent, o::T) where T = PICKUP(istransportable(T), a, o)
function (::Pickup)(::Transportable, a::Agent, o::AbstractObject)
if a.inv == nothing
a.inv = o
return true
end
return false
end
function (::Pickup)(::Transportable, a::Array_agent, o::AbstractObject)
for (i, v) in enumerate(a.inv) # Picked up objects go into the first empty index
if v == nothing
a.inv[i] = o
return true
end
end
return false
end
pickup(::Nontransportable, a::AbstractAgent, o::AbstractObject) = nothing

function (::Drop)(a::Agent)
if a.inv != nothing
x = a.inv
a.inv = nothing
return x
end
return nothing
end
function (::Drop)(a::Array_agent)
for (i, v) in Iterators.reverse(enumerate(a.inv)) # Most recently picked up objects are dropped first
if typeof(v)<:AbstractObject
a.inv[i] = nothing
return v
end
end
return nothing
end