tests/lua/stuckcake.lua
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 12640 36a650c0a885
permissions -rw-r--r--
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.

--[[ Stuck Cake Test

In this test, 2 hedgehogs are placed very close to each other, tightly
crammed between girders. The first hog (Cake Hog) launches a cake. Now
the test waits until the cake explodes due to time.

The cake must not take too long or forever to explode. The test succeeds
if the cake explodes before CAKE_MAX_EXPLOSION_TIME ticks (rough estimate)
after the cake spawned and fails otherwise.

This test case has been written in response to bug 194.

]]

-- Cake must explode before this many ticks for the test to succeed
local CAKE_MAX_EXPLOSION_TIME = 15000

-- Give up if cake is still running after this many ticks
local CAKE_GIVE_UP_TIME = 20000 

local hhs = {}

function onGameInit()

	ClearGameFlags()
	EnableGameFlags(gfDisableWind, gfPerHogAmmo, gfOneClanMode, gfInvulnerable, gfSolidLand)
	Map = ""
	Seed = "{84f5e62e-6a12-4444-b53c-2bc62cfd9c62}"
	Theme = "Cave"
	MapGen = mgDrawn
	MapFeatureSize = 12
	TemplateFilter = 3
	TemplateNumber = 0
	TurnTime = 9999000
	Explosives = 0
	MinesNum = 0
	CaseFreq = 0
	WaterRise = 0
	HealthDecrease = 0
	Ready = 0

	------ TEAM LIST ------

	AddTeam("Test Team", 0xFFFF02, "Statue", "Tank", "Default", "cm_test")
	
	hhs[1] = AddHog("Cake Hog", 0, 100, "NoHat")
	SetGearPosition(hhs[1], 771, 1344)
	
	hhs[2] = AddHog("Passive Hog", 0, 100, "NoHat")
	SetGearPosition(hhs[2], 772, 1344)
	HogTurnLeft(hhs[2], true)

end

function onAmmoStoreInit()
	SetAmmo(amCake, 9, 0, 0, 0)
	SetAmmo(amSkip, 9, 0, 0, 0)
end

function onGameStart()

	PlaceSprite(784, 1361, sprAmGirder, 4, 0xFFFFFFFF, nil, nil, nil, lfNormal)
	PlaceSprite(730, 1271, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
	PlaceSprite(753, 1270, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
	PlaceSprite(798, 1271, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
	PlaceSprite(777, 1243, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
	
end

local cakeTestPhase = 0
--[[ Test phases:
 0: Waiting for turn start
 1: Cake selected, waiting for attack
 2: Cake gear added
 3: Cake gead destroyed ]]

function onNewTurn()
	if cakeTestPhase == 0 then
		SetWeapon(amCake)
		cakeTestPhase = 1
	end
end

local cakeTicks = 0

function onGearAdd(gear)
	if GetGearType(gear) == gtCake then
		cakeTestPhase = 2
	end
end

function onGearDelete(gear)
	if GetGearType(gear) == gtCake and cakeTestPhase == 2 then
		WriteLnToConsole(string.format("TEST: The cake exploded after %d ticks.", cakeTicks))
		cakeTestPhase = 3
		if cakeTicks > CAKE_MAX_EXPLOSION_TIME then
			WriteLnToConsole("TEST RESULT: Failed because cake took too long to explode.")
			EndLuaTest(TEST_FAILED)
		else
			WriteLnToConsole("TEST RESULT: Succeeded because cake exploded in time.")
			EndLuaTest(TEST_SUCCESSFUL)
		end			

	end
end

function onGameTick()
	if cakeTestPhase == 1 then
		ParseCommand("+attack")
	elseif cakeTestPhase == 2 then
		cakeTicks = cakeTicks + 1
		if cakeTicks > CAKE_GIVE_UP_TIME then
			WriteLnToConsole(string.format("TEST RESULT: Failed because the cake still didn't explode after %d ticks.", CAKE_GIVE_UP_TIME))
			cakeTestPhase = 3
			EndLuaTest(TEST_FAILED)
		end
	end
end