-- FRAG v0.04
-- By: Soulkiller
-- added font to HUD,added weapon view during weapon change, added decal Over and Under layer, added bullet shooting function, new player collision method, New eboot

require "game.l"
require "config.ini"
math.randomseed(os.time())
hud = {	
	rectile = Image.load("img/HUD/rectile.png"),
	statHolder = Image.load("img/HUD/statHolder.png"),
	healthBar = Image.load("img/HUD/healthBar.png"),
	armorBar = Image.load("img/HUD/armorBar.png"),
	stamanaBar = Image.load("img/HUD/stamanaBar.png"),
	ammoHolder = Image.load("img/HUD/ammoHolder.png"),
	magazine = Image.load("img/HUD/magazine.png"),
	ammoBar = Image.load("img/HUD/ammoBar.png"),
	ammoBack = Image.load("img/HUD/ammoBack.png"),
	statHolderBack = Image.load("img/HUD/statHolderBack.png"),
	weaponSelect = Image.load("img/HUD/weaponSelect.png"),
	weaponSelectUp = Image.load("img/HUD/weaponSelectUp.png"),
	weaponSelectDown = Image.load("img/HUD/weaponSelectDown.png"),
	weaponSelectLeft = Image.load("img/HUD/weaponSelectLeft.png"),
	weaponSelectRight = Image.load("img/HUD/weaponSelectRight.png"),
	gunHolder = Image.load("img/HUD/gunHolder.png"),
	font = Font.load("fonts/HUD.ttf"),
	fontColor = Color.new(255,255,255),
}
hud.font:setPixelSizes(0,10)
mapPlayers = {
	{
		name = "player.Fang",
		type = "fang",
		collision = true,
		flag = false,
		image = 1,
		x = 0,
		y = 0,
		frame = 1,
		timer = Timer.new(0),
		delay = 60,
		freeze = false,
		angle = 90,
		team = 1,
		playerStats = {
			health = 100,
			armor = 100,
			stamana = 100,
			currentItem = 1,
			items = {
				{
					name = "M4A1",
					magazine = 4,
					clip = 32,
					maxClip = 32,
					HUDimage = 2,
					stat = 1,
				},
				{
					name = "USP",
					magazine = 5,
					clip = 12,
					maxClip = 12,
					HUDimage = 1,
					stat = 1,
				},
				{
					name = "PARA",
					magazine = 3,
					clip = 67,
					maxClip = 100,
					HUDimage = 3,
					stat = 1,
				},
				{
					name = "FRAG",
					magazine = 0,
					clip = 0,
					maxClip = 100,
					HUDimage = 4,
					stat = 1,
				},
			},
		},
	},
	{
		name = "player.guy",
		type = "fang",
		collision = true,
		flag = false,
		image = 1,
		x = 0,
		y = 0,
		frame = 1,
		timer = Timer.new(0),
		delay = 60,
		freeze = false,
		angle = 90,
		team = 1,
		playerStats = {
			health = 100,
			armor = 100,
			stamana = 100,
			currentItem = 1,
			items = {
				{
					name = "M4A1",
					magazine = 4,
					clip = 32,
					maxClip = 32,
					HUDimage = 2,
					stat = 1,
				},
				{
					name = "USP",
					magazine = 5,
					clip = 12,
					maxClip = 12,
					HUDimage = 1,
					stat = 1,
				},
				{
					name = "PARA",
					magazine = 3,
					clip = 67,
					maxClip = 100,
					HUDimage = 3,
					stat = 1,
				},
				{
					name = "FRAG",
					magazine = 0,
					clip = 0,
					maxClip = 100,
					HUDimage = 4,
					stat = 1,
				},
			},
		},
	},
}
gunStats = {
-- everything is rated 1/10
	{
		name = "USP",
		power = 3,
		spray = 2,
		fireRate = 0,
	},
}
		
HudGuns = {
	Image.load("usp.png"),
	Image.load("m4a1.png"),
	Image.load("para.png"),
	Image.load("frag.png"),
}
playerImage = {
	fang = {
		{
			name = "Fang_down",
			images = {
				Image.load("img/fang/down/fang1.png"),
				Image.load("img/fang/down/fang2.png"),
				Image.load("img/fang/down/fang3.png"),
				Image.load("img/fang/down/fang4.png"),
				Image.load("img/fang/down/fang5.png"),
				Image.load("img/fang/down/fang6.png"),
				Image.load("img/fang/down/fang7.png"),
				Image.load("img/fang/down/fang8.png"),
				Image.load("img/fang/down/fang9.png"),
				Image.load("img/fang/down/fang10.png"),
				Image.load("img/fang/down/fang11.png"),
				Image.load("img/fang/down/fang12.png"),
				Image.load("img/fang/down/fang13.png"),
				Image.load("img/fang/down/fang14.png"),
				Image.load("img/fang/down/fang15.png"),
				Image.load("img/fang/down/fang16.png"),
			},
		},
		{
			name = "Fang_left",
			images = {
				Image.load("img/fang/left/fang1.png"),
				Image.load("img/fang/left/fang2.png"),
				Image.load("img/fang/left/fang3.png"),
				Image.load("img/fang/left/fang4.png"),
				Image.load("img/fang/left/fang5.png"),
				Image.load("img/fang/left/fang6.png"),
				Image.load("img/fang/left/fang7.png"),
				Image.load("img/fang/left/fang8.png"),
				Image.load("img/fang/left/fang9.png"),
				Image.load("img/fang/left/fang10.png"),
				Image.load("img/fang/left/fang11.png"),
				Image.load("img/fang/left/fang12.png"),
				Image.load("img/fang/left/fang13.png"),
				Image.load("img/fang/left/fang14.png"),
				Image.load("img/fang/left/fang15.png"),
				Image.load("img/fang/left/fang16.png"),
			},
		},
		{
			name = "Fang_up",
			images = {
				Image.load("img/fang/up/fang1.png"),
				Image.load("img/fang/up/fang2.png"),
				Image.load("img/fang/up/fang3.png"),
				Image.load("img/fang/up/fang4.png"),
				Image.load("img/fang/up/fang5.png"),
				Image.load("img/fang/up/fang6.png"),
				Image.load("img/fang/up/fang7.png"),
				Image.load("img/fang/up/fang8.png"),
				Image.load("img/fang/up/fang9.png"),
				Image.load("img/fang/up/fang10.png"),
				Image.load("img/fang/up/fang11.png"),
				Image.load("img/fang/up/fang12.png"),
				Image.load("img/fang/up/fang13.png"),
				Image.load("img/fang/up/fang14.png"),
				Image.load("img/fang/up/fang15.png"),
				Image.load("img/fang/up/fang16.png"),
			},
		},
		{
			name = "Fang_right",
			images = {
				Image.load("img/fang/right/fang1.png"),
				Image.load("img/fang/right/fang2.png"),
				Image.load("img/fang/right/fang3.png"),
				Image.load("img/fang/right/fang4.png"),
				Image.load("img/fang/right/fang5.png"),
				Image.load("img/fang/right/fang6.png"),
				Image.load("img/fang/right/fang7.png"),
				Image.load("img/fang/right/fang8.png"),
				Image.load("img/fang/right/fang9.png"),
				Image.load("img/fang/right/fang10.png"),
				Image.load("img/fang/right/fang11.png"),
				Image.load("img/fang/right/fang12.png"),
				Image.load("img/fang/right/fang13.png"),
				Image.load("img/fang/right/fang14.png"),
				Image.load("img/fang/right/fang15.png"),
				Image.load("img/fang/right/fang16.png"),
			},
		},
	}
}
decals = {
	{
		name = "bulletImpact",
		images = {
			Image.load("bulletImpact.png"),
			Image.load("bulletImpact.png"),
			Image.load("bulletImpact.png"),
			Image.load("bulletImpact.png"),
			Image.load("bulletImpact.png"),
		},
		delay = 60,
	},
	{
		name = "blood",
		images = {
			Image.load("blood.png"),
		},
		delay = 60,
	},
}
decalUnderEntities = {}
decalOverEntities = {}
decalUnderLayer = {}
decalOverLayer = {}
for row = 1,table.getn(map.layer2) do
	decalUnderLayer[row] = {}
	decalOverLayer[row] = {}
	for column = 1,table.getn(map.layer2[row]) do
		decalUnderLayer[row][column] = 0
		decalOverLayer[row][column] = 0
	end
