Rule engine now running very relaibly with no errors showing.

This commit is contained in:
Simon Brooke 2014-07-20 15:14:10 +01:00
parent d23239a5c6
commit 3491784282
2 changed files with 48 additions and 9 deletions

View file

@ -33,7 +33,7 @@
* `map` a map;
* `key` a symbol or keyword, presumed to be a key into the `map`."
[map key]
(cond map
(cond (map? map)
(let [v (map key)]
(cond (and v (integer? v)) v
true 0))
@ -89,12 +89,17 @@
* `depth` an integer representing the distance from [x,y] that
should be searched;
* `property` a keyword representing a property of the neighbours;
* `value` a value of that property;
* `value` a value of that property (or, possibly, the name of another);
* `op` a comparator function to use in place of `=`.
It gets messy."
([world x y depth property value op]
(filter #(eval (list op (get % property) value)) (get-neighbours world x y depth)))
(filter
#(eval
(list op
(or (get % property) (get-int % property))
value))
(get-neighbours world x y depth)))
([world x y depth property value]
(get-neighbours-with-property-value world x y depth property value =))
([world cell depth property value]

View file

@ -1,8 +1,5 @@
(ns mw-engine.utils-test
(:use [mw-engine.core :as core]
[mw-engine.world :as world]
[mw-engine.heightmap :as height]
[mw-engine.natural-rules :as rules])
(:use [mw-engine.world :as world])
(:require [clojure.test :refer :all]
[mw-engine.utils :refer :all]))
@ -65,5 +62,42 @@
{ :altitude 72, :x 3, :y 2, }
{ :altitude 91, :x 4, :y 2, })))
(is (= (get-neighbours-with-property-value world 3 3 1 :altitude 100)
'({ :altitude 100, :x 3, :y 4, }))))))
'({ :altitude 100, :x 3, :y 4, }))))
(let [world '(({ :altitude 13, :x 0, :y 0, :deer 0}
{ :altitude 20, :x 1, :y 0, :deer 0}
{ :altitude 29, :x 2, :y 0, :deer 0}
{ :altitude 39, :x 3, :y 0, :deer 0}
{ :altitude 51, :x 4, :y 0, :deer 0})
({ :altitude 19, :x 0, :y 1, :deer 0}
{ :altitude 29, :x 1, :y 1, :deer 0}
{ :altitude 41, :x 2, :y 1, :deer 0}
{ :altitude 55, :x 3, :y 1, :deer 0}
{ :altitude 72, :x 4, :y 1, :deer 0})
({ :altitude 27, :x 0, :y 2, :deer 0}
{ :altitude 41, :x 1, :y 2, :deer 0}
{ :altitude 55, :x 2, :y 2, :deer 0}
{ :altitude 72, :x 3, :y 2, :deer 2}
{ :altitude 91, :x 4, :y 2, :deer 0})
({ :altitude 33, :x 0, :y 3, :deer 0}
{ :altitude 47, :x 1, :y 3, :deer 0}
{ :altitude 68, :x 2, :y 3, :deer 4}
{ :altitude 91, :x 3, :y 3, :deer 0}
{ :altitude 111, :x 4, :y 3, :deer 4})
({ :altitude 36, :x 0, :y 4, :deer 0}
{ :altitude 53, :x 1, :y 4, :deer 0}
{ :altitude 75, :x 2, :y 4, }
{ :altitude 100, :x 3, :y 4, :deer 2}
{ :altitude 123, :x 4, :y 4,}))]
(is (= (get-neighbours-with-property-value world 3 3 1 :deer 2
>)
'({:altitude 68, :x 2, :y 3, :deer 4}
{:altitude 111, :x 4, :y 3, :deer 4})))
(is (= (get-neighbours-with-property-value world 3 3 1 :deer 2
<)
'({:altitude 55, :x 2, :y 2, :deer 0}
{:altitude 75, :x 2, :y 4}
{:altitude 91, :x 4, :y 2, :deer 0}
{:altitude 123, :x 4, :y 4})))
(is (= (get-neighbours-with-property-value world 3 3 1 :deer 2)
'({:altitude 72, :x 3, :y 2, :deer 2}
{:altitude 100, :x 3, :y 4, :deer 2}))))))