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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
parse("mp_dispenser_money 200") -- Dispenser gives 200$/sec
parse("mp_dispenser_health 2") -- Dispenser gives 2hp/sec
parse("sv_gm 2")
parse("mp_hud 24")
parse("mp_hovertext 17")
conload = {} -- Table managing loading of Construction2D
builder = {} -- Table for all builder's powers upgrades and similar stuff
consave = {} -- For temporarely storing stuff (like power)
conimg = {}
contur = {}
obj = {health = {},ammo = {},type ={},x = {},y = {},objimg = {},angle = {},status = {}}
building = {id={},x={},y={},ap={},up={}}
-- INFO : The more team buildings a player destroys, the more vulnerable he is to enemy team damage. E.g. 5 destroyed buildings = 5 extra damage to yourself when getting hit. Resets on round start.
-----------------------
------- HOOKS ---------
-----------------------
addhook("objectkill","con_objkill")
addhook("objectdamage","con_objdamage")
addhook("buildattempt","con_attempt")
addhook("startround","con_round")
addhook("spawn","con_spawn")
addhook("clientdata","con_data")
addhook("join","con_join")
addhook("buy","con_buy")
addhook("leave","con_leave")
addhook("team","con_team")
addhook("serveraction","con_sv")
addhook("menu","con_menu")
addhook("second","con_sec")
addhook("kill", "con_kill")
addhook("hit","con_hit")
addhook("projectile","con_projectile")
addhook("reload","con_rel")
addhook("build","con_build")
addhook("objectupgrade","con_upgrade")
addhook("spray","con_spray")
addhook("ms100","con_ms100")
function con_join(id) -- ^^ means it's done
builder[id] = {} -- ^^
builder[id].level = 0 -- ^^
builder[id].points = 0 	-- ^^
builder[id].upgradespd = 1 -- ^^ Player : Buildings Upgrade Speed | 4 Levels (Up to 5% upgrade per hit)
builder[id].udispenserhp = 0 -- ^^ Upgrade : Dispenser Health | 9 Levels (Up to 20HP/sec because 2HP is default dispenser)
builder[id].udispensercash = 0 -- ^^ Upgrade : Dispenser Cash | 3 Levels (Up to 2000$/sec as +600$)
builder[id].buildshield = 0 -- ^^ Upgrade : Buildings Shield ammount (Make a glowing shield image rotating) -- Player can still upgrade while shield is on, when shield is off and hits, upgrade = decreases depending on damage dealt. | 10 Levels
builder[id].maxturrets = 1 -- ^^ Upgrade : How many turrets can player make at max ?
	builder[id].cturret = 1 -- Upgrade : Custom turret. Allows you to shoot Grenades (30 shots),Gas Grenades(5 Shots),Rockets(20 shots),He Grenades(15 Shots) and at max level - Airstrikes (5 Shots). Shots recharge depending on level and every 30 seconds. |5 Levels
builder[id].wallrepair = "No" -- ^^ Upgrade : Wall Auto Repair
builder[id].expbullets = 0 -- ^^ Upgrade : Turret Bullets Explode on player when he's hit (low damage) | Explosion range 32px*level
builder[id].replenish = 0 -- (Not tested) ^^ Upgrade : Turret (and player) regains health when it kills an enemy | 5 Levels - 15%/25%/35%/50%/(65% and player current hp + player current hp)
builder[id].sneakturret = 0 -- ^^ Player : you throw airstrike and when it explodes, smoke grenade dropped and turret * level appears | 3 Levels
builder[id].maxhp = 20 -- ^^ Player : Max Health = maxhp*5 | 20 Levels (200 HP MAX)
builder[id].hexp = 0 -- ^^ Player : Explodes * ammount of times (HE grenade) | 4 Levels (so max 5 times including original)
builder[id].infammo = "No" -- ^^ Player : Infinite Ammo
builder[id].noreload = "No" -- ^^ Player : No need to reload
	builder[id].ignorance = 0 -- Player : Ignore damage of BUILDINGS (not players) for 5*ammount seconds | 3 Levels
builder[id].ignoring = "No"
builder[id].speed = 0 -- ^^ Player : STARTING speed (It's decreased until it's half the starting speed on getting hit (0 minimum))
	builder[id].buildvirus = 0 -- Upgrade : When TURRET kills an enemy it spawns a building in his position | Barbed-GateField-Turret
