ABMEv_world.jl 1.09 KB
Newer Older
Victor's avatar
Victor committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# this used to be the worldalive
mutable struct World{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number}
    agents::Vector{AbstractAgentM}
    space::S
    parameters::Dict{String,Any}
    t::T
end

parameters(world::World) = world.parameters
time(w::World) = w.time
space(w::World) = w.space
# this throws indices that are occupied by agents
_get_idx(world::World) = collect(eachindex(skipmissing(world.agents)))
# this throws agents of an abstract array of size size(world)
Base.getindex(w::World,i::Int) = w.agents[_get_idx(w)[i]]

# this throws an iterators of agents in the world
agents(world) = skipmissing(world.agents)
size(world) = size(world.agents) - count(ismissing,world.agents)
maxsize(w::World) = length(w.agents)
_findfreeidx(w::World) = findfirst(findfirst(ismissing,w.agents))
22
addAgent!(w::World,a::AbstractAgent) = begin
Victor's avatar
Victor committed
23
24
25
26
    idx = _findfreeidx(w)
    w.agents[idx] = a
    return nothing
end
27
removeAgent!(w::World,i::Int) = begin
Victor's avatar
Victor committed
28
29
30
31
32
33
34
35
    world[i] = missing
    return nothing
end

update_clock!(w::World{A,S,T},dt) where {A,S,T} = begin
    w.t = convert(T,sum(w.t + dt))
    return nothing
end