This repository has been archived by the owner on Nov 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-only.txt
419 lines (400 loc) · 10.7 KB
/
auto-only.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
;; Calculate win
(deffunction calculate-win (?ascore ?cscore)
(bind ?advantage (- ?ascore ?cscore)) ;; difference in scores
(if (>= ?advantage 2) then (bind ?result win))
(if (<= ?advantage -2) then (bind ?result loss))
(if (eq ?advantage 0) then (bind ?result draw))
(if (eq ?advantage 1) then
(bind ?luck (mod (random) 2))
(if (eq ?luck 1) then (bind ?result win)
else (bind ?result draw)
)
)
(if (eq ?advantage -1) then
(bind ?luck (mod (random) 2))
(if (eq ?luck 0) then (bind ?result loss)
else (bind ?result draw)
)
)
(return ?result)
)
;; Creature template
(deftemplate creature
(slot name)
(slot weapon)
(slot speed)
(slot intelligence)
(slot attitude)
)
;; Player Stats and Items
(deffacts adventurer
(creature
(name Adventurer)
(weapon 2)
(speed 3)
(intelligence 4)
)
;; Gadget that should be removed after used
;; Effect +2weapon +3speed +2int
(specials flashgun jetpack textbook)
)
;; Reset stats after using a special
(defrule reset-stats
(declare (salience 200))
?adventurer <- (creature (name Adventurer)
(weapon ?wep)
(speed ?speed)
(intelligence ?int)
)
?u <- (just-use ?gadget)
(not (creature (name ~Adventurer)))
=>
(printout t "Your stats have reset back to normal." crlf crlf)
(retract ?u)
(if (eq ?gadget flashgun)
then
(modify ?adventurer (weapon (- ?wep 2)))
)
(if (eq ?gadget jetpack)
then
(modify ?adventurer (speed (- ?speed 3)))
)
(if (eq ?gadget textbook)
then
(modify ?adventurer (intelligence (- ?int 2)))
)
)
;; Turn
(deffacts game-facts
(turn-counter 0)
)
;; Generate creature
(defrule gen-creature
(not (creature (name ~Adventurer)))
(turn-counter ?n) ;; updating this will refresh this rule
=>
(bind ?wep (+ 1 (mod (random) 5)))
(bind ?speed (+ 1 (mod (random) 5)))
(bind ?int (+ 1 (mod (random) 5)))
(bind ?att (+ 1 (mod (random) 3)))
(if (eq ?att 1)
then (bind ?atttext scared))
(if (eq ?att 2)
then (bind ?atttext curious))
(if (eq ?att 3)
then (bind ?atttext aggressive))
(assert (creature
(name (random))
(weapon ?wep)
(speed ?speed)
(intelligence ?int)
(attitude ?atttext)))
;;; The next lines are all for user interface purposes
(bind ?weptypeno (+ 1 (mod (random) 3)))
;; Weapon description
(if (eq ?weptypeno 1)
then (bind ?weptype teeth))
(if (eq ?weptypeno 2)
then (bind ?weptype claws))
(if (eq ?weptypeno 3)
then (bind ?weptype rocks))
(if (eq ?wep 1)
then (bind ?weptext "very blunt"))
(if (eq ?wep 2)
then (bind ?weptext "blunt"))
(if (eq ?wep 3)
then (bind ?weptext "fairly sharp"))
(if (eq ?wep 4)
then (bind ?weptext "sharp"))
(if (eq ?wep 5)
then (bind ?weptext "razor sharp"))
;; Speed description
(if (eq ?speed 1)
then (bind ?speedtext "very slow"))
(if (eq ?speed 2)
then (bind ?speedtext "slow"))
(if (eq ?speed 3)
then (bind ?speedtext "fairly fast"))
(if (eq ?speed 4)
then (bind ?speedtext "fast"))
(if (eq ?speed 5)
then (bind ?speedtext "lightning quick"))
;; Intelligence description
(if (eq ?int 1)
then (bind ?inttext "very dumb"))
(if (eq ?int 2)
then (bind ?inttext "dumb"))
(if (eq ?int 3)
then (bind ?inttext "fairly smart"))
(if (eq ?int 4)
then (bind ?inttext "smart"))
(if (eq ?int 5)
then (bind ?inttext "really smart"))
;; Monster description
(printout t "A creature appears!" crlf
"It has " ?weptext " " ?weptype "." crlf
"It looks " ?speedtext " and " ?inttext "." crlf
"It acts in a " ?atttext " manner." crlf crlf)
)
;; Use gadget
(defrule use-gadget
(declare (salience 190))
?adventurer <- (creature (name Adventurer)
(weapon ?wep)
(speed ?speed)
(intelligence ?int)
)
?u <- (use ?gadget)
?s <- (specials $?first ?gadget $?last)
=>
(retract ?u)
(retract ?s)
(assert (just-use ?gadget))
(printout t "You used " ?gadget "." crlf crlf)
(assert (specials $?first $?last))
(if (eq ?gadget flashgun)
then (modify ?adventurer (weapon (+ ?wep 2))))
(if (eq ?gadget jetpack)
then (modify ?adventurer (speed (+ ?speed 3))))
(if (eq ?gadget textbook)
then (modify ?adventurer (intelligence (+ ?int 2))))
)
;; Fight
(defrule fight
?turn <- (turn-counter ?n)
(creature (name Adventurer)
(weapon ?aWeapon))
?c <- (creature (name ~Adventurer)
(weapon ?cWeapon))
?a <- (action fight)
=>
(retract ?a)
(bind ?result (calculate-win ?aWeapon ?cWeapon))
(if (eq ?result win)
then (printout t "You killed the creature." crlf crlf))
(if (eq ?result draw)
then (printout t "It’s a draw and the creature ran away." crlf crlf))
(if (eq ?result loss)
then (printout t "The creature killed you." crlf
"You lasted " ?n " turns." crlf crlf)
(halt))
(if (neq ?result loss)
then
(retract ?turn)
(retract ?c)
(assert (turn-counter (+ ?n 1)))
)
)
;; Flee
(defrule flee
?turn <- (turn-counter ?n)
(creature (name Adventurer)
(speed ?aSpeed))
?c <- (creature (name ~Adventurer)
(speed ?cSpeed))
?a <- (action flee)
=>
(retract ?a)
(bind ?result (- ?aSpeed ?cSpeed))
(if (> ?result 0)
then (printout t "You ran away." crlf crlf))
(if (eq ?result 0)
then (printout t "It’s a draw and the creature ran away." crlf crlf))
(if (< ?result 0)
then (printout t "The creature caught and killed you." crlf
"You lasted " ?n " turns." crlf crlf)
(halt))
(if (>= ?result 0)
then
(retract ?turn)
(retract ?c)
(assert (turn-counter (+ ?n 1)))
)
)
;; Hypnotise
(defrule hypnotise
?turn <- (turn-counter ?n)
(creature (name Adventurer)
(intelligence ?aInt))
?c <- (creature (name ~Adventurer)
(intelligence ?cInt))
?a <- (action hypnotise)
=>
(retract ?a)
(bind ?result (calculate-win ?aInt ?cInt))
(if (eq ?result win)
then (printout t "The creature falls asleep." crlf crlf))
(if (eq ?result draw)
then (printout t "It’s a draw and the creature runs away." crlf crlf))
(if (eq ?result loss)
then (printout t "The creature hypnotised and killed you." crlf
"You lasted " ?n " turns." crlf crlf)
(halt))
(if (neq ?result loss)
then
(retract ?turn)
(retract ?c)
(assert (turn-counter (+ ?n 1)))
)
)
;; Do nothing
(defrule do_nothing
?turn <- (turn-counter ?n)
(creature (name Adventurer))
?c <- (creature (name ~Adventurer)
(attitude ?att))
?a <- (action do_nothing)
=>
(retract ?a)
(if (eq ?att scared)
then (printout t "The " ?att " creature ran away." crlf crlf))
(if (eq ?att curious)
then (printout t "The " ?att " creature watches you pass by." crlf crlf))
(if (eq ?att aggressive)
then (printout t "The " ?att " creature killed you." crlf
"You lasted " ?n " turns." crlf crlf)
(halt))
(if (neq ?att aggressive)
then
(retract ?turn)
(retract ?c)
(assert (turn-counter (+ ?n 1)))
)
)
;;; AI pattern matching
;; AI behaviour 1
;; Do nothing when fight a scared or curious creature
(defrule ai-behaviour1
(creature (name ~Adventurer)
(attitude scared|curious))
(not (action ?)) ;; Stop the rule from firing
=>
(printout t "Behaviour 1: Do nothing." crlf crlf)
(assert (action do_nothing))
)
;; AI behaviour 2: when creature is aggressive but weak
;; Rule 1: Hypnotise dumb creature
(defrule ai-behaviour2-rule1
(declare (salience 180))
(creature (name Adventurer)
(intelligence ?aInt))
(creature (name ~Adventurer)
(intelligence ?cInt)
(attitude aggressive))
(test (>= ?aInt ?cInt))
(not (action ?))
=>
(printout t "Behaviour 2 - rule 1: Hypnotise." crlf crlf)
(assert (action hypnotise))
)
;; Rule 2: Run away from slower creature
(defrule ai-behaviour2-rule2
(declare (salience 180))
(creature (name Adventurer)
(speed ?aSpeed))
?c <- (creature (name ~Adventurer)
(speed ?cSpeed)
(attitude aggressive))
(test (>= ?aSpeed ?cSpeed))
(not (action ?))
=>
(printout t "Behaviour 2 - rule 2: Flee." crlf crlf)
(assert (action flee))
)
;; Rule 3: Kill weaker creature
(defrule ai-behaviour2-rule3
(declare (salience 180))
(creature (name Adventurer)
(weapon ?aWeapon))
(creature (name ~Adventurer)
(weapon ?cWeapon)
(attitude aggressive))
(test (>= ?aWeapon ?cWeapon))
(not (action ?))
=>
(printout t "Behaviour 2 - rule 3: Fight." crlf crlf)
(assert (action fight))
)
;; AI behaviour 3: when creature is aggressive and strong
;; Rule 1: if the creature can be killed after using flashgun
(defrule ai-behaviour3-rule1
(declare (salience 150))
(creature (name Adventurer)
(weapon ?aWeapon))
(creature (name ~Adventurer)
(weapon ?cWeapon)
(attitude aggressive))
(test (< ?aWeapon ?cWeapon))
(test (>= (+ ?aWeapon 2) ?cWeapon))
(specials $?first flashgun $?last)
(not (action ?))
(not (use ?))
=>
(printout t "Behaviour 3 - rule 1: Use flashgun." crlf crlf)
(assert (use flashgun))
)
;; Rule 2: if adventurer can run away after using jetpack
(defrule ai-behaviour3-rule2
(declare (salience 150))
(creature (name Adventurer)
(speed ?aSpeed))
?c <- (creature (name ~Adventurer)
(speed ?cSpeed)
(attitude aggressive))
(test (< ?aSpeed ?cSpeed))
(test (>= (+ ?aSpeed 3) ?cSpeed))
(specials $?first jetpack $?last)
(not (action ?))
(not (use ?))
=>
(printout t "Behaviour 3 - rule 2: Use jetpack." crlf crlf)
(assert (use jetpack))
)
;; Rule 3: if the creature can be hypnotised after using textbook
(defrule ai-behaviour3-rule3
(declare (salience 150))
(creature (name Adventurer)
(intelligence ?aInt))
(creature (name ~Adventurer)
(intelligence ?cInt)
(attitude aggressive))
(test (< ?aInt ?cInt))
(test (>= (+ ?aInt 2) ?cInt))
(specials $?first textbook $?last)
(not (action ?))
(not (use ?))
=>
(printout t "Behaviour 3 - rule 3: Use textbook." crlf crlf)
(assert (use textbook))
)
;; AI behaviour 4: when the aggressive creature is too strong
;; so the AI will try to get the 50% draw
;; from -1 score on Int
;; and try Hypnotise
(defrule ai-behaviour4
(declare (salience 120))
(creature (name Adventurer)
(intelligence ?aInt))
(creature (name ~Adventurer)
(intelligence ?cInt)
(attitude aggressive))
(test (eq (- ?aInt ?cInt) -1))
(not (action ?))
(not (specials $?first textbook $?last))
=>
(printout t "Behaviour 4: Try Hypnotise (50% draw or lose)." crlf crlf)
(assert (action hypnotise))
)
;; AI behaviour 5: when there is no solution
;; This behaviour is rarely fired
;; because the game usually stop at behaviour4-rule1
(defrule ai-behaviour5
(declare (salience 50))
(creature (name ~Adventurer)
(attitude aggressive))
(not (action ?))
(not (use ?))
=>
(printout t "Behaviour 5: Give up." crlf crlf)
(assert (action do_nothing))
)