From 820d7a7d6610083806b0bcdcae431054c1134d21 Mon Sep 17 00:00:00 2001
From: Daniel Vedder <daniel.vedder@idiv.de>
Date: Sat, 26 Nov 2022 00:07:36 +0100
Subject: [PATCH] Gave the Wolpertinger something to do...

---
 README.md                  |  2 +-
 src/core/input.jl          | 12 +-----------
 src/core/simulation.jl     |  7 +++----
 src/nature/nature.jl       | 11 +++++++----
 src/nature/wolpertinger.jl | 17 ++++++++++++-----
 5 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/README.md b/README.md
index 8772a49..7337725 100644
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ optional arguments:
                         name of the map file
   -o, --outdir OUTDIR   location of the output directory
   -l, --loglevel LOGLEVEL
-                        verbosity: "debug", "info", or "errors"
+                        verbosity: "debug", "info", or "quiet"
   -r, --runtime RUNTIME
                         duration in days that the simulation will run
                         (type: Int64)
diff --git a/src/core/input.jl b/src/core/input.jl
index 193fae5..5390565 100644
--- a/src/core/input.jl
+++ b/src/core/input.jl
@@ -107,7 +107,7 @@ function parsecommandline()
             arg_type = String
             required = false
         "--loglevel", "-l"
-            help = "verbosity: \"debug\", \"info\", or \"errors\""
+            help = "verbosity: \"debug\", \"info\", or \"quiet\""
             arg_type = String
             required = false
         "--runtime", "-r"
@@ -122,13 +122,3 @@ function parsecommandline()
     args
 end
 
-"""
-    readtiffmapfile(filename)
-
-Read in a TIFF map file and return it as an array.
-"""
-function readtiffmapfile(filename)
-    tiff = GeoArrays.read(filename)
-    space = GridSpace(size(tiff)[1:2], periodic=false)
-    #TODO this requires GeoArrays
-end
diff --git a/src/core/simulation.jl b/src/core/simulation.jl
index 8b0fc72..8337356 100644
--- a/src/core/simulation.jl
+++ b/src/core/simulation.jl
@@ -11,8 +11,10 @@ and instantiate the AgentBasedModel object.
 """
 function initialise(config::String=PARAMFILE)
     initsettings(config)
+    Random.seed!(param("core.seed"))
     setupdatadir()
     landcover = GeoArrays.read(param("core.mapfile"))
+    #TODO load field map
     space = GridSpace(size(landcover)[1:2], periodic=false)
     properties = Dict{Symbol,Any}(:day=>0,
                                   :landcover=>landcover)
@@ -25,7 +27,6 @@ function initialise(config::String=PARAMFILE)
     model
 end
 
-
 """
     stepsimulation!(model)
 
@@ -40,19 +41,17 @@ function stepsimulation!(model::AgentBasedModel)
     #TODO
 end
 
-
 """
     finalise(model)
 
 Wrap up the simulation. Output all remaining data and exit.
 """
 function finalise(model::AgentBasedModel)
-    @info "Simulation ran. Nothing happened. But it will!"
+    @info "Simulation ran. Things are beginning to happen!"
     #TODO
     genocide!(model)
 end
 
-
 """
     simulate(config)
 
diff --git a/src/nature/nature.jl b/src/nature/nature.jl
index e2b7cf1..87776e2 100644
--- a/src/nature/nature.jl
+++ b/src/nature/nature.jl
@@ -13,8 +13,8 @@ initialise their population and update each individual.
 """
 struct Species
     name::String
-    initpop::Function # takes one argument: model
-    update::Function # takes two arguments: animal, model
+    initpop!::Function # takes one argument: model
+    update!::Function # takes two arguments: animal, model
 end
 
 """
@@ -31,6 +31,7 @@ by the `species` struct passed by them during initialisation.
     energy::Int32
 end
 
+# This dict stores the definitions for all species that can be simulated
 let specieslist = Dict{String, Species}()
     """
         registerspecies(species)
@@ -59,8 +60,9 @@ Update an animal by one day.
 """
 function stepagent!(animal::Animal, model::AgentBasedModel)
     animal.age += 1
-    animal.species.update(animal,model)
+    animal.species.update!(animal,model)
     if animal.energy <= 0
+        @debug "$(animal.species.name) $(animal.id) has died."
         kill_agent!(animal, model)
     end
 end
@@ -71,8 +73,9 @@ end
 Initialise the model with all simulated animal populations.
 """
 function initnature!(model::AgentBasedModel)
+    #The config file determines which species are simulated in this run
     for s in param("nature.targetspecies")
-        getspecies(s).initpop(model)
+        getspecies(s).initpop!(model)
     end
 end
 
diff --git a/src/nature/wolpertinger.jl b/src/nature/wolpertinger.jl
index 8875ec5..2cb2b92 100644
--- a/src/nature/wolpertinger.jl
+++ b/src/nature/wolpertinger.jl
@@ -2,7 +2,7 @@
 ###
 ### This file holds the code for the Wolpertinger (https://en.wikipedia.org/wiki/Wolpertinger).
 ### NOT FOR ACTUAL USE! This is of course only a test species ;-)
-### Although I dare say the Wolpertinger is rather endangered...
+### Although I dare say the Wolpertinger is probably rather endangered...
 ###
 
 """
@@ -12,8 +12,8 @@ Initialise a population of Wolpertingers in random locations around the landscap
 """
 function initwolpertinger!(model::AgentBasedModel)
     species = getspecies("Wolpertinger")
-    worldsize = size(model.landcover)[1:2]
-    popsize = round(worldsize[1]*worldsize[2]/1000)
+    x, y = size(model.landcover)
+    popsize = Int(round((x*y)/10000))
     for i in 1:popsize
         add_agent!(Animal, model, species, hermaphrodite, 0, 100)
     end
@@ -27,8 +27,15 @@ Wolpertingers are rather stupid creatures, all they do is move around randomly
 and occasionally reproduce by spontaneous parthogenesis...
 """
 function updatewolpertinger!(w::Animal, model::AgentBasedModel)
-    #TODO
-    @debug "The Wolpertinger is a mysterious animal..." w.id
+    # walk in a random direction
+    direction = Tuple(rand([-1,1], 2))
+    walk!(w, direction, model; ifempty=false)
+    w.energy -= rand([1, 10])*5
+    # reproduce every once in a blue moon
+    if rand() < 0.01
+        @debug "Wolpertinger $(w.id) has reproduced."
+        add_agent!(w.pos, Animal, model, getspecies("Wolpertinger"), hermaphrodite, 0, 100)
+    end
 end
 
 registerspecies(Species("Wolpertinger", initwolpertinger!, updatewolpertinger!))
-- 
GitLab