
;; Basic Crazy.java example translated into JessRobocode

;;(watch rules)
;;(watch facts)
(defrule move-by-default
    ?f <- (TurnRemaining ?x)
    (NewTurn)
    (not (OnTheMove))
    =>
    (retract ?f)
    (store Ahead 40000)
    (store TurnRight 90)
    (assert (NowMoving Ahead))
    (assert (NowTurning Right))
    (assert (OnTheMove)))

(defrule when-turn-right-completes
    ?cond <- (NowTurning Right)
    ?fact <- (TurnRemaining ?x&:(= ?x 0))
    =>
    (retract ?cond)
    (retract ?fact)
    (store TurnLeft 180)
    (assert (NowTurning Left)))

(defrule when-turn-left-completes
    ?cond <- (NowTurning Left)
    ?fact <- (TurnRemaining ?x&:(= ?x 0))
    =>
    (retract ?cond)
    (retract ?fact)
    (store TurnRight 180)
    (assert (NowTurning Right)))

(defrule make-sure-it-moves-ahead
    (NowMoving Ahead)
    (not (HitWallEvent))
    ?fact <- (DistanceRemaining ?x&:(= ?x 0))
    =>
    (retract ?fact)
    (store Ahead 40000))

(defrule make-sure-it-moves-back
    (NowMoving Back)
    (not (HitWallEvent))
    ?fact <- (DistanceRemaining ?x&:(= ?x 0))
    =>
    (retract ?fact)
    (store Back 40000))

(defrule back-on-hitting-walls
    ?event <- (HitWallEvent)
    ?fact <- (NowMoving Ahead)
    =>
    (store Back 40000)
    (retract ?event)
    (retract ?fact)
    (assert (NowMoving Back)))

(defrule ahead-on-hitting-walls
    ?event <- (HitWallEvent)
    ?fact <- (NowMoving Back)
    =>
    (store Ahead 40000)
    (retract ?event)
    (retract ?fact)
    (assert (NowMoving Ahead)))

(defrule fire-when-scanning-anything
    (ScannedRobotEvent)
    =>
    (store Fire 1))


