-
-
-;;; Events Engine
-
-(define def-mobs-event #f)
-(define run-mobs-events #f)
-(define clear-events-data #f)
-
-(define mobs-events (make-hash-table))
-(define returned-data (make-hash-table))
-
-(let ((nop #f))
- (set! def-mobs-event
- (lambda (pair fun)
- (cond ((not fun)
- (hash-remove! mobs-events pair))
- (else
- (hash-set! mobs-events pair fun)))))
-
- (set! run-mobs-events
- (lambda* (#:optional (mobs (get-active-mobs)))
- (hash-for-each
- (lambda (types fun)
- (let* ((t1 (car types)) (t2 (cadr types))
- (mobs-t1 (filter (lambda (m) (eq? (m 'get-type) t1)) mobs))
- (mobs-t2 (filter (lambda (m) (eq? (m 'get-type) t2)) mobs)))
- (cond ((not (or (null? mobs-t1) (null? mobs-t2)))
- (for-each
- (lambda (m1)
- (let ((id1 (m1 'get-mob-id)))
- (for-each
- (lambda (m2)
- (let ((id2 (m2 'get-mob-id)))
- (cond ((not (eq? id1 id2))
- (let ((res (catch #t
- (lambda () (fun (m1 'get-data) (m2 'get-data)))
- (lambda (key . args) #f))))
- (cond ((and (list? res) (>= (length res) 2))
- (return-data id1 t2 (car res))
- (return-data id2 t1 (cadr res)))))))))
- mobs-t2)))
- mobs-t1)))))
- mobs-events)))
-
-
- (define (return-data mob-id mob-type data)
- (let* ((key (list mob-id mob-type))
- (res (hash-ref returned-data key)))
- (hash-set! returned-data key
- (cond (res (cons data res))
- (else (list data))))))
-
- (set! clear-events-data
- (lambda ()
- (hash-clear! returned-data))))
-
-(define-macro (define-mobs-event types-pair . body)
- `(def-mobs-event
- ',types-pair
- ,(cond ((null? body) #f)
- (else `(lambda (,(car types-pair) ,(cadr types-pair)) ,@body)))))