builder[id].builtturrets = 0 -- ^^ How many turrets a player already has built.
conload[id] = 0 			-- done
end
function con_round(roundID)
	building = {id={},x={},y={},ap={},up={}}
	obj = {health = {},ammo = {},type ={},x = {},y = {},objimg ={},angle = {},status = {}}
end
function con_spawn(id)
	if not player(id,"bot") then
		parse("setmaxhealth "..id.." "..builder[id].maxhp*5)
		parse("speedmod "..id.." "..builder[id].speed)
		conimg.hud = {}
		conimg.hud.hpheart = image('gfx/sprites/RainingMammoths/SMO/interface/bar_heart.png',0,0,2,id)
		conimg.hud.hpheartfill = image('gfx/sprites/RainingMammoths/SMO/interface/bhf_10.png',0,0,2,id)
		imagepos(conimg.hud.hpheart,26,464,0)
		imagepos(conimg.hud.hpheartfill,26,464,0)
		return "74"
	end
end
function con_menu(id,t,b)
	if t=="Player Menu" then
		if b==1 then
			menu(id,"Building Upgrades@b,Turret Virus|"..builder[id].buildvirus..",Dispenser Health Regen|"..builder[id].udispenserhp..",Dispenser Money|"..builder[id].udispensercash..",Building Shield|"..builder[id].buildshield..",Max Turrets|"..builder[id].maxturrets..",Turret Damage|"..builder[id].cturret..",Walls Auto Repair|"..builder[id].wallrepair..",Explosive Turret Bullets|"..builder[id].expbullets..",Abnormal Turrets|Level "..builder[id].replenish)
		elseif b==2 then
			local menu_health_show = builder[id].maxhp * 5
			menu(id,"Player Upgrades@b,Level Up|Unlock Better Upgrades,Max Health|"..menu_health_show..",Starting Speed|"..builder[id].speed..",Super Grenade|Level "..builder[id].hexp..",Infinite Ammo|"..builder[id].infammo..",No Reload|"..builder[id].noreload..",Upgrade Speed|+"..builder[id].upgradespd.."%/hit,Ignorance|Level "..builder[id].ignorance..",Sneaky Turret|"..builder[id].sneakturret) -- Make here
		end
	elseif t=="Building Upgrades" then
		if b==1 then
			if che(id,builder[id].buildvirus,150) then
				if builder[id].buildvirus~= 3 then
					builder[id].buildvirus=builder[id].buildvirus + 1
				else
					Error(id,"You already have the maximum upgrade for Sneak Turret !")
				end
			end
		elseif b==2 then
			if che(id,1,0) then --
				if builder[id].udispenserhp~= 9 then
					builder[id].udispenserhp=builder[id].udispenserhp + 1
				else
					Error(id,"You already have the maximum upgrade for Dispenser Regen !")
				end
			end
		elseif b==3 then
			if che(id,1,0) then --
				if builder[id].udispensercash~= 3 then
					builder[id].udispensercash=builder[id].udispensercash + 1
				else
					Error(id,"You already have the maximum upgrade for Dispenser Cash !")
				end
			end
		elseif b==4 then
			if che(id,math.floor(builder[id].buildshield/10),50) then --
				if builder[id].buildshield~= 100 then
					builder[id].buildshield=builder[id].buildshield + 1
				else
					Error(id,"You already have the maximum upgrade for Buildings Shield !")
				end
			end
		elseif b==5 then
			if che(id,builder[id].maxturrets,50) then --
				if builder[id].maxturrets~= 7 then
					builder[id].maxturrets=builder[id].maxturrets + 1
				else
					msg2(id,"\169255000000You already have the maximum upgrade for Max Turrets !")
				end
			end
		elseif b==6 then -- change this
			if che(id,builder[id].cturret*2,500) then --
				if builder[id].cturret~= 6 then
					builder[id].cturret=builder[id].cturret + 1
				else
					Error(id,"You already have the maximum upgrade for Custom Turret !")
				end
			end
		elseif b==7 then
			if che(id,5,500) then --
				if builder[id].wallrepair~= "Yes" then
					builder[id].wallrepair="Yes"
				else
					Error(id,"You already have Automatic Wall Repair !")
				end
			end
		elseif b==8 then
			if che(id,builder[id].expbullets+1,0) then
				if builder[id].expbullets~= 5 then
					builder[id].expbullets=builder[id].expbullets + 1
				else
					Error(id,"You already have the maximum upgrade for Explosive Turret Bullets !")
				end
			end
		elseif b==9 then
			if che(id,builder[id].replenish+1,100) then
				if builder[id].replenish~= 5 then
					builder[id].replenish=builder[id].replenish + 1
				else
					Error(id,"You already have the maximum upgrade for Abnormal Turrets !")
				end
			end
		end
	elseif t=="Player Upgrades" then
		if b==1 then
			if builder[id].points>builder[id].level*300 then
				builder[id].points=builder[id].points-(builder[id].level*100+250)
				msg2(id,"\169000255000You Leveled up to "..builder[id].level+1 .." level !")
				builder[id].level=builder[id].level+1
			else
				Error(id,"You don't have enough points to level up ! You need "..(builder[id].level*100+250).." points !")
			end
		elseif b==2 then
			if che(id,5,0) then
				if builder[id].maxhp~= 40 then
					builder[id].maxhp=builder[id].maxhp + 1
				else
					Error(id,"You already have the maximum upgrade for Maximum Health !")
				end
			end
		elseif b==3 then
			if che(id,5,0) then
				if builder[id].speed~= 20 then
					builder[id].speed=builder[id].speed + 1
				else
					Error(id,"You already have the maximum upgrade for Starting Speed !")
				end
			end
		elseif b==4 then
			if che(id,3,150) then
				if builder[id].hexp~= 4 then
					builder[id].hexp=builder[id].hexp + 1
				else
					Error(id,"You already have the maximum upgrade for Super Grenade !")
				end
			end
		elseif b==5 then
			if che(id,2,300) then
				if builder[id].inf~= "Yes" then
					builder[id].inf="Yes"
				else
					Error(id,"You already have the Infinite Ammo upgrade !")
				end
			end
		elseif b==6 then
			if che(id,3,300) then
				if builder[id].noreload~= "Yes" then
					builder[id].noreload="Yes"
				else
					Error(id,"You already have No Reload upgrade !")
				end
			end
		elseif b==7 then
			if che(id,builder[id].upgradespd,150) then
				if builder[id].upgradespd~= 5 then
					builder[id].upgradespd=builder[id].upgradespd + 1
				else
					Error(id,"You already have the maximum upgrade for Upgrade Speed !")
				end
			end
		elseif b==8 then
			if che(id,builder[id].ignorance+1,0) then
				if builder[id].ignorance~= 3 then
					builder[id].ignorance=builder[id].ignorance + 1
				else
					Error(id,"You already have the maximum upgrade for Ignorance !")
				end
			end
		elseif b==9 then
			if che(id,2+builder[id].sneakturret,100) then
				if builder[id].sneakturret~= 3 then
					builder[id].sneakturret=builder[id].sneakturret + 1
				else
					Error(id,"You already have the maximum upgrade for Sneak Turrets !")
				end
			end
		end
	end
