-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terminal.scm
171 lines (137 loc) · 4.92 KB
/
Terminal.scm
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
(module Terminal
(createScreen!
destroyScreen!
pushMsg!
askMsg
getKey
drawPlayer!
drawMap!
drawPath!)
(import chicken scheme)
(use srfi-1 srfi-13 srfi-25 extras ncurses)
(define log '())
(define mapWin)
(define logWin)
(define statWin)
(define (createScreen!)
(initscr)
(curs_set 0)
(noecho)
(let* ((mapWinWidth 50)
(mapWinHeight 20)
(statWinWidth 10)
(logWinHeight 3)
(logWinWidth mapWinWidth)
(statWinHeight (+ mapWinHeight logWinHeight)))
(let-values (((termHeight termWidth) (getmaxyx stdscr)))
(if (or (< termWidth logWinWidth) (< termHeight statWinHeight))
(destroyScreen! (format #f "Terminal size (~a, ~a) too small!\n" termWidth termHeight))
(if (not (has_colors))
(destroyScreen! (format #f "Terminal doesn't support colors"))
(begin
(start_color)
(init_pair 1 COLOR_RED COLOR_BLACK)
(init_pair 2 COLOR_YELLOW COLOR_WHITE)
(init_pair 3 COLOR_BLUE COLOR_BLACK)
(init_pair 4 COLOR_YELLOW COLOR_BLACK)
(init_pair 5 COLOR_BLACK COLOR_WHITE)
(set! mapWin (newwin mapWinHeight mapWinWidth 0 0))
(set! logWin (newwin logWinHeight logWinWidth mapWinHeight 0))
(set! statWin (newwin statWinHeight statWinWidth 0 mapWinWidth))))))))
(define (destroyScreen! msg)
(endwin)
(format #t "~a\n" msg))
(define (getKey)
(wgetch mapWin))
(define (pushMsg! msg)
(define (push! e)
(begin
(when (= (length log) 3)
(pull!))
(set! log (cons e log))))
(define (pull!)
(let ((l (last log)))
(begin
(drop-right! log 1)
l)))
(begin
(if (eq? log '())
(push! `(,msg 1))
(if (string=? msg (first (first log)))
(set-car! log `(,msg ,(+ (second (first log)) 1)))
(push! `(,msg 1))))
(wclear logWin)
(let ((y 0))
(for-each (lambda (msgPair)
(let* ((m (first msgPair)) (n (second msgPair))
(str (if (> n 1) (format #f "~a (x~a)" m n) (format #f "~a" m))))
(mvwaddstr logWin y 0 str)
(set! y (+ y 1))))
log))
(wrefresh logWin)))
(define (mvwgetstr win y x) ;there is a bug with the original version, so I rewrote it using getch
(wmove win y x)
(let loop ((str ""))
(let ((ch (wgetch win)))
(if (eq? (char->integer ch ) 10) ;10 is the ENTER KEY code
str
(if (or (>= (char->integer ch) 127) (< (char->integer ch) 32))
(loop str)
(if (= (char->integer ch) 8)
(loop (string-take str (- (string-length str) 1)))
(loop (string-append str (string ch)))))))))
(define (askMsg msg)
(let ((answer ""))
(echo)
(curs_set 1)
(pushMsg! msg)
(set! answer (mvwgetstr logWin 0 (string-length msg)))
(curs_set 0)
(noecho)
(set-car! log `(,(string-append msg answer) 1))
answer))
(define (drawPlayer!)
(let-values (((height width) (getmaxyx mapWin)))
(wattron mapWin (COLOR_PAIR 3))
(mvwaddch mapWin (quotient height 2) (quotient width 2) #\@)
(wattroff mapWin (COLOR_PAIR 3)))
(wrefresh mapWin)
)
(define (drawMap! dungeon fov x y dungeonWidth dungeonHeight)
(wclear mapWin)
(let-values (((mapWinHeight mapWinWidth) (getmaxyx mapWin)))
(for-each (lambda (yScreen)
(for-each (lambda (xScreen)
(let ((xMap (+ x (- xScreen (quotient mapWinWidth 2))))
(yMap (+ y (- yScreen (quotient mapWinHeight 2)))))
(when (and (< xMap dungeonWidth) (< yMap dungeonHeight)
(> xMap 0) (> yMap 0))
(when (eq? (array-ref dungeon xMap yMap) #\#)
(wattron mapWin (COLOR_PAIR 5)))
(when (array-ref fov xMap yMap)
(wattron mapWin A_BOLD)
;(when (eq? (array-ref dungeon xMap yMap) #\#)
; (wattron mapWin (COLOR_PAIR 2)))
(when (eq? (array-ref dungeon xMap yMap) #\.)
(wattron mapWin (COLOR_PAIR 4))))
(mvwaddch mapWin yScreen xScreen (array-ref dungeon xMap yMap))
(wattroff mapWin A_BOLD)
(wattroff mapWin (COLOR_PAIR 4))
(wattroff mapWin (COLOR_PAIR 5))
(wattroff mapWin (COLOR_PAIR 2)))))
(iota (- mapWinWidth 1))))
(iota (- mapWinHeight 1))))
(wrefresh mapWin))
(define (drawPath! dungeon path x y) ;temporanea
(when (not (null? path))
(let-values (((mapWinHeight mapWinWidth) (getmaxyx mapWin)))
(let* ((cell (car path)) (xMap (first cell)) (yMap (second cell))
(xScreen (- (+ xMap (quotient mapWinWidth 2)) x))
(yScreen (- (+ yMap (quotient mapWinHeight 2)) y)))
(when (and (>= xScreen 0) (>= yScreen 0) (< xScreen (- mapWinWidth 1)) (< yScreen (- mapWinHeight 1)))
(wattron mapWin (COLOR_PAIR 1))
(mvwaddch mapWin yScreen xScreen #\o)
(wattroff mapWin (COLOR_PAIR 1))
)
(drawPath! dungeon (cdr path) x y)))))
)