end
function makeCharacters()
	map.players = mapPlayers
	mapPlayers = nil
	local openSpawn = {}
	for a = 1,table.getn(map.spawnPoints) do
		openSpawn[a] = {}
		for b = 1,table.getn(map.spawnPoints[a]) do
			openSpawn[a][b] = false
		end
	end
	for length = 1,table.getn(map.players) do
		local calSpawn = true
		local spawnpoint
		while calSpawn do
			spawnpoint = math.random(1,table.getn(openSpawn[map.players[length].team]))
			if openSpawn[map.players[length].team][spawnpoint] == false then
				calSpawn = false
				openSpawn[map.players[length].team][spawnpoint] = true
			end
		end
		map.players[length].x = map.spawnPoints[map.players[length].team][spawnpoint].column
		map.players[length].y = map.spawnPoints[map.players[length].team][spawnpoint].row
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row+24)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column+24)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row+24)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column+24)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row-24)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column-24)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row-24)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column-24)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row+24)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column-24)/24)] = length*-1
		map.layer2[math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].row-24)/24)][math.ceil((map.spawnPoints[map.players[length].team][spawnpoint].column+24)/24)] = length*-1
	end
end
function printMap(cameraX,cameraY)
	local width = math.floor(480*(((cameraX-239)/480)-math.floor((cameraX-239)/480)))
	local height = math.floor(272*(((cameraY-135)/272)-math.floor((cameraY-135)/272)))
	screen:blit(0,0,map.backgroundImages[map.background[math.ceil((cameraY-135)/272)][math.ceil((cameraX-239)/480)]].image,width,height,480-width,272-height)
	screen:blit(0,272-height,map.backgroundImages[map.background[math.ceil((cameraY+136)/272)][math.ceil((cameraX-239)/480)]].image,width,0,480-width,height)
	screen:blit(480-width,0,map.backgroundImages[map.background[math.ceil((cameraY-135)/272)][math.ceil((cameraX+240)/480)]].image,0,height,width,272-height)
	screen:blit(480-width,272-height,map.backgroundImages[map.background[math.ceil((cameraY+136)/272)][math.ceil((cameraX+240)/480)]].image,0,0,width,height)
	if BLOOD_DECALS then
		local rowpos = math.floor(-24*(((cameraY-135)/24)-math.floor((cameraY-135)/24)))
		local colpos = math.floor(-24*(((cameraX-239)/24)-math.floor((cameraX-239)/24)))
		for rows = math.ceil((cameraY-135)/24),math.ceil((cameraY+136)/24) do
			for columns = math.ceil((cameraX-239)/24),math.ceil((cameraX+240)/24) do
				if decalUnderLayer[rows][columns] ~= 0 then
					if decalUnderEntities[decalUnderLayer[rows][columns]].type == "table" then
						for length = 1,table.getn(decalUnderEntities[decalUnderLayer[rows][columns]]) do
							screen:blit(colpos+(decalUnderEntities[decalUnderLayer[rows][columns]][length].x-(columns*24)),rowpos+(decalUnderEntities[decalUnderLayer[rows][columns]][length].y-(rows*24)),decals[decalUnderEntities[decalUnderLayer[rows][columns]][length].image].images[decalUnderEntities[decalUnderLayer[rows][columns]][length].frame])
							if decalUnderEntities[decalUnderLayer[rows][columns]][length].timer:time() >= decals[decalUnderEntities[decalUnderLayer[rows][columns]][length].image].delay then
								if decalUnderEntities[decalUnderLayer[rows][columns]][length].frame == table.getn(decals[decalUnderEntities[decalUnderLayer[rows][columns]][length].image].images) then
									if decalUnderEntities[decalUnderLayer[rows][columns]][length].life == "animEnd" then
										table.remove(decalUnderEntities,decalUnderLayer[rows][columns])
										decalUnderLayer[rows][columns] = 0
									else
										decalUnderEntities[decalUnderLayer[rows][columns]][length].frame = 1
									end
								else
									decalUnderEntities[decalUnderLayer[rows][columns]][length].frame = decalUnderEntities[decalUnderLayer[rows][columns]][length].frame + 1
								end
							end
						end
					else
						screen:blit(colpos+(decalUnderEntities[decalUnderLayer[rows][columns]].x-(columns*24)),rowpos+(decalUnderEntities[decalUnderLayer[rows][columns]].y-(rows*24)),decals[decalUnderEntities[decalUnderLayer[rows][columns]].image].images[decalUnderEntities[decalUnderLayer[rows][columns]].frame])
						if decalUnderEntities[decalUnderLayer[rows][columns]].timer:time() >= decals[decalUnderEntities[decalUnderLayer[rows][columns]].image].delay then
							if decalUnderEntities[decalUnderLayer[rows][columns]].frame == table.getn(decals[decalUnderEntities[decalUnderLayer[rows][columns]].image].images) then
								if decalUnderEntities[decalUnderLayer[rows][columns]].life == "animEnd" then
									table.remove(decalUnderEntities,decalUnderLayer[rows][columns])
									decalUnderLayer[rows][columns] = 0
								else
									decalUnderEntities[decalUnderLayer[rows][columns]].frame = 1
								end
							else
								decalUnderEntities[decalUnderLayer[rows][columns]].frame = decalUnderEntities[decalUnderLayer[rows][columns]].frame + 1
							end
						end
					end
				end
				colpos = colpos + 24
			end
			colpos = math.floor(-24*(((cameraX-239)/24)-math.floor((cameraX-239)/24)))
			rowpos = rowpos + 24
		end
	end
	local rowpos = math.floor(-24*(((cameraY-135)/24)-math.floor((cameraY-135)/24)))
	local colpos = math.floor(-24*(((cameraX-239)/24)-math.floor((cameraX-239)/24)))
	for rows = math.ceil((cameraY-135)/24),math.ceil((cameraY+136)/24) do
		for columns = math.ceil((cameraX-239)/24),math.ceil((cameraX+240)/24) do
			if map.layer2[rows][columns] > 0 then
				if map.layerEntities[map.layer2[rows][columns]].image ~= false then
					screen:blit(colpos+(12-(map.layerEntities[map.layer2[rows][columns]].image:width()/2)),rowpos+(12-(map.layerEntities[map.layer2[rows][columns]].image:height()/2)),map.layerEntities[map.layer2[rows][columns]].image)
				end
				-- OLD
				--if map.layerEntities[map.layemap.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].magaziner2[rows][columns]].flag ~= false then
				--	map:flags(map.layerEntities[map.layer2[rows][columns]].flagmap.layerEntities[entitie].timer.flagName,map.layerEntities[map.layer2[rows][columns]].flag.flagArg)
				--end
			elseif map.layer2[rows][columns] < 0 then
				local tableValue = math.abs(map.layer2[rows][columns])
				if type(map.players[tableValue].image) == "number" then
					if map.players[tableValue].type == "fang" then
						screen:blit(colpos+(map.players[tableValue].x-(columns*24)),rowpos+(map.players[tableValue].y-(rows*24)),playerImage.fang[map.players[tableValue].image].images[playerAnimation(tableValue)])
					end
				else
					screen:blit(colpos+(map.players[tableValue].x-(columns*24)),rowpos+(map.players[tableValue].y-(rows*24)),map.players[tableValue].image)
				end
			end
			colpos = colpos + 24
		end
		colpos = math.floor(-24*(((cameraX-239)/24)-math.floor((cameraX-239)/24)))
		rowpos = rowpos + 24
	end
	if GUNSHOT_DECALS then
		rowpos = math.floor(-24*(((cameraY-135)/24)-math.floor((cameraY-135)/24)))
		colpos = math.floor(-24*(((cameraX-239)/24)-math.floor((cameraX-239)/24)))
		for rows = math.ceil((cameraY-135)/24),math.ceil((cameraY+136)/24) do
			for columns = math.ceil((cameraX-239)/24),math.ceil((cameraX+240)/24) do
				if decalOverLayer[rows][columns] ~= 0 then
					screen:blit(colpos+(decalOverEntities[decalOverLayer[rows][columns]].x-(columns*24)),rowpos+(decalOverEntities[decalOverLayer[rows][columns]].y-(rows*24)),decals[decalOverEntities[decalOverLayer[rows][columns]].image].images[decalOverEntities[decalOverLayer[rows][columns]].frame])
					if decalOverEntities[decalOverLayer[rows][columns]].timer:time() >= decals[decalOverEntities[decalOverLayer[rows][columns]].image].delay then
						if decalOverEntities[decalOverLayer[rows][columns]].frame == table.getn(decals[decalOverEntities[decalOverLayer[rows][columns]].image].images) then
							if decalOverEntities[decalOverLayer[rows][columns]].life == "animEnd" then
								decalOverEntities[decalOverLayer[rows][columns]] = nil
								decalOverLayer[rows][columns] = 0
							else
								decalOverEntities[decalOverLayer[rows][columns]].frame = 1
							end
						else
							decalOverEntities[decalOverLayer[rows][columns]].frame = decalOverEntities[decalOverLayer[rows][columns]].frame + 1
						end
					end
				end
				colpos = colpos + 24
			end
			colpos = math.floor(-24*(((cameraX-239)/24)-math.floor((cameraX-239)/24)))
			rowpos = rowpos + 24
		end
	end
