:- module ian5. :- interface. :- import_module io. :- pred main(io::di,io::uo) is det. :- implementation. :- import_module labelled_program, list, instrs, world. main(!IO) :- Fortress = m0, Searching = m1, Food = m2, TurnAround = [ turn(left,next), turn(left,next), turn(left,next) ], MoveForward = [ move(next, current) ], % (label) MoveLabel = [ move(goto("label"), current) ], % (exit, guard) SetUpGuard = [ sense(ahead, goto("exit"), goto("check guard 1"), friend), label("check guard 1"), turn(left, next), sense(left_ahead, goto("check guard 2"), goto("guard 1 move"), friend), label("check guard 2"), turn(right, next), turn(right, next), sense(right_ahead, goto("check guard 3"), goto("guard 2 move"), friend), label("check guard 3"), turn(left, next), sense(left_ahead, goto("exit"), next, friend), sense(right_ahead, goto("exit"), goto("guard"), friend), label("guard 1 move"), turn(right, next), turn(right, next), macro(MoveForward, [], [], 1), macro(MoveForward, [], [], 1), macro(MoveForward, [], [], 1), macro(MoveLabel, ["label"], ["guard"], 1), label("guard 2 move"), turn(left, next), turn(left, next), macro(MoveForward, [], [], 1), macro(MoveForward, [], [], 1), macro(MoveForward, [], [], 1), macro(MoveForward, [], [], 1), turn(left, next), macro(MoveLabel, ["label"], ["guard"], 1) ], Guard = [ mark(Fortress, next), label("start"), label("guard"), sense(ahead, goto("turn"), goto("check food"), marker(Fortress)), label("check food"), sense(ahead, goto("get food"), goto("turn"), food), label("turn"), turn(left, goto("guard")), label("get food"), move(next, goto("guard")), pickup(next, next), macro(TurnAround, [], [], 1), move(next, current), drop(goto("guard")) ], FaceOutwards = [ label("start"), sense(ahead, goto("turn"), next, friend), sense(left_ahead, goto("turn"), goto("end"), friend), label("turn"), turn(left, goto("start")), label("end") ], LeaveHome = [ macro(FaceOutwards, [], [], 1), label("move"), move(jump(2), next), flip(20, jump(2), previous), sense(here, goto("move"), jump(2), home), turn(right, goto("move")) ], %(found) %TryLeftSearching = [ % sense(ahead, jump(2), next, marker(Searching)), % move(goto("found"), next), % turn(left, next) %], %ChooseRandomDirection = [ % flip(2, next, jump(2)), % turn(left, jump(2)), % turn(right, next) %], WaitRandomTime = [ flip(62, next, current) ], RandomGather = [ label("start"), macro(wonder(food, marker(Food), Searching, 100, 999), [], [], 1), pickup(next, next), macro(mark_area(Food, 4, 6), [], [], 1), macro(wonder(home, marker(Searching), Food, 100, 999), [], [], 1) ], %RandomGather2 = [ % macro(choose_random_dir, [], [], 1), % macro(goalcheck(marker(Food)), "found", "not found") DepositFood = [ macro(find_target_in_area(marker(Fortress), home, 411, "found", "not found"), ["found", "not found"], ["found", "finished"], 1), label("found"), drop(goto("finished")) ], Main = [ label("start"), macro(SetUpGuard, ["exit", "guard"], ["scout", "guard"], 1), label("guard"), macro(Guard, [], [], 1), label("scout"), macro(WaitRandomTime, [], [], 1), label("go again"), macro(LeaveHome, [], [], 1), macro(RandomGather, [], [], 1), label("deposit food"), macro(DepositFood, ["finished"], ["go again"], 1) ], % generate the program using the Main macro ( if P = gen_program(Main) then instrs.write_program(P, !IO) else write_string("error\n", !IO) ). :- func goalcheck(condition, string, string) = labelled_program. goalcheck(Goal, Found, NotFound) = [ sense(ahead, goto("move"), next, Goal), sense(left_ahead, goto("turn left"), next, Goal), sense(right_ahead, goto("turn right"), goto(NotFound), Goal), label("turn left"), turn(left, goto("move")), label("turn right"), turn(right, goto("move")), label("move"), move(goto(Found), next), % give up after a while: flip(62, goto(NotFound), previous) ]. :- func guidecheck(condition, string, string) = labelled_program. guidecheck(Goal, Found, NotFound) = [ flip(3, goto("check1"), next), flip(2, goto("check2"), goto("check3")), label("check1"), sense(ahead, goto(Found), next, Goal), sense(left_ahead, goto("turn left"), next, Goal), sense(right_ahead, goto("turn right"), goto(NotFound), Goal), label("check2"), sense(right_ahead, goto("turn right"), next, Goal), sense(ahead, goto(Found), next, Goal), sense(left_ahead, goto("turn left"), goto(NotFound), Goal), label("check3"), sense(left_ahead, goto("turn left"), next, Goal), sense(right_ahead, goto("turn right"), next, Goal), sense(ahead, goto(Found), goto(NotFound), Goal), label("turn left"), turn(left, goto(Found)), label("turn right"), turn(right, goto(Found)) ]. :- func choose_random_dir = labelled_program. choose_random_dir = [ flip(4, next, goto("end")), flip(2, next, jump(2)), turn(left, jump(2)), turn(right, next), label("end") ]. :- func wonder(condition, condition, marker, int, int) = labelled_program. wonder(Goal, Guide, Marker, Factor, StayFactor) = [ label("start"), mark(Marker, next), flip(Factor, goto("nomark start"), next), macro(goalcheck(Goal, "found", "not found"), ["found", "not found"], ["found", "not found"], 1), label("not found"), macro(guidecheck(Guide, "found", "not found"), ["found", "not found"], ["found guide", "not found guide"], 1), label("not found guide"), macro(choose_random_dir, [], [], 1), move(goto("start"), goto("start")), label("found guide"), move(next, next), macro(follow_trail(Goal, Guide, StayFactor, "found", "nofound"), ["found", "nofound"], ["found", "nomark start"], 1), label("nomark start"), macro(goalcheck(Goal, "found", "nomark not found"), ["found", "nomark not found"], ["found", "nomark not found"], 1), label("nomark not found"), macro(guidecheck(Guide, "found", "not found"), ["found", "not found"], ["nomark found guide", "nomark not found guide"], 1), label("nomark not found guide"), macro(choose_random_dir, [], [], 1), move(goto("nomark start"), goto("nomark start")), label("nomark found guide"), move(next, next), macro(follow_trail(Goal, Guide, StayFactor, "found", "nofound"), ["found", "nofound"], ["found", "nomark start"], 1), label("found") ]. :- func wonder_noguide(condition, marker, int) = labelled_program. wonder_noguide(Goal, Marker, Factor) = [ label("start"), mark(Marker, next), flip(Factor, goto("nomark start"), next), macro(goalcheck(Goal, "found", "not found"), ["found", "not found"], ["found", "not found"], 1), label("not found"), macro(choose_random_dir, [], [], 1), move(goto("start"), goto("start")), label("nomark start"), macro(goalcheck(Goal, "found", "nomark not found"), ["found", "nomark not found"], ["found", "nomark not found"], 1), label("nomark not found"), macro(choose_random_dir, [], [], 1), move(goto("nomark start"), goto("nomark start")), label("found") ]. :- func find_target_in_area(condition, condition, int, string, string) = labelled_program. find_target_in_area(Goal, Area, Factor, Found, NotFound) = [ label("begin"), sense(ahead, goto(Found), next, Goal), sense(left_ahead, goto("found left"), next, Goal), sense(right_ahead, goto("found right"), goto("not-found"), Goal), label("not-found"), % exit at random flip(Factor, goto(NotFound), next), macro(choose_random_dir, [], [], 1), sense(ahead, next, goto("not-found"), Area), move(goto("begin"), goto("begin")), label("found left"), turn(left, goto(Found)), label("found right"), turn(right, goto(Found)) ]. :- func mark_area(marker, int, int) = labelled_program. mark_area(Marker, Depth, Width) = [ sense(here, goto("end"), next, marker(Marker)), macro(mark_section(Marker, Width), [], [], Depth), label("end") ]. :- func mark_section(marker, int) = labelled_program. mark_section(Marker, Width) = [ label("start"), turn(right, next), macro(mark_bit(Marker), [], [], Width), turn(left, next), move(next, next), turn(left, next), turn(left, next), macro(mark_bit(Marker), [], [], Width), turn(right, next), move(next, next), turn(right, next) ]. :- func mark_bit(marker) = labelled_program. mark_bit(Marker) = [ mark(Marker, next), move(next, next) ]. :- func follow_trail(condition, condition, int, string, string) = labelled_program. follow_trail(Goal, Area, Factor, Found, NotFound) = [ label("begin"), sense(ahead, goto(Found), next, Goal), sense(left_ahead, goto("found left"), next, Goal), sense(right_ahead, goto("found right"), goto("not-found"), Goal), label("not-found"), % exit at random flip(Factor, goto(NotFound), next), macro(find_area(Area, "found area", "not found area"), ["found area", "not found area"], ["move", NotFound], 1), label("move"), move(goto("begin"), next), macro(choose_random_dir, [], [], 1), flip(2, goto(NotFound), goto("begin")), label("found left"), turn(left, goto(Found)), label("found right"), turn(right, goto(Found)) ]. :- func find_area(condition, string, string) = labelled_program. find_area(Area, Found, NotFound) = [ flip(2, goto("left"), goto("right")), label("left"), macro(try_dir(left, Area, "found"), ["found"], [Found], 5), flip(1, goto(NotFound), goto(NotFound)), label("right"), macro(try_dir(right, Area, "found"), ["found"], [Found], 5), flip(1, goto(NotFound), goto(NotFound)) ]. :- func try_dir(left_or_right, condition, string) = labelled_program. try_dir(Dir, Marker, Found) = [ sense(ahead, next, jump(2), Marker), move(goto(Found), next), turn(Dir, next) ].