2009-09-28 08:57:29 +00:00
|
|
|
;; p l a n t e y e s [ copyright (c) 2009 foam vzw : gpl v3 ]
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
#lang scheme
|
2009-09-28 08:57:29 +00:00
|
|
|
(require scheme/class
|
|
|
|
fluxus-016/fluxus
|
|
|
|
"message.ss"
|
|
|
|
"list-utils.ss")
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(provide (all-defined-out))
|
|
|
|
|
|
|
|
(define branch-probability 6) ; as in one in branch-probability chance
|
|
|
|
(define branch-width-reduction 0.5)
|
|
|
|
(define twig-jitter 0.1)
|
|
|
|
(define branch-jitter 0.5)
|
2009-08-21 15:03:36 +00:00
|
|
|
(define start-twig-points 15)
|
2009-07-13 11:39:34 +00:00
|
|
|
(define start-twig-dist 0.05)
|
2009-08-15 08:03:28 +00:00
|
|
|
(define start-twig-width 0.1)
|
2009-09-25 16:19:48 +00:00
|
|
|
(define default-max-twigs 2)
|
2009-07-13 11:39:34 +00:00
|
|
|
(define default-scale-factor 1.05)
|
|
|
|
(define num-pickups 10)
|
|
|
|
(define pickup-dist-radius 200)
|
|
|
|
(define pickup-size 1)
|
2009-08-25 08:55:32 +00:00
|
|
|
(define ornament-grow-probability 2)
|
2009-07-13 11:39:34 +00:00
|
|
|
(define curl-amount 40)
|
|
|
|
(define start-size 50)
|
2009-08-19 10:29:01 +00:00
|
|
|
(define max-ornaments 10) ; per twig
|
2009-08-25 14:48:05 +00:00
|
|
|
(define nutrient-twig-size-increase 2)
|
2009-09-25 16:19:48 +00:00
|
|
|
(define num-worms 10)
|
|
|
|
(define num-spiders 10)
|
|
|
|
(define num-butterflies 10)
|
|
|
|
(define auto-twig-var 5)
|
|
|
|
(define auto-time 5)
|
|
|
|
(define pickup-check-prob 20)
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
; the base class logic object - all logic side objects can
|
|
|
|
; send messages to the render side at any time by calling add-message
|
|
|
|
; this takes care of the propagation of information. (not just oo fetish, I hope)
|
|
|
|
(define game-logic-object%
|
|
|
|
(class object%
|
|
|
|
(field
|
|
|
|
(messages '())
|
|
|
|
(children '()))
|
|
|
|
|
|
|
|
(define/public (send-message name data)
|
2009-07-30 15:03:21 +00:00
|
|
|
(set! messages (append messages (list (make-object message% name data)))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
; convert a list of lists in to just a single list - needed to convert
|
|
|
|
; the update lists into one big list of messages
|
|
|
|
(define (flatten l)
|
|
|
|
(cond
|
|
|
|
((null? l) '())
|
|
|
|
((list? (car l)) (append (flatten (car l)) (flatten (cdr l))))
|
|
|
|
(else (cons (car l) (flatten (cdr l))))))
|
|
|
|
|
2009-09-25 16:19:48 +00:00
|
|
|
(define/pubment (update t d) ; need to augement this if we have child logic objects,
|
|
|
|
(let ((l (inner '() update t d)) ; and call update on them too.
|
2009-08-19 16:16:48 +00:00
|
|
|
(m messages))
|
2009-07-13 11:39:34 +00:00
|
|
|
(set! messages '())
|
|
|
|
(append
|
|
|
|
m
|
2009-08-19 16:16:48 +00:00
|
|
|
(flatten l)))) ; the augmented method gets called here
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(super-new)))
|
|
|
|
|
2009-09-25 16:19:48 +00:00
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
(define insect-logic%
|
|
|
|
(class game-logic-object%
|
|
|
|
(init-field
|
|
|
|
(id 0)
|
|
|
|
(pos 0)
|
|
|
|
(type 'none)
|
|
|
|
(d (if (eq? type 'worm) (+ 20 (* 20 (rndf)))
|
|
|
|
(+ 10 (* 2 (rndf)))))) ; time to get from one place to another
|
|
|
|
|
|
|
|
(field
|
|
|
|
(next-update 0)
|
|
|
|
(centre (vector 0 0 0)))
|
|
|
|
|
|
|
|
(inherit send-message)
|
|
|
|
|
|
|
|
(define/public (get-id)
|
|
|
|
id)
|
|
|
|
|
|
|
|
(define/public (get-pos)
|
|
|
|
pos)
|
|
|
|
|
|
|
|
(define/public (get-type)
|
|
|
|
type)
|
|
|
|
|
|
|
|
(define/public (set-centre s)
|
|
|
|
(set! centre s))
|
|
|
|
|
|
|
|
(define (move)
|
|
|
|
; todo check stones
|
|
|
|
(let ((speed (if (eq? type 'worm) 5 50)))
|
|
|
|
(if (> (vdist pos centre) 100)
|
|
|
|
(set! pos (vadd pos (vmul (vnormalise (vsub centre pos)) speed)))
|
|
|
|
(set! pos (vadd pos (vmul (srndvec) speed))))
|
|
|
|
;(when (< (vdist pos centre) 12) (move))
|
|
|
|
(when (and (or (eq? type 'spider) (eq? type 'worm)) (> (vy pos) 0))
|
|
|
|
(set! pos (vector (vx pos) 0 (vz pos))))
|
|
|
|
(when (and (eq? type 'butterfly) (< (vy pos) 50))
|
|
|
|
(set! pos (vector (vx pos) 50 (vz pos))))))
|
|
|
|
|
|
|
|
(define/augment (update time delta)
|
|
|
|
(cond ((> time next-update)
|
|
|
|
(move)
|
|
|
|
; todo: drop stuff
|
|
|
|
;(when (zero? (random pickup-drop-probability))
|
|
|
|
; (send cell set-pickup! 'default))
|
|
|
|
(set! next-update (+ time d))
|
|
|
|
(send-message 'insect-move (list
|
|
|
|
(list 'insect-id id)
|
|
|
|
(list 'pos pos)
|
|
|
|
(list 'duration d)))))
|
|
|
|
'())
|
|
|
|
|
|
|
|
(super-new)))
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
; a twig, which can contain other twigs things.
|
|
|
|
; (roots and shoots are both twigs)
|
|
|
|
(define twig-logic%
|
|
|
|
(class game-logic-object%
|
|
|
|
(init-field
|
|
|
|
(last-point (vector 0 0 0))
|
|
|
|
(id #f) ; our id (for matching up with the renderer geometry)
|
|
|
|
(plant #f) ; the plant we belong to
|
|
|
|
(type 'root) ; or 'shoot
|
|
|
|
(dir (vector 0 1 0)) ; the general direction we are pointing in
|
|
|
|
(width 0) ; the width of this root
|
2009-08-21 15:03:36 +00:00
|
|
|
(num-points start-twig-points) ; number of points in this twig
|
2009-07-13 11:39:34 +00:00
|
|
|
(render-type 'extruded) ; the way to tell the view to render this twig
|
2009-07-27 08:26:41 +00:00
|
|
|
(dist start-twig-dist) ; distance between points
|
|
|
|
(parent-twig-id -1)
|
|
|
|
(parent-twig-point-index -1))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(field
|
|
|
|
(points '()) ; the 3d points for this twig
|
|
|
|
(widths '())
|
|
|
|
(twigs '()) ; children are stored with the point number they are connected to.
|
|
|
|
(ornaments '()) ; the things attached to this twig, an assoc list with point index
|
|
|
|
(w 0) ; the width of this segment
|
2009-08-21 15:03:36 +00:00
|
|
|
(curl (vmul (crndvec) curl-amount))) ; the angles to turn each point, if curly
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(inherit send-message)
|
|
|
|
|
|
|
|
(define/public (get-id)
|
|
|
|
id)
|
|
|
|
|
|
|
|
(define/public (set-id! s)
|
|
|
|
(set! id s))
|
|
|
|
|
|
|
|
(define/public (get-type)
|
|
|
|
type)
|
2009-07-30 15:03:21 +00:00
|
|
|
|
|
|
|
(define/public (get-dist)
|
|
|
|
dist)
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-08-25 14:48:05 +00:00
|
|
|
(define/public (get-w)
|
|
|
|
w)
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (get-dir)
|
|
|
|
dir)
|
|
|
|
|
|
|
|
(define/public (get-width)
|
|
|
|
width)
|
|
|
|
|
|
|
|
(define/public (get-num-points)
|
|
|
|
num-points)
|
|
|
|
|
|
|
|
(define/public (get-render-type)
|
|
|
|
render-type)
|
|
|
|
|
|
|
|
(define/public (get-point point-index)
|
|
|
|
(list-ref points point-index))
|
|
|
|
|
|
|
|
(define/public (get-length)
|
|
|
|
(length points))
|
|
|
|
|
|
|
|
(define/public (get-end-pos)
|
|
|
|
(if (not (null? points))
|
|
|
|
(list-ref points (- (get-length) 1))
|
|
|
|
#f))
|
|
|
|
|
2009-07-30 15:03:21 +00:00
|
|
|
(define/public (growing?)
|
|
|
|
(< (length points) num-points))
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (scale a)
|
|
|
|
(set! width (* width a))
|
|
|
|
(set! dist (* dist a)))
|
2009-07-30 15:03:21 +00:00
|
|
|
|
|
|
|
(define/public (grow pos)
|
|
|
|
(when (growing?)
|
2009-07-13 11:39:34 +00:00
|
|
|
(let ((new-point (if (zero? (length points))
|
|
|
|
; first point should be at edge of the seed if we are a branch
|
2009-07-30 15:03:21 +00:00
|
|
|
(if (eq? parent-twig-id -1) pos
|
2009-07-13 11:39:34 +00:00
|
|
|
last-point)
|
2009-07-30 15:03:21 +00:00
|
|
|
pos)))
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-08-21 15:03:36 +00:00
|
|
|
(set! w (if (zero? (- num-points 2)) width
|
2009-08-25 14:48:05 +00:00
|
|
|
(* width (- 1 (/ (length points) (- num-points 2))))))
|
|
|
|
(set! dist (* w 1.5))
|
2009-07-13 11:39:34 +00:00
|
|
|
(set! last-point new-point)
|
|
|
|
(set! points (append points (list new-point)))
|
|
|
|
(set! widths (append widths (list w)))
|
2009-07-30 15:03:21 +00:00
|
|
|
(send-message 'add-twig-point (list
|
2009-07-13 11:39:34 +00:00
|
|
|
(list 'plant-id (send plant get-id))
|
|
|
|
(list 'twig-id id)
|
|
|
|
(list 'point new-point)
|
2009-08-21 15:03:36 +00:00
|
|
|
(list 'width w))))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-07-27 08:26:41 +00:00
|
|
|
(define/public (get-desc-list)
|
|
|
|
(list
|
|
|
|
(list 'plant-id (send plant get-id))
|
|
|
|
(list 'parent-twig-id parent-twig-id)
|
|
|
|
(list 'point-index parent-twig-point-index)
|
|
|
|
(list 'twig-id id)
|
|
|
|
(list 'type type)
|
|
|
|
(list 'dir dir)
|
|
|
|
(list 'width width)
|
|
|
|
(list 'num-points num-points)
|
|
|
|
(list 'render-type render-type)))
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (add-twig point-index dir)
|
|
|
|
(let ((twig (make-object twig-logic%
|
|
|
|
(get-point point-index)
|
|
|
|
(send plant get-next-twig-id)
|
|
|
|
plant
|
|
|
|
type
|
|
|
|
dir
|
|
|
|
(list-ref widths point-index)
|
|
|
|
(quotient num-points 2)
|
|
|
|
render-type
|
2009-07-27 08:26:41 +00:00
|
|
|
dist
|
|
|
|
id
|
|
|
|
point-index
|
|
|
|
)))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-07-27 08:26:41 +00:00
|
|
|
(send-message 'new-twig (send twig get-desc-list))
|
2009-07-13 11:39:34 +00:00
|
|
|
(set! twigs (cons (list point-index twig) twigs))
|
|
|
|
twig))
|
|
|
|
|
2009-07-27 08:26:41 +00:00
|
|
|
(define/public (serialise)
|
|
|
|
(append
|
|
|
|
(list (make-object message% 'new-twig (get-desc-list)))
|
|
|
|
(append (map
|
|
|
|
(lambda (point width)
|
|
|
|
(make-object message% 'twig-grow (list
|
|
|
|
(list 'plant-id (send plant get-id))
|
|
|
|
(list 'twig-id id)
|
|
|
|
(list 'point point)
|
|
|
|
(list 'width width))))
|
|
|
|
points widths))
|
|
|
|
(append
|
|
|
|
(map
|
|
|
|
(lambda (twig)
|
|
|
|
(send (cadr twig) serialise))
|
|
|
|
twigs))))
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (get-twig point-index)
|
|
|
|
(cadr (assq point-index twigs)))
|
|
|
|
|
|
|
|
(define/public (get-random-twig)
|
|
|
|
(if (or (null? twigs) (zero? (random 10)))
|
|
|
|
this
|
|
|
|
(send (cadr (choose twigs)) get-random-twig)))
|
|
|
|
|
|
|
|
(define/public (add-ornament point-index ornament)
|
2009-08-19 10:29:01 +00:00
|
|
|
(when (< (length ornaments) max-ornaments)
|
|
|
|
(send-message 'new-ornament
|
2009-07-13 11:39:34 +00:00
|
|
|
(list
|
|
|
|
(list 'plant-id (send plant get-id))
|
|
|
|
(list 'twig-id id)
|
|
|
|
(list 'point-index point-index)
|
|
|
|
(list 'property (send ornament get-property))))
|
2009-08-19 10:29:01 +00:00
|
|
|
(set! ornaments (cons (list point-index ornament) ornaments))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(define/public (get-ornament point-index)
|
|
|
|
(cadr (assq point-index ornaments)))
|
|
|
|
|
|
|
|
; adds the ornament if it's close, and checks sub-twigs
|
|
|
|
; returns true if it's succeded
|
|
|
|
(define/public (check-pickup pickup)
|
|
|
|
; check each point in our twig
|
|
|
|
(let* ((i -1) (found (foldl
|
|
|
|
(lambda (point found)
|
|
|
|
(set! i (+ i 1))
|
2009-08-21 15:03:36 +00:00
|
|
|
; if we havent found anything yet and it's intersecting
|
2009-08-04 08:06:14 +00:00
|
|
|
(cond ((and (not found) (< (vdist (vadd (send plant get-pos) point)
|
2009-08-15 08:03:28 +00:00
|
|
|
(send pickup get-pos))
|
2009-08-21 15:03:36 +00:00
|
|
|
10 #;(+ width (send pickup get-size))))
|
2009-07-13 11:39:34 +00:00
|
|
|
(send plant add-property (send pickup get-type))
|
|
|
|
(send pickup pick-up) ; this will remove the pickup for us
|
|
|
|
(send-message 'pick-up-pickup
|
|
|
|
(list
|
|
|
|
(list 'pickup-id (send pickup get-id))))
|
|
|
|
#t)
|
|
|
|
(else #f)))
|
|
|
|
#f
|
|
|
|
points)))
|
|
|
|
; now check each sub-twig
|
|
|
|
(if (not found)
|
|
|
|
(foldl
|
|
|
|
(lambda (twig found)
|
|
|
|
(if (not found)
|
|
|
|
(send (cadr twig) check-pickup pickup)
|
|
|
|
#f))
|
|
|
|
#f
|
|
|
|
twigs)
|
|
|
|
found)))
|
2009-08-19 13:41:02 +00:00
|
|
|
|
2009-09-25 16:19:48 +00:00
|
|
|
(define/augment (update t d)
|
2009-07-13 11:39:34 +00:00
|
|
|
(append
|
|
|
|
(map
|
|
|
|
(lambda (ornament)
|
2009-09-25 16:19:48 +00:00
|
|
|
(send (cadr ornament) update t d))
|
2009-07-13 11:39:34 +00:00
|
|
|
ornaments)
|
|
|
|
(map
|
|
|
|
(lambda (twig)
|
2009-09-25 16:19:48 +00:00
|
|
|
(send (cadr twig) update t d))
|
2009-07-13 11:39:34 +00:00
|
|
|
twigs)))
|
|
|
|
|
|
|
|
(super-new)))
|
|
|
|
|
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
; abilities live on twigs, and can do things.
|
|
|
|
; this is the base class for all abilities.
|
|
|
|
(define ornament-logic%
|
|
|
|
(class game-logic-object%
|
|
|
|
(init-field
|
|
|
|
(id -1)
|
|
|
|
(property 'none)
|
|
|
|
(plant #f) ; the plant we belong to
|
|
|
|
(twig #f) ; the twig we are on
|
|
|
|
(point-index -1)) ; the index to the point on our twig
|
|
|
|
|
|
|
|
(field
|
|
|
|
(pos (send twig get-point point-index))) ; figure out the position here
|
|
|
|
|
|
|
|
(define/public (get-property)
|
|
|
|
property)
|
|
|
|
|
|
|
|
(define/public (get-pos)
|
|
|
|
pos)
|
|
|
|
|
|
|
|
(super-new)))
|
|
|
|
|
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
; pickups map to abilities, and live out in space
|
|
|
|
; this is the base class for all pickups.
|
|
|
|
(define pickup-logic%
|
|
|
|
(class game-logic-object%
|
|
|
|
(init-field
|
|
|
|
(id -1)
|
|
|
|
(type 'none)
|
|
|
|
(pos (vector 0 0 0)))
|
|
|
|
|
|
|
|
(field
|
|
|
|
(size pickup-size)
|
|
|
|
(picked-up #f))
|
|
|
|
|
|
|
|
(define/public (picked-up?)
|
|
|
|
picked-up)
|
|
|
|
|
|
|
|
(define/public (pick-up)
|
|
|
|
(set! picked-up #t))
|
|
|
|
|
|
|
|
(define/public (get-id)
|
|
|
|
id)
|
|
|
|
|
|
|
|
(define/public (get-type)
|
|
|
|
type)
|
|
|
|
|
|
|
|
(define/public (get-pos)
|
|
|
|
pos)
|
|
|
|
|
|
|
|
(define/public (get-size)
|
|
|
|
size)
|
|
|
|
|
|
|
|
(super-new)))
|
|
|
|
|
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
(define plant-logic%
|
|
|
|
(class game-logic-object%
|
|
|
|
(init-field
|
|
|
|
(id #f)
|
2009-07-24 19:02:49 +00:00
|
|
|
(pos (vector 0 0 0))
|
|
|
|
(col (vector 1 1 1))
|
2009-09-25 16:19:48 +00:00
|
|
|
(tex "fff")
|
|
|
|
(is-player #f))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(field
|
2009-07-30 15:03:21 +00:00
|
|
|
(twigs '()) ; a assoc list map of ids to twigs
|
|
|
|
(leader-twig #f) ; the temporary twig controlled by the player
|
2009-09-25 16:19:48 +00:00
|
|
|
(properties '(flower)) ; a list of symbols - properties come from pickups
|
2009-07-13 11:39:34 +00:00
|
|
|
(ornaments '()) ; map of ids to ornaments on the plant
|
|
|
|
(size start-size) ; the age of this plant
|
|
|
|
(max-twigs default-max-twigs) ; the maximum twigs allowed at any time - oldest removed first
|
|
|
|
(next-twig-id 0)
|
|
|
|
(next-ornament-id 0)
|
2009-08-21 15:03:36 +00:00
|
|
|
(grow-amount default-scale-factor)
|
2009-09-25 16:19:48 +00:00
|
|
|
(twig-size start-twig-points)
|
|
|
|
(auto-pilot-t 0)
|
|
|
|
(auto-pilot-d (* (+ 1 (rndf)) auto-time))
|
|
|
|
(auto-twig #f)
|
|
|
|
(auto-twig-dir (hsrndvec))
|
|
|
|
(auto-twig-pos (vector 0 0 0)))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(inherit send-message)
|
|
|
|
|
|
|
|
(define/public (get-id)
|
|
|
|
id)
|
|
|
|
|
|
|
|
(define/public (get-pos)
|
|
|
|
pos)
|
|
|
|
|
|
|
|
(define/public (get-size)
|
|
|
|
size)
|
2009-07-24 19:02:49 +00:00
|
|
|
|
|
|
|
(define/public (get-col)
|
|
|
|
col)
|
|
|
|
|
|
|
|
(define/public (get-tex)
|
|
|
|
tex)
|
2009-08-21 15:03:36 +00:00
|
|
|
|
|
|
|
(define/public (get-twig-size)
|
|
|
|
twig-size)
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-07-30 15:03:21 +00:00
|
|
|
(define/public (grow pos)
|
|
|
|
(when leader-twig
|
|
|
|
(send leader-twig grow pos)
|
|
|
|
(when (not (send leader-twig growing?))
|
|
|
|
(send-message 'start-growing (list
|
|
|
|
(list 'plant-id id)
|
|
|
|
(list 'twig-id (send leader-twig get-id))))
|
|
|
|
(set! leader-twig #f))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(define/public (add-property name)
|
2009-08-21 15:03:36 +00:00
|
|
|
(if (eq? name 'nutrient)
|
|
|
|
(set! twig-size (+ twig-size nutrient-twig-size-increase))
|
|
|
|
(set! properties (cons name properties))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
; we need to maintain our list of twig ids here, for this plant
|
|
|
|
(define/public (get-next-twig-id)
|
|
|
|
(let ((id next-twig-id))
|
|
|
|
(set! next-twig-id (+ next-twig-id 1))
|
|
|
|
next-twig-id))
|
|
|
|
|
|
|
|
; we need to maintain our list of ornament ids here, for this plant
|
|
|
|
(define/public (get-next-ornament-id)
|
|
|
|
(let ((id next-ornament-id))
|
|
|
|
(set! next-ornament-id (+ next-ornament-id 1))
|
|
|
|
next-ornament-id))
|
|
|
|
|
|
|
|
(define/public (check-pickup pickup)
|
2009-09-25 16:19:48 +00:00
|
|
|
(when (or is-player (random pickup-check-prob)) ; reduce the frequency for non-player plants
|
|
|
|
(when leader-twig
|
|
|
|
(send leader-twig check-pickup pickup))
|
2009-08-21 15:03:36 +00:00
|
|
|
|
|
|
|
#;(foldl
|
2009-07-13 11:39:34 +00:00
|
|
|
(lambda (twig found)
|
|
|
|
(if (not found)
|
2009-08-21 15:03:36 +00:00
|
|
|
(when (send (cadr twig) growing?)
|
|
|
|
(send (cadr twig) check-pickup pickup))
|
2009-07-13 11:39:34 +00:00
|
|
|
#f))
|
|
|
|
#f
|
2009-09-25 16:19:48 +00:00
|
|
|
twigs)))
|
2009-08-21 15:03:36 +00:00
|
|
|
|
|
|
|
(define/public (destroy-twig twig)
|
|
|
|
(send-message 'shrink-twig
|
|
|
|
(list (list 'plant-id id)
|
|
|
|
(list 'twig-id (send (cadr twig) get-id))))
|
|
|
|
#;(send-message 'destroy-branch-twig (list
|
|
|
|
(list 'plant-id id)
|
|
|
|
(list 'twig-id (send (cadr twig) get-id))
|
|
|
|
)))
|
2009-08-19 16:16:48 +00:00
|
|
|
|
2009-08-21 15:03:36 +00:00
|
|
|
; a util to keep a fixed size list of twigs, calling destroy twig when needed.
|
|
|
|
(define (cons-twig thing in count out)
|
|
|
|
(cond
|
|
|
|
((null? in)
|
|
|
|
(cons thing out))
|
|
|
|
((zero? count)
|
|
|
|
(destroy-twig (car in))
|
|
|
|
(cons thing out))
|
|
|
|
(else (cons-twig thing (cdr in) (- count 1) (append out (list (car in)))))))
|
|
|
|
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (add-twig twig)
|
|
|
|
(send twig set-id! (get-next-twig-id))
|
|
|
|
(set! size (* size grow-amount))
|
|
|
|
(send twig scale size)
|
2009-07-30 15:03:21 +00:00
|
|
|
(set! leader-twig twig)
|
2009-08-12 11:14:47 +00:00
|
|
|
#;(send-message 'grow-seed (list
|
2009-07-13 11:39:34 +00:00
|
|
|
(list 'plant-id id)
|
|
|
|
(list 'amount grow-amount)))
|
2009-07-27 08:26:41 +00:00
|
|
|
(send-message 'new-twig (send twig get-desc-list))
|
2009-08-21 15:03:36 +00:00
|
|
|
(set! twigs (cons-twig (list (send twig get-id) twig) twigs max-twigs '())))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-07-30 15:03:21 +00:00
|
|
|
(define/public (add-sub-twig ptwig point-index dir)
|
2009-08-15 08:03:28 +00:00
|
|
|
(set! leader-twig (send ptwig add-twig point-index dir))
|
|
|
|
leader-twig)
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (get-random-twig)
|
|
|
|
(if (not (null? twigs))
|
2009-08-19 16:16:48 +00:00
|
|
|
(send (cadr (choose twigs)) get-random-twig)
|
2009-07-13 11:39:34 +00:00
|
|
|
#f))
|
|
|
|
|
|
|
|
(define/public (get-twig-from-dir dir)
|
|
|
|
(let ((dir (vnormalise dir)))
|
|
|
|
(cadr (foldl
|
|
|
|
(lambda (twig l)
|
2009-08-19 16:16:48 +00:00
|
|
|
(let ((d (vdot (vnormalise (send (cadr twig) get-dir)) dir)))
|
2009-07-13 11:39:34 +00:00
|
|
|
(if (> d (car l))
|
2009-08-19 16:16:48 +00:00
|
|
|
(list d (cadr twig))
|
2009-07-13 11:39:34 +00:00
|
|
|
l)))
|
|
|
|
(list -99 #f)
|
|
|
|
twigs))))
|
|
|
|
|
2009-07-27 08:26:41 +00:00
|
|
|
(define/public (serialise)
|
|
|
|
(append (list (make-object message% 'new-plant (list
|
2009-08-15 08:03:28 +00:00
|
|
|
(list 'plant-id id)
|
2009-08-12 11:14:47 +00:00
|
|
|
(list 'pos pos)
|
|
|
|
(list 'size size)
|
|
|
|
(list 'col col)
|
|
|
|
(list 'tex tex))))
|
|
|
|
|
2009-07-27 08:26:41 +00:00
|
|
|
(append
|
|
|
|
(map
|
|
|
|
(lambda (twig)
|
2009-08-19 16:16:48 +00:00
|
|
|
(send (cadr twig) serialise))
|
2009-07-27 08:26:41 +00:00
|
|
|
twigs))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
2009-09-25 16:19:48 +00:00
|
|
|
(define/public (run-auto-pilot t d)
|
|
|
|
(when (> t auto-pilot-t)
|
|
|
|
(set! auto-pilot-t (+ t auto-pilot-d))
|
|
|
|
(when (or (not auto-twig) (not (send auto-twig growing?)))
|
|
|
|
(set! auto-twig (make-object twig-logic% (vector 0 0 0) 0 this 'root
|
|
|
|
auto-twig-dir
|
|
|
|
start-twig-width
|
|
|
|
twig-size
|
|
|
|
'ribbon))
|
|
|
|
(set! auto-twig-dir (hsrndvec))
|
|
|
|
(set! auto-twig-pos auto-twig-dir)
|
|
|
|
(add-twig auto-twig))
|
|
|
|
(set! auto-twig-dir (vmul (vnormalise (vadd auto-twig-dir (vmul (srndvec) auto-twig-var)))
|
|
|
|
(send auto-twig get-dist)))
|
|
|
|
(set! auto-twig-pos (vadd auto-twig-pos auto-twig-dir))
|
|
|
|
(grow auto-twig-pos)))
|
|
|
|
|
|
|
|
(define/augment (update t d)
|
2009-07-13 11:39:34 +00:00
|
|
|
; grow a new ornament?
|
|
|
|
(when (and (not (null? properties)) (zero? (random ornament-grow-probability)))
|
|
|
|
(let ((twig (get-random-twig)))
|
2009-08-21 15:03:36 +00:00
|
|
|
(when (and twig (> (send twig get-length) 3) (not (send twig growing?)))
|
2009-07-13 11:39:34 +00:00
|
|
|
(let
|
|
|
|
((property (choose properties))
|
2009-08-04 08:06:14 +00:00
|
|
|
(point-index (+ 1 (random (- (send twig get-length) 2)))))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(send twig add-ornament point-index
|
|
|
|
(make-object ornament-logic%
|
|
|
|
(get-next-ornament-id)
|
|
|
|
property
|
|
|
|
this
|
|
|
|
twig
|
2009-08-25 08:55:32 +00:00
|
|
|
point-index))))))
|
2009-07-13 11:39:34 +00:00
|
|
|
(map
|
|
|
|
(lambda (twig)
|
2009-09-25 16:19:48 +00:00
|
|
|
(send (cadr twig) update t d))
|
2009-07-13 11:39:34 +00:00
|
|
|
twigs))
|
|
|
|
|
|
|
|
(super-new)))
|
|
|
|
|
|
|
|
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
(define game-logic%
|
|
|
|
(class game-logic-object%
|
|
|
|
(field
|
|
|
|
(plants '())
|
2009-07-22 15:35:15 +00:00
|
|
|
(pickups '())
|
2009-09-25 16:19:48 +00:00
|
|
|
(player #f)
|
|
|
|
(insects '()))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(inherit send-message)
|
|
|
|
|
2009-08-04 08:06:14 +00:00
|
|
|
(define/public (setup world-list)
|
2009-08-21 15:03:36 +00:00
|
|
|
(let ((pickups (list-ref world-list 1)))
|
2009-08-15 08:03:28 +00:00
|
|
|
(let ((i 0))
|
2009-08-04 08:06:14 +00:00
|
|
|
(for-each
|
2009-08-15 08:03:28 +00:00
|
|
|
(lambda (pickup)
|
|
|
|
(add-pickup (make-object pickup-logic% i (list-ref pickup 0)
|
|
|
|
(list-ref pickup 2)))
|
|
|
|
(set! i (+ i 1)))
|
2009-09-25 16:19:48 +00:00
|
|
|
pickups)
|
|
|
|
(for ((id (in-range 0 num-worms)))
|
|
|
|
(add-insect (make-object insect-logic% id (vmul (srndvec) 100) 'worm)))
|
|
|
|
(for ((id (in-range 0 num-spiders)))
|
|
|
|
(add-insect (make-object insect-logic% (+ id num-worms) (vmul (srndvec) 100) 'spider)))
|
|
|
|
(for ((id (in-range 0 num-butterflies)))
|
|
|
|
(add-insect (make-object insect-logic% (+ id num-worms num-butterflies) (vmul (srndvec) 100) 'butterfly)))
|
|
|
|
)))
|
2009-07-30 15:03:21 +00:00
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (add-player plant)
|
|
|
|
(send-message 'player-plant (list
|
|
|
|
(list 'plant-id (send plant get-id))
|
2009-07-22 15:35:15 +00:00
|
|
|
(list 'pos (send plant get-pos))
|
2009-07-24 19:02:49 +00:00
|
|
|
(list 'size (send plant get-size))
|
|
|
|
(list 'col (send plant get-col))
|
|
|
|
(list 'tex (send plant get-tex))))
|
2009-07-22 15:35:15 +00:00
|
|
|
(set! player plant)
|
2009-09-25 16:19:48 +00:00
|
|
|
(set! plants (cons plant plants))
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
(lambda (insect)
|
|
|
|
(send insect set-centre (send plant get-pos)))
|
|
|
|
insects))
|
2009-08-12 11:14:47 +00:00
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(define/public (add-plant plant)
|
|
|
|
(send-message 'new-plant (list
|
|
|
|
(list 'plant-id (send plant get-id))
|
|
|
|
(list 'pos (send plant get-pos))
|
2009-07-24 19:02:49 +00:00
|
|
|
(list 'size (send plant get-size))
|
|
|
|
(list 'col (send plant get-col))
|
|
|
|
(list 'tex (send plant get-tex))))
|
2009-07-13 11:39:34 +00:00
|
|
|
(set! plants (cons plant plants)))
|
|
|
|
|
|
|
|
(define/public (add-pickup pickup)
|
|
|
|
(send-message 'new-pickup
|
|
|
|
(list
|
|
|
|
(list 'pickup-id (send pickup get-id))
|
|
|
|
(list 'type (send pickup get-type))
|
|
|
|
(list 'pos (send pickup get-pos))))
|
|
|
|
(set! pickups (cons pickup pickups)))
|
2009-09-25 16:19:48 +00:00
|
|
|
|
|
|
|
(define/public (add-insect insect)
|
|
|
|
(send-message 'new-insect (list
|
|
|
|
(list 'insect-id (send insect get-id))
|
|
|
|
(list 'pos (send insect get-pos))
|
|
|
|
(list 'type (send insect get-type))))
|
|
|
|
(send insect set-centre (send player get-pos))
|
|
|
|
(set! insects (cons insect insects)))
|
2009-08-12 11:14:47 +00:00
|
|
|
|
2009-07-27 08:26:41 +00:00
|
|
|
(define/public (serialise)
|
|
|
|
(send player serialise))
|
2009-09-25 16:19:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
(define/public (run-auto-pilot t d)
|
|
|
|
(for-each
|
|
|
|
(lambda (plant)
|
|
|
|
(when (not (eq? plant player))
|
|
|
|
(send plant run-auto-pilot t d)))
|
|
|
|
plants))
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
; todo - distribute the checking of stuff like
|
|
|
|
; this to a random selection of pickups/plants
|
|
|
|
; to distribute the cpu load
|
2009-09-25 16:19:48 +00:00
|
|
|
(define/augment (update t d)
|
|
|
|
|
|
|
|
(run-auto-pilot t d)
|
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
(for-each
|
|
|
|
(lambda (pickup)
|
2009-09-25 16:19:48 +00:00
|
|
|
(for-each
|
|
|
|
(lambda (plant)
|
|
|
|
(send plant check-pickup pickup))
|
|
|
|
plants))
|
2009-08-19 16:16:48 +00:00
|
|
|
pickups)
|
2009-08-21 15:03:36 +00:00
|
|
|
|
2009-07-13 11:39:34 +00:00
|
|
|
; remove the pickups that have been 'picked up'
|
|
|
|
(set! pickups (filter
|
|
|
|
(lambda (pickup)
|
|
|
|
(not (send pickup picked-up?)))
|
|
|
|
pickups))
|
|
|
|
|
2009-09-25 16:19:48 +00:00
|
|
|
(append
|
|
|
|
(map
|
|
|
|
(lambda (plant)
|
|
|
|
(send plant update t d))
|
|
|
|
plants)
|
|
|
|
(map
|
|
|
|
(lambda (insect)
|
|
|
|
(send insect update t d))
|
|
|
|
insects)))
|
2009-07-13 11:39:34 +00:00
|
|
|
|
|
|
|
(super-new)))
|