;; -*- lisp -*-
;; Test Civilian RuleSet
;; CommentOut Charactor is ';'
(
 ;; Global Variable Call
 (gsetq *self_id   0)
 (gsetq *refuge_id 0)
 (gsetq *previous_refuge_id 0)
 (gsetq *house_id  0)
 (gsetq *previous_cry_time 0)

 ;; Initial Situation Posit
 (defposit init
   (defrule initial_state
     :condition (?know (and
			(not (== *house_id 0))
			(world :time *time :cond (<= *time 3))
			(self :position *pos)
			(building :id *build_id :cond (== *build_id *pos))
			))
     :activity 1
     :action (gsetq *house_id *pos)
     )
   (defrule wait2move
     :condition (?know (and
			;; Civlian agent can act after 'time 3'
			(world :time *time :cond (> *time 3))
			(self :id *id :buriedness *bn :cond (== *bn 0))
			(gsetq *self_id *id)
			(print (list ID: *self_id [init]))))
     :activity 10
     :action (progn 
	       (print (list ID: *self_id "wait2move"))
	       (!add goto_refuge)
	       (!remove init))
     )
   (defrule wait2cry
     :condition (?know (and
			(world :time *time :cond (> *time 3))
			(self :buriedness *bn :cond (> *bn 0))))
     :activity *bn
     :action (progn
	       (print (list ID: *self_id "wait2cry"))
	       (!add cry_help)
	       ;;(!add check_self_sitation)
	       (!remove init))
     )		       
   )
 ;;--------------------------------------------------
 ;;  On Simulation Field
 ;;--------------------------------------------------
 ;; in_building
 (defposit in_building
   (defrule in_own_house
     :condition (?know (and
			(not (== *house_id 0))
			(world :time *time)
			(self :position *pos :cond (== *house_id *pos))))
     :activity 1
     :action (gsetq *now *time)
     )
   )
 ;;------------------------------
 ;; situation::GotoRefuge
 (defposit goto_refuge
   ;; if agent does not know refuge
   (defrule search_refuge
     :condition (== *refuge_id 0)
     :activity 10
     :action (!add select_target_refuge)
     )
   ;; simple go to refuge
   (defrule goto_refuge1
     :condition (?know (and
			(print (list ID: *self_id [posit is goto_refuge]))
			(> *refuge_id 0)))
     :activity (+ 1)
     :action (!move :id *refuge_id)
     )
   ;; simple get on refuge
   (defrule get_on_refuge
     :condition (?know (and
			;; if civilian is on the target refuge
			(self :position *id :cond (== *refuge_id *id))))
     :activity (+ 2)
                    
     :action (progn  ;; and sleeping after
		    (!remove goto_refuge)
		    (!add in_refuge))
     )
   ;; if can see fire near refuge
   (defrule can_see_fire_near_refuge
     :condition (?know (and
			(not (== *refuge_id 0))
			(print (list ID: *self_id scan: can_see_fire_near_fire))
			(building :id *id :fieryness *fn :dist *build_dist
				  :cond (and (> *fn 0)(< *fn 3)
					     (< *build_dist 50000)))
			(< (getdist *id *refuge_id) 30000)
			))
     :activity (/ (- 30000 (getdist *id *refuge_id)) 1000)
     :action (!add select_target_refuge)
     )
   )
 ;; situation::InRefuge
 (defposit in_refuge
   (defrule say_message1
     :condition (?know (and
			(print (list ID: *self_id [in_refuge]))
			(self :position *pos)
			(refuge :id *refuge :cond (== *refuge *pos))))
     :activity 1
     :action (print (list ID: *self_id in_refuge))
     )
   (defrule getaway_this_refuge
     :condition (?know (and
			(building :id *fire_build_id 
				  :fieryness *fn
				  :dist *fire_build_dist
				  :cond (and (> *fn 0) (< *fn 4)
					     (< *fire_build 20000)))
			(print (list Building :id *fire_build_id :dist *fire_build_dist))
			(road :id *target_road_id :dist *target_road_dist
			      :cond (and 
				     (> *target_road_dist 20000)
				     (< *target_road_dist 40000)
				     (> (getdist *target_riad_id *fire_build_id) 20000)))))
     :activity (/ 40000 *fire_build_dist)
     :action (progn 
	       (!move :id *target_road_id)
	       (gsetq *previous_refuge_id *refuge_id)
	       (gsetq *refuge_id 0)
	       (!add select_target_refuge)
	       (!remove in_refuge)
	       )
     )
   )
 ;;--------------------------------------------------
 ;;  Say Action
 ;;--------------------------------------------------
 (defposit cry_help
   (defrule say_help
     :condition (?know (and
			(world :time *time)
			(not (== *previous_cry_time 0))
			(not (> *previous_cry_time (- *time 5)))))
     :activity (* 2 (- *time *previous_cry_time))
     :action (progn (!say :context (quote (help me))))
     )
   (defrule remove_cry_help
     :condition (?know (and
			(self :buriedness *bn :cond (== *bn 0))))
     :activity 100
     :action (progn (!add in_building)
		    (!remove cry_help))
     )
   )   
 ;;--------------------------------------------------
 ;;  Decide Target Thread
 ;;--------------------------------------------------
 ;; select refuge to go away 
 (defposit select_target_refuge
   ;; select nearest refuge
   (defrule nearest_refuge_select
     :condition (?know (and
			(print (list ID: *selfid [select_target__refuge]))
			(world :time *time)
			(or (and 
			     (== *previous_refuge_id 0)
			     (refuge :id *id :dist min)
			     )
			    (and
			     (not (== *previous_refuge_id 0))
			     (refuge :id *id :dist min 
				     :cond (not (== *previous_refuge_id *id)))
			     )
			    )))
     :activity (- 50 *time)
     :action (and 
	      (print (list selct nearest_refuge rule))
	      (gsetq *refuge_id *id)
	      (!move :id *id)
	      (!remove select_target_refuge))
     )
   ;; select visible refuge
   (defrule can_see_refuge
     :condition (?know (and
			;; can see refuge
			(world :time *time) 
			(refuge :id *id :dist *dist :cond (< *dist 10000))
			(or (and (> *time 50) (setq *t (- *time 50)))
			    (and (<= *time 50) (setq *t 0)))
			)
		       )
     :activity (- 50 *time)
     :action (and 
	      (gsetq *refuge_id *id)
	      (!move :id *id)
	      (!remove select_target_refuge))
     )
   ;; select safty refuge
   (defrule safty_refuge
     :condition 
     (?know (and
	     (not (== *refuge_id 0))
	     (self :position *pos)
	     (print (quote (posit is select_target__refuge1)))
	     (refuge :id *id :dist *dist 
		     :cond (and (not (== *id *refuge_id))
				(> *dist (getdist *pos *refuge_id))))
	     (print (quote (posit is select_target__refuge2)))
	     (not (building :id *build_id :fieryness *fn
			    :cond (and (> *fn 0) (< *fn 4) 
				       (< (getdist *build_id *id) 20000))))))
     :activity 25
     :action (progn 
	       (gsetq *refuge_id *id)
	       (!move :id *refuge_id)
	       (!remove select_target_refuge))
     )
   )

 )
;; then say "What_a_relief!" (!say :content What_a_relief!)