end
function controlHUD(entitie,controls)
	if ((controls:analogX() > 20 or controls:analogX() < -20 or controls:analogY() > 20 or controls:analogY() < -20)) and controller.rectileLook then
		map.players[entitie].angle = analogControl(controls)
		image_sel = map.players[entitie].angle / 90
		if image_sel <= math.floor(image_sel) + .5 then 
			image_sel = math.floor(image_sel)
		else
			image_sel = math.ceil(image_sel)
		end
		if image_sel == 0 then image_sel = 4 end
		map.players[entitie].image = image_sel
	end	
	local rise,run = riseRunAngle(map.players[entitie].angle,110)
	local x,y = 240,136
	if map.players[entitie].x < 240 then
		x = map.players[entitie].x
	elseif map.players[entitie].x > (table.getn(map.layer2[1])*24)-240 then
		x = 480*((map.players[entitie].x/480)-math.floor(map.players[entitie].x/480))
	end
	if map.players[entitie].y < 136 then
		y = map.players[entitie].y
	elseif map.players[entitie].y > (table.getn(map.layer2)*24)-136 then
		y = 272*((map.players[entitie].y/272)-math.floor(map.players[entitie].y/272))
	end
	screen:blit((x+run)-(hud.rectile:width()/2),(y+rise)-(hud.rectile:height()/2),hud.rectile)
	if controller.weaponChange then
		screen:blit(14+HudGuns[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].HUDimage]:width(),6,hud.gunHolder)
		screen:blit(116,10,HudGuns[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].HUDimage])
	end
	screen:blit(7,4,hud.ammoBack)
	screen:blit(350,2,hud.statHolderBack)
	screen:blit(7,4,hud.ammoBar,0,0,hud.ammoBar:width()*map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].clip/map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].maxClip,hud.ammoBar:height())
	screen:blit(350,3,hud.healthBar,0,0,hud.healthBar:width()*map.players[entitie].playerStats.health/100,hud.healthBar:height())
	screen:blit(350,19,hud.armorBar,0,0,hud.armorBar:width()*map.players[entitie].playerStats.armor/100,hud.armorBar:height())
	screen:blit(363,16,hud.stamanaBar,0,0,hud.stamanaBar:width()*map.players[entitie].playerStats.stamana/100,hud.stamanaBar:height())
	screen:blit(350,2,hud.statHolder)
	screen:blit(4,3,hud.ammoHolder)
	screen:fontPrint(hud.font,40,15,map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].clip.."/"..map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].maxClip,hud.fontColor)
	for magazine = 1,map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].magazine do
		screen:blit(20+(11*magazine),24,hud.magazine)
	end
	if ((controls:analogX() > 20 or controls:analogX() < -20 or controls:analogY() > 20 or controls:analogY() < -20)) and controller.weaponChange then
		local angle = analogControl(controls)
		local pos = angle / 90
		if pos <= math.floor(pos) + .5 then 
			pos = math.floor(pos)
		else
			pos = math.ceil(pos)
		end
		if pos == 0 then pos = 4 end
		if pos == 1 then
			screen:blit(7,188,hud.weaponSelect)
			screen:blit(13,224,hud.weaponSelectDown)
			if table.getn(map.players[entitie].playerStats.items) == 1 then
				screen:fontPrint(hud.font,35,248,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 2 then
				screen:fontPrint(hud.font,35,248,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,226,map.players[entitie].playerStats.items[2].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 3 then
				screen:fontPrint(hud.font,35,248,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,226,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,35,204,map.players[entitie].playerStats.items[3].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 4 then
				screen:fontPrint(hud.font,35,248,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,226,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,35,204,map.players[entitie].playerStats.items[3].name,hud.fontColor)
				screen:fontPrint(hud.font,57,228,map.players[entitie].playerStats.items[4].name,hud.fontColor)
			end
		end
		if pos == 2 then
			screen:blit(15,192,hud.weaponSelect)
			screen:blit(7,195,hud.weaponSelectLeft)
			if table.getn(map.players[entitie].playerStats.items) == 1 then
				screen:fontPrint(hud.font,43,253,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 2 then
				screen:fontPrint(hud.font,43,253,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,22,231,map.players[entitie].playerStats.items[2].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 3 then
				screen:fontPrint(hud.font,43,253,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,22,231,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,43,209,map.players[entitie].playerStats.items[3].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 4 then
				screen:fontPrint(hud.font,43,253,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,22,231,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,43,209,map.players[entitie].playerStats.items[3].name,hud.fontColor)
				screen:fontPrint(hud.font,65,233,map.players[entitie].playerStats.items[4].name,hud.fontColor)
			end
		end
		if pos == 3 then
			screen:blit(7,195,hud.weaponSelect)
			screen:blit(13,188,hud.weaponSelectUp)
			if table.getn(map.players[entitie].playerStats.items) == 1 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 2 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 3 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,35,212,map.players[entitie].playerStats.items[3].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 4 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,35,212,map.players[entitie].playerStats.items[3].name,hud.fontColor)
				screen:fontPrint(hud.font,57,236,map.players[entitie].playerStats.items[4].name,hud.fontColor)
			end
		end
		if pos == 4 then
			screen:blit(7,195,hud.weaponSelect)
			screen:blit(48,200,hud.weaponSelectRight)
			if table.getn(map.players[entitie].playerStats.items) == 1 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 2 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 3 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,35,212,map.players[entitie].playerStats.items[3].name,hud.fontColor)
			end
			if table.getn(map.players[entitie].playerStats.items) == 4 then
				screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
				screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
				screen:fontPrint(hud.font,35,212,map.players[entitie].playerStats.items[3].name,hud.fontColor)
				screen:fontPrint(hud.font,57,236,map.players[entitie].playerStats.items[4].name,hud.fontColor)
			end
		end
		if table.getn(map.players[entitie].playerStats.items) == 1 then
			if pos == 1 then
				map.players[entitie].playerStats.currentItem = pos
			end
		end
		if table.getn(map.players[entitie].playerStats.items) == 2 then
			if pos == 1 or pos == 2 then
				map.players[entitie].playerStats.currentItem = pos
			end
		end
		if table.getn(map.players[entitie].playerStats.items) == 3 then
			if pos == 1 or pos == 2 or pos == 3 then
				map.players[entitie].playerStats.currentItem = pos
			end
		end
		if table.getn(map.players[entitie].playerStats.items) == 4 then
			map.players[entitie].playerStats.currentItem = pos
		end
	elseif controller.weaponChange then
		screen:blit(7,195,hud.weaponSelect)
		if table.getn(map.players[entitie].playerStats.items) == 1 then
			screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
		end
		if table.getn(map.players[entitie].playerStats.items) == 2 then
			screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
		end
		if table.getn(map.players[entitie].playerStats.items) == 3 then
			screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
			screen:fontPrint(hud.font,35,212,map.players[entitie].playerStats.items[3].name,hud.fontColor)
		end
		if table.getn(map.players[entitie].playerStats.items) == 4 then
			screen:fontPrint(hud.font,35,256,map.players[entitie].playerStats.items[1].name,hud.fontColor)
			screen:fontPrint(hud.font,14,234,map.players[entitie].playerStats.items[2].name,hud.fontColor)
			screen:fontPrint(hud.font,35,212,map.players[entitie].playerStats.items[3].name,hud.fontColor)
			screen:fontPrint(hud.font,57,236,map.players[entitie].playerStats.items[4].name,hud.fontColor)
		end
	end
end
function playerAnimation(entitie)
	if map.players[entitie].freeze and map.players[entitie].timer:time() >= map.players[entitie].delay then
		if map.players[entitie].type == "fang" then
			if map.players[entitie].frame == table.getn(playerImage.fang[map.players[entitie].image].images) then
				map.players[entitie].frame = 1
			else
				map.players[entitie].frame = map.players[entitie].frame +1
			end
			map.players[entitie].timer:reset()
			map.players[entitie].timer:start()
		end
 	end
	return map.players[entitie].frame
end
function collisionCheck(x,y,rise,run,characterName)
	local newX = x+run
	local newY = y+rise
	local boolrise = true
	local boolrun = true
	if map.layer2[math.ceil(y/24)][math.ceil(newX/24)] == math.abs(map.layer2[math.ceil(y/24)][math.ceil(newX/24)]) then
		if map.layer2[math.ceil(y/24)][math.ceil(newX/24)] == 0 or map.layerEntities[map.layer2[math.ceil(y/24)][math.ceil(newX/24)]].collision == false then
			boolrun = false
		end
	else
		if map.layer2[math.ceil(y/24)][math.ceil(newX/24)] == 0 or map.players[math.abs(map.layer2[math.ceil(y/24)][math.ceil(newX/24)])].collision == false or characterName == map.players[math.abs(map.layer2[math.ceil(y/24)][math.ceil(newX/24)])].name then
			boolrun = false
		end
	end
	if map.layer2[math.ceil(newY/24)][math.ceil(x/24)] == math.abs(map.layer2[math.ceil(newY/24)][math.ceil(x/24)]) then
		if map.layer2[math.ceil(newY/24)][math.ceil(x/24)] == 0 or map.layerEntities[map.layer2[math.ceil(newY/24)][math.ceil(x/24)]].collision == false then
			boolrise = false
		end
	else
		if map.layer2[math.ceil(newY/24)][math.ceil(x/24)] == 0 or map.players[math.abs(map.layer2[math.ceil(newY/24)][math.ceil(x/24)])].collision == false or characterName == map.players[math.abs(map.layer2[math.ceil(newY/24)][math.ceil(x/24)])].name then
			boolrise = false
		end
	end
	return boolrise,boolrun
end
function cameraControl(mode,...)
	if cameraControlVar == nil then
		cameraControlVar = {
			mode = false,
			timer = false,
			step = 1,
			
		}
	end
	if mode == nil then
		if cameraControlVar.mode == "followEntitie" then
			-- arg[1] = Entitie
			-- arg[2] = camerax
			-- arg[3] = cameray
			local cameraX = map.players[arg[1]].x
			local cameraY = map.players[arg[1]].y
			if map.players[arg[1]].x < 240 then
				cameraX = 240
			end 
			if map.players[arg[1]].x > (table.getn(map.layer2[1])*24)-240 then
				cameraX = (table.getn(map.layer2[1])*24)-240
			end
			if map.players[arg[1]].y < 136 then
				cameraY = 136
			end
			if map.players[arg[1]].y > (table.getn(map.layer2)*24)-144 then
				cameraY = (table.getn(map.layer2)*24)-144
			end
			return cameraX,cameraY
		end
		if cameraControlVar.mode == "fixedPosition" then
			-- arg[1] = positionx
			-- arg[2] = positiony
			local cameraX = arg[1]
			local cameraY = arg[2]
			if arg[1] < 240 then
				cameraX = 240
			end 
			if arg[1] > (table.getn(map.layer2[1])*24)-240 then
				cameraX = (table.getn(map.layer2[1])*24)-240
			end
			if arg[2] < 136 then
				cameraY = 136
			end
			if arg[2] > (table.getn(map.layer2)*24)-144 then
				cameraY = (table.getn(map.layer2)*24)-144
			end
			return cameraX,cameraY
		end
		if cameraControlVar.mode == "scrollToPosition" then
			-- arg[1] = positionx
			-- arg[2] = positiony
			-- arg[3] = camerax
			-- arg[4] = cameray
			-- arg[5] = speed
			if (arg[3] <= arg[1]-arg[5] or arg[3] >= arg[1]+arg[5]) and (arg[4] <= arg[2]-arg[5] or arg[4] >= arg[2]+arg[5]) then
				local rise,run = riseRunPos(arg[3],arg[4],arg[1],arg[2])
				local cameraX,cameraY = (run * arg[5]) + arg[3],(rise * arg[5]) + arg[4]
				if cameraX < 240 then
					cameraX = 240
				end 
				if cameraX > (table.getn(map.layer2[1])*24)-240 then
					cameraX = (table.getn(map.layer2[1])*24)-240
				end
				if cameraY < 136 then
					cameraY = 136
				end
				if cameraY > (table.getn(map.layer2)*24)-144 then
					cameraY = (table.getn(map.layer2)*24)-144
				end
				return cameraX,cameraY
			else
				return arg[1],arg[2]
			end
		end
		if cameraControlVar.mode == "explosionShake" then
			-- arg[1] = positionx
			-- arg[2] = positiony
			-- arg[3] = intesity
			-- arg[4] = delay
			-- arg[5] = camerax
			-- arg[6] = cameray
			if cameraControlVar.step == 1 then
				local cameraX = arg[1] - (4*arg[3]/2)
				local cameraY = arg[2] - (2*arg[3]/2)
				if arg[1] < 240 then
					cameraX = 240
				end 
				if arg[1] > (table.getn(map.layer2[1])*24)-240 then
					cameraX = (table.getn(map.layer2[1])*24)-240
				end
				if arg[2] < 136 then
					cameraY = 136
				end
				if arg[2] > (table.getn(map.layer2)*24)-144 then
					cameraY = (table.getn(map.layer2)*24)-144
				end
				cameraControlVar.timer = Timer.new(0)
				cameraControlVar.timer:start()
				cameraControlVar.step = 2
				return cameraX,cameraY
			end
			if cameraControlVar.timer:time() >= arg[4] then
				if cameraControlVar.step == 2 then
					local cameraX = arg[1] + (4*arg[3]/2)
					local cameraY = arg[2] - (2*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
					cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 3
					return cameraX,cameraY
				elseif cameraControlVar.step == 3 then
					local cameraX = arg[1] - (4*arg[3]/2)
					local cameraY = arg[2] + (2*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 4
					return cameraX,cameraY
				elseif cameraControlVar.step == 4 then
					local cameraX = arg[1] - (4*arg[3]/2)
					local cameraY = arg[2] - (2*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 5
					return cameraX,cameraY
				elseif cameraControlVar.step == 5 then
					local cameraX = arg[1] + (2*arg[3]/2)
					local cameraY = arg[2] + (1.5*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 6
					return cameraX,cameraY
				elseif cameraControlVar.step == 6 then
					local cameraX = arg[1] + (2*arg[3]/2)
					local cameraY = arg[2] - (1.5*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 7
					return cameraX,cameraY
				elseif cameraControlVar.step == 7 then
					local cameraX = arg[1] - (2*arg[3]/2)
					local cameraY = arg[2] + (1.5*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 8
					return cameraX,cameraY
				elseif cameraControlVar.step == 8 then
					local cameraX = arg[1] - (2*arg[3]/2)
					local cameraY = arg[2] - (1.5*arg[3]/2)
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 9
					return cameraX,cameraY
				elseif cameraControlVar.step == 9 then
					local cameraX = arg[1]
					local cameraY = arg[2]
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 10
					return cameraX,cameraY
				elseif cameraControlVar.step == 10 then
					local cameraX = arg[1]
					local cameraY = arg[2]
					if arg[1] < 240 then
						cameraX = 240
					end 
					if arg[1] > (table.getn(map.layer2[1])*24)-240 then
						cameraX = (table.getn(map.layer2[1])*24)-240
					end
					if arg[2] < 136 then
						cameraY = 136
					end
					if arg[2] > (table.getn(map.layer2)*24)-144 then
						cameraY = (table.getn(map.layer2)*24)-144
					end
					cameraControlVar.timer:reset()
					cameraControlVar.timer:start()
					cameraControlVar.step = 1
					return cameraX,cameraY,true
				end
			else
				return arg[5],arg[6]
			end
		end
	else
		cameraControlVar.mode = mode
	end
end
function bulletTrail(x,y,angle,line,endLine)
	local rise,run = riseRunAngle(angle,1)
	local pX,pY = x,y
	local pXX,pYY
	local riseA,runA
	for a = 1,100 do
		screen:blit(pX,pY,line,true)
		pX = pX+run
		pY = pY+rise
		if angle <= 90 then
			riseA,runA = riseRunAngle(360-(90-angle),1)
		else
			riseA,runA = riseRunAngle(angle-90,1)
		end
		pXX,pYY = math.floor(pX+runA),math.floor(pY+riseA)
		screen:blit(pXX,pYY,endLine,true)
		if angle > 270 then
			riseA,runA = riseRunAngle(angle-270,1)
		else
			riseA,runA = riseRunAngle(angle+90,1)
		end
		pXX,pYY = math.floor(pX+runA),math.floor(pY+riseA)
		screen:blit(pXX,pYY,endLine,true)
	end
end
function playerMove(entitie,controls)
	if STOP_LOOK then
		if (controls:analogX() > 20 or controls:analogX() < -20 or controls:analogY() > 20 or controls:analogY() < -20) and not controller.rectileLook and not controller.weaponChange then
			if not map.players[entitie].freeze then map.players[entitie].freeze = true end
			local angle,dis = analogControl(controls,5)
			local rise,run = riseRunAngle(angle,dis)
			local calCollision = false
			if math.ceil(map.players[entitie].x/24) ~= math.ceil((map.players[entitie].x+run)/24) then
				calCollision = true
			end
			if math.ceil(map.players[entitie].y/24) ~= math.ceil((map.players[entitie].y+rise)/24) then
				calCollision = true
			end 
			if calCollision then
				local boolrise,boolrun = collisionCheck(map.players[entitie].x,map.players[entitie].y,rise,run,map.players[entitie].name)
				printrise,printrun = boolrise,boolrun
				if boolrise then
					rise = 0
				end
				if boolrun then
					run = 0
				end
				map.layer2[math.ceil(map.players[entitie].y/24)][math.ceil(map.players[entitie].x/24)] = 0
				if map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil(map.players[entitie].x/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil(map.players[entitie].x/24)] = 0
				end
				if map.layer2[math.ceil(map.players[entitie].y/24)][math.ceil((map.players[entitie].x+24)/24)] == entitie*-1 then
					map.layer2[math.ceil(map.players[entitie].y/24)][math.ceil((map.players[entitie].x+24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x+24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x+24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y)/24)][math.ceil((map.players[entitie].x-24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y)/24)][math.ceil((map.players[entitie].x-24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x-24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x-24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x+24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x+24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x-24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x-24)/24)] = 0
				end
				map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run)/24)] = entitie*-1
				if map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run+24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run+24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run-24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run-24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] = entitie*-1
				end
			end
			return run,rise
		else
			if map.players[entitie].freeze then map.players[entitie].freeze = false end
			return 0,0
		end
	else
		if (controls:analogX() > 20 or controls:analogX() < -20 or controls:analogY() > 20 or controls:analogY() < -20) and not controller.weaponChange then
			if not map.players[entitie].freeze then map.players[entitie].freeze = true end
			local angle,dis = analogControl(controls,5)
			local rise,run = riseRunAngle(angle,dis)
			local calCollision = false
			if math.ceil(map.players[entitie].x/24) ~= math.ceil((map.players[entitie].x+run)/24) then
				calCollision = true
			end
			if math.ceil(map.players[entitie].y/24) ~= math.ceil((map.players[entitie].y+rise)/24) then
				calCollision = true
			end 
			if calCollision then
				local boolrise,boolrun = collisionCheck(map.players[entitie].x,map.players[entitie].y,rise,run,map.players[entitie].name)
				printrise,printrun = boolrise,boolrun
				if boolrise then
					rise = 0
				end
				if boolrun then
					run = 0
				end
				map.layer2[math.ceil(map.players[entitie].y/24)][math.ceil(map.players[entitie].x/24)] = 0
				if map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil(map.players[entitie].x/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil(map.players[entitie].x/24)] = 0
				end
				if map.layer2[math.ceil(map.players[entitie].y/24)][math.ceil((map.players[entitie].x+24)/24)] == entitie*-1 then
					map.layer2[math.ceil(map.players[entitie].y/24)][math.ceil((map.players[entitie].x+24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x+24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x+24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y)/24)][math.ceil((map.players[entitie].x-24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y)/24)][math.ceil((map.players[entitie].x-24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x-24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x-24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x+24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y-24)/24)][math.ceil((map.players[entitie].x+24)/24)] = 0
				end
				if map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x-24)/24)] == entitie*-1 then
					map.layer2[math.ceil((map.players[entitie].y+24)/24)][math.ceil((map.players[entitie].x-24)/24)] = 0
				end
				map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run)/24)] = entitie*-1
				if map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run+24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run+24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run-24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise)/24)][math.ceil((map.players[entitie].x+run-24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise+24)/24)][math.ceil((map.players[entitie].x+run-24)/24)] = entitie*-1
				end
				if map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] == 0 then
					map.layer2[math.ceil((map.players[entitie].y+rise-24)/24)][math.ceil((map.players[entitie].x+run+24)/24)] = entitie*-1
				end
			end
			return run,rise
		else
			if map.players[entitie].freeze then map.players[entitie].freeze = false end
			return 0,0
		end
	end
