001 (ns the-great-game.gossip.news-items
002 "Categories of news events interesting to gossip agents"
003 (:require [the-great-game.world.location :refer [distance-between]]
004 [the-great-game.time :refer [now]]))
005
006 ;; The ideas here are based on the essay 'The spread of knowledge in a large
007 ;; game world', q.v.; they've advanced a little beyond that and will doubtless
008 ;; advance further in the course of writing and debugging this namespace.
009
010 ;; A news item is a map with the keys:
011 ;;
012 ;; * `date` - the date on which the reported event happened;
013 ;; * `nth-hand` - the number of agents the news item has passed through;
014 ;; * `verb` - what it is that happened (key into `news-topics`);
015 ;;
016 ;; plus other keys taken from the `keys` value associated with the verb in
017 ;; `news-topics`
018
019 (def news-topics
020 "Topics of interest to gossip agents. Topics are keyed in this map by
021 their `verbs`. The `keys` associated with each topic are the extra pieces
022 of information required to give context to a gossip item. Generally:
023
024 * `actor` is the id of the character who it is reported performed the
025 action;
026 * `other` is the id of the character on whom it is reported the action
027 was performed;
028 * `location` is the place at which the action was performed;
029 * `object` is an object (or possibly list of objects?) relevant to the
030 action;
031 * `price` is special to buy/sell, but of significant interest to merchants.
032
033 #### Notes:
034
035 ##### Characters:
036
037 *TODO* but note that at most all the receiver can learn about a character
038 from a news item is what the giver knows about that character, degraded by
039 what the receiver finds interesting about them. If we just pass the id here,
040 then either the receiver knows everything in the database about the
041 character, or else the receiver knows nothing at all about the character.
042 Neither is desirable. Further thought needed.
043
044 ##### Locations:
045
046 A 'location' value is a list comprising at most the x/y coordinate location
047 and the ids of the settlement and region (possibly hierarchically) that contain
048 the location. If the x/y is not local to the home of the receiving agent, they
049 won't remember it and won't pass it on; if any of the ids are not interesting
050 So location information will degrade progressively as the item is passed along.
051
052 It is assumed that the `:home` of a character is a location in this sense.
053
054 ##### Inferences:
055
056 If an agent learns that Adam has married Betty, they can infer that Betty has
057 married Adam; if they learn that Charles killed Dorothy, that Dorothy has died.
058 I'm not convinced that my representation of inferences here is ideal.
059 "
060 { ;; A significant attack is interesting whether or not it leads to deaths
061 :attack {:verb :attack :keys [:actor :other :location]}
062 ;; Deaths of characters may be interesting
063 :die {:verb :attack :keys [:actor :location]}
064 ;; Deliberate killings are interesting.
065 :kill {:verb :kill :keys [:actor :other :location]
066 :inferences [{:verb :die :actor :other :other :nil}]}
067 ;; Marriages may be interesting
068 :marry {:verb :marry :keys [:actor :other :location]
069 :inferences [{:verb :marry :actor :other :other :actor}]}
070 ;; The end of ongoing open conflict between to characters may be interesting
071 :peace {:verb :peace :keys [:actor :other :location]
072 :inferences [{:verb :peace :actor :other :other :actor}]}
073 ;; Things related to the plot are interesting, but will require special
074 ;; handling. Extra keys may be required by particular plot events.
075 :plot {:verb :plot :keys [:actor :other :object :location]}
076 ;; Rapes are interesting.
077 :rape {:verb :rape :keys [:actor :other :location]
078 ;; Should you also infer from rape that actor is male and adult?
079 :inferences [{:verb :attack}
080 {:verb :sex}
081 {:verb :sex :actor :other :other :actor}]}
082 ;; Merchants, especially, are interested in prices in other markets
083 :sell {:verb :sell :keys [:actor :other :object :location :price]}
084 ;; Sex can juicy gossip, although not normally if the participants are in an
085 ;; established sexual relationship.
086 :sex {:verb :sex :keys [:actor :other :location]
087 :inferences [{:verb :sex :actor :other :other :actor}]}
088 ;; Thefts are interesting
089 :steal {:verb :steal :keys [:actor :other :object :location]}
090 ;; The succession of rulers is interesting; of respected craftsmen,
091 ;; potentially also interesting.
092 :succession {:verb :succession :keys [:actor :other :location :rank]}
093 ;; The start of ongoing open conflict between to characters may be interesting
094 :war {:verb :war :keys [:actor :other :location]
095 :inferences [{:verb :war :actor :other :other :actor}]}
096 })
097
098
099 (defn interest-in-character
100 "Integer representation of how interesting this `character` is to this
101 `gossip`.
102 *TODO:* this assumes that characters are passed as keywords, but, as
103 documented above, they probably have to be maps, to allow for degradation."
104 [gossip character]
105 (count
106 (concat
107 (filter #(= (:actor % character)) (:knowledge gossip))
108 (filter #(= (:other % character)) (:knowledge gossip)))))
109
110 (defn interesting-character?
111 "Boolean representation of whether this `character` is interesting to this
112 `gossip`."
113 [gossip character]
114 (> (interest-in-character gossip character) 0))
115
116 (defn interest-in-location
117 "Integer representation of how interesting this `location` is to this
118 `gossip`."
119 [gossip location]
120 (cond
121 (and (map? location) (number? (:x location)) (number? (:y location)))
122 (if-let [home (:home gossip)]
123 (let [d (distance-between location home)
124 i (/ 10000 d) ;; 10000 at metre scale is 10km; interest should
125 ;;fall of with distance from home, but possibly on a log scale
126 ]
127 (if (> i 1) i 0))
128 0)
129 (coll? location)
130 (reduce
131 +
132 (map
133 #(interest-in-location gossip %)
134 location))
135 :else
136 (count
137 (filter
138 #(some (fn [x] (= x location)) (:location %))
139 (:knowledge gossip)))))
140
141 (defn interesting-location?
142 "True if the location of this news `item` is interesting to this `gossip`."
143 [gossip item]
144 (> (interest-in-location gossip (:location item)) 1))
145
146 (defn interesting-object?
147 [gossip object]
148 ;; TODO: Not yet (really) implemented
149 true)
150
151 (defn interesting-topic?
152 [gossip topic]
153 ;; TODO: Not yet (really) implemented
154 true)
155
156 (defn interesting-item?
157 "True if anything about this news `item` is interesting to this `gossip`."
158 [gossip item]
159 (or
160 (interesting-character? gossip (:actor item))
161 (interesting-character? gossip (:other item))
162 (interesting-location? gossip (:location item))
163 (interesting-object? gossip (:object item))
164 (interesting-topic? gossip (:verb item))))
165
166 (defn infer
167 "Infer a new knowledge item from this `item`, following this `rule`"
168 [item rule]
169 (reduce merge
170 item
171 (cons
172 {:verb (:verb rule)}
173 (map (fn [k] {k (apply (k rule) (list item))})
174 (remove
175 #(= % :verb)
176 (keys rule))))))
177
178 (declare learn-news-item)
179
180 (defn make-all-inferences
181 "Return a list of knowledge entries inferred from this news `item` by this
182 `gossip`."
183 [item]
184 (set
185 (reduce
186 concat
187 (map
188 #(:knowledge (learn-news-item {} (infer item %) false))
189 (:inferences (news-topics (:verb item)))))))
190
191 (defn degrade-character
192 "Return a character specification like this `character`, but comprising
193 only those properties this `gossip` is interested in."
194 [gossip character]
195 ;; TODO: Not yet (really) implemented
196 character)
197
198 (defn degrade-location
199 "Return a location specification like this `location`, but comprising
200 only those elements this `gossip` is interested in. If none, return
201 `nil`."
202 [gossip location]
203 (let [l (if
204 (coll? location)
205 (filter
206 #(when (interesting-location? gossip %) %)
207 location))]
208 (when-not (empty? l) l)))
209
210 (defn learn-news-item
211 "Return a gossip like this `gossip`, which has learned this news `item` if
212 it is of interest to them."
213 ;; TODO: Not yet implemented
214 ([gossip item]
215 (learn-news-item gossip item true))
216 ([gossip item follow-inferences?]
217 (if
218 (interesting-item? gossip item)
219 (let [g (assoc gossip :knowledge
220 (cons
221 (assoc
222 item
223 :nth-hand (if
224 (number? (:nth-hand item))
225 (inc (:nth-hand item))
226 1)
227 :date (if (number? (:date item)) (:date item) (now))
228 :location (degrade-location gossip (:location item))
229 ;; ought to degratde the location
230 ;; ought to maybe-degrade characters we're not yet interested in
231 )
232 ;; ought not to add knowledge items we already have, except
233 ;; to replace if new item is of increased specificity
234 (:knowledge gossip)))]
235 (if follow-inferences?
236 (assoc
237 g
238 :knowledge
239 (concat (:knowledge g) (make-all-inferences item)))
240 g))
241 gossip)))
242
243
244