tests/lua/drillrockets_drill.lua
changeset 10042 703931eec680
parent 10026 14a3f7feeb39
child 10028 9e742fc72696
equal deleted inserted replaced
10041:82d062e36e96 10042:703931eec680
       
     1 
       
     2  -- taken from http://code.google.com/p/hedgewars/wiki/LuaDrawing
       
     3  PointsBuffer = ''  -- A string to accumulate points in
       
     4  function AddPoint(x, y, width, erase)
       
     5      PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
       
     6      if width then
       
     7          width = bor(width,0x80)
       
     8          if erase then
       
     9              width = bor(width,0x40)
       
    10          end
       
    11          PointsBuffer = PointsBuffer .. string.char(width)
       
    12      else
       
    13          PointsBuffer = PointsBuffer .. string.char(0)
       
    14      end
       
    15      if #PointsBuffer > 245 then
       
    16          ParseCommand('draw '..PointsBuffer)
       
    17          PointsBuffer = ''
       
    18      end
       
    19  end
       
    20  function FlushPoints()
       
    21      if #PointsBuffer > 0 then
       
    22          ParseCommand('draw '..PointsBuffer)
       
    23          PointsBuffer = ''
       
    24      end
       
    25  end
       
    26 
       
    27 
       
    28 local ta_pointsize = 63
       
    29 local ta_radius = (ta_pointsize * 10 + 6) / 2
       
    30 
       
    31 -- creates round test area
       
    32 function AddTestArea(testarea)
       
    33 	step = 200
       
    34 	xstep = step * testarea["xdir"]
       
    35 	ystep = step * testarea["ydir"]
       
    36 	x = testarea["x"]
       
    37 	y = testarea["y"]
       
    38 	AddPoint(x, y, ta_pointsize);
       
    39 	AddPoint(x + xstep, y + ystep);
       
    40 end
       
    41 
       
    42 -- vertical test areas
       
    43 local taa_v1 = {x= 350, y= 400, xdir= 0, ydir= 1}
       
    44 local taa_v2 = {x= 350, y=1500, xdir= 0, ydir=-1}
       
    45 -- horizontal test areas
       
    46 local taa_h1 = {x=1150, y= 400, xdir= 1, ydir= 0}
       
    47 local taa_h2 = {x=1200, y=1100, xdir=-1, ydir= 0}
       
    48 -- diagonal test areas
       
    49 local taa_d1 = {x=2200, y= 400, xdir= 1, ydir= 1}
       
    50 local taa_d2 = {x=2000, y=1500, xdir= 1, ydir=-1}
       
    51 local taa_d3 = {x=3300, y= 400, xdir=-1, ydir= 1}
       
    52 local taa_d4 = {x=3300, y=1500, xdir=-1, ydir=-1}
       
    53 
       
    54 -- fail counter
       
    55 local nfailed = 0
       
    56 local nspawned = 0
       
    57 local ndied = 0
       
    58 
       
    59 function onGameInit()
       
    60 	-- At first we have to overwrite/set some global variables
       
    61 	-- that define the map, the game has to load, as well as
       
    62 	-- other things such as the game rules to use, etc.
       
    63 	-- Things we don't modify here will use their default values.
       
    64 
       
    65 	-- The base number for the random number generator
       
    66 	Seed = 1
       
    67 	-- The map to be played
       
    68 	MapGen = 2
       
    69 	-- The theme to be used
       
    70 	Theme = "Bamboo"
       
    71 	-- Game settings and rules
       
    72 	EnableGameFlags(gfOneClanMode, gfDisableWind, gfDisableLandObjects, gfDisableGirders, gfSolidLand)
       
    73 	CaseFreq = 0
       
    74 	MinesNum = 0
       
    75 	Explosives = 0
       
    76 
       
    77 	-- No damage please
       
    78 	DamagePercent = 1
       
    79 
       
    80 	-- Draw Map
       
    81 	AddPoint(10,30,0) -- hog spawn platform
       
    82 	-- test areas
       
    83 	AddTestArea(taa_v1)
       
    84 	AddTestArea(taa_v2)
       
    85 	AddTestArea(taa_h1)
       
    86 	AddTestArea(taa_h2)
       
    87 	AddTestArea(taa_d1)
       
    88 	AddTestArea(taa_d2)
       
    89 	AddTestArea(taa_d3)
       
    90 	AddTestArea(taa_d4)
       
    91 
       
    92 	FlushPoints()
       
    93 
       
    94 	-- Create the player team
       
    95 	AddTeam("'Zooka Team", 14483456, "Simple", "Island", "Default")
       
    96 	-- And add a hog to it
       
    97 	player = AddHog("Hunter", 0, 1, "NoHat")
       
    98 	-- place it on how spawn platform
       
    99 	SetGearPosition(player, 10, 10)
       
   100 end
       
   101 
       
   102 -- xdir/ydir is direction in which to fire the drills
       
   103 function SpawnDrillRocketArray(testarea)
       
   104 	xdir = testarea["xdir"]
       
   105 	ydir = testarea["ydir"]
       
   106 	centerx = testarea["x"]
       
   107 	centery = testarea["y"]
       
   108 	distance = 23
       
   109 	d = distance
       
   110 	radius = ta_radius
       
   111 	local xmin, xmax, ymin, ymax
       
   112 	if (xdir ~= 0) and (ydir ~= 0) then
       
   113 		sqrttwo = math.sqrt(2)
       
   114 		d = d / sqrttwo
       
   115 		radius = radius / sqrttwo
       
   116 	end
       
   117 	centerx = centerx - (xdir * (radius + 20))
       
   118 	centery = centery - (ydir * (radius + 20))
       
   119 	radius = radius - 6
       
   120 	xn = ydir
       
   121 	yn = -xdir
       
   122 	startx = centerx - (radius * xn)
       
   123 	starty = centery - (radius * yn)
       
   124 	endx = centerx + (radius * xn)
       
   125 	endy = centery + (radius * yn)
       
   126 
       
   127 	-- spawn loop
       
   128 	x = startx
       
   129 	y = starty
       
   130 	xd = d * xn
       
   131 	yd = d * yn
       
   132 	if (xd < 0) and (startx < endx) then x = endx end
       
   133 	if (yd < 0) and (starty < endy) then y = endy end
       
   134 	nsteps = math.floor(math.max(math.abs(startx - endx),math.abs(starty - endy)) / d)
       
   135 	for i = 1, nsteps, 1 do
       
   136 		AddGear(math.floor(x), math.floor(y), gtDrill, 0, 900000 * xdir, 900000 * ydir, 0)
       
   137 		nspawned = nspawned + 1
       
   138 		x = x + xd
       
   139 		y = y + yd
       
   140 	end
       
   141 end
       
   142 
       
   143 function onGearDelete(gear)
       
   144 	if GetGearType(gear) == gtDrill then
       
   145 		if GetTimer(gear) > 0 then
       
   146 			nfailed = nfailed + 1
       
   147 		end
       
   148 		ndied = ndied + 1
       
   149 		if ndied == nspawned then
       
   150 			WriteLnToConsole('TESTRESULT: ' .. nfailed .. ' of ' .. nspawned .. ' drill rockets exploded prematurely')
       
   151 			if (nfailed > 0) then
       
   152 				EndLuaTest(TEST_FAILED)
       
   153 			else
       
   154 				EndLuaTest(TEST_SUCCESSFUL)
       
   155 			end
       
   156 		end
       
   157 	end
       
   158 end
       
   159 
       
   160 function onGameStart()
       
   161 	SetGravity(1)
       
   162 
       
   163 	SpawnDrillRocketArray(taa_h1)
       
   164 	SpawnDrillRocketArray(taa_h2)
       
   165 	SpawnDrillRocketArray(taa_v1)
       
   166 	SpawnDrillRocketArray(taa_v2)
       
   167 	SpawnDrillRocketArray(taa_d1)
       
   168 	SpawnDrillRocketArray(taa_d2)
       
   169 	SpawnDrillRocketArray(taa_d3)
       
   170 	SpawnDrillRocketArray(taa_d4)
       
   171 end
       
   172