Author Topic: Genesis Trees  (Read 2629 times)

Offline Nilrin

  • Friend
  • **
  • Posts: 86
    • View Profile
Genesis Trees
« on: August 06, 2009, 12:20:59 pm »

Basically a tree that grows and spawns new seeds, following certain 'environmental' rules.

Written in DS2.9a13 but should work with most DS libs.

generic_seed.c

Code: [Select]
// Generic Genesis Seed
// By Nilrin from Rebirth
// Created on 8/5/2009

#include <lib.h>
#include <vendor_types.h>
inherit LIB_ITEM;

int age = 0;
int nutrients = 1;
int starvation = 0;  //Older trees can starve longer without dying.
int is_dead = 0;
int time_to_seed = 0;

//This is only called when a tree is starving, to try and help keep
//it alive. Be sure that the nutrients that are subtracted here is
//the same number as are added in the branch file.

//Branches allow for three things. They can be picked up and moved
//unlike trees, which allows for people to 'fertalize' different
//areas. They also add to realism, but most importantly, if we didn't
//have these or another similar feature, seedlings would too easily
//starve out the big ole trees.

eventBranch() {

    if (!random(3)) {
        nutrients -= 200;
        clone_object("/domains/new/obj/generic_branch.c")->eventMove(environment());
        return;
    }

}

//Chance for a tree to drop one or more seeds. Depending on how many
//exits there are, there's more of a chance to drop a seed in the same
//room than in a adjacent one.

eventSeed() {

    time_to_seed += 1;

    if (time_to_seed > 20 && starvation <= 0) {
        time_to_seed = 0;
        if (!random(4)) {
            clone_object("/domains/new/obj/generic_seed.c")->eventMove(environment());
        }
        foreach(string exit in (string *)environment()->GetExits()){
            if (!random(20)) {
                clone_object("/domains/new/obj/generic_seed.c")->eventMove(environment()->GetExit(exit));
                nutrients -= 1;
            }
        }
    }
}

//General aging of the tree and consumption of nutrients. This should
//be changed ultimately so that short and long descriptions are not set
//at every age mark for optimization purposes.

eventAge() {

    if (age >= 0 && age <= 9) {
        if (environment()->GetNutrients() >= 1) {
            environment()->SubtractNutrients(0);
            SetShort("a generic seed");
            SetLong("This is a generic seed.");
            age += 1;
            return;
        }
    }

    if (age >= 10 && age <= 49) {
        if (environment()->GetNutrients() >= 1) {
            environment()->SubtractNutrients(1);
            nutrients += 1;
            SetShort("a generic seedling");
            SetLong("This is a generic seedling.");
            age += 1;
            return;
        }

        else {
            SetShort("a generic dead seedling");
            SetLong("This is a generic dead seedling");
            is_dead = 1;
        }
    }

    if (age >= 50 && age <= 99) {
        if (environment()->GetNutrients() >= 5) {
            environment()->SubtractNutrients(5);
            nutrients += 5;
            SetShort("a generic sapling");
            SetLong("This is a generic sapling.");
            age += 1;
            if (starvation > 0) {
                starvation -= 1;
            }
            return;
        }

        else {
            starvation += 1;
            if (starvation > 5) {
                SetShort("a generic dead sapling");
                SetLong("This is a generic dead sapling.");
                is_dead = 1;
            }
        }
    }

    if (age >= 100 && age <= 999) {
        if (environment()->GetNutrients() >= 10) {
            environment()->SubtractNutrients(10);
            nutrients += 10;
            SetShort("a generic tree");
            SetLong("This is a generic tree.");
            eventSeed();
            age += 1;
            if (starvation > 0) {
                starvation -= 1;
                eventBranch();
            }
            return;
        }

        else {
            starvation += 1;
            if (starvation > 10) {
                SetShort("a generic fallen tree");
                SetLong("This is a generic fallen tree.");
                is_dead = 1;
            }
        }
    }

    if (age >= 1000 && age <= 9999) {
        if (environment()->GetNutrients() >= 5) {
            environment()->SubtractNutrients(5);
            nutrients += 5;
            SetShort("a generic old tree");
            SetLong("This is a generic old tree.");
            eventSeed();
            age += 1;
            if (starvation > 0) {
                starvation -= 1;
                eventBranch();
            }
            return;
        }

        else {
            starvation += 1;
            if (starvation > 50) {
                SetShort("a generic old fallen tree");
                SetLong("This is a generic old fallen tree.");
                is_dead = 1;
            }
        }
    }

    if (age >= 10000) {
        if (environment()->GetNutrients() >= 2) {
            environment()->SubtractNutrients(2);
            nutrients += 2;
            SetShort("a generic ancient tree");
            SetLong("This is a generic ancient tree.");
            eventSeed();
            age += 1;
            if (starvation > 0) {
                starvation -= 1;
                eventBranch();
            }
            return;
        }

        else {
            starvation += 1;
            if (starvation > 100) {
                SetShort("a generic ancient fallen tree");
                SetLong("This is a generic ancient fallen tree.");
                is_dead = 1;
            }
        }
    }
}