end
function bulletPath(entitie,angle)
	local rise,run = riseRunAngle(angle,5)
	local checkCol = true
	local newX,newY = map.players[entitie].x,map.players[entitie].y
	while checkCol do
		newX,newY = newX+run,newY+rise
		if map.layer2[math.ceil(newY/24)][math.ceil(newX/24)] == math.abs(map.layer2[math.ceil(newY/24)][math.ceil(newX/24)]) then
			if map.layer2[math.ceil(newY/24)][math.ceil(newX/24)] ~= 0 and map.layerEntities[map.layer2[math.ceil(newY/24)][math.ceil(newX/24)]].collision == true then
				checkCol = false
			end
		else	
			if map.layer2[math.ceil(newY/24)][math.ceil(newX/24)] ~= 0 and map.players[math.abs(map.layer2[math.ceil(newY/24)][math.ceil(newX/24)])].collision == true and entitie ~= math.abs(map.layer2[math.ceil(newY/24)][math.ceil(newX/24)]) then
				checkCol = false
			end
		end
	end
	return newX,newY
end
line = Image.load("middle.png")
endLine = Image.load("end.png")
function uspShoot(entitie)
	math.randomseed(os.time())
	local colideX,colideY,boolRun,boolRise = bulletPath(entitie,map.players[entitie].angle)
	if math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)]) ~= map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)] then
		local distance = math.sqrt(((map.players[entitie].x-map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].x)^2)+((map.players[entitie].y-map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].y)^2))
		local damageRating = 0
		bulletTrail(map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].x,map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].y,map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].angle,line,endLine)
		if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
			local damage = math.random((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)-((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)/12),(gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)+((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)/12))
			map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
		else
			local damage = math.random((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)-((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)/12),(gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)+((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*800/distance)/12))
			map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
		end
