Adjust emitters to not produce side-effects in 'wire-state' method.

This commit is contained in:
David Thompson 2024-05-23 16:33:45 -04:00
parent a5e9011678
commit bed3604685

View file

@ -127,20 +127,19 @@
(match-lambda* (match-lambda*
(('type) 'emitter) (('type) 'emitter)
(('position) position) (('position) position)
(('tick grid-info) ($ timer (+ ($ timer) 1))) (('tick grid-info)
(let ((t (modulo (+ ($ timer) 1) interval)))
($ timer t)
(when (= t 0)
($ grid-info 'append-event `(emit ,x ,y)))))
(('post-tick grid-info) #f) (('post-tick grid-info) #f)
(('enter obj grid-info) #f) (('enter obj grid-info) #f)
(('exit obj grid-info) #f) (('exit obj grid-info) #f)
(('wire-state grid-info) (('wire-state grid-info)
(let ((t ($ timer))) (match ($ timer)
(cond (0 'electron-head)
((= (modulo t interval) 0) (1 'electron-tail)
($ grid-info 'append-event `(emit ,x ,y)) (_ 'copper)))
'electron-head)
((= (modulo t interval) 1)
'electron-tail)
(else
'copper))))
(('update-wire-state grid-info) #f) (('update-wire-state grid-info) #f)
(('alive?) #t) (('alive?) #t)
(('describe) `(clock-emitter ,position)) (('describe) `(clock-emitter ,position))
@ -155,27 +154,26 @@
(('position) position) (('position) position)
(('tick grid-info) (('tick grid-info)
(when ($ on?) (when ($ on?)
($ timer (+ ($ timer) 1)))) (let ((t (modulo (+ ($ timer) 1) interval)))
($ timer t)
(when (= t 0)
($ grid-info 'append-event `(emit ,x ,y))))))
(('post-tick grid-info) #f) (('post-tick grid-info) #f)
(('enter obj grid-info) #f) (('enter obj grid-info) #f)
(('exit obj grid-info) #f) (('exit obj grid-info) #f)
(('activate grid-info) (('activate grid-info)
($ on? #t) ($ on? #t)
($ timer 0) ($ timer -1)
($ grid-info 'append-event `(emitter-on ,x ,y))) ($ grid-info 'append-event `(emitter-on ,x ,y)))
(('deactivate grid-info) (('deactivate grid-info)
($ on? #f) ($ on? #f)
($ grid-info 'append-event `(emitter-off ,x ,y))) ($ grid-info 'append-event `(emitter-off ,x ,y)))
(('wire-state grid-info) (('wire-state grid-info)
(when ($ on?) (and ($ on?)
(let ((t ($ timer))) (match ($ timer)
(cond (0 'electron-head)
((= (modulo t interval) 0) (1 'electron-tail)
'electron-head) (_ 'copper))))
((= (modulo t interval) 1)
'electron-tail)
(else
'copper)))))
(('update-wire-state grid-info) #f) (('update-wire-state grid-info) #f)
(('alive?) #t) (('alive?) #t)
(('on?) ($ on?)) (('on?) ($ on?))