-------------------------------------
-- Chernobyl
-- September 1 - 2009
-- Rick Dark
-- Waffler

-- thnx to reich rp for teh script
--kane
-- looter-
--ghost
--otis

--to do: ammo boxes  --done --NOT WORKING PROPERLY
-- fix item and gun prices( correct them ) --done --no need to do more
-- loot bodies  --done --maybe do a menu?
-- put old chat box back  --done

-- Do STALKER Style Weapon System( 1 rifle, sidearm )
-- Properties?
-- fix bugs when making new character while ingame
-- fix ProcessBar bugs
-- fix items
-- do faction spawning
-- find which anomoly which is fucking up
-- artifacts
-- design a new scoreboard maybe
-- redo all of the copy-paste TS1 Code. --done sqlite lol
-------------------------------------

require( "tmysql" )

DeriveGamemode( "Gear" );
DeriveGamemode( "sandbox" );

function CreatePlayerTables()

	sql.Query( "CREATE TABLE IF NOT EXISTS STALKERChars ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, SteamID TEXT, RPName TEXT, Model TEXT, Rubles INTEGER, Age INTEGER, Title TEXT, STATAim INTEGER, STATStrength INTEGER, STATSpeed INTEGER, STATEndurance INTEGER, SecondTitle TEXT, containers TEXT, Items TEXT, PlayerFlags TEXT );" );
	
end

CreatePlayerTables()

function CreateUserTables()

	sql.Query( "CREATE TABLE IF NOT EXISTS STALKERUsers ( userid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, SteamID TEXT, UserName TEXT, HasToolTrust TEXT, CanDupe TEXT, FactionFlags TEXT );" );
	
end	

CreateUserTables()

GM.Name = "LOLDONGS?!";

--require( "ts2" );
AddCSLuaFile( "cl_scoreboard.lua" );
AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );
AddCSLuaFile( "cl_hud.lua" );
AddCSLuaFile( "cl_radiochat.lua" );
AddCSLuaFile( "cl_umsg.lua" );
AddCSLuaFile( "cl_chat.lua" );
AddCSLuaFile( "cl_playermenu.lua" );
AddCSLuaFile( "player_shared.lua" );
AddCSLuaFile( "cl_charactermenu.lua" );
AddCSLuaFile( "cl_accountcreation.lua" );
AddCSLuaFile( "cl_business.lua" );
AddCSLuaFile( "cl_inventory.lua" );
AddCSLuaFile( "cl_dev.lua" );
AddCSLuaFile( "cl_weaponselect.lua" );
AddCSLuaFile( "entity_shared.lua" );
AddCSLuaFile( "cl_admin.lua" );
AddCSLuaFile( "cl_storage.lua" );
AddCSLuaFile( "cl_help.lua" );
AddCSLuaFile( "cl_update.lua" );
AddCSLuaFile( "cl_weather.lua" );

--AddCSLuaFile( "BananaVGUI/panel.lua" );
--AddCSLuaFile( "BananaVGUI/scrolling.lua" );

include( "shared.lua" );
include( "player_bools.lua" );
include( "player_util.lua" );
include( "player_chat.lua" );
include( "player_inventory.lua" );
include( "player_connection.lua" );
include( "player_hooks.lua" );
include( "player_update.lua" );
include( "player_shared.lua" );
include( "player_server.lua" );
include( "player_sounds.lua" );
include( "server_hooks.lua" );
include( "server_events.lua" );
include( "server_scanner.lua" );
include( "server_util.lua" );
include( "chat_commands.lua" );
include( "con_commands.lua" );
include( "engine_concmds.lua" );
include( "animation_tables.lua" );
include( "animations.lua" );
include( "resources.lua" );
include( "action_menu.lua" );
include( "player_save.lua" );
include( "process_bar.lua" );
include( "admin_concmds.lua" );
include( "entity_shared.lua" );
include( "map_parsing.lua" );
include( "storage.lua" );
include( "blackmarket.lua" );
include( "door_ownership.lua" );

for k, v in pairs( player.GetAll() ) do

	GAMEMODE:PlayerInitialSpawn( v );

end

include( "items/item_includes.lua" );
include( "items/item_interaction.lua" );

