Jump to content

Working Turret Merchant


Aki

Recommended Posts

avorion-turret.png

 

Working, simple turret merchant script. Just change your current turret merchant's source to this one.

 

package.path = package.path .. ";data/scripts/lib/?.lua"
require ("galaxy")
require ("utility")
require ("faction")
require ("player")
require ("randomext")

total = 15

if onClient() == 1 then
current = 1
end

function interactionPossible(playerIndex)
    local relationLevel = Player():getRelations(getParentFaction().index)
local station = getParentEntity()

local dist = Player().craft:getNearestDistance(station)
if dist > 500 then
	return 0, "You need to be at maximum of 5km from station."
end

    local threshold = -5000
    if relationLevel < threshold then
        return 0, "This faction doesn't like you enough."
    end

    return 1
end

-- create all required UI elements for the client side
function initUI()
    local res = getResolution();
    local size = vec2(500, 300)

    local menu = Menu()
    window = menu:createWindow(Rect(res * 0.5 - size * 0.5, res * 0.5 + size * 0.5));
    menu:registerWindow(window, "Buy Turrets");

window.caption = "Turret Merchant"
    window.showCloseButton = 1
    window.moveable = 1

local fontSize = 14

window:createFrame(Rect(60, 10, 500-60, 300-10));
window:createButton(Rect(60+10, 300-10-10-50, 500-60-10, 300-10-10), "Buy", "onBuyTurretButtonPressed")
window:createButton(Rect(10, 100, 50, 200), "<", "onPrevButtonPressed")
window:createButton(Rect(500-50, 100, 500-10, 200), ">", "onNextButtonPressed")
label_title  = window:createLabel(vec2(70, 20),  "",  fontSize+6)
label_damage = window:createLabel(vec2(70, 50),  "",  fontSize)
label_rate   = window:createLabel(vec2(70, 70),  "",  fontSize)
label_range  = window:createLabel(vec2(70, 90),  "",  fontSize)
label_cost   = window:createLabel(vec2(70, 110),  "",  fontSize)

invokeServerFunction("getTurretInfo", Player().name, current)
end

function getTurretInfo(name, number)
local turret = turrets[number]

-- TITLE
local title = turret.weaponPrefix .. " Turret";
    -- add number predicate
    local prefix = "";
    if turret.numVisibleWeapons == 1 then
        prefix = ""
    elseif turret.numVisibleWeapons == 2 then
        prefix = "Double "
    elseif turret.numVisibleWeapons == 3 then
        prefix = "Triple "
    elseif turret.numVisibleWeapons == 4 then
        prefix = "Quad "
    else
        prefix = "Multi "
    end
    if turret.weaponType == WeaponType.MiningLaser then
        prefix = prefix .. turret.material.name .. " "
    end
    if prefix ~= "" then
        title = prefix .. title;
    end

-- RANGE
local range = "Range: " .. round(turret.reach * 10 / 1000, 2)

-- DAMAGE and RATE
local damage
local rate
if turret.weaponType == WeaponType.Laser then
	damage = "Damage/s: " .. round(turret.dps, 1)
	rate = ""
elseif turret.weaponType == WeaponType.MiningLaser then
	damage = "Damage/s: " .. round(turret.dps, 1)
	rate = "Efficiency: " .. round(turret.efficiency * 100) .. "%"
elseif turret.weaponType == WeaponType.ChainGun then
	damage = "Damage: " .. round(turret.damage, 1) .. "x" .. turret.shotsPerFiring
	rate = "Fire rate: " .. round(turret.fireRate, 1)
end

-- COST
local cost = "Cost: " .. getTurretCost(number)

local requester = Player(Galaxy():findFaction(name).index)
invokeClientFunction(requester, "setTurretInfo", title, damage, rate, range, cost)
end

function setTurretInfo(title, damage, rate, range, cost)
label_title.caption  = title
label_damage.caption = damage
label_rate.caption   = rate
label_range.caption  = range
label_cost.caption   = cost
end

function getTurretCost(number)
local cost = 100
local turret = turrets[number]
if turret.weaponType == WeaponType.Laser then
	cost = cost * turret.dps * round(turret.reach * 10 / 1000, 2) * 1.5
elseif turret.weaponType == WeaponType.MiningLaser then
	cost = cost * turret.efficiency * 10 * turret.dps
elseif turret.weaponType == WeaponType.ChainGun then
	cost = cost * turret.damage * turret.shotsPerFiring * turret.fireRate * 0.4 * round(turret.reach * 10 / 1000, 2) * 1.5
end
return round(cost, 0)
end

-- this function gets called on creation of the entity the script is attached to, on client and server
function initialize()
    local station = getParentEntity()

local x, y = Sector():getCoordinates()
local dps, tech = Balancing_GetSectorWeaponDPS(x, y)
if onServer() == 1 then
	turrets = {}
	for i=0, 4 do
		table.insert(turrets, InventoryTurret(GenerateTurretTemplate(random():createSeed(), WeaponType.ChainGun, dps, tech, Rarity(0), Material(MaterialType.Iron))))
	end
	for i=0, 4 do
		table.insert(turrets, InventoryTurret(GenerateTurretTemplate(random():createSeed(), WeaponType.Laser, dps, tech, Rarity(0), Material(MaterialType.Iron))))
	end
	for i=0, 4 do
		table.insert(turrets, InventoryTurret(GenerateTurretTemplate(random():createSeed(), WeaponType.MiningLaser, dps, tech, Rarity(0), Material(MaterialType.Iron))))
	end
end

    if station.title == "" then
        station.title = "Turret Merchant"
    end
end

function onBuyTurretButtonPressed()
invokeServerFunction("buyTurret", Player().name, current)
end

function onPrevButtonPressed()
current = current - 1
if current < 1 then
	current = total
end
invokeServerFunction("getTurretInfo", Player().name, current)
end

function onNextButtonPressed()
current = (current % total) + 1
invokeServerFunction("getTurretInfo", Player().name, current)
end

function buyTurret(name, number)
local buyer = Player(Galaxy():findFaction(name).index)
local station = getParentEntity()
local requiredMoney = getTurretCost(number)
local canPay, msg = buyer:canPay(requiredMoney)

    if canPay == 0 then
        Server():sendChatMessage(buyer, msg, station.title, 1);
        return
    end

    buyer:pay(requiredMoney)

buyer:getInventory():add(turrets[number])
end

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...