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