Skip to content

Commit

Permalink
fix running server and client separately
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vanderson committed Aug 18, 2018
1 parent 9b73445 commit 3faa702
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 71 deletions.
3 changes: 2 additions & 1 deletion client.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,8 @@
)

(define textfont (load-font! sd #:size TEXTH #:face "Verdana" #:family 'modern))
(load-ships! sd)
(load-ships!)
(add-ship-sprites! sd)
(plasma-setup-pre! sd)
(explosion-setup-pre! sd)

Expand Down
4 changes: 2 additions & 2 deletions draw.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
((ship? o)
(define spr '())
(define si (hash-ref ship-list (ship-type o)))
(define sym (ship-info-bm si))
(define sym (car (ship-info-bm si)))
(define eng (ship-tool o 'engine))
(cond
((warping? o)
Expand All @@ -183,7 +183,7 @@
(prepend! spr (obj-sprite o csd center scale LAYER_SHIPS
sym
(/ (ship-sprite-size o)
(sprite-size csd (ship-info-bm si)))
(sprite-size csd (car (ship-info-bm si))))
fowa (obj-r o) (make-color (get-red space o) 0 0 1.0)))

(define w (ship-w o scale))
Expand Down
3 changes: 2 additions & 1 deletion server.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"change.rkt"
"physics.rkt"
"quadtree.rkt"
"ships.rkt"
"pilot.rkt"
"warp.rkt"
"plasma.rkt"
Expand All @@ -17,7 +18,6 @@
"cannon.rkt"
"shield.rkt"
"scenario.rkt"
"scenarios/testing.rkt"
"upgrade.rkt")

(provide start-server)
Expand Down Expand Up @@ -764,6 +764,7 @@


(define (start-server (port PORT) #:scenario (scenario sc-pick) #:spacebox (spbox #f))
(load-ships!)
(change-scenario! scenario)
(set! spacebox spbox)
(set! server-listener (tcp-listen port 100 #t))
Expand Down
149 changes: 82 additions & 67 deletions ships.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,97 @@
(provide (all-defined-out))

(struct ship-info (filename size bm engine-bms) #:mutable #:prefab)
; each bm is actually (cons sym bm)

(define ship-list
(for/hash ((name '("spacesuit"
"probe"
"missile"
"cannonball"
"asteroid_87"
"asteroid_43"
"blue-station"
"red-station"
"blue-frigate"
"red-frigate"
"blue-fighter"
"red-fighter"
"red-destroyer"
"blue-cruiser"
"red-cruiser"

;"missile1" (ship-info "missile1" 16.0 #f)
;"missile2" (ship-info "missile2" #f #f)
;"missile3" (ship-info "missile3" #f #f)
;"probe1" (ship-info "probe1" #f #f)
;"probe2a" (ship-info "probe2a" #f #f)
;"probe2b" (ship-info "probe2b" #f #f)
;"probe3" (ship-info "probe3" #f #f)
)))
(define size #f)
(define filename name)
(cond
((equal? name "blue-fighter")
(set! size 20.0))
((equal? name "red-fighter")
(set! size 24.0))
((equal? name "missile")
(set! size 10.0))
((equal? name "cannonball")
(set! filename "asteroid_43")
(set! size 10.0)))
(values name (ship-info filename size #f '()))))

(define ship-list #f)
(define ship-list-sema (make-semaphore 1))

(define (engine-filename base i k (ext ".png"))
(string-append base "-e" (number->string i) (number->string k) ext))

(define (engine-path base i k (ext ".png"))
(build-path IMAGEDIR (engine-filename base i k ext)))


(define (load-ships! sd)
(for (((name si) (in-hash ship-list)))
(define sym (string->symbol name))
(define basename (ship-info-filename si))
(define bm (read-bitmap (build-path IMAGEDIR (string-append basename ".png")) 'png/alpha))
(add-sprite!/value sd sym bm)
(set-ship-info-bm! si sym)
(when (not (ship-info-size si))
(set-ship-info-size! si (max (send bm get-width) (send bm get-height))))
; both client and server call this, but we only want it to happen once
; also they could be threads in the same process
(define (load-ships!)
(semaphore-wait ship-list-sema)
(printf "load-ships!\n")
(when (not ship-list)
(printf "load-ships! actual\n")
(set! ship-list
(for/hash ((name '("spacesuit"
"probe"
"missile"
"cannonball"
"asteroid_87"
"asteroid_43"
"blue-station"
"red-station"
"blue-frigate"
"red-frigate"
"blue-fighter"
"red-fighter"
"red-destroyer"
"blue-cruiser"
"red-cruiser"

;"missile1" (ship-info "missile1" 16.0 #f)
;"missile2" (ship-info "missile2" #f #f)
;"missile3" (ship-info "missile3" #f #f)
;"probe1" (ship-info "probe1" #f #f)
;"probe2a" (ship-info "probe2a" #f #f)
;"probe2b" (ship-info "probe2b" #f #f)
;"probe3" (ship-info "probe3" #f #f)
)))
(define size #f)
(define filename name)
(cond
((equal? name "blue-fighter")
(set! size 20.0))
((equal? name "red-fighter")
(set! size 24.0))
((equal? name "missile")
(set! size 10.0))
((equal? name "cannonball")
(set! filename "asteroid_43")
(set! size 10.0)))
(values name (ship-info filename size #f '()))))

(for ((i (in-naturals 1)))
#:break (not (file-exists? (engine-path basename i 1)))
; i goes over different sized engine animations
(set-ship-info-engine-bms! si (append (ship-info-engine-bms si) (list (list))))
(for ((k (in-naturals 1)))
; k goes over the frames of a single animation
(define filename (engine-path basename i k))
#:break (not (file-exists? filename))
(define bm (read-bitmap filename 'png/alpha))
(define sym (string->symbol (engine-filename name i k "")))
(add-sprite!/value sd sym bm)
(set-ship-info-engine-bms! si
(for/list ((bms (ship-info-engine-bms si))
(z (in-naturals 1)))
(if (equal? i z)
(append bms (list sym))
bms)))))))

(for (((name si) (in-hash ship-list)))
(define sym (string->symbol name))
(define basename (ship-info-filename si))
(define bm (read-bitmap (build-path IMAGEDIR (string-append basename ".png")) 'png/alpha))
(set-ship-info-bm! si (cons sym bm))
(when (not (ship-info-size si))
(set-ship-info-size! si (max (send bm get-width) (send bm get-height))))

(for ((i (in-naturals 1)))
#:break (not (file-exists? (engine-path basename i 1)))
; i goes over different sized engine animations
(set-ship-info-engine-bms! si (append (ship-info-engine-bms si) (list (list))))
(for ((k (in-naturals 1)))
; k goes over the frames of a single animation
(define filename (engine-path basename i k))
#:break (not (file-exists? filename))
(define bm (read-bitmap filename 'png/alpha))
(define sym (string->symbol (engine-filename name i k "")))
(set-ship-info-engine-bms! si
(for/list ((bms (ship-info-engine-bms si))
(z (in-naturals 1)))
(if (equal? i z)
(append bms (list (cons sym bm)))
bms)))))))
(semaphore-post ship-list-sema))

(define (add-ship-sprites! sd)
(for (((name si) (in-hash ship-list)))
(define c (ship-info-bm si))
(add-sprite!/value sd (car c) (cdr c))
(for* ((i (in-list (ship-info-engine-bms si)))
(k (in-list i)))
(add-sprite!/value sd (car k) (cdr k)))))


(define (make-spacesuit name ship)
(define ang (random-between -1.0 1.0))
Expand Down

0 comments on commit 3faa702

Please sign in to comment.