//Decay of already dead trees.

eventDecay() {

    if (nutrients >= 50) {
        environment()->AddNutrients(20);
        nutrients -= 20;
        return;
    }
    if (nutrients < 50) {
        environment()->AddNutrients(nutrients);
        nutrients = 0;
        destruct(this_object());
    }

    else {
        destruct(this_object());
    }
}

void create(){

    int a;   

    ::create();
    SetKeyName("seed");
    SetId( ({"tree", "seed", "sapling", "seedling"}) );
    SetAdjectives( ({"generic","sample","template"}) );
    SetShort("a generic seed");
    SetLong("This is a generic seed.");
    SetMass(20);
    SetBaseCost("silver",10);
    SetVendorType(VT_TREASURE);

//Change to whatever. A heart beat of 1 is good for testing, but in
//very large areas gets out of hand and will strangle your cpu.

    a = random(30) + 60;
    set_heart_beat(a);
}

mixed CanGet(object ob) {

    if (age >= 10) {
        return 0;
    }

    else {
        return 1;
    }
}

void init(){
    ::init();
}

heart_beat(){

    if (is_dead == 1) {
        eventDecay();
    }

    else {
        eventAge();
    }
}

generic_branch.c

Code: [Select]
//Branch to be used with Genesis seed.
// By Nilrin from Rebirth
// Created on 8/5/2009

#include <lib.h>
#include <vendor_types.h>
inherit LIB_ITEM;

int nutrients = 200;

eventDecay() {

    if (nutrients >= 10) {
        environment()->AddNutrients(10);
        nutrients -= 10;
        return;
    }

    if (nutrients < 10) {
        environment()->AddNutrients(nutrients);
        nutrients = 0;
        destruct(this_object());
    }
}
void create(){
    ::create();
    SetKeyName("branch");
    SetId( ({"thing","item","thang","dingus"}) );
    SetAdjectives( ({"generic","sample","template"}) );
    SetShort("a generic branch");
    SetLong("This is a generic branch.");
    SetMass(20);
    SetBaseCost("silver",10);
    SetVendorType(VT_TREASURE);
    set_heart_beat(1);
}
void init(){
    ::init();
}

heart_beat() {
    eventDecay();
}

These query functions plus the nutrients variable need to be in any rooms you want trees to grow in.

Code: [Select]
int nutrients = 10000;

SetNutrients(int a) {
    nutrients = a;
}

GetNutrients() {
    return nutrients;
}

SubtractNutrients(int a) {
    nutrients = nutrients - a;
}

AddNutrients(int a) {
    nutrients = nutrients + a;
}

Not sure this has any practical purpose. Having 20,000+ objects with heartbeats in my virtual room wilderness probably isn't the best way to go about things. Surely this could be used small scale in things like flower beds, vegetable gardens, etc...

By the way, this is, by no means, optimized code. Use at your own risk. ;)