end
function che(id,lvl,ptz)
local pts = ptz + builder[id].level * 100
	if builder[id].level>=lvl then
		if builder[id].points>pts then
			builder[id].points=builder[id].points - pts
			msg2(id,"\169000255010You successfully bought the item !")
			return true
		else
			Error(id,"You don't have enough points ! "..pts.." points are needed !")
			return false
		end
	else
		Error(id,"Your level is not high enough ! "..lvl.." level is needed !")
		return false
	end
end
function con_leave(id)
	if conload[id] == 1 then
		_consavestats(id)
	end
end
function con_team(id)
	if conload[id] == 0 then
		_conloadstats(id)
		 conload[id] = 1
	end	
end
function con_spray(id)
	if builder[id].ignorance > 0 then
		builder[id].ignorant = "Yes"
		timer(5000*user[id].ignorance,"parse","lua builder[id].ignorant='No'")
	end
end
function _conloadstats(id)
print(conload[id])
local usgn = player(id,"usgn")
	if usgn > 0 then
		local file = io.open("sys/lua/saves/"..usgn.."con.txt","r")
		if file then
			local lvl,pt,upgs,disc,dishp,bshield,maxt,turdmg,wall,expbull,replen,sneak,maxhp,he,inf,reload,ign,spd,virus = file:read("*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l","*l")
			builder[id].level = tonumber(lvl) or 1		-- done
			builder[id].points = tonumber(pt) or 0 	-- done
			builder[id].upgradespd = tonumber(upgs) or 1 -- Player : Buildings Upgrade Speed | 4 Levels (Up to 5% upgrade per hit)
			builder[id].udispenserhp = tonumber(dishp) or 0 -- Upgrade : Dispenser Health | 9 Levels (Up to 20HP/sec because 2HP is default dispenser)
			builder[id].udispensercash = tonumber(disc) or 0 -- Upgrade : Dispenser Cash | 3 Levels (Up to 2000$/sec as +600$)
			builder[id].buildshield = tonumber(bshield) or 0 -- Upgrade : Buildings Shield ammount (Make a glowing shield image rotating) -- Player can still upgrade while shield is on, when shield is off and hits, upgrade = decreases depending on damage dealt. | 10 Levels
			builder[id].maxturrets = tonumber(maxt) or 0 -- Upgrade : How many turrets can player make at max ?
			builder[id].cturret = tonumber(ctur) or 0 -- Upgrade : Turret Damage.
			builder[id].wallrepair = tostring(wall) or "No" -- Upgrade : Wall Auto Repair
			builder[id].expbullets = tonumber(expbull) or 0 -- Upgrade : Turret Bullets Explode on player when he's hit (low damage) | Explosion range 32px*level
			builder[id].replenish = tonumber(replen) or 0 -- Upgrade : Turret (and player) regains health when it kills an enemy | 5 Levels - 15%/25%/35%/50%/65% and player current hp + player current hp
			builder[id].sneakturret = tonumber(sneak) or 0 -- Player : you throw airstrike and when it explodes, smoke grenade dropped and turret * level appears | 3 Levels
			builder[id].maxhp = tonumber(maxhp) or 20 -- Player : Max Health = maxhp*5 | 20 Levels (200 HP MAX)
			builder[id].hexp = tonumber(he) or 0 -- Player : Explodes * ammount of times (HE grenade) | 4 Levels (so max 5 times including original)
			builder[id].infammo = tostring(inf) or "No" -- Player : Infinite Ammo
			builder[id].noreload = tostring(reload) or "No" -- Player : No need to reload
			builder[id].ignorance = tonumber(ign) or 0 -- Player : Ignore damage of BUILDINGS (not players) for 5*ammount seconds | 3 Levels
			builder[id].speed = tonumber(spd) or 0 -- Player : STARTING speed (It's decreased until it's half the starting speed on getting hit (0 minimum))
			builder[id].buildvirus = tonumber(virus) or 0 -- Upgrade : When TURRET kills an enemy it spawns a building in his position | Barbed-GateField-Turret
			msg2(id,"©000255000Your Construction2D save has been loaded correctly.@C")
			file:close()
		else
			msg2(id,"©255000000Failed to load your Construction2D save.@C")
			builder[id].level = 1
			builder[id].points = 0
		end
	else
		msg2(id,"©255000000You are not logged in. To save stats, register at unrealsoftware.de@C")
		builder[id].level = 1
		builder[id].points = 0
	end
