Added logging; simulation ALMOST runs

This commit is contained in:
Simon Brooke 2019-05-15 07:18:09 +01:00
parent 984feb850a
commit 28db4866eb
9 changed files with 59 additions and 30 deletions

View file

@ -40,6 +40,24 @@
#(= (:location %) (:location g))
(vals (:gossips world))))))}})))
(defn move-gossip
"Return a world like this `world` but with this `gossip` moved to this
`new-location`. Many gossips are essentially shadow-records of agents of
other types, and the movement if the gossip should be controlled by the
run function of the type of the record they shadow. The [[#run]] function
below does NOT call this function."
[gossip world new-location]
(let [id (cond
(map? gossip)
(:id (-> world :gossipe gossip)
(keyword? gossip)
gossip))]
(deep-merge
world
{:gossips
{id
{:location new-location}}})))
(defn run
"Return a world like this `world`, with news items exchanged between gossip
agents."

View file

@ -1,6 +1,7 @@
(ns the-great-game.merchants.markets
"Adjusting quantities and prices in markets."
(:require [the-great-game.utils :refer [deep-merge]]
(:require [taoensso.timbre :as l :refer [info error]]
[the-great-game.utils :refer [deep-merge]]
[the-great-game.world.world :refer [actual-price default-world]]))
(defn new-price
@ -38,12 +39,15 @@
(> su st)
(- su st)
true 0)
price (new-price p st su d)]
n (new-price p st su d)]
(if
(not (= p n))
(l/info "Price of " commodity " at " id " has changed from " (float p) " to " (float n)))
{:cities {id
{:stock
{commodity (+ (- st decrement) increment)}
:prices
{commodity price}}}}))
{commodity n}}}}))
(defn update-markets

View file

@ -2,6 +2,7 @@
"Trade planning for merchants, primarily."
(:require [taoensso.timbre :as l :refer [info error]]
[the-great-game.utils :refer [deep-merge]]
[the-great-game.gossip.gossip :refer [move-gossip]]
[the-great-game.world.routes :refer [find-route]]
[the-great-game.world.world :refer [actual-price default-world]]))
@ -309,16 +310,19 @@
:cash (+ (:cash market) p)}}})
;; if no plan, then if at home stay put
(= (:location m) (:home m))
{}
(do
(l/info "Merchant " id " remains at home in " location)
{})
;; else move towards home
true
(let [route (find-route world location (:home m))
next-location (nth route 1)]
(l/info "No trade possible at " location "; merchant " id " moves to " next-location)
{:merchants
(merge
{:merchants
{id
{:location next-location}}})))))
{:location next-location}}}
(move-gossip id world next-location)))))))
(defn re-plan
"Having failed to sell a cargo at current location, re-plan a route to
@ -338,7 +342,6 @@
{id
{:plan plan}}})))
(defn sell-and-buy
"Return a new world like this `world`, in which this `merchant` has sold
their current stock in their current location, and planned a new trade, and
@ -381,7 +384,6 @@
;; else
(re-plan merchant world))))
(defn move-merchant
"Handle general en route movement of this `merchant` in this `world`."
[merchant world]
@ -396,18 +398,23 @@
next-location (if plan
(nth 1 (find-route world (:location m) (:destination plan)))
(:location m))]
(l/info "Merchant " id " at " (:location m))
(cond at-destination?
(sell-and-buy merchant world plan)
(nil? (:plan m))
(plan-and-buy merchant world)
true
{:merchants
{id
{:id id
:location next-location
:known-prices (add-known-prices m world)}}})))
(l/info "Merchant " id " has moved from " (:location m) " to " next-location)
(cond
;; if the merchant is at the destination of their current plan
;; sell all cargo and repurchase.
at-destination?
(sell-and-buy merchant world plan)
;; if they don't have a plan, seek to create one
(nil? (:plan m))
(plan-and-buy merchant world)
;; otherwise, move one step towards their destination
true
(deep-merge
{:merchants
{id
{:location next-location
:known-prices (add-known-prices m world)}}}
(move-gossip id world next-location)))))
(defn run
"Return a world like this `world`, but with each merchant moved."

View file

@ -185,6 +185,6 @@
(defn run
"Return a world like this `world` with only the `:date` value updated
(incremented by one). For running other aspects of the simulation, see
[[the-great-game.world.run#var-run]]."
[[the-great-game.world.run]]."
[world]
(assoc world :date (inc (or (:date world) 0))))