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