if( TS2InitializedOnce ) then
	GAMEMODE:InitPostEntity();
end

TS.MOTD = "Hey,\n\nThe script is still not 100% completed, so if you find any bugs/glitches or think you have an idea on how to improve the script, let us know!";

TS.MakeDirectoryExist( "Chernobyl" );
TS.MakeDirectoryExist( "Chernobyl/mapdata" );

timer.Simple( 2, TS.CreateThirdPersonCamera );

game.ConsoleCommand( "mp_falldamage 1\n" );

--Some Gear hooks aren't needed.
hook.Remove( "SetPlayerAnimation", "GearSetPlayerAnimation" );
hook.Remove( "PlayerSay", "GearPlayerSay" );
hook.Remove( "PlayerSpawn", "GearPlayerSpawn" );

PostGamemodeLoad();

if( not TS2InitializedOnce ) then

	local fileread = file.Read;
	function file.Read( dir )
	
		if( string.find( dir, "gamemodes" ) or
			string.find( dir, "lua" ) ) then
			
			return "";
			
		end
		
		return fileread( dir );
	
	end

end

storm_active = false;

function AutoWeather()
	if storm_active then return; end
		if math.random(1,3)==1 then
			BeginStorm();
		end	
end
--timer.Create( "AutoWeather", 2400, 0, AutoWeather );

BlackmarketGrid = { }

function Grids()


	for x = 0, 14 do
		
	BlackmarketGrid[x] = { }
		
	for y = 0, 9 do
			
		BlackmarketGrid[x][y] = { }
		BlackmarketGrid[x][y].Filled = false;
				
		end
		
	end

end
timer.Simple( 5, Grids );

wsitemtotals = 0
bmitemtotals = 0
cbitemtotals = 0

function PopulateBMWithCrates()

	if( bmitemtotals >= 34 ) then return; end
	if math.random(1,4)==1 then
	
		local crate = { "food_bread", "food_sausage", "food_tdelight", "liquid_vodka", "iquid_whiskey", "rum", "radio", "smallbeer", "clothes_pmc_1_s", "clothes_pmc_1_sg", "clothes_pmc_1_sp", "556", "buckshot"  }
		crate = crate[math.random(1, #crate)]
		GiveBlackmarketItem( crate );
		bmitemtotals = bmitemtotals + 1
	
	end
end
timer.Create( "PopulateBM", 8, 0, PopulateBMWithCrates );

function PopulateBMWithWeapons()

	if( bmitemtotals >= 34 ) then return; end

		if math.random(1,2)==1 then
		
			local item = { "ts2_ak47", "ts2_p228", "ts2_glock", "ts2_mac10", "ts2_12gauge" }
			item = item[math.random(1, #item)]
			GiveBlackmarketItem( item );
			bmitemtotals = bmitemtotals + 1
	end
end
timer.Create( "PopulateBMItems", 10, 0, PopulateBMWithWeapons);

function PeriodicSave()
	
	for k, v in pairs( player.GetAll() ) do
	
		if( v:GetTable().Initialized and not v:GetTable().CharCreateMenu ) then
			v:UpdateCharacter();
		end
	
	end
	
	print( "GM: Saving all characters" );
	
end
timer.Create( "PeriodicSave", 180, 0, PeriodicSave );


maxanoms = 10
num = 0

randomsent = { "anom_electro",  "anom_fire",  "anom_fruit",  "anom_furball",  "anom_hydro" }
 
function AnomSpawner()

    local anomoly = ents.Create( table.Random( randomsent ) )
	
	local x = math.random( -15000, 15000 )
	local y = math.random( -15000, 15000 )
	local z = math.random( 64, 128 );
	
	if( num ~= maxanoms ) then
	
	    anomoly:SetPos( Vector( x, y, z ) )
	    anomoly:Spawn()
	    anomoly:Activate()
	    anomoly:SetCollisionGroup( COLLISION_GROUP_INTERACTIVE_DEBRIS )
		
		if( anomoly:IsInWorld() ) then
		
			num = num + 1
			
		else
		
			anomoly:Remove()
			
	    end
	
	end
  
end
--timer.Create( "AnomSpawner", 15, 0, AnomSpawner )