--		******OLD DAMAGE********
--		if distance <= 72 then
--			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
--				local damage = math.random((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)-((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)/12),(gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)+((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
--				if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
--			else
--				local damage = math.random((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)-((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)/12),(gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)+((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
--			end
--			damageRating = 1
--			--point blank
--		elseif distance <= 144 then
--			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
--				if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
---			else
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
--			end
--			damageRating = 2
--			--shooting range
--		elseif distance <= 192 then
--			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/3)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/3)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
--				if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
---			else
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/3)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/3)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
--			end
--			damageRating = 3
--			--fair distance
--		elseif distance <= 240 then
--			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/4)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/4)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
--				if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
--			else
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/4)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/4)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
--			end
--			damageRating = 4
--			--hard to aim
--		elseif distance <= 360 then
--			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/5)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/5)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
--				if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
--			else
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/5)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/5)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
--			end
--			damageRating = 5
--			--can't see
--		else
--			if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor > 0 then
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/6)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/6)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor-damage
--				if map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor < 0 then map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.armor = 0 end
--			else
--				local damage = math.random(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)-(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/2)*10)/12),((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/6)*10)+(((gunStats[map.players[entitie].playerStats.items[map.players[entitie].playerStats.currentItem].stat].power/6)*10)/12))
--				map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health = map.players[math.abs(map.layer2[math.ceil(colideY/24)][math.ceil(colideX/24)])].playerStats.health-damage
--			end
--			damageRating = 6
--			--snipper
--		end
		if BLOOD_DECALS then
			math.randomseed(os.time())
			colideX = (math.ceil(colideX/24)*24)+math.random(1,24)
			colideY = (math.ceil(colideY/24)*24)+math.random(1,24)
			if decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)] ~= 0 then
				if decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].type == "table" then
					table.insert(decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]],{x=colideX,y=colideY,image=2,life = "forever",frame = 1,timer = Timer.new()})
				else
					decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]] = {
						type = "table",
						{x = decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].x,y = decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].y,image=decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].image,life=decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].life,frame=decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].frame,timer=decalUnderEntities[decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)]].timer},
						{x=colideX,y=colideY,image=2,life = "forever",frame = 1,timer = Timer.new()},
					}
				end
			else
				table.insert(decalUnderEntities,{x=colideX,y=colideY,image=2,type = "single",life = "forever",frame = 1,timer = Timer.new()})
				decalUnderLayer[math.ceil(colideY/24)][math.ceil(colideX/24)] = table.getn(decalUnderEntities)
			end
		end
	else
		if GUNSHOT_DECALS then
			colideX = (math.ceil(colideX/24)*24)
			colideY = (math.ceil(colideY/24)*24)
			table.insert(decalOverEntities,{x=colideX,y=colideY,image=1,life = "animEnd",frame = 1,timer = Timer.new()})
			decalOverLayer[math.ceil(colideY/24)][math.ceil(colideX/24)] = table.getn(decalOverEntities)
		end
	end