end
function _consavestats(id)
local usgn = player(id,"usgn")
	if usgn > 0 then
		local file = io.open("sys/lua/saves/"..usgn.."con.txt","w")
		local lvl,pt,upgs,disc,dishp,bshield,maxt,ctur,wall,expbull,replen,sneak,maxhp,he,inf,reload,ign,spd,virus = builder[id].level,builder[id].points,builder[id].upgradespd,builder[id].udispensercash,builder[id].udispenserhp,builder[id].buildshield,builder[id].maxturrets,builder[id].cturret,builder[id].wallrepair,builder[id].expbullets,builder[id].replenish,builder[id].sneakturret,builder[id].maxhp,builder[id].hexp,builder[id].infammo,builder[id].noreload,builder[id].ignorance,builder[id].speed,builder[id].buildvirus
		file:write(lvl.."\n"..pt.."\n"..upgs.."\n"..disc.."\n"..dishp.."\n"..bshield.."\n"..maxt.."\n"..ctur.."\n"..wall.."\n"..expbull.."\n"..replen.."\n"..sneak.."\n"..maxhp.."\n"..he.."\n"..inf.."\n"..reload.."\n"..ign.."\n"..spd.."\n"..virus)
		print("©000255000Saved Construction2D stats .")
		file:close()
	end
