blob: b1774d56b3abcf81902de9381e3c97f493bb9760 (
plain)
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
|
(defpackage game
(:use :cl)
(:export :new-game
:buy-item
:sell-item
:change-zone
:get-profit
:get-profit-in-zone
:get-cur-zone
:get-zone
:commodities-panel
:player-stats))
(in-package :game)
(defclass game ()
((player :initarg :player
:accessor player)
(zones :initarg :zones
:accessor zones)
(cur-zone :initarg :start-zone
:accessor cur-zone
:reader get-cur-zone)))
(defun new-game (player start-zone zones)
(make-instance 'game
:player player
:zones zones
:start-zone start-zone))
(defmethod buy-item ((g game) name amount)
(with-slots (player cur-zone) g
(let ((item (zone:get-commodity cur-zone name)))
(when item
(player:buy-item player item amount)))))
(defmethod sell-item ((g game) name amount)
(with-slots (player cur-zone) g
(let ((item (zone:get-commodity cur-zone name)))
(when item
(player:sell-item player item amount)))))
(defmethod change-zone ((g game) name)
(with-slots (cur-zone zones) g
(let ((z (find-if (lambda (z) (string= name
(zone:name z)))
zones)))
(when z
(zone:update-zone cur-zone)
(setq cur-zone z)))))
(defmethod get-profit-in-zone ((g game) name zone)
(with-slots (player) g
(let ((player-item (car (player:find-item player name)))
(zone-item (zone:get-commodity zone name)))
(if (and player-item zone-item)
(- (commodities:price zone-item)
(commodities:price player-item))
0))))
(defmethod get-profit ((g game) name)
(with-slots (cur-zone) g
(get-profit-in-zone g name cur-zone)))
(defmethod get-zone ((g game) zone)
(with-slots (zones) g
(find-if (lambda (x) (string= (zone:name x) zone))
zones)))
(defmethod commodities-panel ((g game) zone)
(with-slots (player) g
(mapcar (lambda (c)
(let* ((name (commodities:name c))
(price (commodities:price c))
(player-item (player:find-item player name))
(quantity (if player-item (cdr player-item) 0))
(profit (if player-item
(- price (commodities:price (car player-item)))
0)))
(list :name name
:price price
:qty quantity
:profit profit)))
(zone:get-commodities zone))))
(defmethod player-stats ((g game))
(with-slots (player) g
(list :cash (player:get-cash player)
:debt (player:get-debt player)
:inv (player:inventory-stats player))))
|