tests/lua/luaAPI/gravity_get_set.lua
changeset 10043 ca075f0f7cfb
parent 10029 3b3d3e465e6a
child 10048 97d11e754dd2
equal deleted inserted replaced
10042:703931eec680 10043:ca075f0f7cfb
       
     1 
       
     2 
       
     3 -- * let grenade fall
       
     4 -- * after a second capture fall distance
       
     5 -- * change gravity value every second and see if the fall distance in the
       
     6 --   following second is about what we'd expect it to be
       
     7 
       
     8 local spawnX = 10
       
     9 local spawnY = -500
       
    10 
       
    11 local defaultG = nil
       
    12 local currentG = nil
       
    13 
       
    14 local defaultDY = nil
       
    15 local expectedY = nil
       
    16 
       
    17 local testGs = nil
       
    18 
       
    19 local nFails = 0
       
    20 
       
    21 function onGameInit()
       
    22 
       
    23 
       
    24 	-- The base number for the random number generator
       
    25 	Seed = 1
       
    26 	-- The map to be played
       
    27 	Map = "Ruler"
       
    28 	-- The theme to be used
       
    29 	Theme = "Bamboo"
       
    30 	-- Game settings and rules
       
    31 	EnableGameFlags(gfOneClanMode, gfInvulnerable)
       
    32 	CaseFreq = 0
       
    33 	MinesNum = 0
       
    34 	Explosives = 0
       
    35 
       
    36 	-- Create the player team
       
    37 	AddTeam("O_o", 14483456, "Simple", "Island", "Default")
       
    38 	-- And add a hog to it
       
    39 	player = AddHog("o_O", 0, 1, "NoHat")
       
    40 	SetGearPosition(player, 100, 100)
       
    41 end
       
    42 
       
    43 local tol = 0
       
    44 
       
    45 function IsKindaSame(a, b)
       
    46 	tol = 1 + math.max(1,math.abs(currentG) / 100)
       
    47 	return (a >= b-tol) and (a <= b+tol)
       
    48 end
       
    49 
       
    50 function SpawnGrenade()
       
    51 	AddGear(spawnX, spawnY, gtGrenade, 0, 0, 0, 1000)
       
    52 end
       
    53 
       
    54 local gIdx = 1
       
    55 
       
    56 function onGearDelete(gear)
       
    57 	if GetGearType(gear) ~= gtGrenade then
       
    58 		return
       
    59 	end
       
    60 
       
    61 	-- catch initial measuring drop
       
    62 	if defaultDY == nil then
       
    63 		defaultDY = GetY(gear) - spawnY
       
    64 	elseif not IsKindaSame(GetY(gear), expectedY) then
       
    65 		nFails = nFails + 1
       
    66 		WriteLnToConsole("FAIL: Unexpected Y position! " .. GetY(gear) .. " returned, expected " .. expectedY .. ' (max tolerated difference = ' .. tol .. ')')
       
    67 	else
       
    68 		WriteLnToConsole("Y position OK! " .. GetY(gear) .. " returned, expected " .. expectedY .. ' (max tolerated difference = ' .. tol .. ')')
       
    69 	end
       
    70 
       
    71 	returnedG = GetGravity()
       
    72 	if (returnedG ~= currentG) then
       
    73 		WriteLnToConsole("GetGravity did not return the value that we used with SetGravity! " .. returnedG .. " returned, expected " .. currentG)
       
    74 		nFails = nFails + 1
       
    75 	end
       
    76 
       
    77 	currentG = testGs[gIdx]
       
    78 	gIdx = gIdx + 1
       
    79 	-- after last test
       
    80 	if currentG == nil then
       
    81 		if (nFails > 0) then
       
    82 			EndLuaTest(TEST_FAILED)
       
    83 		else
       
    84 			EndLuaTest(TEST_SUCCESSFUL)
       
    85 		end
       
    86 	end
       
    87 
       
    88 	WriteLnToConsole("SetGravity(" .. currentG .. ") ...")
       
    89 	SetGravity(currentG)
       
    90 
       
    91 	SpawnGrenade()
       
    92 	expectedY = spawnY + math.floor(currentG * defaultDY / 100)
       
    93 end
       
    94 
       
    95 function onGameStart()
       
    96 	currentG = 100
       
    97 	defaultG = GetGravity()
       
    98 	if (defaultG ~= 100) then
       
    99 		WriteLnToConsole("GetGravity did not return 100 at game start")
       
   100 		nFails = 1
       
   101 	end
       
   102 
       
   103 	SpawnGrenade()
       
   104 
       
   105 	testGs = {150, 200, 300, 10, 1, 13, 15, 0, 27, -600, -10, nil}
       
   106 end
       
   107