Skip to content

Commit

Permalink
make ai missiles a bit better at hitting small targets
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vanderson committed Aug 2, 2018
1 parent da5554a commit 15c0e8f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion client.rkt
Expand Up @@ -588,7 +588,7 @@
(prepend! buttons zbutton zkeyb xkeyb))

; auto-center button
(define acby (+ (top) 70))
(define acby (+ (top) 60))
(define acbw 180)
(define acbh 40)
(cond
Expand Down
16 changes: 1 addition & 15 deletions draw.rkt
Expand Up @@ -153,12 +153,7 @@
((effect? o)
(draw-effect csd center scale space o fowa))
((upgrade? o)
(define spr '())
(prepend! spr (draw-upgrade csd center scale space o fowa))
(when showplayers?
(prepend! spr (draw-cargolist csd textr textsr center scale o "gray" fowa
(list (upgrade-type o)) '())))
spr)
(draw-upgrade csd center scale space o fowa))
((ship? o)
(define spr '())
(define si (hash-ref ship-list (ship-type o)))
Expand Down Expand Up @@ -236,15 +231,6 @@
(define spr '())
(define col (send the-color-database find-color colstr))
(define-values (sx sy) (obj->screen obj center scale))
(prepend! spr (sprite (+ sx 25.0) (+ sy 25.0) (sprite-idx csd 'square) #:layer LAYER_MAP
#:mx (/ 71.0 (sprite-width csd (sprite-idx csd 'square)))
#:my (/ 2.0 (sprite-height csd (sprite-idx csd 'square)))
#:r (send col red) #:g (send col green) #:b (send col blue)
#:theta (/ pi 4) #:a fowa))
(prepend! spr (sprite (+ sx 85.0) (+ sy 50.0) (sprite-idx csd 'square) #:layer LAYER_MAP
#:mx (/ 70.0 (sprite-width csd (sprite-idx csd 'square)))
#:my (/ 2.0 (sprite-height csd (sprite-idx csd 'square)))
#:r (send col red) #:g (send col green) #:b (send col blue) #:a fowa))
(for ((t text-above) (i (in-naturals)))
(prepend! spr (text-sprite textr textsr t
(+ sx 55.0) (+ sy 50.0 (- (* (+ i 1) 14)))
Expand Down
8 changes: 4 additions & 4 deletions missile.rkt
Expand Up @@ -78,7 +78,7 @@
(values #f changes))))


(define (missile-enemy? o)
(define (missile-target? o)
(or (spaceship? o) (probe? o)))

; return a list of changes
Expand All @@ -91,7 +91,7 @@
((- (space-time space) last-time) . > . 5000.0))
(ship-flying? ownship)
(tool-online? t))
(define ne (nearest-enemy qt ownship missile-enemy?))
(define ne (nearest-enemy qt ownship missile-target?))
(when ne
;(printf "firing\n")
(set-tool-rc! t (space-time space))
Expand All @@ -107,7 +107,7 @@
(define live? #t)

(for ((o (in-list (qt-retrieve qt (obj-x m) (obj-y m) (ship-radar m))))
#:when (missile-enemy? o))
#:when (missile-target? o))
(define d (distance o m))
(when (d . <= . (ship-radar m))
(define foe? ((faction-check (ship-faction m) (ship-faction o)) . < . 0))
Expand All @@ -120,6 +120,6 @@
(when (d . < . maxd)
(define z (- maxd d)) ; meters inside maxd
(set! f (+ f (* z z (if foe? 1.0 -1.0))))
(when (d . < . hd)
(when (d . < . (+ hd (/ AI_HIT_CLOSE 2)))
(set! live? #f)))))
(values f live?))
7 changes: 4 additions & 3 deletions physics.rkt
Expand Up @@ -67,12 +67,13 @@
(if ((abs newv) . < . .01) 0 newv))


(define (physics! pv dt (drag_xy 0.0) (acc? #f))
(define (physics! pv dt (drag_xy 0.0))
(set-posvel-x! pv (+ (posvel-x pv) (* dt (posvel-dx pv))))
(set-posvel-y! pv (+ (posvel-y pv) (* dt (posvel-dy pv))))
(set-posvel-r! pv (angle-add (posvel-r pv) (* dt (posvel-dr pv))))
(set-posvel-dx! pv (drag (posvel-dx pv) dt drag_xy))
(set-posvel-dy! pv (drag (posvel-dy pv) dt drag_xy)))
(when (not (= drag_xy 0.0))
(set-posvel-dx! pv (drag (posvel-dx pv) dt drag_xy))
(set-posvel-dy! pv (drag (posvel-dy pv) dt drag_xy))))


(define EDGE_THRUST 10.0)
Expand Down
17 changes: 9 additions & 8 deletions pilot.rkt
Expand Up @@ -256,12 +256,13 @@
((distance ownship o) . <= . 500.0)))
(qt-retrieve qt (obj-x ownship) (obj-y ownship) 500.0)))

(define predict-secs
(define-values (predict-secs fit-per-sec)
(if (missile? ownship)
(inexact->exact (round (max 0 (- (tool-rc (ship-tool ownship 'endrc))
(/ (obj-age space ownship) 1000.0)))))
;(inexact->exact (round (/ 250.0 (tool-val ft))))
8
(values
(inexact->exact (round (max 0 (- (tool-rc (ship-tool ownship 'endrc))
(/ (obj-age space ownship) 1000.0)))))
2)
(values 8 1)
))

(define origpv (struct-copy posvel (obj-posvel ownship)))
Expand All @@ -287,8 +288,8 @@
(for/list ((i 16))
(state origf origf basec basec (struct-copy posvel origpv) 0.0 1 #t)))

(for ((i (in-range predict-secs)))
(for ((s (in-list ships))) (physics! (obj-posvel s) 1.0))
(for ((i (in-range (* predict-secs fit-per-sec))))
(for ((s (in-list ships))) (physics! (obj-posvel s) (/ 1.0 fit-per-sec)))

;(printf "states is now ~v\n" states)
(for ((s (in-list states))
Expand All @@ -304,7 +305,7 @@
(when st (set-tool-rc! st (state-c s)))
; time update ownship
(for ((z (in-range 5)))
(update-physics! space ownship 0.2))
(update-physics! space ownship (/ 0.2 fit-per-sec)))
; calculate fitness
(define-values (f live?)
((if (missile? ownship) missile-fitness pilot-fitness) space qt ownship))
Expand Down
3 changes: 2 additions & 1 deletion scenarios/asteroid-search.rkt
Expand Up @@ -141,7 +141,8 @@
(strategy (space-time ownspace) "return" (ob-id enemy-base))))))


(define enemy-base (make-ship "blue-station" "a" "a" #:x 1500.0 #:y 1500.0 #:ai? #t #:hangar '()))
(define enemy-base (make-ship "blue-station" "a" "a" #:x 1500.0 #:y 1500.0 #:ai? #t
#:dr 0.1 #:hangar '()))
(set-ship-stats! enemy-base (stats (next-id) "blue-station" "Rebel Outpost" "Rebel"
;con maxcon mass radar drag start-ship?
750.0 750.0 1000.0 600.0 0.4 #t))
Expand Down

0 comments on commit 15c0e8f

Please sign in to comment.