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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
turretmod = {}
turretmod.spawnedturrets = {}
turretmod.spawnedprojectiles = {}
turretmod.buildingsites = {}
turretmod.player = {}
turretmod.game = {
	helptime = 0;
	helptimec = 12;
}
dofile('sys/lua/turretmod/turrets.lua')
dofile('sys/lua/turretmod/projectiles.lua')
dofile('sys/lua/turretmod/effects.lua')
addhook('always', 'turretmod.hook.always')
addhook('objectkill', 'turretmod.hook.objectkill')
addhook('ms100', 'turretmod.hook.ms100')
addhook('join', 'turretmod.hook.join')
addhook('team', 'turretmod.hook.team')
addhook('kill', 'turretmod.hook.kill')
addhook('menu', 'turretmod.hook.menu')
addhook('buildattempt', 'turretmod.hook.buildattempt')
--Functions
turretmod.func = {
	spawnturret = function(turret, x, y, objecttype, team, pl)
		if not tile(x, y, 'walkable') then return end
		parse('spawnobject '.. objecttype ..' '.. x ..' '.. y ..' 0 0 '.. team ..' '.. pl)
		local objectid
		for k, v in pairs(object(0, 'table')) do
			if object(v, 'tilex') == x and object(v, 'tiley') == y then
				objectid = v
			end
		end
		local tbl = {
			baseimage = image(turretmod.turrets[turret].baseimage, x * 32 + 16, y * 32 + 16, 1);
			lightimage = image('gfx/sprites/flare1.bmp', x * 32 + 16, y * 32 + 16, 1);
			gunimage = image(turretmod.turrets[turret].gunimage, x * 32 + 16, y * 32 + 16, 1);
			rootrotation = player(pl, 'rot');
			rotationvariable = 0;
			turret = turret;
			x = x;
			y = y;
			team = team;
			player = pl;
			objectid = objectid;
			time = 0;
			timec = 0;
			locked = false;
			length = 400;
		}
		tween_rotateconstantly(tbl.lightimage, 2)
		imageblend(tbl.lightimage, 1)
		local color = (team == 1 and {255, 0, 0} or {0, 0, 255})
		imagecolor(tbl.lightimage, color[1], color[2], color[3])
		table.insert(turretmod.spawnedturrets, tbl)
		if turretmod.turrets[turret].onspawn then turretmod.turrets[turret].onspawn(#turretmod.spawnedturrets) end
	end;
	
	spawnturretbs = function(turret, x, y, objecttype, team, pl, time)
		if not tile(x, y, 'walkable') then return end
		parse('spawnobject 10 '.. x ..' '.. y ..' 0 0 '.. team ..' '.. pl)
		local objectid
		for k, v in pairs(object(0, 'table')) do
			if object(v, 'tilex') == x and object(v, 'tiley') == y then
				objectid = v
			end
		end
		local tbl = {
			turret = turret;
			x = x;
			y = y;
			ot = objecttype;
			id = objectid;
			team = team;
			pl = pl;
			time = os.clock();
			timec = time;
		}
		table.insert(turretmod.buildingsites, tbl)
	end;
	
	spawnprojectile = function(projectile, x, y, dir, team, pl, speed, livelength)
		local tbl = {
			image = image(turretmod.projectiles[projectile].image, x, y, 1);
			speed = speed;
			x = x;
			y = y;
			team = team;
			player = pl;
			dir = dir;
			projectile = projectile;
			livelength = livelength or "0";
		}
		table.insert(turretmod.spawnedprojectiles, tbl)
		if turretmod.projectiles[projectile].onspawn then turretmod.projectiles[projectile].onspawn(#turretmod.spawnedprojectiles) end
	end;
	
	spawnlaser = function(laser, x1, y1, x2, y2, team, pl)
		local dir = -math.deg(math.atan2(x1 - x2, y1 - y2))
		local dist = math.sqrt((x1 - x2)^2 + (y1 - y2)^2)
		local x, y = x1 + math.sin(math.rad(dir)) * dist/2, y1 - math.cos(math.rad(dir)) * dist/2
		local img = image(turretmod.lasers[laser].image, x, y, 1)
		imageblend(img, 0)
		imagepos(img, x, y, dir)
		imagescale(img, 1, dist)
		imagecolor(img, turretmod.lasers[laser].r, turretmod.lasers[laser].g, turretmod.lasers[laser].b)
		tween_alpha(img, 1000, 0)
		timer(1000, "freeimage", img)
		local ex, ey = math.sin(math.rad(dir)), -math.cos(math.rad(dir))
		local exc = {v}
		for k, v in pairs(player(0, 'team'.. team)) do
			local xx, yy = x1, y1
			for i = 1, math.floor(dist) do
				xx = xx + ex
				yy = yy + ey
				if math.floor(xx / 32) == player(v, 'tilex') and math.floor(yy / 32) == player(v, 'tiley') then
					turretmod.lasers[laser].onplayercollision(v, pl)
					table.insert(exc, v)
					break
				end
			end
		end
	end;
	
	affect = function(player, effect, source)
		for k, v in pairs(turretmod.player[player].effects) do
			if v.effect == effect then return end
		end
		local tbl = {
			effect = effect;
			expiretime = os.clock() + turretmod.effects[effect].time;
			source = source
		}
		table.insert(turretmod.player[player].effects, tbl)
		if turretmod.effects[effect].onspawn then turretmod.effects[effect].onspawn(player, #turretmod.player[player].effects) end
	end;
	
	killprojectile = function(pid)
		if not turretmod.spawnedprojectiles[pid] then return end
		if turretmod.projectiles[turretmod.spawnedprojectiles[pid].projectile].ondeath then turretmod.projectiles[turretmod.spawnedprojectiles[pid].projectile].ondeath(pid) end
		freeimage(turretmod.spawnedprojectiles[pid].image)
		table.remove(turretmod.spawnedprojectiles, pid)
	end;
	
	freeline = function(x1, y1, x2, y2)
		local dir = -math.deg(math.atan2(x1 - x2, y1 - y2))
		local ex, ey = math.sin(math.rad(dir)), -math.cos(math.rad(dir))
		x1 = x1 + ex * 23
		y1 = y1 + ey * 23
		repeat
			x1 = x1 + ex
			y1 = y1 + ey
			if tile(math.floor(x1 / 32), math.floor(y1 / 32), "wall") then
				return false
			end
		until math.floor(x1 / 32) == math.floor(x2 / 32) and math.floor(y1 / 32) == math.floor(y2 / 32)
		return true
	end;
	
	openturretmenu = function(id, page, x, y)
		local page = page or 1
		local pages = math.ceil(#turretmod.turrets / 5)
		local str = "Build Turret P".. page
		for i = page * 5 - 4, page * 5 do
			str = str .. (turretmod.turrets[i] and ','.. turretmod.turrets[i].name ..'|Price: '.. turretmod.turrets[i].price or ',')
		end
		str = str ..','.. (page == pages and ',(Next Page -->)' or ',Next Page -->')
		str = str .. (page == 1 and ',(<-- Previous Page)' or ',<-- Previous Page')
		menu(id, str)
	end;
}
--Hooked functions
turretmod.hook = {
	join = function(id)
		turretmod.player[id] = {joined = false};
	end;
	
	team = function(id, team, look)
		if player(id, 'bot') then return end
		if not turretmod.player[id].joined then
			turretmod.player[id] = {
				joined = true;
				turretpoints = 0;
				bposx = 0;
				bposy = 0;
				effects = {};
			}
			parse('hudtxt2 '.. id ..' 1 "'.. string.char(169) ..'000150255Turret points: 0" 5 410 0')
		end
	end;
	
	kill = function(killer, victim, weapon, x, y)
		turretmod.player[killer].turretpoints = turretmod.player[killer].turretpoints + 1
		parse('hudtxt2 '.. killer ..' 1 "'.. string.char(169) ..'000150255Turret points: '.. turretmod.player[killer].turretpoints ..'" 5 410 0')
	end;
	
	always = function()
		for k, v in pairs(turretmod.spawnedturrets) do
			if v.timec + turretmod.turrets[v.turret].shootingcooldown < os.clock() and not v.locked then
				v.rotationvariable = v.rotationvariable + 1
				imagepos(v.gunimage, v.x * 32 + 16, v.y * 32 + 16, v.rootrotation + math.sin(math.rad(v.rotationvariable)) * 60)
			end
		end	
		
		for k, v in pairs(turretmod.spawnedprojectiles) do
			local nx, ny = v.x + math.sin(math.rad(v.dir)) * v.speed, v.y - math.cos(math.rad(v.dir)) * v.speed
			if tile(math.floor(nx / 32), math.floor(ny / 32), 'wall') then
				if turretmod.projectiles[v.projectile].onwallcollision then turretmod.projectiles[v.projectile].onwallcollision(k) end
			end
			for _, id in pairs(player(0, 'table')) do
				if player(id, 'tilex') == math.floor(nx / 32) and player(id, 'tiley') == math.floor(ny / 32) then
					if turretmod.projectiles[v.projectile].onplayercollision then turretmod.projectiles[v.projectile].onplayercollision(k, id) end
				end
			end
			v.x = nx
			v.y = ny
			imagepos(v.image, v.x, v.y, v.dir)	
			
			if v.livelength ~= "0" then
				v.livelength = v.livelength - v.speed
				if v.livelength <= 0 then
					turretmod.func.killprojectile(k)
				end
			end
		end
	end;
	
	objectkill = function(id, pl)
		for k, v in pairs(turretmod.spawnedturrets) do
			if v.objectid == id then
				freeimage(v.gunimage)
				freeimage(v.baseimage)
				freeimage(v.lightimage)
				turretmod.turrets[v.turret].ondeath(k)
				table.remove(turretmod.spawnedturrets, k)
				break
			end
		end
		
		for k, v in pairs(turretmod.buildingsites) do
			if v.id == id then
				table.remove(turretmod.buildingsites, k)
				break
			end
		end
	end;
	
	ms100 = function()
		for k, v in pairs(turretmod.spawnedturrets) do
			local team = v.team
			if not turretmod.turrets[v.turret].forallies then
				if team == 1 then team = 2 else team = 1 end
			end
			local closest
			local length = v.length
			for _, id in pairs(player(0, 'team'.. team ..'living')) do
				if player(id, 'x') > (v.x*32+16)-320 and player(id, 'y') > (v.y*32+16)-240 and player(id, 'x') < (v.x*32+16)+320 and player(id, 'y') < (v.y*32+16)+240 then
					if math.sqrt(((v.x * 32 + 16) - player(id, 'x'))^2 + ((v.y * 32 + 16) - player(id, 'y'))^2) <= length then
						if turretmod.func.freeline(v.x*32+16, v.y*32+16, player(id, 'x'), player(id, 'y')) then
							closest = id
							length = math.sqrt(((v.x * 32 + 16) - player(id, 'x'))^2 + ((v.y * 32 + 16) - player(id, 'y'))^2)
						end
					end
				end
			end
			if closest then
				v.rootrotation = -math.deg(math.atan2(v.x*32+16 - player(closest, 'x'), v.y*32+16 - player(closest, 'y')))
				v.locked = true
				v.rotationvariable = 0
				if v.time + turretmod.turrets[v.turret].shootingcooldown < os.clock() then
					turretmod.turrets[v.turret].whenclosest(k, closest)
					v.time = os.clock()
					v.timec = os.clock()
				end
			else
				v.locked = false
				if v.time + turretmod.turrets[v.turret].shootingcooldown < os.clock() then
					v.time = os.clock()
				end
			end
			imagepos(v.gunimage, v.x * 32 + 16, v.y * 32 + 16, v.rootrotation)
		end
		
		for k, v in pairs(turretmod.buildingsites) do
			if v.time + v.timec < os.clock() then
				parse('killobject '.. v.id)
				turretmod.func.spawnturret(v.turret, v.x, v.y, v.ot, v.team, v.pl)
				table.remove(turretmod.buildingsites, k)
			end
		end
		
		if turretmod.game.helptime + turretmod.game.helptimec < os.clock() then
			turretmod.game.helptime = os.clock()
			parse('hudtxt 0 "'.. string.char(169) ..'000255000Commands: !givepoints [id] [ammount] - Share your turretpoints with another player. | !info [id] - See the information about an another player. | !turretlist - See the list of the avaible turrets." 650 395 0')
			parse('hudtxtmove 0 0 80000 -10000 395')
		end
		
		for k, v in pairs(player(0, 'tableliving')) do
			for _, effect in pairs(turretmod.player[v].effects) do
				if turretmod.effects[effect.effect].update then turretmod.effects[effect.effect].update(v) end
				if os.clock() > effect.expiretime then
					table.remove(turretmod.player[v].effects, _)
					if turretmod.effects[effect.effect].onexpire then turretmod.effects[effect.effect].onexpire(v) end
				end
			end	
		end
	end;
	
	menu = function(id, title, button)
		if title:sub(1, 14) == 'Build Turret P' then
			local page = tonumber(title:sub(15))
			if button == 7 then
				turretmod.func.openturretmenu(id, page + 1)
			elseif button == 8 then
				turretmod.func.openturretmenu(id, page - 1)
			else
				local i = (page * 5 - 5) + button
				if turretmod.player[id].turretpoints >= turretmod.turrets[i].price then
					local ptx, pty, btx, bty = player(id, 'tilex'), player(id, 'tiley'), turretmod.player[id].bposx, turretmod.player[id].bposy
					if ptx >= btx - 1 and pty >= bty - 1 and ptx <= btx + 1 and pty <= bty + 1 then
						turretmod.player[id].turretpoints = turretmod.player[id].turretpoints - turretmod.turrets[i].price
						turretmod.func.spawnturretbs(i, turretmod.player[id].bposx, turretmod.player[id].bposy, 5, player(id, 'team'), id, 3)
						parse('hudtxt2 '.. id ..' 1 "'.. string.char(169) ..'000150255Turret points: '.. turretmod.player[id].turretpoints ..'" 5 410 0')
					else
						msg2(id, string.char(169) .."255000000You can't build like that@C")
					end
				else
					msg2(id, string.char(169) .."255000000You have insufficient funds@C")
				end
			end
		end
	end;
	
	buildattempt = function(id, type, x, y, mode)
		if type == 8 then
			turretmod.player[id].bposx, turretmod.player[id].bposy = x, y
			turretmod.func.openturretmenu(id, 1)
			return 1
		end
	end;
}