end
function buttonConfig(controls)
	if RECTILE_LOOK == "L" then
		controller.rectileLook = controls:l()
	elseif RECTILE_LOOK == "R" then
		controller.rectileLook = controls:r()
	elseif RECTILE_LOOK == "UP" then
		controller.rectileLook = controls:up()
	elseif RECTILE_LOOK == "DOWN" then
		controller.rectileLook = controls:down()
	elseif RECTILE_LOOK == "LEFT" then
		controller.rectileLook = controls:left()
	elseif RECTILE_LOOK == "RIGHT" then
		controller.rectileLook = controls:right()
	elseif RECTILE_LOOK == "CROSS" then
		controller.rectileLook = controls:cross()
	elseif RECTILE_LOOK == "SQUARE" then
		controller.rectileLook = controls:square()
	elseif RECTILE_LOOK == "CIRCLE" then
		controller.rectileLook = controls:circle()
	elseif RECTILE_LOOK == "TRIANGLE" then
		controller.rectileLook = controls:triangle()
	end
	if WEAPON_CHANGE == "L" then
		controller.weaponChange = controls:l()
	elseif WEAPON_CHANGE == "R" then
		controller.weaponChange = controls:r()
	elseif WEAPON_CHANGE == "UP" then
		controller.weaponChange = controls:up()
	elseif WEAPON_CHANGE == "DOWN" then
		controller.weaponChange = controls:down()
	elseif WEAPON_CHANGE == "LEFT" then
		controller.weaponChange = controls:left()
	elseif WEAPON_CHANGE == "RIGHT" then
		controller.weaponChange = controls:right()
	elseif WEAPON_CHANGE == "CROSS" then
		controller.weaponChange = controls:cross()
	elseif WEAPON_CHANGE == "SQUARE" then
		controller.weaponChange = controls:square()
	elseif WEAPON_CHANGE == "CIRCLE" then
		controller.weaponChange = controls:circle()
	elseif WEAPON_CHANGE == "TRIANGLE" then
		controller.weaponChange = controls:triangle()
	end
