;; -*- lisp -*-
;; Test Civilian RuleSet
;; CommentOut Charactor is ';'
(
 (gsetq *self_id   0)
 (gsetq *refuge_id 0)
 (gsetq *previous_refuge_id 0)
 (gsetq *house_id  0)
 (gsetq *previous_cry_time 1)
 
;; Initial Situation Posit
 (defposit init
   (defrule initial_state
     :condition (?know (and
			(not (== *house_id 0))
			(world :time *time :cond (<= *time 5))
			(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 5))
			(self :buriedness *bn :cond (== *bn 0))))
     :activity 1
     :action (progn (!add select_target_refuge)
		    (!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 (!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
   ;; simple go to refuge
   (defrule goto_refuge1
     :condition (?know (and 
			;;(print (quote (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
   ;;			(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 (quote (posit is in_refuge)))
			(world :time *time)
			(self :position *pos)
			(refuge :id *refuge :cond (== *refuge *pos))))
     :activity (* 10 *time)
     :action (progn (!say :content What_a_relief!)
                    (!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 10)))))
     :activity (* 2 (- *time *previous_cry_time))
     :action (progn 
	       (gsetq *previous_cry_time *time)
	       (!say :content 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 (quote (posit is select_target__refuge)))
			(refuge :id *id :dist min)))
     :activity 15
     :action (and 
	      ;;(print (quote (rule is nearest_refuge_select1)))
	      (gsetq *refuge_id *id)
	      ;;(print (quote (rule is nearest_refuge_select2)))
	      (!move :id *id)
	      (!remove select_target_refuge))
     )
   ;; select visible refuge
   (defrule can_see_refuge
     :condition (?know (and
			;; can see refuge
			(refuge :id *id :dist *dist :cond (< *dist 10000)))
		       )
     :activity 20
     :action (and 
	      (gsetq *refuge_id *id)
	      (!move :id *id)
	      (!remove select_target_refuge))
     )
   ;; select safty refuge
   ;; text_tmp2.txt
   )
 )
;; then say "What_a_relief!" (!say :content What_a_relief!)