// Agent searchDrone in project airSeaRescue.mas2j /* Initial beliefs and rules */ free. life(500). /* Initial goals */ // next line is the bottom of the quadrant // if 2 lines bellow is too far calc_new_y(AgY,QuadY2,QuadY2) :- AgY+2 > QuadY2. // otherwise, the next line is 2 lines bellow calc_new_y(AgY,_,Y) :- Y = AgY+2. +gsize(_,_) : true <- !send_init_pos. +!send_init_pos : pos(X,Y) <- .print("Position initialised"); .send(operator,tell,init_pos(X,Y)). +!send_init_pos : not pos(_,_) // if I do not know my position yet <- .wait("+pos(X,Y)", 500); // wait for it and try again !!send_init_pos(S). +free : last_checked(X,Y) <- !prep_around(X,Y). +free : quadrant(X1,Y1,X2,Y2) <- !prep_around(X1,Y1). +free : true <- !wait_for_quad. @pwfq[atomic] +!wait_for_quad : free & quadrant(_,_,_,_) <- -+free. +!wait_for_quad : free <- .wait("+quadrant(X1,Y1,X2,Y2)", 500); !!wait_for_quad. +!wait_for_quad : not free <- .print("No longer free while waiting for quadrant."). -!wait_for_quad // .wait might fail <- !!wait_for_quad. // if I am around the upper-left corner, move to upper-right corner +around(X1,Y1) : quadrant(X1,Y1,X2,Y2) & free <- .print("in Q1 to ",X2,"x",Y1); !prep_around(X2,Y1). // if I am around the bottom-right corner, move to upper-left corner +around(X2,Y2) : quadrant(X1,Y1,X2,Y2) & free <- .print("in Q4 to ",X1,"x",Y1); !prep_around(X1,Y1). // if I am around the right side, move to left side two lines bellow +around(X2,Y) : quadrant(X1,Y1,X2,Y2) & free <- ?calc_new_y(Y,Y2,YF); .print("in Q2 to ",X1,"x",YF); !prep_around(X1,YF). // if I am around the left side, move to right side two lines bellow +around(X1,Y) : quadrant(X1,Y1,X2,Y2) & free <- ?calc_new_y(Y,Y2,YF); .print("in Q3 to ", X2, "x", YF); !prep_around(X2,YF). // last "around" was none of the above, go back to my quadrant +around(X,Y) : quadrant(X1,Y1,X2,Y2) & free & Y <= Y2 & Y >= Y1 <- .print("in no Q, going to X1"); !prep_around(X1,Y). +around(X,Y) : quadrant(X1,Y1,X2,Y2) & free & X <= X2 & X >= X1 <- .print("in no Q, going to Y1"); !prep_around(X,Y1). +around(X,Y) : quadrant(X1,Y1,X2,Y2) <- .print("It should never happen!!!!!! - go home"); !prep_around(X1,Y1). +!prep_around(X,Y) : free <- -around(_,_); -last_dir(_); !around(X,Y). +!around(X,Y) : // I am around to some location if I am near it or // the last action was skip (meaning that there are no paths to there) (pos(AgX,AgY) & searchDrone.neighbour(AgX,AgY,X,Y)) | last_dir(skip) <- +around(X,Y). +!around(X,Y) : not around(X,Y) <- !next_step(X,Y); !!around(X,Y). +!around(X,Y) : true <- !!around(X,Y). +!next_step(X,Y) : pos(AgX,AgY) <- searchDrone.get_direction(AgX, AgY, X, Y, D); //.print("from ",AgX,"x",AgY," to ", X,"x",Y," -> ",D); -+last_dir(D); do(D). +!next_step(X,Y) : not pos(_,_) // I still do not know my position <- !next_step(X,Y). -!next_step(X,Y) : true // failure handling -> start again! <- .print("Failed next_step to ", X,"x",Y," fixing and trying again!"); -+last_dir(null); !next_step(X,Y). /* Finding victims */ +cell(X,Y,victim,V): true <- .print("Found vimctim",V); .print("Announcing ",victim(X,Y)," to others"); .broadcast(tell,victim(X,Y)); .send(V,tell,stayHere(X,Y)).