separate(gameutils)

--NOTICE:  typically, dt<<0.1 second  (between iterations of main loop)
--         but when it is that big, it's probably because foldtime is stale
procedure moveforward( currenttime: gldouble ) is
	dt : gldouble := currenttime-foldtime;
	et : float := float(dt);
	--lagfac : constant float := 1.0;

	yyme, yhalf,
	kxl,kxh,kyl,kyh,kzl,kzh,
	okxl,okxh, okzl, okzh: float;
	ixmx: float;
begin

if et>0.1 then et:=0.0; end if;
foldtime:=currenttime;

if scene=4 then
	ixmx:=ixmax/3.0;
else
	ixmx:=ixmax;
end if;

if 
	not gatewait
	and not labwait
	and not lionwait
	and not mazewait
	--and not batwait
then

	oxme:=xme;
	oyme:=yme;
	ozme:=zme;

	xme:=xme+et*speed*fspd*xlook;
	zme:=zme+et*speed*fspd*zlook;
	direction:=1;

	-- Velocity Direction
	veldir := fmath.arctan(xlook,zlook);

	-- set yme appropriately:
	if scene=2 then
		yme := aheight;
	elsif not interior then
		yme := aheight + land_alt(xme,zme);
	else
		yme := -iymax+aheight;
	end if;




	if scene=2 then -- (x,z) in [-10..0, -10..0]

		if (xme>-margin) then xme:=-margin; end if;
		if (xme<-10.0+margin) then xme:=-10.0+margin; end if;
		if (zme>-margin) then zme:=-margin; end if;
		if (zme<-10.0+margin) then zme:=-10.0+margin; end if;

	elsif interior then

		-- limit pos to be within walls:
		if (xme>+ixmx-margin) then xme:=+ixmx-margin; end if;
		if (xme<-ixmx+margin) then xme:=-ixmx+margin; end if;
		if (zme>+izmax-margin) then zme:=+izmax-margin; end if;
		if (zme<-izmax+margin) then zme:=-izmax+margin; end if;

	else --exterior scene

		-- limit pos to be within user bounds:
		if (xme>+xmaxu-margin) then xme:=+xmaxu-margin; end if;
		if (xme<-xmaxu+margin) then xme:=-xmaxu+margin; end if;
		if (zme>+zmaxu-margin) then zme:=+zmaxu-margin; end if;
		if (zme<-zmaxu+margin) then zme:=-zmaxu+margin; end if;

	end if;


	-- set yhalf appropriately:
	if scene=2 then
		yyme:=aheight;
		yhalf:=yyme-aheight/2.0;
	elsif scene=1 then
		yyme:=aheight;
		yhalf:=yyme-aheight/2.0;
	else
		yyme:=-iymax+aheight;
		yhalf:=yyme-aheight/2.0;
	end if;





	-- further, limit pos to avoid ko zones:
	for i in 1..nko loop
	if scene=koscene(i) and i/=kgate then --this KO applies here

		kxl:=xme-koxlo(i)+margin;
		kxh:=koxhi(i)+margin-xme;

		kyl:=yhalf-koylo(i);
		kyh:=koyhi(i)-yhalf;

		kzl:=zme-kozlo(i)+margin;
		kzh:=kozhi(i)+margin-zme;


		if (kxl*kxh>0.0) and (kyl*kyh>0.0) and (kzl*kzh>0.0) then 
		--intrusion into ko

			okxl:=oxme-koxlo(i)+margin;
			okxh:=koxhi(i)+margin-oxme;
			okzl:=ozme-kozlo(i)+margin;
			okzh:=kozhi(i)+margin-ozme;

			if      ( (okxl*okxh>0.0) and (okzl*okzh<=0.0) ) then
				zme:=ozme;
			elsif ( (okzl*okzh>0.0) and (okxl*okxh<=0.0) ) then
				xme:=oxme;
			end if;

		end if; --intrusion into ko

	end if;
	end loop; --for i

	updategamestate;

end if;

end moveforward;


