1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function InTable(n,v)
	local Table = {}
	for i = 0, n, 1 do Table[i] = v end
	return(Table)
end
ImagePath = "gfx/lua/ball.png"	-- Image = 32*32 (Circle)
WeaponsPush = true
Ball = { X , Y, XS, YS, IMG = {}, Cur }
Pl = { Kick = InTable(32,0) }
addhook("startround","FootBallStart")
addhook("always","FootBallUpdate")
addhook("use","FootBallKick")
addhook("attack","FootBallPush")
function FootBallStart() LoadBall(9,9) end
function SetArea(X,Y,W,H) Area = { X = X * 32 + 16, Y = Y * 32 + 16, W = W * 32 + 16, H = H * 32 + 16 } end
function LoadBall(X,Y)
	Ball.X, Ball.Y = X * 32 + 16, Y * 32 + 16
	Ball.XS, Ball.YS = 0, 0
	for k, v in pairs(Ball.IMG) do freeimage(v) end
	Ball.IMG = {}
	local ID = image(ImagePath,Ball.X,Ball.Y,1)
	table.insert(Ball.IMG,ID)
	Ball.Cur = ID
end
function FootBallUpdate()
	local X, Y
	for k, v in pairs(player(0,"table")) do
		X, Y = player(v,"x"), player(v,"y")
		if (Dist(X,Y,Ball.X,Ball.Y) < 32) then
			local RAD = math.atan2(Y - Ball.Y,X - Ball.X)
			local Power = 2
			if (Pl.Kick[v] > 0) then
				Power, Pl.Kick[v] = 12, 0
				msg2(v,"KICK!@C")
				msg2(v,"Kick: OFF")
			end
			Ball.XS = Ball.XS - math.cos(RAD) * Power
			Ball.YS = Ball.YS - math.sin(RAD) * Power
			Ball.X = X - math.cos(RAD) * 33
			Ball.Y = Y - math.sin(RAD) * 33
		 end
	end
	-- This should be improved by tile collisions
	if (Ball.X > Area.W or Ball.X < Area.X) then Ball.XS = -Ball.XS end
	if (Ball.Y > Area.H or Ball.Y < Area.Y) then Ball.YS = -Ball.YS end
	if (Ball.X > Area.W) then Ball.X = Area.W end
	if (Ball.Y > Area.H) then Ball.Y = Area.H end
	if (Ball.X < Area.X) then Ball.X = Area.X end
	if (Ball.Y < Area.Y) then Ball.Y = Area.Y end	
	if (Ball.XS > 12) then Ball.XS = 12 elseif (Ball.XS < -12) then Ball.XS = -12 end
	if (Ball.YS > 12) then Ball.YS = 12 elseif (Ball.YS < -12) then Ball.YS = -12 end
	Ball.X, Ball.Y = Ball.X + Ball.XS, Ball.Y + Ball.YS
	imagepos(Ball.Cur,Ball.X,Ball.Y,0)
	Ball.XS, Ball.YS = Ball.XS / 1.05, Ball.YS / 1.05
end
function Dist(X1,Y1,X2,Y2)
	return( ((X1 - X2) ^ 2 + (Y1 - Y2) ^ 2) ^ .5 )
end
function FootBallKick(ID)
	local Switch = {"Off","On"}
	if (Pl.Kick[ID] > 0) then Pl.Kick[ID] = 0 else Pl.Kick[ID] = 1 end
	msg2(ID,"Kick: "..Switch[Pl.Kick[ID] + 1])
end
function FootBallPush(ID)
	local WPN = player(ID,"weapontype")
	if (WPN > 0 and WPN < 10 or WPN > 19 and WPN < 41) and WeaponsPush then
		local X = player(ID,"x")
		local Y = player(ID,"y")
		local ROT = player(ID,"rot") - 90
		local FLY = true
		while FLY do
			X, Y = X + math.cos(math.rad(ROT)) * 5, Y + math.sin(math.rad(ROT)) * 5
			if Dist(X,Y,Ball.X,Ball.Y) < 16 then
				local DMG = itemtype(WPN,"dmg") / 4
				Ball.XS, Ball.YS = Ball.XS + math.cos(math.rad(ROT)) * DMG, Ball.YS + math.sin(math.rad(ROT)) * DMG
				break
			else
				if tile(math.ceil(X/32) - 1,math.ceil(Y/32) - 1,"wall") then FLY = false end
			end
		end
	end
end
FootBallStart()
SetArea(2,2,18,18)