--------------------------------------------------------------------------------
-- SimNetwork
-- globals.lua
-- Global variables and commonly used functions
--------------------------------------------------------------------------------

SimNetwork_GLOBAL = true

viewport	= {}
scene2d		= nil
gameScene	= nil

profitBalance = 1000			--records player's profit
overallCustSatisfaction = 100 	--records ovarall customer satisfaction (for all customers)

gameEnd = false 				-- indicates if the game has ended

-- -----------------------------------------------------------------------------
-- Function: init_viewport()
-- Inputs: none
-- Returns: none
--
-- Initialize extend of the viewport (in order to scale the game layout to any 
-- arbitrary display size
function init_viewport()
	viewport.x, viewport.y, viewport.w, viewport.h = E.get_display_union()
                             
	local width = 512 + 384 * viewport.w / viewport.h

	viewport.a = viewport.h / viewport.w
	viewport.k = viewport.w / width
	
	viewport.h =  width * viewport.a
	viewport.B = -width * viewport.a / 2 * viewport.k
	viewport.T =  width * viewport.a / 2 * viewport.k
			     
	viewport.w =  width 
	viewport.L = -width / 2 * viewport.k
	viewport.R =  width / 2 * viewport.k
    
	--Debug
	if debug then

		E.print_console("Viewportx:", viewport.x)
		E.print_console("\n","Viewporty:",viewport.y)
		E.print_console("\n","Viewportw:",viewport.w)
		E.print_console("\n","Viewporth:",viewport.h)
		E.print_console("\n","Viewporta:",viewport.a)
		E.print_console("\n","Viewportk:",viewport.k)
		E.print_console("\n","ViewportB:",viewport.B)
		E.print_console("\n","ViewportT:",viewport.T)
		E.print_console("\n","ViewportL:",viewport.L)
		E.print_console("\n","ViewportR:",viewport.R)
	end
end

--------------------------------------------------------------------------------
-- Function: make_sprite()
-- Inputs: filename
-- Returns: sprite
--
-- Creates new sprite using given filename, return sprite
function make_sprite(filename)
	local image = E.create_image(filename)
	local brush = E.create_brush()
	E.set_brush_flags(brush, E.brush_flag_transparent, false)
	E.set_brush_flags(brush, E.brush_flag_unlit,       true)
	E.set_brush_flags(brush, E.brush_flag_diffuse,     false)
	E.set_brush_flags(brush, E.brush_flag_specular,     true)
	E.set_brush_flags(brush, E.brush_flag_ambient,     true)
	E.set_brush_flags(brush, E.brush_flag_shiny,    true)
	E.set_brush_color(brush, 1.0, 1.0, 1.0, 1.0)
	E.set_brush_image(brush, image)
	
	return E.create_sprite(brush)
end

--------------------------------------------------------------------------------
-- Function: create_overlay()
-- Inputs: file, scale, posx, posy, posz
-- Returns: sprite
--
-- Create new sprite, add it to scene2d, set its position and scale on the scene 
-- and return it as object
function create_overlay(filename, scale, posx, posy, posz)
	
	if scale == nil then scale = 1 end
	if posx == nil then posx = 0 end
	if posy == nil then posy = 0 end
	if posz == nil then posz = 0 end
	
	sprite = make_sprite(filename)
	E.parent_entity   (sprite, scene2d)
	E.set_entity_scale(sprite, scale, scale, scale)
	E.set_entity_position(sprite, posx,posy,posz)
	return sprite
end

--------------------------------------------------------------------------------
-- Function: hide(entity)
-- Inputs: entity
-- Returns: entity
-- Hide entity
function hide(entity)
	E.set_entity_flags(entity, E.entity_flag_hidden, true)
	return entity
end                     

--------------------------------------------------------------------------------
-- Function: show(entity)
-- Inputs: entity
-- Returns: entity
-- Show entity
function show(entity)
	E.set_entity_flags(entity, E.entity_flag_hidden, false)
	return entity
end

--------------------------------------------------------------------------------
-- Function: v_mult()
-- Inputs: 3 tuple vector, scalar
-- Returns: vector
-- Multiplies the input vector by the scalar and returns the result
function v_mult(v, c)
	local s = c
	return {v[1]*s, v[2]*s, v[3]*s}
end

--------------------------------------------------------------------------------
-- Function: distance()
-- Inputs:	p, q: two points in the 3D space
-- Returns:	distance between p and q
function distance(p, q)
	return math.sqrt((p[1]-q[1])^2 + (p[2] - q[2])^2 + (p[3] - q[3])^2)
end

