
--
-- Copyright (C) 2022  <fastrgv@gmail.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You may read the full text of the GNU General Public License
-- at <http://www.gnu.org/licenses/>.
--


with matutils;  use matutils;
with gametypes;  use gametypes;
with gl; use gl;
with text_io;



with glfw3; use glfw3;
with zoomwheel;



with ellipsoid;
with w3treeobj;
with circtexsurfobj;
with annusurfobj;




package gameutils is


	mainWindow : access GLFWwindow;


	procedure myassert( 
		condition : boolean;  
		flag: integer:=0;
		msg: string := "");

	function odd( i: integer ) return boolean;

	function sqr( x: float ) return float;
	function min( x,y: float ) return float;
	function mini( i,j: integer ) return integer;

	function atleftportal return boolean;
	function atrightportal return boolean;
	function atexit return boolean;

	function intersectsko( 
		x0,y0,z0, x1,y1,z1 : float;  
		k : in out integer ) return boolean;

	function inpool( xx,yy,zz : float ) return boolean;

	function anybarrelisat( xx,zz : float ) return boolean;

	procedure testbarrel(  nbx,nby,nbz: in out float;
		wasmoved, hitko, hitwall : in out boolean );

	procedure testPortals;

	function ypos( 
		nowtime, yp0, yvinit, deltaSecs : float ) return float;

	function distance( x1,y1,z1, x2,y2,z2 : float ) return float;
	function hordistance( x1,y1, x2,y2 : float ) return float;

	procedure moveforward( currenttime: gldouble );
	procedure movebackward( currenttime: gldouble );

	procedure shootleft( mbtntime: gldouble );
	procedure shootright( mbtntime: gldouble );

	procedure updategamestate( currenttime : gldouble );

	procedure drawbeams( nowtime : gldouble );

	procedure output( a : mat44 );


	procedure handle_gc_look(gcx,gcy,slufac:float);
	procedure handle_gc_move(nowTime:gldouble; gcx,gcy:float);



	procedure readPuzzle( level: integer );

	----------------------------------------------------------

	procedure island_texture_setup;

	procedure prepwormhole( 
		currenttime : gldouble;
		oxme,oyme,ozme, 
		nxme,nyme,nzme, 
		fxme,fyme,fzme : float );

	procedure worm(
		currenttime: gldouble;
		xme,yme,zme : in out float );
		--worming : in out boolean );


	procedure drawWormHole( nowTime: gldouble; lmvp: mat44 );

	-- lava turtle:
	procedure updateFerryPos( 
		tm: in float; 
		xb,yb,zb,ab, xh,yh,zh,ah: out float );


	function dumpGLerrorQueue(id: string) return integer;

	---------------------------------------------------------
	procedure updateCamera(init: boolean := false);
	procedure slewToAv;
	procedure slewToAvLook;

	procedure drawAvatar( mytime:gldouble; xme,yme,zme,hang : float );

	procedure aim(x,y,z: out float);

	procedure updateMVP( wid,hit : float);
	procedure updateMVP2( wid,hit, 
		xpos,ypos,zpos,
		cxluk,cyluk,czluk,
		xrt,yrt,zrt : float);

	--circular angle is reset to lie between -pi..+pi
	function mindifangl( angle: float ) return float;

	function branchnear( angAva, angCam : float ) return float;

	procedure updateVert;

	procedure fclamp(x: in out float; a, b : float);



	function is_blank( line : string; len:integer ) return boolean;

	procedure GetInt( Rcd:string;
							Bgn:in out natural;
							Int: in out gl.glint );

	procedure getNbInt(
		tfile:text_io.file_type; 
		rcd: in out string; 
		k: in out gl.glint);

	procedure GetFlt( Rcd:string;
						  Bgn:in out natural;
						  Flt: in out float );

	procedure getNbFlt(
		tfile:text_io.file_type; 
		rcd: in out string; 
		t: in out float);


	procedure setup_textures;
	procedure release_textures;

	function oppo( dir: dirtype ) return dirtype;

	procedure drawKawhoosh( 
		kcenx,kceny,kcenz, kradx,krady,kradz: glfloat;
		levl: glint;
		matr: mat44
		);

------------ newly visible ftns 24apr18 -------------------------


function signum( x : float ) return float;

function sandht( x,z, hmax, wlev, r1, r2 : float ) return float;

procedure mtestbarrel( nbx,nby,nbz: in out float;
	wasmoved, hitko : in out boolean );




-- Mac Mini 2009 limits:
-- note that ocean(200x200) runs fine
-- but island(16x10 or 30x5) = maximum 

-- 60 angles, 20 rings
-- WAAAAY better than previous (16,10)
--package isandy is new circtexsurfobj(60,20,sandht); -- 12mar16

--more rings essential to monkey keeping feet on ground:
package isandy is new circtexsurfobj(60,40,sandht); -- 25may18
island : isandy.circtex; -- prolog sandy island


-- note that ocean(500,200) & island(300,20) is jerky 
-- on MBPro, with or without HDPI.
-- Reduce these numbers if your graphics are jerky...
-- 100 angles, 100 rings for prolog ocean
--package sea is new annusurfobj(100,100); --good for Mac HiDpi
package sea is new annusurfobj(100,200);
	ocean   : sea.annusurf;



package ellobj is new ellipsoid(20,20);
mars : ellobj.eloid;
jupit : ellobj.eloid;


function max( x,y : float ) return float;


procedure updateMonkey( 
	mytime:gldouble; xme,zme: float;
	xm,ym,zm,angm: in out float;
	idir: out integer
	);


procedure drawMonkey( 
	mytime:gldouble; xme,yme,zme,hang : float;
	idir: integer
	);



--================ new stuff from island ======================

procedure pickLeft; --used on island

procedure myst_movebackward( currenttime: gldouble );

procedure myst_moveforward( currenttime: gldouble );




end gameutils;
