Missions and NPC Interaction Code Example

From PioneerWiki
Jump to: navigation, search
local Event = require "Event"
local Character = require 'Character'

local Rand = require 'Rand'
local rand = Rand.New()

local person = nil

local generatecitizen = function(citizen)
    citizen = citizen or 1 --default
    print("Generating person...")
    if citizen == 1 then
        person = Character.New()
    elseif citizen == 2 then
        person = Character.New()
        person:RollNew()
    elseif citizen == 3 then
        person = Character.New()
        person:RollNew(true)
    elseif citizen == 4 then
        --person = Character.New()
        local AceRoll = function ()
            -- 32 + 2xD16, range is 34..64 averaging 49
            return ( 32 + rand:Integer(1,16) + rand:Integer(1,16) )
        end
        person = Character.New({
            luck = AceRoll(),
            intelligence = AceRoll(),
            charisma = AceRoll(),
            notoriety = AceRoll(),
            lawfulness = AceRoll(),
            engineering = AceRoll(),
            piloting = AceRoll(),
            navigation = AceRoll(),
            sensors = AceRoll()})
    else
        print("generatecitizen called with wrong argument: " .. citizen)
    end
        person:Save()
end

local onChat = function (form, ref, option)
    form:Clear()

    if option == -1 then
        form:Close()
        return
    elseif option == 1 then
        generatecitizen(1)
    elseif option == 2 then
        generatecitizen(2)
    elseif option == 3 then
        generatecitizen(3)
    elseif option == 4 then
        generatecitizen(4)
    end
	
    form:SetTitle("Populate the planet!")
    form:SetFace(person)
    form:SetMessage("Hi! I'm " .. person.name .. " and I'm newly generated. As you may well know there isn't enough of us here. Please push some buttons to generate more citizens. We believe in you!")

    form:AddOption("Average", 1)
    form:AddOption("Random", 2)
    form:AddOption("Crew", 3)
    form:AddOption("Ace", 4)
end

local onCreateBB = function (station)
    generatecitizen()
    station:AddAdvert('Population issue',onChat)
end

onGameEnd = function ()
    for person in Character.Find()
    do
        person:PrintStats()
        print("Available: " .. tostring(person.available) .. "\n")
        -- If you instead want to see all elements of the generated characters, comment
        -- out the print statements above and uncomment the two lines below.
        --for key,value in pairs(person) do print(key,value) end
        --print("")
    end
end

Event.Register("onGameEnd", onGameEnd)
Event.Register("onCreateBB", onCreateBB)