tests/lua/stuckcake.lua
changeset 12640 36a650c0a885
equal deleted inserted replaced
12639:2473b03629c8 12640:36a650c0a885
       
     1 --[[ Stuck Cake Test
       
     2 
       
     3 In this test, 2 hedgehogs are placed very close to each other, tightly
       
     4 crammed between girders. The first hog (Cake Hog) launches a cake. Now
       
     5 the test waits until the cake explodes due to time.
       
     6 
       
     7 The cake must not take too long or forever to explode. The test succeeds
       
     8 if the cake explodes before CAKE_MAX_EXPLOSION_TIME ticks (rough estimate)
       
     9 after the cake spawned and fails otherwise.
       
    10 
       
    11 This test case has been written in response to bug 194.
       
    12 
       
    13 ]]
       
    14 
       
    15 -- Cake must explode before this many ticks for the test to succeed
       
    16 local CAKE_MAX_EXPLOSION_TIME = 15000
       
    17 
       
    18 -- Give up if cake is still running after this many ticks
       
    19 local CAKE_GIVE_UP_TIME = 20000 
       
    20 
       
    21 local hhs = {}
       
    22 
       
    23 function onGameInit()
       
    24 
       
    25 	ClearGameFlags()
       
    26 	EnableGameFlags(gfDisableWind, gfPerHogAmmo, gfOneClanMode, gfInvulnerable, gfSolidLand)
       
    27 	Map = ""
       
    28 	Seed = "{84f5e62e-6a12-4444-b53c-2bc62cfd9c62}"
       
    29 	Theme = "Cave"
       
    30 	MapGen = mgDrawn
       
    31 	MapFeatureSize = 12
       
    32 	TemplateFilter = 3
       
    33 	TemplateNumber = 0
       
    34 	TurnTime = 9999000
       
    35 	Explosives = 0
       
    36 	MinesNum = 0
       
    37 	CaseFreq = 0
       
    38 	WaterRise = 0
       
    39 	HealthDecrease = 0
       
    40 	Ready = 0
       
    41 
       
    42 	------ TEAM LIST ------
       
    43 
       
    44 	AddTeam("Test Team", 0xFFFF02, "Statue", "Tank", "Default", "cm_test")
       
    45 	
       
    46 	hhs[1] = AddHog("Cake Hog", 0, 100, "NoHat")
       
    47 	SetGearPosition(hhs[1], 771, 1344)
       
    48 	
       
    49 	hhs[2] = AddHog("Passive Hog", 0, 100, "NoHat")
       
    50 	SetGearPosition(hhs[2], 772, 1344)
       
    51 	HogTurnLeft(hhs[2], true)
       
    52 
       
    53 end
       
    54 
       
    55 function onAmmoStoreInit()
       
    56 	SetAmmo(amCake, 9, 0, 0, 0)
       
    57 	SetAmmo(amSkip, 9, 0, 0, 0)
       
    58 end
       
    59 
       
    60 function onGameStart()
       
    61 
       
    62 	PlaceSprite(784, 1361, sprAmGirder, 4, 0xFFFFFFFF, nil, nil, nil, lfNormal)
       
    63 	PlaceSprite(730, 1271, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
       
    64 	PlaceSprite(753, 1270, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
       
    65 	PlaceSprite(798, 1271, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
       
    66 	PlaceSprite(777, 1243, sprAmGirder, 6, 0xFFFFFFFF, nil, nil, nil, lfNormal)
       
    67 	
       
    68 end
       
    69 
       
    70 local cakeTestPhase = 0
       
    71 --[[ Test phases:
       
    72  0: Waiting for turn start
       
    73  1: Cake selected, waiting for attack
       
    74  2: Cake gear added
       
    75  3: Cake gead destroyed ]]
       
    76 
       
    77 function onNewTurn()
       
    78 	if cakeTestPhase == 0 then
       
    79 		SetWeapon(amCake)
       
    80 		cakeTestPhase = 1
       
    81 	end
       
    82 end
       
    83 
       
    84 local cakeTicks = 0
       
    85 
       
    86 function onGearAdd(gear)
       
    87 	if GetGearType(gear) == gtCake then
       
    88 		cakeTestPhase = 2
       
    89 	end
       
    90 end
       
    91 
       
    92 function onGearDelete(gear)
       
    93 	if GetGearType(gear) == gtCake and cakeTestPhase == 2 then
       
    94 		WriteLnToConsole(string.format("TEST: The cake exploded after %d ticks.", cakeTicks))
       
    95 		cakeTestPhase = 3
       
    96 		if cakeTicks > CAKE_MAX_EXPLOSION_TIME then
       
    97 			WriteLnToConsole("TEST RESULT: Failed because cake took too long to explode.")
       
    98 			EndLuaTest(TEST_FAILED)
       
    99 		else
       
   100 			WriteLnToConsole("TEST RESULT: Succeeded because cake exploded in time.")
       
   101 			EndLuaTest(TEST_SUCCESSFUL)
       
   102 		end			
       
   103 
       
   104 	end
       
   105 end
       
   106 
       
   107 function onGameTick()
       
   108 	if cakeTestPhase == 1 then
       
   109 		ParseCommand("+attack")
       
   110 	elseif cakeTestPhase == 2 then
       
   111 		cakeTicks = cakeTicks + 1
       
   112 		if cakeTicks > CAKE_GIVE_UP_TIME then
       
   113 			WriteLnToConsole(string.format("TEST RESULT: Failed because the cake still didn't explode after %d ticks.", CAKE_GIVE_UP_TIME))
       
   114 			cakeTestPhase = 3
       
   115 			EndLuaTest(TEST_FAILED)
       
   116 		end
       
   117 	end
       
   118 end