gKH = {}; --our global namespace gKH.dbglevel = 1; --controls amount of print and debug info. gKH.SceneGlobals = {}; --our sceneglobals sub namespace gKH.SceneGlobals.GenerationMarker = 0; gKH.SceneGlobals.PerHangerChancePct = 25; --percent chance for each hanger. gKH.SceneGlobals.RedbaseHangerMax = 3; --cap at 6 aircraft. gKH.SceneGlobals.RedbaseHangerCounter = 0; --keep track of how many are generated. gKH.SceneGlobals.RedbaseAircraftCounter = 8; --starting point for aircraft names (1 gets added) --table holding locations of 6 possible hangers. gKH.SceneGlobals.Redbase1Hangers = { {index=1,latitude='10.9056518851787', longitude='114.086331168915'}, {index=2,latitude='10.9046518851787', longitude='114.086331168915'}, {index=3,latitude='10.9036518851787', longitude='114.086331168915'}, {index=4,latitude='10.9026518851787', longitude='114.086331168915'}, {index=5,latitude='10.9016518851787', longitude='114.086331168915'}, {index=6,latitude='10.9006518851787', longitude='114.086331168915'} } --returns latitude and longitude from our table or nil upon failure. function gKH.GetRedbaseLocation(hindex) local marker = false; for k,v in pairs(gKH.SceneGlobals.Redbase1Hangers) do if v.index == hindex then marker = true return v.latitude,v.longitude; end end return nil; end --generates randome seed. function gKH.ImprovedRandomseed(quality) if quality == nil then quality=10 end -- os.time() removes dependency on how long the software has been running currently -- os.clock() provides miliseconds, unlike os.time() -- we "chain" this with previous math.random() output to retain any accumulated entropy -- and "stir" a little by repeating more than once, though not much because the execution time -- probably doesn't vary much between calls and spending too much time will be a waste unless we want -- to spend several seconds doing this. math.randomseed(os.time() + math.random(90071992547)) -- preserve some previous entropy if there was any for i = 1, quality do -- Retain some previous PRNG state while adding a little jitter entropy, but not much. -- Jitter entropy comes from thread preemption, interrupt handing, and stuff like that in the OS that is -- somewhat random. This means you might not get much if any on a powerful and calm system. -- If we had a higher precision clock with ns instead of just ms then that would be more helpful. math.randomseed(((os.clock() * 1000) % 1000) + math.random(900719925470000)) end end --handles our dicerolling. function gKH.RandomDiceRoll(pct) gKH.ImprovedRandomseed(10) -- reinitialize the random number generator with a new seed once per function use. local r = math.random(1,1000) -- get randome number between 1 and 1000; 1000 adds maybe more variabily in sequence? if gKH.dbglevel > 0 then print('RandomDiceRoll(): random number gen: ' .. tostring(r)); end if (r <= (pct * 10)) then -- is random less than or equal to our set percentage? if gKH.dbglevel > 1 then print('RandomDiceRoll(): Match! returning true'); end return true; else return false; end end --creates hangers and aircraft, does group and mission assignment function gKH.CreateHangerUnitPackage(theside,hangerindex,idbid,iloadout,strmission) local retval = true; local lat,lon = gKH.GetRedbaseLocation(hangerindex) if lat ~=nil and lon ~=nil then --add hanger. local h = ScenEdit_AddUnit({type = 'Facility', name = 'RedAirbase_Hanger_' .. hangerindex, heading = 0, dbid = 41, side = theside, Latitude=lat,Longitude=lon, altitude="3 ft",autodetectable="false",holdfire="true",proficiency=4}); if h ~=nil then h.group = "Red_Airbase"; --add new hanger to the base. gKH.SceneGlobals.RedbaseAircraftCounter = gKH.SceneGlobals.RedbaseAircraftCounter + 1; local u = ScenEdit_AddUnit({type = 'Aircraft', name = 'Argonaut #' .. gKH.SceneGlobals.RedbaseAircraftCounter, loadoutid = iloadout, heading = 0, dbid = idbid, side = theside, Latitude=lat,Longitude=lon, altitude="36100 ft",autodetectable="false",holdfire="true",proficiency=4,speed=350,base=h.name}); ScenEdit_SetLoadout({UnitName=u.guid,loadoutID=0,TimeToReady_Minutes=0,IgnoreMagazines=true}) ScenEdit_AssignUnitToMission (u.guid, strmission) gKH.SceneGlobals.RedbaseAircraftCounter = gKH.SceneGlobals.RedbaseAircraftCounter + 1; local u = ScenEdit_AddUnit({type = 'Aircraft', name = 'Argonaut #' .. gKH.SceneGlobals.RedbaseAircraftCounter, loadoutid = iloadout, heading = 0, dbid = idbid, side = theside, Latitude=lat,Longitude=lon, altitude="36100 ft",autodetectable="false",holdfire="true",proficiency=4,speed=350,base=h.name}); ScenEdit_SetLoadout({UnitName=u.guid,loadoutID=0,TimeToReady_Minutes=0,IgnoreMagazines=true}) ScenEdit_AssignUnitToMission (u.guid, strmission) else print("Hanger creation failed."); retval = false; end else print("Hanger location lookup failed."); retval = false; end return retval end local function DoRandomHangerGeneration() for i = 1, #gKH.SceneGlobals.Redbase1Hangers do if gKH.RandomDiceRoll(gKH.SceneGlobals.PerHangerChancePct) == true then if gKH.SceneGlobals.RedbaseHangerCounter < gKH.SceneGlobals.RedbaseHangerMax then --Dont do more than 6 aircraft. if gKH.CreateHangerUnitPackage('Red',i,4449,22439,'Fiery Cross CAP 1') == true then gKH.SceneGlobals.RedbaseHangerCounter = gKH.SceneGlobals.RedbaseHangerCounter + 1 else print("an error occured generating hanger or aircreaft for index#" .. i); end else if gKH.dbglevel > 0 then print("Skipping, Maximum hangers reached.") end end end end gKH.SceneGlobals.GenerationMarker = 1; --update global value ScenEdit_SetKeyValue("HangerGenerationMarker",gKH.SceneGlobals.GenerationMarker); --save to file. end local function CheckHangerGeneration() local tmp = ScenEdit_GetKeyValue("HangerGenerationMarker") --get from save file if exists. if tmp == nil or tmp == "" then gKH.SceneGlobals.GenerationMarker = 0; --does not exit, set to 0 else gKH.SceneGlobals.GenerationMarker = tonumber(tmp); --exists convert to number. end if gKH.SceneGlobals.GenerationMarker < 1 then --attempt to create them otherwise it's already done and > 0 is read from the savefile. DoRandomHangerGeneration(); else print("HangerGenerationMarker active in save already. Skipping.") end end ---end functions -- print("---") CheckHangerGeneration()