tests/lua/staticmines.lua
branchui-scaling
changeset 15283 c4fd2813b127
parent 13390 0135e64c6c66
parent 15279 7ab5cf405686
child 15663 d92eeb468dad
equal deleted inserted replaced
13390:0135e64c6c66 15283:c4fd2813b127
     1 --[[ Static Mines Test
       
     2 
       
     3 This test tests if mines are able to stand still on the ground for a very long time. 8 mines are spawned on a few girders.
       
     4 
       
     5 The test passes if the mines don't move for a very long time.
       
     6 The test failes if any mine moves even the tiniest bit or is destroyed.
       
     7 
       
     8 This test case has been created as response to bug 96.
       
     9 ]]
       
    10 
       
    11 -- Max. time to test
       
    12 local TEST_TIME_LIMIT = 3600000 * 5 -- 5 hours
       
    13 
       
    14 local hhs = {}
       
    15 
       
    16 function onGameInit()
       
    17 
       
    18 	ClearGameFlags()
       
    19 	EnableGameFlags(gfDisableWind, gfOneClanMode)
       
    20 	Map = ""
       
    21 	Seed = 12
       
    22 	Theme = "Desert"
       
    23 	MapGen = mgDrawn
       
    24 	TurnTime = -1
       
    25 	Explosives = 0
       
    26 	MinesNum = 0
       
    27 	CaseFreq = 0
       
    28 	Delay = 100
       
    29 	WaterRise = 0
       
    30 	HealthDecrease = 0
       
    31 	AirMinesNum = 0
       
    32 
       
    33 
       
    34 	AddTeam("Test Team", 0xFFFF00, "Statue", "Tank", "Default", "cm_test")
       
    35 	hhs[1] = AddHog("Test Hog", 0, 100, "cm_test")
       
    36 	SetGearPosition(hhs[1], 300, 1450)
       
    37 end
       
    38 
       
    39 local mines = {
       
    40 	{ x = 231, y = 1708},
       
    41 	{ x = 290, y = 1708},
       
    42 	{ x = 342, y = 1708},
       
    43 	{ x = 261, y = 1708},
       
    44 	{ x = 319, y = 1708},
       
    45 	{ x = 403, y = 1706},
       
    46 	{ x = 428, y = 1706},
       
    47 	{ x = 461, y = 1706},
       
    48 	{ x = 498, y = 1706},
       
    49 	{ x = 518, y = 1706},
       
    50 }
       
    51 
       
    52 function LoadGearData()
       
    53 	------ GIRDER LIST ------
       
    54 	PlaceGirder(290, 1718, 4)
       
    55 	PlaceGirder(290, 1790, 4)
       
    56 	PlaceGirder(452, 1716, 4)
       
    57 	PlaceGirder(452, 1790, 4)
       
    58 	
       
    59 	PlaceGirder(300, 1500, 4)
       
    60 
       
    61 	------ MINE LIST ------
       
    62 	for m=1, #mines do
       
    63 		mines[m].gear = AddGear(mines[m].x, mines[m].y, gtMine, 0, 0, 0, 0)
       
    64 	end
       
    65 
       
    66 end
       
    67 
       
    68 function onGameStart()
       
    69 	LoadGearData()
       
    70 end
       
    71 
       
    72 function onGearDelete(gear)
       
    73 	for m=#mines, 1, -1 do
       
    74 		if gear == mines[m] then
       
    75 			WriteLnToConsole(string.format("Mine %d died!", m))
       
    76 			table.remove(mines, m)
       
    77 		end
       
    78 	end
       
    79 end
       
    80 
       
    81 -- Give a short time for the mines to settle first.
       
    82 local checkTimer = -5000
       
    83 local initPosCheck = false
       
    84 -- Count the total times the mines managed to stand still
       
    85 local totalTime = 0
       
    86 local fin = false
       
    87 function onGameTick20()
       
    88 	if fin then
       
    89 		return
       
    90 	end
       
    91 	-- Infinite turn time
       
    92 	if TurnTimeLeft < 6000 then
       
    93 		TurnTimeLeft = -1
       
    94 	end
       
    95 	checkTimer = checkTimer + 20
       
    96 	if initPosCheck then
       
    97 		totalTime = totalTime + 20
       
    98 	end
       
    99 	if checkTimer >= 1000 then
       
   100 		local failed = false
       
   101 		for m=1, #mines do
       
   102 			if not initPosCheck then
       
   103 				-- Position initialization
       
   104 				-- Save “real” x and y values after the mines have settled
       
   105 				local x, y = GetGearPosition(mines[m].gear)
       
   106 				mines[m].rx = x
       
   107 				mines[m].ry = y
       
   108 			else
       
   109 				-- Position check
       
   110 				local x, y = GetGearPosition(mines[m].gear)
       
   111 				local rx, ry = mines[m].rx, mines[m].ry
       
   112 				if not x or not y then
       
   113 					WriteLnToConsole(string.format("Mine %d has died!", m))
       
   114 					failed = true
       
   115 				elseif x ~= rx or y ~= ry then
       
   116 					WriteLnToConsole(string.format("Mine %d has moved! Expected: (%d, %d). Actual: (%d, %d)", m, rx, ry, x, y))
       
   117 					failed = true
       
   118 				end
       
   119 			end
       
   120 		end
       
   121 		if not initPosCheck then
       
   122 			initPosCheck = true
       
   123 		end
       
   124 		if failed then
       
   125 			WriteLnToConsole(string.format("Test failed. The mines managed to stand still for %d ticks.", totalTime))
       
   126 			EndLuaTest(TEST_FAILED)
       
   127 			fin = true
       
   128 			return
       
   129 		end
       
   130 	end
       
   131 	if totalTime >= TEST_TIME_LIMIT then
       
   132 		WriteLnToConsole(string.format("All mines have been static for over %d ticks! Success!", TEST_TIME_LIMIT))
       
   133 		EndLuaTest(TEST_SUCCESSFUL)
       
   134 		fin = true
       
   135 		return
       
   136 	end
       
   137 end