Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------
- --- GENERAL FUNCTIONS ---
- -------------------------
- function contains(table, element)
- for _, value in ipairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- function table.remove(table, element)
- if #table > 1 then
- for key, value in ipairs(table) do
- if value == element then
- table[key] = nil
- end
- end
- else
- table = {}
- end
- end
- function table.insert2(table, element)
- if #table > 0 then
- for key, value in ipairs(table) do
- if value == nil then
- table[key] = element
- end
- end
- else
- table[1]=element
- end
- end
- function angledir(rot,x,y,factor)
- if rot<-90 then rot=rot+360 end
- local angle=math.rad(math.abs(rot+90))-math.pi
- local cx=x+(math.cos(angle)*factor)
- local cy=y+(math.sin(angle)*factor)
- return cx,cy
- end
- function distance(x1,y1,x2,y2)
- local x=x1-x2
- local y=y1-y2
- return math.sqrt(x^2+y^2)
- end
- ------------------------------
- ---- SCRIPT-RELATED STUFF ----
- ------------------------------
- ------ VARIABLES AND TABLES ------
- turret={}
- plydata={}
- plydata.cimg = "gfx/sprites/block.bmp" -- image of the player controlling the turret; modify this variable to change
- turret.hp = 250 -- health of any given turret; modify this variable to change
- turret.bimg = "gfx/sprites/block.bmp" -- image of any given turret base; modify this variable to change
- turret.gimg = "gfx/weapons/turret.bmp" -- image of any given turret gun; modify this variable to change
- turret.aux = {} -- auxiliary functions table (e.g. say functions, switch functions etc.)
- turret.controlling = {} -- table of players that are controlling turrets
- ------ TURRET-RELATED FUNCTIONS ------
- function turret.new(ply,x,y) -- create new turret; x y are tiles!
- table.insert2(turret,{
- owner = ply,
- pos = {x,y},
- health = turret.hp,
- bimg = image(turret.bimg,x*32+16,y*32+16,3),
- gimg = image(turret.gimg,x*32+16,y*32+16,3),
- })
- local id = #turret
- if not plydata[ply] then plydata[ply]={} end
- plydata[ply].turret = id
- return id
- end
- function turret.kill(id) -- destroy a turret
- local ply = turret[id].owner
- if (turret[id].controlled) then
- turret.scontrol(ply)
- end
- parse("explosion "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 128 20 "..ply)
- freeimage(turret[id].bimg)
- freeimage(turret[id].gimg)
- turret[id] = nil
- return
- end
- function turret.hit(id,dmg) -- hit a turret
- if (turret[id].health > dmg) then
- turret[id].health = turret[id].health - dmg
- parse("effect \"flare\" "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 3 8 255 255 0")
- else
- turret.kill(id)
- return
- end
- end
- ------ PLAYER-RELATED FUNCTIONS ------
- function turret.control(ply) -- assume control of a turret
- plydata[ply].controlling = true
- local id = plydata[ply].turret
- turret[id].controlled = true
- table.insert2(turret.controlling,ply)
- plydata[ply].cimg = image(plydata.cimg,player(ply,"x"),player(ply,"y"),0)
- imagepos(plydata[ply].cimg,player(ply,"x"),player(ply,"y"),player(ply,"rot"))
- plydata[ply].armor = player(ply,"armor") -- old armor to revert to after destruction or control stop
- plydata[ply].pos = {player(ply,"x"),player(ply,"y")} -- old position to teleport to
- plydata[ply].speed = player(ply,"speedmod") -- old speedmod to revert to
- parse("setarmor "..ply.." 206")
- parse("setpos "..ply.." "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16))
- if not contains(playerweapons(ply),40) then -- if a player doesn't already have a machine gun then it's equipped, leave it otherwise
- parse("equip "..id.." 40")
- plydata[ply].meq=true
- else
- plydata[ply].meq=false
- end
- parse("speedmod "..ply.." -100")
- parse("setweapon "..ply.." 40")
- end
- function turret.scontrol(ply) -- cease control of a turret
- plydata[ply].controlling = false
- local id = plydata[ply].turret
- turret[id].controlled = false
- parse("setarmor "..ply.." "..plydata[ply].armor)
- if (plydata[ply].meq) then
- parse("strip "..ply.." 40")
- end
- parse("setpos "..ply.." "..plydata[ply].pos[1].." "..plydata[ply].pos[2])
- parse("speedmod "..ply.." "..plydata[ply].speed)
- freeimage(plydata[ply].cimg)
- table.remove(turret.controlling,ply)
- plydata[ply].cimg = nil
- plydata[ply].armor = nil
- plydata[ply].pos = nil
- plydata[ply].speed = nil
- end
- function turret.acquire(ply)
- if not plydata[ply] then plydata[ply]={} end
- plydata[ply].tpacked = true -- player has turret packed
- msg2(ply,"You have acquired a remote controlled turret.")
- end
- ------ AUXILIARY FUNCTIONS ------
- function turret.aux.update()
- for id=1,#turret do
- if (turret[id].controlled) then
- imagepos(turret[id].gimg,player(turret[id].owner,"x"),player(turret[id].owner,"y"),player(turret[id].owner,"rot"))
- end
- end
- end
- function turret.aux.ms100()
- for id=1,#turret do
- if (turret[id].health<=turret.hp/2) then
- parse("effect \"smoke\" "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 3 4 255 255 0")
- if (turret[id].health<=turret.hp/4) then
- parse("effect \"fire\" "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 3 4 255 255 0")
- end
- end
- end
- end
- function turret.aux.say(ply,t)
- if (t:sub(1,8) == "!control") then
- if (plydata[ply].turret) then
- turret.control(ply)
- end
- return 1
- end
- if (t:sub(1,5) == "!stop") then
- if (plydata[ply].controlling) then
- turret.scontrol(ply)
- end
- return 1
- end
- if (t:sub(1,7) == "!deploy") then
- if (plydata[ply].tpacked) then
- turret.new(ply,player(ply,"tilex"),player(ply,"tiley"))
- end
- return 1
- end
- end
- function turret.aux.check(x1,y1,x2,y2) -- check path between two points (tiles)
- for x=math.max(x1,x2),math.min(x1,x2) do
- for y=math.max(y1,y2),math.min(y1,y2) do
- if not (tile(x,y,"walkable")) then
- return false
- end
- end
- end
- return true
- end
- function turret.aux.hit(ply,plys,_,hpdmg)
- if (plydata[ply] and plydata[ply].controlling) then
- turret.hit(plydata[ply].turret,hpdmg)
- return 1
- end
- if (plydata[plys] and plydata[plys].controlling and player(ply,"health")<=hpdmg) then
- parse("customkill "..plys.." \"RC Turret\" "..ply)
- return 1
- end
- end
- function turret.aux.shoot(ply)
- for i=1,#turret.controlling do
- local ply2=turret.controlling[i]
- if (turret.aux.check(player(ply,"tilex"),player(ply,"tiley"),player(ply2,"tilex"),player(ply2,"tiley"))) then
- for i=1,640 do
- local x,y=angledir(player(ply,"rot"),player(ply,"x"),player(ply,"y"),i)
- if (plydata[ply2].pos and distance(plydata[ply2].pos[1],plydata[ply2].pos[2],x,y)<=16) then
- msg(x)
- msg(y)
- msg(plydata[ply2].pos[1])
- msg(plydata[ply2].pos[2])
- if (player(ply2,"health")>itemtype(player(ply,"weapon"),"dmg")) then
- parse("sethealth "..ply2.." "..player(ply2,"health")-itemtype(player(ply,"weapon"),"dmg"))
- else
- parse("customkill "..ply.." "..itemtype(player(ply,"weapon"),"name").." "..ply2)
- end
- break
- end
- end
- end
- end
- for id=1,#turret do
- if (not turret[id].controlled and turret.aux.check(player(ply,"tilex"),player(ply,"tiley"),turret[id].pos[1],turret[id].pos[2])) then
- for i=32,640 do
- local x,y=angledir(player(ply,"rot"),player(ply,"x"),player(ply,"y"),i)
- if (distance(turret[id].pos[1]*32+16,turret[id].pos[2]*32+16,x,y)<=16) then
- turret.hit(id,itemtype(player(ply,"weapon"),"dmg"))
- break
- end
- end
- end
- end
- end
- function turret.aux.select(ply,type)
- if (plydata[ply].controlling) then
- if (type ~= 40) then
- parse("setweapon "..ply.." 40")
- end
- end
- end
- function turret.aux.die(ply)
- if (plydata[ply] and plydata[ply].controlling) then
- plydata[ply].spos = plydata[ply].pos
- turret.scontrol(ply)
- end
- end
- function turret.aux.spawn(ply)
- if (plydata[ply] and plydata[ply].spos) then
- parse("setpos "..ply.." "..plydata[ply].spos[1].." "..plydata[ply].spos[2])
- plydata[ply].spos = nil
- end
- end
- ------ HOOKS ------
- addhook("always","turret.aux.update")
- addhook("ms100","turret.aux.ms100")
- addhook("attack","turret.aux.shoot")
- addhook("hit","turret.aux.hit")
- addhook("say","turret.aux.say")
- addhook("select","turret.aux.select")
- addhook("die","turret.aux.die")
- addhook("spawn","turret.aux.spawn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement