Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Script for asexual reproduction.  (Read 693 times)

bloop_bleep

  • Bay Watcher
    • View Profile
Script for asexual reproduction.
« on: October 21, 2017, 01:17:42 am »

Hey everyone,

I've been researching a lot of scripts recently and have finally lopped together Frankenstein-style a script that allows specified races to reproduce asexually. (Originally it was a script for a community fortress that allowed lawnmowers specifically to reproduce asexually, but I generalized it.) I was hoping all of you more experienced modders could take a look and see if I have done everything correctly. (I haven't tested it yet, unfortunately.)

Code: [Select]
local usage = [====[

asex_reproduce
==============
Allows asexual reproduction of specified races.

Arguments:

``-help``
    Prints this help message.
``-enable``
    Enables this script.
``-disable``
    Disables this script
``-start RACE -chance FLOAT -length NUMBER``
    Starts asexual reproduction for RACE. Every 50 ticks any non-pregnant members of RACE have a FLOAT chance of becoming pregnant. Pregnancies last for NUMBER ticks.
``-stop RACE``
    Stops asexual reproduction for RACE.
]===]


local utils = require 'utils'
local repeat_util = require 'repeat-util'

local IMPREGNATION_REPEAT = 50

local impregnation_table = {}
local rng = dfhack.random.new()

local function select_caste(race)
    local n = rng.random(99)
    local raws = df.global.world.raws.creatures.all[race]
    for i, chance in ipairs(raws.pop_ratio) do
        if n < chance then
            return i
        end
        n = n - chance
    end
end

--local function gen_random_genes(race, caste)
--end

local function impregnate_unit(unit, impregnation_data)
    unit.pregnancy_timer = impregnation_data.length
    unit.pregnancy_caste = select_caste(unit.race)
    unit.pregnancy_genes = unit.appearance.genes --gen_random_genes(unit.race, unit.pregnancy_caste)
end

local function impregnate_race(race_name, impregnation_data)
    for i, unit in ipairs(df.global.world.units.all) do
        if df.global.world.raws.creatures.all[unit.race_id] == race_name then
            if unit.pregnancy_timer == 0 then
                if generator.drandom() < impregnation_data.chance then
                    impregnate_unit(unit, impregnation_data)
                end
            end
        end
    end
end

local function impregnate_races()
    for race, data in pairs(impregnation_table) do
        impregnate_race(race, data)
    end
end



local function begin_scheduling()
    repeat_util.scheduleEvery('impregnate_races()', IMPREGNATION_REPEAT, 'ticks', impregnate_races)
end

local function stop_scheduling()
    repeat_util.cancel('impregnate_races()')
end

local enabled = true
begin_scheduling()

validArgs = validArgs or utils.invert({
    'help',
    'enable',
    'disable',
    'start',
    'chance',
    'length',
    'stop'
})

local args = utils.processArgs({...}, validArgs)

if args.help then
    print(usage)
    return
end

if args.enable and not enabled then
    enabled = true
    begin_scheduling()
    return
end

if args.disable and enabled then
    enabled = false
    end_scheduling()
    return
end

if args.start then
    impregnation_table[args.start] = { chance = tonumber(args.chance), length = tonumber(args.length) }
    return
end

if args.stop then
    impregnation_table:remove(args.stop)
    return
end

If you have any other suggestions/additions, let me know!

(Additional note: As you may see from the code, I was thinking of making the script randomly generate new genes, since currently it just copies the parent's genes, but I have no idea how to do that. Ideas?)
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.