end
function cps(CpsPrint,memory)
	if cpsVar == nil then
		cpsVar = {
			time = Timer.new(),
			cps = 0,
			cpsprint = 0,
			boxprint = Image.createEmpty(100,8)
		}
		cpsVar.time:start()
		cpsVar.boxprint:clear(Color.new(0,0,0))
	end
	cpsVar.cps = cpsVar.cps + 1
	if cpsVar.time:time() > 1000 then
		cpsVar.cpsprint = cpsVar.cps
		cpsVar.cps = 0
		cpsVar.time:stop()
		cpsVar.time:reset()
		cpsVar.time:start()
	end
	if CpsPrint then
		screen:blit(380,0,cpsVar.boxprint)
		screen:print(380,0,"CPS = "..cpsVar.cpsprint,Color.new(255,255,255))
	end
	if memory then
		screen:blit(380,8,cpsVar.boxprint)
		screen:print(380,8,"RAM = ".. System.getFreeMemory()/ 1024 .. "kb",Color.new(255,255,255))
	end
end
camerax = 240
cameray = 136
rise = 0
run = 0
white = Color.new(255,255,255)
cycle = 1
cameraControl("followEntitie")
controller = {
	rectileLook = false,
	weaponChange = false,
}
image_sel = 0
if FRAMESKIP > 0 then
	frame_counter = 0