end
function con_hit(id,sour,wep,hpdmg,apdmg,rawdmg)
	if not player(id,"bot") then
		local number = (player(id,"health")/player(id,"maxhealth")-player(id,"health")/player(id,"maxhealth")%0.1)*10
		freeimage(conimg.hud.hpheartfill)
		conimg.hud.hpheartfill = image('gfx/sprites/RainingMammoths/SMO/interface/bhf_'..number..'.png',0,0,2,id)
		imagepos(conimg.hud.hpheartfill,26,464,0)
		if builder[id].speed > 0 and builder[id].speed ~= 1 then
			local newspeed = math.floor(builder[id].speed/2)
			if player(id,"team")~=player(sour,"team") then
				parse("speedmod "..id.." "..newspeed)
			end
		end
		if wep == 253 and builder[object(sour,"player")].expbullets > 0 then
			parse("explosion "..player(id,"x").." "..player(id,"y").." "..builder[id].expbullets*32 .." "..builder[id].expbullets*6+15 .." "..sour)
		end
		if builder[id].ignoring == "Yes" then
			if sour == 0 then
				return 1
			end
		end
	end
end
function con_upgrade(id,p,progress,total) -- Object ID,Player,How much upgraded and total upgrade needed.
	for k=1, #building.id do
		if object(id,"tilex")==building.x[k] and object(id,"tiley")==building.y[k] and player(p,"money")>199 then
			building.up[k]=building.up[k]+builder[p].upgradespd
			parse("setmoney "..p.." "..player(p,"money")-200)
			if building.up[k]>=100 then -- this part works
				if object(id,"type")==8 then -- single turret
					parse("damageobject "..id.." 1000 0")
					parse("spawnobject 11 "..building.x[k].." "..building.y[k].." 0 0 "..player(p,"team").." "..p)
					building.up[k] = 0
				elseif object(id,"type")==11 then
					if building.up[k]>=100 and object(id,"tilex")==building.x[k] and object(id,"tiley")==building.y[k] then
						parse("damageobject "..id.." 1000 0")
						parse("spawnobject 12 "..building.x[k].." "..building.y[k].." 0 0 "..player(p,"team").." "..p)
						building.up[k] = 0
					end
				end
			end
		end
	end
	return 1
end
function con_objdamage(id,damage,player)
	for k=1, #building.id do
		if object(id,"tilex")==building.x[k] and object(id,"tiley")==building.y[k] then
			if damage < building.ap[k] and damage > 0 then
				building.ap[k] = building.ap[k] - damage
				return 1
			end
		end
	end
end	
function con_attempt(id,type,x,y,mode)
	if type == 8 and builder[id].builtturrets == builder[id].maxturrets then
		Error(id,"You already reached max turret building limit !")
		return 1
	end
	return 0
end
function con_objkill(id,p)
	if builder[p].builtturrets > 0 then
		builder[p].builtturrets = builder[p].builtturrets - 1
	end
