--------------------------------------------------------------------------------
-- 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: isHidden(entity)
-- Inputs: Electro entity
-- Returns: Boolean
-- Hide entity
function isHidden(entity)
	return E.get_entity_flags(entity, E.entity_flag_hidden)
end

--------------------------------------------------------------------------------
-- Function: hide(entity)
-- Inputs: Electro entity
-- Returns: Electro entity
-- Hides 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: v_mult()
-- Inputs: 2 vectors of floats/integers: x, y
-- Returns: vector x + y
-- Returns the sum vector
function v_add(x, y)
	local temp = {}
	for i=1, table.getn(x) do
		temp[i] = x[i] + y[i]
	end
	return temp
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

--------------------------------------------------------------------------------
-- Function:	searchTable()
-- Inputs:	table: a table
--		field: the field (of table elements) to compare value against
--		value: value we're looking for
function searchTable(table, field, value)

	for index, element in table do
		if field ~= nil and field ~= "" then
			if element[field] == value then
				return index
			end
		else
			if element == value then
				return index
			end
		end
	end
end

-------------------------------------------------------------------------------
-- Function:	copyTable()
-- Note: table must be simple (i.e, contains no other tables as elements),
-- otherwise, 2nd level tables are not duplicated, instead, they're referenced
-- from
function copyTable(table)
	local temp = {}
	for index, element in table do
		temp[index] = element
	end
	return temp
end

