Move from github, collapse gltk and strictmath, add candle
[clnl] / src / main / clnl / nvm / agent.lisp
1 (in-package #:clnl-nvm)
2
3 (defun agent-value (var &optional (agent *self*))
4  "AGENT-VALUE VAR &optional AGENT => RESULT
5
6 ARGUMENTS AND VALUES:
7
8   VAR: A variable name
9   AGENT: an agent, defaulting to *self*
10   RESULT: the value of VAR
11
12 DESCRIPTION:
13
14   AGENT-VALUE is the general agent variable access function.  For many
15   NetLogo reporters, the compilation results is AGENT-VALUE.  The list of
16   valid values are any builtin variable in the NetLogo dictionary, as well
17   as any *-own variable.
18
19   See http://ccl.northwestern.edu/netlogo/docs/dictionary.html for builtins"
20  (agent-value-inner agent (intern (string-upcase var) :keyword)))
21
22 (defsetf agent-value (var &optional (agent '*self*)) (new-value)
23  `(set-agent-value-inner ,agent ,var ,new-value))
24
25 (defgeneric set-agent-value-inner (agent var new-value))
26 (defgeneric agent-value-inner (agent var))
27
28 (defmacro defagent-value (type symb &optional accessor)
29  (let
30   ((accessor (or accessor (intern (format nil "~A-~A" type symb))))
31    (agent (gensym))
32    (var (gensym))
33    (new-val (gensym)))
34   `(progn
35     (defmethod agent-value-inner ((,agent ,type) (,var (eql ,symb))) (,accessor ,agent))
36     (defmethod set-agent-value-inner ((,agent ,type) (,var (eql ,symb)) ,new-val) (setf (,accessor ,agent) ,new-val)))))
37
38 ; Don't want the setter for :who
39 (defmethod agent-value-inner ((turtle turtle) (var (eql :who))) (turtle-who turtle))
40
41 (defmethod agent-value-inner ((turtle turtle) (var (eql :pcolor)))
42  (patch-color (patch-at (turtle-xcor turtle) (turtle-ycor turtle))))
43
44 (defmethod set-agent-value-inner ((turtle turtle) (var (eql :pcolor)) new-val)
45  (setf (patch-color (patch-at (turtle-xcor turtle) (turtle-ycor turtle))) new-val))
46
47 (defagent-value patch :pcolor patch-color)
48
49 (defagent-value turtle :color)
50 (defagent-value turtle :label)
51 (defagent-value turtle :label-color)
52 (defagent-value turtle :size)
53
54 (defmethod agent-value-inner ((turtle turtle) var)
55  (when (not (find var *turtles-own-vars*)) (error "~S is not a turtle variable" var))
56  (or (getf (turtle-own-vars turtle) var) 0d0))
57
58 (defmethod set-agent-value-inner ((turtle turtle) var new-val)
59  (when (not (find var *turtles-own-vars*)) (error "~S is not a turtle variable" var))
60  (if (getf (turtle-own-vars turtle) var)
61   (setf (getf (turtle-own-vars turtle) var) new-val)
62   (setf (turtle-own-vars turtle) (append (list var new-val) (turtle-own-vars turtle)))))
63
64 (defmethod agent-value-inner ((patch patch) var)
65  (when (not (find var *patches-own-vars*)) (error "~S is not a patch variable" var))
66  (or (getf (patch-own-vars patch) var) 0d0))
67
68 (defmethod set-agent-value-inner ((patch patch) var new-val)
69  (when (not (find var *patches-own-vars*)) (error "~S is not a patch variable" var))
70  (if (getf (patch-own-vars patch) var)
71   (setf (getf (patch-own-vars patch) var) new-val)
72   (setf (patch-own-vars patch) (append (list var new-val) (patch-own-vars patch)))))