end
function MakeBuilding(id,x,y,ap,up)
	building.id[#building.id+1]=id -- Building BUILDER ID
	building.x[#building.x+1]=x -- Not used
	building.y[#building.y+1]=y -- Not used
	building.ap[#building.ap+1]=ap -- Not used
	building.up[#building.up+1]=0 -- Not used
end
function con_build(id,type,x,y,mode,objectid)
	MakeBuilding(id,x,y,builder[id].buildshield*50,0)
	builder[id].builtturrets = builder[id].builtturrets + 1
end
function con_ms100()
	for _,id in ipairs (player(0,"tableliving")) do
		reqcld(id,2)
	end
end
function con_data(id,mode,x,y)
	if mode==2 then
		local tx=math.floor(x/32) -- If there's something
		local ty=math.floor(y/32) -- on coordinates x|y (in tiles)
		for key=1, #building.id do
			if tx == building.x[key] and ty == building.y[key] then
				hud_x = x - player(id,"x") + 320
				hud_y = y - player(id,"y") + 240
				local txt="\169255255255Shield: "..building.ap[key].." Upgrade %: "..building.up[key]
				parse('hudtxt2 '..id..' 14 "'..txt..'" '..hud_x-64 ..' '..hud_y+4 ..' 0')
				return
			end
		end
		parse("hudtxt2 "..id.." 14")
	end
end
function con_sv(id,b)
	if b==2 then
		menu(id,"Player Menu,Building Upgrades,Player Upgrades")
	elseif b == 3 then -- Building
		if obj.objimg[id] == nil then
			MakeStructure(500,id,60,"grenade",player(id,"tilex"),player(id,"tiley"))
		else
			Error(id,"You already have a turret placed...")
		end
	end
end
function con_rel(id,mode) -- On Finish make infinite Ammo,On start make no reload
	if mode== 2 then
		if builder[id].infammo=="On" then
			parse("equip "..id.." 61")
			parse("equip "..id.." 62")
		end
	elseif mode== 1 then
		if builder[id].noreload=="On" then
			parse("equip "..id.." "..player(id,"weapontype"))
		end
	end
end
		
function con_projectile(id,wep,x,y) -- Super HE,Sneaky turret
	if builder[id].hexp>0 then
		if wep==51 then
			for i = 1,builder[id].hexp do
				timer(500*i,"parse","explosion "..x.." "..y.." "..(32+48*i).." "..i*25+100 .." "..id)
			end
		end
	end
	if builder[id].sneakturret>0 then
		if wep==76 then
			local tx=math.floor(x/32)
			local ty=math.floor(y/32)
			parse("spawnprojectile "..id.." 53 "..tx*32+16 .." "..ty*32+16 .." 0 0")
			if builder[id].sneakturret==1 then
				parse("spawnobject 8 "..tx.." "..ty.." 0 0 "..player(id,"team").." "..id)
			elseif builder[id].sneakturret==2 then
				parse("spawnobject 11 "..tx.." "..ty.." 0 0 "..player(id,"team").." "..id)
			elseif builder[id].sneakturret==3 then
				parse("spawnobject 12 "..tx.." "..ty.." 0 0 "..player(id,"team").." "..id)
			end
		end
	end
end
function con_buy(id,weapon)
	msg2(id,"Buying is not allowed")
	return 1
end
function MakeStructure(hp,id,amunition,typ,tx,ty) -- Add Hitzone
	obj.health[id]=hp
	obj.ammo[id]=amunition
	obj.type[id]=typ
	obj.x[id]=tx
	obj.y[id]=ty
	obj.angle[id]=0
	obj.status[id] = 0
	if player(id,"team")==1 then
		if obj.objimg[id]== nil then
			obj.objimg[id]=image("gfx/sprites/RainingMammoths/CS2D/base t.png",0,0,1)
			obj.objimg[id*100+1]=image("gfx/flare1.bmp",0,0,1)
			imagecolor(obj.objimg[id*100+1],255,0,0)
			obj.objimg[id*100]=image("gfx/sprites/RainingMammoths/CS2D/turret.png",0,0,1)
		end
	elseif player(id,"team")==2 then
		if obj.objimg[id]== nil then
			obj.objimg[id]=image("gfx/sprites/RainingMammoths/CS2D/base ct.png",0,0,1)
			obj.objimg[id*100+1]=image("gfx/flare1.bmp",0,0,1)
			imagecolor(obj.objimg[id*100+1],0,0,255)
			obj.objimg[id*100]=image("gfx/sprites/RainingMammoths/CS2D/turret.png",0,0,1)
		end
	end
	imagescale(obj.objimg[id*100+1],1.75,1.75)
	imageblend(obj.objimg[id*100+1],1)
	imagepos(obj.objimg[id],obj.x[id]*32+16,obj.y[id]*32+16,0)
	imagepos(obj.objimg[id*100],obj.x[id]*32+16,obj.y[id]*32+16,obj.angle[id])
	imagepos(obj.objimg[id*100+1],obj.x[id]*32+16,obj.y[id]*32+16,obj.angle[id])
	imagehitzone(obj.objimg[id],102,-16,-16,32,32)
	tween_rotateconstantly(obj.objimg[id*100+1],1)
	--BoredRotation(id)
end
function con_kill(id,victim,weapon,x,y) -- Upgrade : Turret (and player) regains health when it kills an enemy
	parse("setmoney "..id.." "..player(id,"money")+math.random(1,900))
	builder[id].points = builder[id].points + 10
	if wep == 253 then
		for _,i in pairs(objectlist) do
			if object(i,"target") == victim and object(i,"cooldown") < 200 then
				local ply = object(i,"target")
				if builder[ply].replenish > 0 and builder[ply].replenish < 5 then
					parse("sethealth "..ply.." "..player(ply,"health")+math.floor(player(ply,"maxhealth")/100*builder[ply].replenish*10))
					parse("damageobject "..i.." "..(-object(i,"health")*3).." 0")
				end
			end
		end
	end
end
function dispenserNearby(id)
	local objectlist=object(0,"table")
	for _,i in pairs(objectlist) do
		for loopX = -1,1 do
			for loopY = -1,1 do
				if object(i,"type")==7 and player(id,"tilex")==object(i,"tilex")+loopX and player(id,"tiley")==object(i,"tiley")+loopY and player(id,"team")==object(i,"team") then
					if builder[id].udispensercash > 0 then
						parse("setmoney "..id.." "..player(id,"money")+builder[id].udispensercash*600)
					end
					if builder[id].udispenserhp > 0 then
						parse("sethealth "..id.." "..player(id,"health")+builder[id].udispenserhp*2)
					end
				end
			end
		end
	end
end
function con_sec()
	for _, id in pairs(player(0,"tableliving")) do
		if not player(id,"bot") then
			dispenserNearby(id)
			if builder[id].wallrepair == "Yes" then
				for _,i in pairs(object(0,"table")) do
					if object(i,"player")==id and object(i,"type")>2 and object(i,"type")<6 then
						parse("damageobject "..i.." -8 "..id)
					end
				end
			end
			parse("hudtxt2 "..id.." 12 \"\169000080047Level : "..builder[id].level.." \" 460 445 0")
			parse("hudtxt2 "..id.." 13 \"\169000080047Points : "..builder[id].points.." \" 460 460 0")
		end
	end
end
--[[ CustomTurret.lua
addhook("startround","Reset")
function Reset()
	obj = {health = {},ammo = {},type ={},x = {},y = {},objimg ={},angle = {},status = {}}
end
function MakeStructure(hp,id,amunition,typ,tx,ty) -- Add Hitzone
	obj.health[id]=hp
	obj.ammo[id]=amunition
	obj.type[id]=typ
	obj.x[id]=tx
	obj.y[id]=ty
	obj.angle[id]=0
	obj.status[id] = 0
	if player(id,"team")==1 then
		if obj.objimg[id]== nil then
			obj.objimg[id]=image("gfx/sprites/RainingMammoths/CS2D/base t.png",0,0,1)
			obj.objimg[id*100+1]=image("gfx/flare1.bmp",0,0,1)
			imagecolor(obj.objimg[id*100+1],255,0,0)
			obj.objimg[id*100]=image("gfx/sprites/RainingMammoths/CS2D/turret.png",0,0,1)
		end
	elseif player(id,"team")==2 then
		if obj.objimg[id]== nil then
			obj.objimg[id]=image("gfx/sprites/RainingMammoths/CS2D/base ct.png",0,0,1)
			obj.objimg[id*100+1]=image("gfx/flare1.bmp",0,0,1)
			imagecolor(obj.objimg[id*100+1],0,0,255)
			obj.objimg[id*100]=image("gfx/sprites/RainingMammoths/CS2D/turret.png",0,0,1)
		end
	end
	imagescale(obj.objimg[id*100+1],1.75,1.75)
	imageblend(obj.objimg[id*100+1],1)
	imagepos(obj.objimg[id],obj.x[id]*32+16,obj.y[id]*32+16,0)
	imagepos(obj.objimg[id*100],obj.x[id]*32+16,obj.y[id]*32+16,obj.angle[id])
	imagepos(obj.objimg[id*100+1],obj.x[id]*32+16,obj.y[id]*32+16,obj.angle[id])
	imagehitzone(obj.objimg[id],102,-16,-16,32,32)
	tween_rotateconstantly(obj.objimg[id*100+1],1)
	--BoredRotation(id)
end
addhook("serveraction","ObjectControl")
function ObjectControl(id,b)
	if b == 3 then -- Building
		if obj.objimg[id] == nil then
			MakeStructure(500,id,60,"grenade",player(id,"tilex"),player(id,"tiley"))
		else
			parse('hudtxt2 '..id..' 1 "\169255000000You already have a turret placed..." 210 415 0')
			freetimer("parse","hudtxt2 "..id.." 1")
			timer(5000,"parse","hudtxt2 "..id.." 1")
			parse("hudtxtalphafade "..id.." 1 5000 0")
		end
	elseif b == 2 then -- Controlling inside of it.
		local distance = math.sqrt((obj.x[id]*32+16 - player(id,"x"))^2 + (obj.y[id]*32+16 - player(id,"y"))^2)
		if distance < 20 then
			if obj.status[id] == 0 then
				parse("setpos "..id.." "..obj.x[id]*32+16 .." "..obj.y[id]*32+16)
				parse("speedmod "..id.." -100")
				obj.status[id] = 1
			elseif obj.status[id] == 1 then
				parse("speedmod "..id.." 0")
				obj.status[id] = 0
			end
		end
	end
end
--function BoredRotation(id)
	obj.angle[id*100] = math.random(-180,180)
	if obj.angle[id*100] == 0 then
		BoredRotation(id)
	end
	tween_rotate(obj.objimg[id*100],2000,obj.angle[id*100])
	timer(2000,"BoredRotation",id)
end
function Angle(a1,a2,b1,b2) -- a1/b1 is y AND a2/b2 is x
	local ang = math.deg(math.atan2(a1 - b1, a2 - b2))
	return ang
end
addhook("use","SendOrder") -- Add Ammo.
function SendOrder(id)
	local distance = math.sqrt((obj.x[id]*32 - player(id,"x"))^2 + (obj.y[id]*32 - player(id,"y"))^2)
	if distance < 435 then
		reqcld(id,2)
		parse('hudtxt2 '..id..' 0 "\169255255255Ammo:'..obj.ammo[id]..' Health:'..obj.health[id]..'" 250 430 0')
	else
		parse('hudtxt2 '..id..' 1 "\169255000000You are too far away to control your turret..." 210 415 0')
		freetimer("parse","hudtxt2 "..id.." 1")
		timer(5000,"parse","hudtxt2 "..id.." 1")
		parse("hudtxtalphafade "..id.." 1 5000 0")
	end
end
addhook("clientdata","Shoot")
function Shoot(id,mode,d1,d2)
	if mode == 2 then
		local distance = math.sqrt((obj.x[id]*32 - d1)^2 + (obj.y[id]*32 - d2)^2)
		obj.angle[id] = Angle(obj.y[id]*32,obj.x[id]*32,d2,d1)-90
		if obj.status[id] == 0 then
			if obj.ammo[id]>0 then
				imagepos(obj.objimg[id*100],obj.x[id]*32+16,obj.y[id]*32+16,obj.angle[id])
				obj.ammo[id] = obj.ammo[id] - 1
				parse("spawnprojectile "..id.." 49 "..obj.x[id]*32+16 .." "..obj.y[id]*32+16 .." "..distance.." "..obj.angle[id]+math.random(-5,5))
				parse("sv_sound2 "..id.." weapons/glaunch.ogg")
			else
				parse('hudtxt2 '..id..' 1 "\169255000000Your turret does not have enough ammo..." 210 415 0')
				freetimer("parse","hudtxt2 "..id.." 1")
				timer(5000,"parse","hudtxt2 "..id.." 1")
				parse("hudtxtalphafade "..id.." 1 5000 0")
			end
		elseif obj.status[id] == 1 then
			if obj.ammo[id]>2 then
				imagepos(obj.objimg[id*100],obj.x[id]*32+16,obj.y[id]*32+16,obj.angle[id])
				obj.ammo[id] = obj.ammo[id] - 3
				parse("spawnprojectile "..id.." 48 "..obj.x[id]*32+16 .." "..obj.y[id]*32+16 .." "..math.random(240,600).." "..obj.angle[id]+math.random(-5,5))
				parse("sv_sound2 "..id.." weapons/rocket.ogg")
			else
				freetimer("parse","hudtxt2 "..id.." 1")
				timer(5000,"parse","hudtxt2 "..id.." 1")
				parse('hudtxt2 '..id..' 1 "\169255000000Your turret does not have enough ammo..." 210 415 0')
				parse("hudtxtalphafade "..id.." 1 5000 0")
			end
		end
	end
end
addhook("hitzone","Sustain")
function Sustain(imageid,sourceplayer,sourceobject,weapon,impx,impy,damage)
	for _, all in pairs(player(0,"table")) do
		if imageid == obj.objimg[all] then
			if player(sourceplayer,"team")~= player(all,"team") then
				obj.health[all] = obj.health[all] - damage
				if damage > obj.health[all] then
					parse("explosion "..obj.x[all]*32+16 .." "..obj.y[all]*32+16 .." 48 0 0")
					freeimage(obj.objimg[all])
					freeimage(obj.objimg[all*100])
					freeimage(obj.objimg[all*100+1])
					obj.objimg[all] = nil
					obj.objimg[all*100] = nil
					obj.objimg[all*100+1] = nil
					obj.status[all] = nil
					obj.health[all] = 0
				end
				parse('hudtxt2 '..all..' 0 "\169255255255Ammo:'..obj.ammo[all]..' Health:'..obj.health[all]..'" 250 430 0')
				break
			end
		end
	end
end]]