end
oldpad = Controls.read()
makeCharacters()
controlPlayer = 1
while true do
	pad = Controls.read()
	buttonConfig(pad)
	run,rise = playerMove(controlPlayer,pad)
	camerax,cameray = cameraControl(nil,controlPlayer,camerax,cameray)
	map.players[controlPlayer].x = map.players[controlPlayer].x + run
	map.players[controlPlayer].y = map.players[controlPlayer].y + rise
	if frame_counter == FRAMESKIP then
		screen:clear()
		printMap(camerax,cameray)
		controlHUD(controlPlayer,pad)
	end
	if pad:cross() and pad:cross() ~= oldpad:cross() then
		if map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].clip > 0 then
			uspShoot(controlPlayer)
			map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].clip = map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].clip - 1
		end
	end
	if pad:square() and pad:square() ~= oldpad:square() then
		if map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].magazine > 0 then
			map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].magazine = map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].magazine - 1
			map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].clip = map.players[controlPlayer].playerStats.items[map.players[controlPlayer].playerStats.currentItem].maxClip
		end
	end
	if pad:circle() and pad:circle() ~= oldpad:circle() then
		if controlPlayer == 1 then controlPlayer = 2 else controlPlayer = 1 end
	end
	if pad:start() then
		break
	end
	if pad:select() and pad:select() ~= oldpad:select() then
		local directory = {}
		local counter = 1
		for index, file in System.listDirectory(System.currentDirectory()) do
			directory[counter] = file.name
			counter = counter+1
		end
		local max = 0
		for length = 1,table.getn(directory) do
			for length2 = 1,table.getn(directory) do
				if directory[length2] == "screenshot"..length..".png" then
					max = length
				end
			end
		end
		max = max+1
		screen:save("screenshot"..max..".png")
	end
	cps(DISPLAY_CPS,DISPLAY_RAM)
	if frame_counter == FRAMESKIP then
		screen.waitVblankStart()
		screen.flip()
		frame_counter = 0
	else
		frame_counter = frame_counter+1
	end
	oldpad = pad
end
