animal.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. (import
  2. (scheme base))
  3. (define (rand bot top)
  4. (+ bot (random-integer (+ 1 (- top bot)))))
  5. (define-operation (set-strategy! t new-strat))
  6. (define-operation (set-food! t new-food))
  7. (define-operation (get-food-pos t))
  8. (define-operation (step! t spd trn))
  9. (define (out-of-bounds? t)
  10. (define box-size 150)
  11. (or (> (abs (get-xcor t)) box-size)
  12. (> (abs (get-ycor t)) box-size)))
  13. (define (check-forward! t dist)
  14. (define old-position (get-pos t))
  15. (define stuck-strategy 'wriggle)
  16. (pen-up! t)
  17. (hide! t)
  18. (forward! t dist)
  19. (let ((forward-failed? (out-of-bounds? t)))
  20. (set-pos! t old-position)
  21. (pen-down! t)
  22. (show! t)
  23. (if forward-failed?
  24. (cond
  25. ((eq? stuck-strategy 'reflect)
  26. (right! t 180))
  27. ((eq? stuck-strategy 'wriggle)
  28. (right! t 1)
  29. (check-forward! t 1))
  30. (else
  31. (error "Stuck strategy not recognized" stuck-strategy)))
  32. (forward! t dist))))
  33. (define (smell t)
  34. (define dist 1)
  35. (define food-position (get-food-pos t))
  36. (define old-position (get-pos t))
  37. (pen-up! t)
  38. (hide! t)
  39. (forward! t dist)
  40. (let ((new-position (get-pos t)))
  41. (set-pos! t old-position)
  42. (pen-down! t)
  43. (show! t)
  44. (if (> (vector-distance new-position food-position)
  45. (vector-distance old-position food-position))
  46. 'weaker
  47. 'stronger)))
  48. (define (find-by-smell-1 t stop-at)
  49. (let loop ((i 0))
  50. (when (< i stop-at)
  51. (forward! t 1)
  52. (when (eq? (smell t) 'weaker)
  53. (right! t 1))
  54. (loop (+ i 1)))))
  55. (define (find-by-smell-2 t trn stop-at)
  56. (let loop ((i 0))
  57. (when (< i stop-at)
  58. (forward! t 1)
  59. (when (eq? (smell t) 'weaker)
  60. (right! t trn))
  61. (loop (+ i 1)))))
  62. (define (find-by-smell-2.5 t d1 d2 smell-trn rand-trn stop-at)
  63. (let loop ((i 0))
  64. (when (< i stop-at)
  65. (forward! t (rand d1 d2))
  66. (left! t (rand (- rand-trn) rand-trn))
  67. (when (eq? (smell t) 'weaker)
  68. (right! t smell-trn))
  69. (loop (+ i 1)))))
  70. (define (make-strategy-circle d a)
  71. (lambda (turt spd trn)
  72. (left! turt (* trn a))
  73. (forward! turt (* spd d))))
  74. (define (make-strategy-random d1 d2 a1 a2)
  75. (lambda (turt spd trn)
  76. (left! turt (* trn (rand a1 a2)))
  77. (check-forward! turt (* trn (rand d1 d2)))))
  78. (define (make-strategy-smell-3 d1 d2 smell-trn rand-trn)
  79. (lambda (turt spd trn)
  80. (forward! turt (* spd (rand d1 d2)))
  81. (left! turt (* trn (rand (- rand-trn) rand-trn)))
  82. (when (eq? (smell turt) 'weaker)
  83. (right! turt (* trn smell-trn)))))
  84. (define (make-strategy-direct d)
  85. ;; directly face the food source
  86. (lambda (turt spd trn)
  87. (face! turt (get-food-pos turt))
  88. (forward! turt (* spd d))))
  89. (define (make-hungry-turtle)
  90. (define strategy #f)
  91. (define food-pos #f)
  92. (object-with-ancestors ((p (make-turtle)))
  93. ((set-strategy! self new-strat)
  94. (set! strategy new-strat))
  95. ((set-food! self new-food)
  96. (set! food-pos new-food))
  97. ((get-food-pos self)
  98. (food-pos self))
  99. ((step! self spd trn)
  100. (strategy self spd trn))))