
function WalkBot_OnLoad()
  --print1("WalkBot Loaded");

  local comlist = {"/tickleft"};
  local desc  = "";
  local id = "TICKLEFT";
  local func = function(msg)
    TickLeft();
  end
  Cosmos_RegisterChatCommand ( id, comlist, func, desc );
  local comlist = {"/tickright"};
  local desc  = "";
  local id = "TICKRIGHT";
  local func = function(msg)
    TickRight();
  end
  Cosmos_RegisterChatCommand ( id, comlist, func, desc );
  local id = "GOTO";
  local func = function(msg)
    WalkBot_Path = {};
    WalkBot_Path.n = 0;
    for xy in string.gfind(msg,"%s*(%d+,%d+)") do
      push(WalkBot_Path,xy);
    end
    if(WalkBot_Path.n == 0) then
      print1(WALKBOT_COMM_INFO);
    end
  end
  Cosmos_RegisterChatCommand ( id, WALKBOT_COMM, func, WALKBOT_COMM_INFO );
  id = "CORPSE";
  Cosmos_RegisterChatCommand ( id, WALKBOT_PC_COMM, PrintCorpseLocation, WALKBOT_PC_COMM_INFO );
  id = "GOTOCORPSE";
  Cosmos_RegisterChatCommand ( id, WALKBOT_GC_COMM, GotoCorpseLocation, WALKBOT_GC_COMM_INFO );
end

function WalkBot_Goto(xy)
  local i,j,x,y = string.find(xy,"(%d+),(%d+)");
  MoveForwardStart();
  WB_Dest = {};
  WB_Dest.x = x/100;
  WB_Dest.y = y/100;
  WalkBot_MyLoc();
end
function TickLeft(t)
  if(Tick) then
    Tick.func();
  end
  if(not t) then
    t = 0.03;
  end
  TurnLeftStart();
  Tick = {};
  Tick.func = TurnLeftStop;
  if(not t) then
    t = 0.03;
  end
  Tick.time = GetTime()+t;
end
function TickRight()
  if(Tick) then
    Tick.func();
  end
  TurnRightStart();
  Tick = {};
  Tick.func = TurnRightStop;
  if(not t) then
    t = 0.03;
  end
  Tick.time = GetTime()+t;
end

function WalkBot_OnEvent(event,arg1,arg2,arg3)
  dprint("An event?  For me???  Thanks!");
end

function WalkBot_MyLoc()
  WB_My = {};
  WB_My.x,WB_My.y = GetPlayerMapPosition("player");
  WB_My.t = GetTime();
end
function WalkBot_OnUpdate()
  if(Tick) then
    dprint("checktick");
    if(GetTime() > Tick.time) then
      Tick.func();
      Tick = nil;
      WalkBot_MyLoc();
    end
  elseif(WB_Dest) then
    if(GetTime() > WB_My.t+0.03) then
      -- evaluate current direction and adjust if necessary
      local x,y = GetPlayerMapPosition("player");
      if(x == WB_My.x and y == WB_My.y) then
        print1(WALKBOT_DONTMOVE);
	WB_Dest = nil;
	WalkBot_Path = nil;
	MoveForwardStop();
	return;
      end
      local xdist = WB_Dest.x-x;
      local ydist = WB_Dest.y-y;
      local dist = sqrt(math.pow(xdist,2)+math.pow(ydist,2));
      if(dist < 0.001) then
        print1(WALKBOT_DONE);
	MoveForwardStop();
	WB_Dest = nil;
	return;
      end
      local mydir = atan2(y-WB_My.y,x-WB_My.x);
      local destdir = atan2(ydist,xdist);
      WalkBot_MyLoc();
      dprint("Mydir: ",mydir,", destdir: ",destdir);
      if(destdir < mydir) then
        destdir = destdir + 360;
      end
      local diff = destdir - mydir;
      if(abs(diff) > 5 and abs(diff) < 355) then
	dprint("Turn, turn, turn! ",diff);
        if(diff > 180) then
	  TickLeft((diff-180) / 360);
	else
	  TickRight(diff / 360);
	end
      end
    end
  elseif(WalkBot_Path and WalkBot_Path.n > 0) then
    WalkBot_Goto(pop(WalkBot_Path));
  end
end

function PrintCorpseLocation() 
	x,y = GetCorpseMapPosition();
	x = math.floor(x*10000)/100;
	y = math.floor(y*10000)/100;
	if ( x==0 and y==0) then Print("Corpse not found.");
	else
		Print(format(WALKBOT_CORPSLOC, x, y));
	end
end

function GotoCorpseLocation()
	x,y = GetCorpseMapPosition();
	x = math.floor(x*100);
	y = math.floor(y*100);
	if ( x==0 and y==0) then Print("Corpse not found.");
	else
		Print(format(WALKBOT_GOTOCORPSLOC, x, y));
		WalkBot_Goto(x..','..y);	
	end
end
