Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

3.) MMO From Scratch - Plant

Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Previous (The AI)
Table of Contents (Introduction)
Next (The Client)

Plants in Surface Tension are stupid, and so are pretty easy to make. The requirements don't specify any growth stages for plants. We don't need to worry about light sources, or water. All we need to do with plants is have them develop seeds and drop seeds. A seed is a rough copy of the plant. We'll assume several breeds of plants will inherit the Plant class when coding the copy. So if a tree inherits the Plant class, when a tree drops a seed, the same species of tree will spawn from that seed.

So a Plant needs 2 feedback loops- DevelopSeed and DropSeed. If we don't require a development cycle (ie: force some amount of time required to create seeds), then plants will just copy themselves every cycle- and that results in a mess of plants very quickly.

In the last tutorial I didn't go into detail in how a feedback loop works. Every feedback loop is required to follow an abstract interface. A feedback loop must take the AI as the argument, and must have a cycle method. If inheriting the brain, which everything should (even if plants don't actually have brains), a feedback loop may use the memory and decision-making engine. In real life, plants don't have a memory to determine if it's time to develop a seed- plants have a finite amount of places seeds may grow, and if there's room for a seed, one develops. I decided to use memories, because this is a computer program and I found it easy enough to use memories for storing information.

So, I'll go into detail with the code in this one- unlike the last tutorial.

We've determined a plant will need to take some parameters, so we'll start the code like this.

Code:
[COLOR=#008000][B]var[/B][/COLOR] Brain [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./brain.js'[/COLOR]);
[COLOR=#008000][B]function[/B][/COLOR] Plant(params) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    self.brain [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Brain();
}

We now have a plant with a brain. If that's not strange enough, let's add a feedback loop to this plant. We should always store the feedback loops outside of the class they are for, as we'll be creating thousands of plants in this game, and they can all share the same functions for feedback loops. I like to put my requires at the top of the file, the feedback loops just under that, and the class that takes the name of the file (plant.js) after the feedback loops. Finally, I'll export that class after it is declared.

Code:
// requirements
// feedback loops
// class
// module.exports = class

The AI will instantiate a new instance of every feedback loop (which is inexpensive in JavaScript). Therefore, we'll capitalize the first letter of the name in that function.
Here's what the code should look like now:
Code:
[COLOR=#008000][B]var[/B][/COLOR] Brain [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./brain.js'[/COLOR]);
[COLOR=#008000][B]function[/B][/COLOR] DevelopSeed(ai) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {

    }
}
[COLOR=#008000][B]function[/B][/COLOR] Plant(params) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    self.brain [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Brain();
    self.brain.createFeedback(DevelopSeed);
}
module.exports [COLOR=#666666]=[/COLOR] Plant;
Right now that feedback loop isn't doing anything. Also, the params passed to the Plant aren't accessible to the feedback loop. We can add the params to the plant's memory- but remember there are 2 sides of the brain, so we need to decide if we want to assign the same memory twice, or sync those memories. Some herbs are required to drop a single seed, and since the brain creates 2 DevelopSeed feedbacks (left and right brain), we need to sync the memories so it's possible to develop a single seed. We should sync the memories before every cycle, and to do that we need to abstract the self.brain.cycle. Let's create a Plant.cycle method, too.

Code:
[COLOR=#008000][B]var[/B][/COLOR] Brain [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./brain.js'[/COLOR]);
[COLOR=#008000][B]function[/B][/COLOR] DevelopSeed(ai) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {

    }
}
[COLOR=#008000][B]function[/B][/COLOR] Plant(params) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] memories;
    [COLOR=#008000][B]function[/B][/COLOR][COLOR=#000000] synchronize() {
[/COLOR]        self.brain.left.sync([COLOR=#BA2121]'memories.maxSeeds'[/COLOR]);
       self.brain.left.sync([COLOR=#BA2121]'memories.seedTime'[/COLOR]);
    }
    self.brain [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Brain();
    memories [COLOR=#666666]=[/COLOR] self.brain.left.memories;
    memories.maxSeeds [COLOR=#666666]=[/COLOR] params.maxSeeds [COLOR=#666666]||[/COLOR] [COLOR=#666666]2[/COLOR];
    memories.seedTime [COLOR=#666666]=[/COLOR] params.seedTime [COLOR=#666666]||[/COLOR] [COLOR=#666666]60000[/COLOR];
    synchronize();
    self.brain.createFeedback(DevelopSeed);
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        synchronize();
        self.brain.cycle();
    }
}
module.exports [COLOR=#666666]=[/COLOR] Plant;

Now let's test this code:
Code:
[COLOR=#008000][B]var[/B][/COLOR] Plant [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./plant.js'[/COLOR]);
[COLOR=#008000][B]var[/B][/COLOR] p [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Plant({});
p.cycle();
console.log(p.brain.left.memories, p.brain.right.memories);

The result is this:
Code:
{
  maxSeeds: 2, 
  seedTime: 60000, 
  lastAction: 'do nothing'
} 
{
  maxSeeds: 2, 
  seedTime: 60000, 
  lastAction: 'do nothing' 
}

This is good, 2 identical memories. Note that between cycles these memories may become out of sync- and that's usually ideal. If desired, memories may be synchronized during a cycle, too- using ai.sync(). The way we determine if a param is undefined is faulty. If we supply 0 to max seeds, it will default to 2. That's not ideal. We should create a function to make params optional.

Code:
[COLOR=#008000][B]function[/B][/COLOR] Plant(params) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] memories;
    [COLOR=#008000][B]function[/B][/COLOR] optional(name, defaultValue) {
        [COLOR=#008000][B]return[/B][/COLOR] params[name] [COLOR=#666666]!==[/COLOR] [COLOR=#008000][B]void[/B][/COLOR] [COLOR=#666666]0[/COLOR] [COLOR=#666666]?[/COLOR] params[name] [COLOR=#666666]:[/COLOR] defaultValue;
    }
    [COLOR=#008000][B]function[/B][/COLOR] synchronize() {
        self.brain.left.sync([COLOR=#BA2121]'memories.maxSeeds'[/COLOR]);
        self.brain.left.sync([COLOR=#BA2121]'memories.seedTime'[/COLOR]);
    }
    self.brain [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Brain();
    memories [COLOR=#666666]=[/COLOR] self.brain.left.memories;
    memories.maxSeeds [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'maxSeeds'[/COLOR], [COLOR=#666666]2[/COLOR]);
    memories.seedTime [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'seedTime'[/COLOR], [COLOR=#666666]60000[/COLOR]);
    synchronize();
    self.brain.createFeedback(DevelopSeed);
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        synchronize();
        [COLOR=#008000][B]return[/B][/COLOR] self.brain.cycle();
    }
}
Now if we test, we can set maxSeeds to 0 and it will work as expected.

Now let's work on that DevelopSeed feedback loop, using the memories we have created. We'll create an endTime based on the current time and the seedTime, and we also need a memory of how many seeds a plant has already, so not to exceed the maxSeeds.

Code:
    ai.memories.seeds [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
    ai.memories.endTime [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now() [COLOR=#666666]+[/COLOR] ai.memories.seedTime;


We'll create a function to use as a decision to create a seed- this function should just do- it shouldn't have any conditional logic. We'll use conditional logic to determine whether to add a decision or not later.

Code:
    [COLOR=#008000][B]function[/B][/COLOR] createSeed() {
        ai.memories.seeds [COLOR=#666666]+=[/COLOR] [COLOR=#666666]1[/COLOR];
        ai.memories.endTime [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now() [COLOR=#666666]+[/COLOR] ai.memories.seedTime;
    }

Now in the cycle, we'll determine if we should add a decision, and set the weight for that decision based on how long the seed has been ready to develop.

Code:
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        [COLOR=#008000][B]var[/B][/COLOR] now [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now();
        [COLOR=#008000][B]var[/B][/COLOR] weight [COLOR=#666666]=[/COLOR] now [COLOR=#666666]-[/COLOR] ai.memories.endTime;
        [COLOR=#008000][B]if[/B][/COLOR] (weight [COLOR=#666666]>[/COLOR] [COLOR=#666666]0[/COLOR] [COLOR=#666666]&&[/COLOR]
            ai.memories.maxSeeds [COLOR=#666666]>[/COLOR] ai.memories.seeds
        ) {
            ai.addDecision({
                weight[COLOR=#666666]:[/COLOR] weight,
                action[COLOR=#666666]:[/COLOR] createSeed,
                title[COLOR=#666666]:[/COLOR] [COLOR=#BA2121]"create seed"[/COLOR]
            });
        }
    }
Altogether,the DevelopSeed feedback loop should look like this:
Code:
[COLOR=#008000][B]function[/B][/COLOR] DevelopSeed(ai) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    ai.memories.seeds [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
    ai.memories.endTime [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now() [COLOR=#666666]+[/COLOR] ai.memories.seedTime;
    [COLOR=#008000][B]function[/B][/COLOR] createSeed() {
        ai.memories.seeds [COLOR=#666666]+=[/COLOR] [COLOR=#666666]1[/COLOR];
        ai.memories.endTime [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now() [COLOR=#666666]+[/COLOR] ai.memories.seedTime;
    }
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        [COLOR=#008000][B]var[/B][/COLOR] now [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now();
        [COLOR=#008000][B]var[/B][/COLOR] weight [COLOR=#666666]=[/COLOR] now [COLOR=#666666]-[/COLOR] ai.memories.endTime;
        [COLOR=#008000][B]if[/B][/COLOR] (weight [COLOR=#666666]>[/COLOR] [COLOR=#666666]0[/COLOR] [COLOR=#666666]&&[/COLOR]
            ai.memories.maxSeeds [COLOR=#666666]>[/COLOR] ai.memories.seeds
        ) {
            ai.addDecision({
                weight[COLOR=#666666]:[/COLOR] weight,
                action[COLOR=#666666]:[/COLOR] createSeed,
                title[COLOR=#666666]:[/COLOR] [COLOR=#BA2121]"create seed"[/COLOR]
            });
        }
    }
}

Now, to test this, we have to run the cycles in an interval. I'm using a unix terminal to test this, so I'm clearing the console every cycle using process.stdout.write('\033c'); If you're using windows, you can use a bunch of empty lines, or some other trick to clear the console screen, instead.

Here is the test code:
Code:
[COLOR=#008000][B]var[/B][/COLOR] Plant [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./plant.js'[/COLOR]);
[COLOR=#008000][B]var[/B][/COLOR] p [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Plant({
    maxSeeds[COLOR=#666666]:[/COLOR] [COLOR=#666666]5[/COLOR], 
    seedTime[COLOR=#666666]:[/COLOR] [COLOR=#666666]5000[/COLOR]
});
[COLOR=#008000][B]var[/B][/COLOR] leftSeeds [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
[COLOR=#008000][B]var[/B][/COLOR] rightSeeds [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
[COLOR=#008000][B]var[/B][/COLOR] interval [COLOR=#666666]=[/COLOR] setInterval([COLOR=#008000][B]function[/B][/COLOR] () {
    process.stdout.write([COLOR=#BA2121]'\033c'[/COLOR]);
    [COLOR=#008000][B]var[/B][/COLOR] result [COLOR=#666666]=[/COLOR] p.cycle();
    [COLOR=#008000][B]if[/B][/COLOR] (p.brain.left.memories.lastAction [COLOR=#666666]===[/COLOR] [COLOR=#BA2121]'create seed'[/COLOR] [COLOR=#666666]&&[/COLOR] p.brain.left.decisions.length [COLOR=#666666]>[/COLOR] [COLOR=#666666]1[/COLOR]) {
        leftSeeds [COLOR=#666666]+=[/COLOR] [COLOR=#666666]1[/COLOR]
    }
    [COLOR=#008000][B]if[/B][/COLOR] (p.brain.right.memories.lastAction [COLOR=#666666]===[/COLOR] [COLOR=#BA2121]'create seed'[/COLOR] [COLOR=#666666]&&[/COLOR] p.brain.right.decisions.length [COLOR=#666666]>[/COLOR] [COLOR=#666666]1[/COLOR]) {
        rightSeeds [COLOR=#666666]+=[/COLOR] [COLOR=#666666]1[/COLOR];
    }
    console.log([COLOR=#BA2121]"Left Decisions:"[/COLOR], p.brain.left.decisions);
    console.log([COLOR=#BA2121]"Right Decisions:"[/COLOR], p.brain.right.decisions);
    console.log([COLOR=#BA2121]"Seeds create (left):"[/COLOR], leftSeeds);
    console.log([COLOR=#BA2121]"Seeds create (right):"[/COLOR], rightSeeds);
    console.log([COLOR=#BA2121]"Total seeds:"[/COLOR], leftSeeds [COLOR=#666666]+[/COLOR] rightSeeds);
}, [COLOR=#666666]1000[/COLOR]);
In this test, I'm interested in what decisions there are to choose from,how many seeds each side of the brain has, and how many seeds there are total. The seed time is 5 seconds in this test, and the maxSeeds is set to 5. When I run this code, it will give me 10 seeds in about 5 seconds. The way this works now makes it impossible for a plant to develop only a single seed. We'll change that in the DevelopSeed code by synchronizing the amount of seeds a plant has. I'm okay with the plants developing 2 seeds at a time, so I won't multiply the seedTime by 2. It really is taking 5 seconds to develop a seed, but plants are developing 2 seeds at once. It's also possible to use a single side of the brain for plants instead of both sides, but I like it this way. Later we may change the Plant code to allow n developments at a time- perhaps requiring a Fibonacci number to mimic real-world plants. But, that's the future.

So,the solution is 1 extra line in the createSeed function. Here is the entire plant.js file so far: (latest change in bold)
Code:
[COLOR=#008000][B]var[/B][/COLOR] Brain [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./brain.js'[/COLOR]);
[COLOR=#008000][B]function[/B][/COLOR] DevelopSeed(ai) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    ai.memories.seeds [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
    ai.memories.endTime [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now() [COLOR=#666666]+[/COLOR] ai.memories.seedTime;
    [COLOR=#008000][B]function[/B][/COLOR] createSeed() {
        ai.memories.seeds [COLOR=#666666]+=[/COLOR] [COLOR=#666666]1[/COLOR];
        ai.memories.endTime [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now() [COLOR=#666666]+[/COLOR] ai.memories.seedTime;
        [B]ai.sync([COLOR=#BA2121]'memories.seeds'[/COLOR]);[/B]
    }
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        [COLOR=#008000][B]var[/B][/COLOR] now [COLOR=#666666]=[/COLOR] [COLOR=#008000]Date[/COLOR].now();
        [COLOR=#008000][B]var[/B][/COLOR] weight [COLOR=#666666]=[/COLOR] now [COLOR=#666666]-[/COLOR] ai.memories.endTime;
        [COLOR=#008000][B]if[/B][/COLOR] (weight [COLOR=#666666]>[/COLOR] [COLOR=#666666]0[/COLOR] [COLOR=#666666]&&[/COLOR]
            ai.memories.maxSeeds [COLOR=#666666]>[/COLOR] ai.memories.seeds
        ) {
            ai.addDecision({
                weight[COLOR=#666666]:[/COLOR] weight,
                action[COLOR=#666666]:[/COLOR] createSeed,
                title[COLOR=#666666]:[/COLOR] [COLOR=#BA2121]"create seed"[/COLOR]
            });
        }
    }
}
[COLOR=#008000][B]function[/B][/COLOR] Plant(params) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] memories;
    [COLOR=#008000][B]function[/B][/COLOR] optional(name, defaultValue) {
        [COLOR=#008000][B]return[/B][/COLOR] params[name] [COLOR=#666666]!==[/COLOR] [COLOR=#008000][B]void[/B][/COLOR] [COLOR=#666666]0[/COLOR] [COLOR=#666666]?[/COLOR] params[name] [COLOR=#666666]:[/COLOR] defaultValue;
    }
    [COLOR=#008000][B]function[/B][/COLOR] synchronize() {
        self.brain.left.sync([COLOR=#BA2121]'memories.maxSeeds'[/COLOR]);
        self.brain.left.sync([COLOR=#BA2121]'memories.seedTime'[/COLOR]);
    }
    self.brain [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Brain();
    memories [COLOR=#666666]=[/COLOR] self.brain.left.memories;
    memories.maxSeeds [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'maxSeeds'[/COLOR], [COLOR=#666666]2[/COLOR]);
    memories.seedTime [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'seedTime'[/COLOR], [COLOR=#666666]60000[/COLOR]);
    synchronize();
    self.brain.createFeedback(DevelopSeed);
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        synchronize();
        [COLOR=#008000][B]return[/B][/COLOR] self.brain.cycle();
    }
}
module.exports [COLOR=#666666]=[/COLOR] Plant;

It's great that plants can develop seeds, but to reproduce they must drop seeds onto the world that grow into copies of themselves. To keep it simple, I'll just have the plants drop copies of themselves, rather than seeds. Players may collect seeds by destroying plants that contain seeds. But the plants will consume seeds when they reproduce, and simply drop a plant somewhere in the world.

Now comes a problem. Where is the plant in this world? Right now the plant has no idea where it is, or what size it is. So we need to add 3 parameters. The place, size, and a function for creating offspring. These 3 params should be synchronized memories. We should also create a feedback loop for DropSeed.

Here's the updated Plant code:
Code:
[COLOR=#008000][B]function[/B][/COLOR] Plant(params) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] memories;
    [COLOR=#008000][B]function[/B][/COLOR] optional(name, defaultValue) {
        [COLOR=#008000][B]return[/B][/COLOR] params[name] [COLOR=#666666]!==[/COLOR] [COLOR=#008000][B]void[/B][/COLOR] [COLOR=#666666]0[/COLOR] [COLOR=#666666]?[/COLOR] params[name] [COLOR=#666666]:[/COLOR] defaultValue;
    }
    [COLOR=#008000][B]function[/B][/COLOR] synchronize() {
        self.brain.left.sync([COLOR=#BA2121]'memories.maxSeeds'[/COLOR]);
        self.brain.left.sync([COLOR=#BA2121]'memories.seedTime'[/COLOR]);
        self.brain.left.sync([COLOR=#BA2121]'memories.place'[/COLOR]);
        self.brain.left.sync([COLOR=#BA2121]'memories.size'[/COLOR]);
        self.brain.left.sync([COLOR=#BA2121]'memories.offspring'[/COLOR]);
    }
    [COLOR=#008000][B]function[/B][/COLOR] makeChild (x, y) {
        [COLOR=#008000][B]var[/B][/COLOR] childParams [COLOR=#666666]=[/COLOR] [COLOR=#008000]Object[/COLOR][COLOR=#000000].create(params);[/COLOR]
        childParams.place [COLOR=#666666]=[/COLOR] [x, y];
        [COLOR=#008000][B]return[/B][/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Plant(childParams);
    }
    self.brain [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]new[/B][/COLOR] Brain();
    memories [COLOR=#666666]=[/COLOR] self.brain.left.memories;
    memories.maxSeeds [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'maxSeeds'[/COLOR], [COLOR=#666666]2[/COLOR]);
    memories.seedTime [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'seedTime'[/COLOR], [COLOR=#666666]60000[/COLOR]);
    memories.place [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'place'[/COLOR], [[COLOR=#666666]0[/COLOR], [COLOR=#666666]0[/COLOR]]);
    memories.size [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'size'[/COLOR], [[COLOR=#666666]25[/COLOR], [COLOR=#666666]25[/COLOR]]);
    memories.offspring [COLOR=#666666]=[/COLOR] optional([COLOR=#BA2121]'offspring'[/COLOR], makeChild);
    synchronize();
    self.brain.createFeedback(DevelopSeed);
[COLOR=#000000]    self.brain.createFeedback(DropSeed);[/COLOR]
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        synchronize();
        [COLOR=#008000][B]return[/B][/COLOR] self.brain.cycle();
    }
}
The offspring function may be overwritten by things that inherit Plant. That's useful if we don't necessarily want the offspring to be exact copies. Perhaps we want the offspring to be a bit different or allow for mutations? Anyway, for now we'll just use exact copies. Everything in the copied plant is the same except for the place. It would be silly to reproduce in the same exact place in the world, we want instead for the offspring to be somewhere next to it's parent. Before we get in too far over our heads, let's write some code for the DropSeed feedback loop.
Code:
[COLOR=#008000][B]function[/B][/COLOR] DropSeed(ai) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] weight [COLOR=#666666]=[/COLOR] [COLOR=#666666]5[/COLOR];
    [COLOR=#008000][B]function[/B][/COLOR] drop () {
        [COLOR=#008000][B]var[/B][/COLOR] x [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
        [COLOR=#008000][B]var[/B][/COLOR] y [COLOR=#666666]=[/COLOR] [COLOR=#666666]0[/COLOR];
        ai.memories.seeds [COLOR=#666666]-=[/COLOR] [COLOR=#666666]1[/COLOR];
        ai.sync([COLOR=#BA2121]'memories.seeds'[/COLOR]);
        [COLOR=#408080][I]// tODO: Determine x, y for child[/I][/COLOR]
        [COLOR=#008000][B]return[/B][/COLOR] ai.memories.offspring(x, y)
    }
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        [COLOR=#008000][B]if[/B][/COLOR] (ai.memories.seeds [COLOR=#666666]>[/COLOR] [COLOR=#666666]0[/COLOR]) {
            ai.addDecision({
                weight[COLOR=#666666]:[/COLOR] weight,
                action[COLOR=#666666]:[/COLOR] drop,
                title[COLOR=#666666]:[/COLOR] [COLOR=#BA2121]"drop seed"[/COLOR]
            });
        }
    }
}
I mentioned before that when we drop a seed, we need to sync the seeds memory. I did that now before I forget to later. I also added a TODO to let me know I need to set x and y to something very different than 0, 0. So, we have some data to help us place a seed next to this plant. We have the place, and the size. But we should place the seed in a random direction- I'll choose one of eight different possibilities [n, nw, w, sw, s, se, e, ne].

For simplicity, I'll just create an array with 8 pre-determined placements, then in the drop function I'll get a random number between 0-7, and use that number to pick the place out of the array.
Code:
[COLOR=#008000][B]function[/B][/COLOR] DropSeed(ai) {
    [COLOR=#008000][B]var[/B][/COLOR] self [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]this[/B][/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] weight [COLOR=#666666]=[/COLOR] [COLOR=#666666]5[/COLOR];
    [COLOR=#008000][B]var[/B][/COLOR] place [COLOR=#666666]=[/COLOR] ai.memories.place;
    [COLOR=#008000][B]var[/B][/COLOR] size [COLOR=#666666]=[/COLOR] ai.memories.size;
    [COLOR=#008000][B]var[/B][/COLOR] seedPlaces [COLOR=#666666]=[/COLOR] [
        [place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]-[/COLOR] size[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]] [COLOR=#666666]-[/COLOR] size[[COLOR=#666666]1[/COLOR]]],
        [place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]-[/COLOR] size[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]]],
        [place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]-[/COLOR] size[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]] [COLOR=#666666]+[/COLOR] size[[COLOR=#666666]1[/COLOR]]],
        [place[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]] [COLOR=#666666]-[/COLOR] size[[COLOR=#666666]1[/COLOR]]],
        [COLOR=#408080][I]//[place[0], place[1]], // Should not put child at same place as parent[/I][/COLOR]
        [place[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]] [COLOR=#666666]+[/COLOR] size[[COLOR=#666666]1[/COLOR]]],
        [place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]+[/COLOR] size[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]] [COLOR=#666666]-[/COLOR] size[[COLOR=#666666]1[/COLOR]]],
        [place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]+[/COLOR] size[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]]],
        [place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]+[/COLOR] size[[COLOR=#666666]0[/COLOR]], place[[COLOR=#666666]1[/COLOR]] [COLOR=#666666]+[/COLOR] size[[COLOR=#666666]1[/COLOR]]]
    ];
    [COLOR=#008000][B]function[/B][/COLOR] drop () {
        [COLOR=#008000][B]var[/B][/COLOR] randPlace [COLOR=#666666]=[/COLOR] seedPlaces[[COLOR=#008000]Math[/COLOR].floor([COLOR=#008000]Math[/COLOR].random() [COLOR=#666666]*[/COLOR] [COLOR=#666666]8[/COLOR])];
        ai.memories.seeds [COLOR=#666666]-=[/COLOR] [COLOR=#666666]1[/COLOR];
        ai.sync([COLOR=#BA2121]'memories.seeds'[/COLOR]);
        [COLOR=#008000][B]return[/B][/COLOR] ai.memories.offspring(randPlace[[COLOR=#666666]0[/COLOR]], randPlace[[COLOR=#666666]1[/COLOR]]);
    }
    self.cycle [COLOR=#666666]=[/COLOR] [COLOR=#008000][B]function[/B][/COLOR] () {
        [COLOR=#008000][B]if[/B][/COLOR] (ai.memories.seeds [COLOR=#666666]>[/COLOR] [COLOR=#666666]0[/COLOR]) {
            ai.addDecision({
                weight[COLOR=#666666]:[/COLOR] weight,
                action[COLOR=#666666]:[/COLOR] drop,
                title[COLOR=#666666]:[/COLOR] [COLOR=#BA2121]"drop seed"[/COLOR]
            });
        }
    }
}

Now that the DropSeed feedback loop is complete, let's test.
Code:
[COLOR=#008000][B]var[/B][/COLOR] Plant [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./plant.js'[/COLOR]);
[COLOR=#008000][B]var[/B][/COLOR] plants [COLOR=#666666]=[/COLOR] [[COLOR=#008000][B]new[/B][/COLOR] Plant({
    maxSeeds[COLOR=#666666]:[/COLOR] [COLOR=#666666]5[/COLOR], 
    seedTime[COLOR=#666666]:[/COLOR] [COLOR=#666666]5000[/COLOR]
})];
[COLOR=#008000][B]var[/B][/COLOR] interval [COLOR=#666666]=[/COLOR] setInterval([COLOR=#008000][B]function[/B][/COLOR] () {
    process.stdout.write([COLOR=#BA2121]'\033c'[/COLOR]);
    plants.forEach([COLOR=#008000][B]function[/B][/COLOR] (plant) {
        [COLOR=#008000][B]var[/B][/COLOR] child [COLOR=#666666]=[/COLOR] plant.cycle();
        [COLOR=#008000][B]if[/B][/COLOR] (child [COLOR=#008000][B]instanceof[/B][/COLOR] Plant) {
            plants.push(child);
        }
        console.log(plant.brain.left.memories.place);
    });
    console.log([COLOR=#BA2121]"Total plants:"[/COLOR], plants.length);
}, [COLOR=#666666]1000[/COLOR]);

If we run this code, we get a whole bunch of plants rather quickly. Much of the plants are spawned in places where other plants already exist! But how to fix that?

Well, we can either give the plants more knowledge of the world around them, or we can simply let the plant think it's reproducing, but prevent that from happening in the test code. I'm going to choose the later this time. So, I'll create another array containing serialized places where plants exist, and if the child plant is at a place that already exists, then it won't be added to the world.

Here is the updated test, changes in bold:
Code:
[COLOR=#008000][B]var[/B][/COLOR] Plant [COLOR=#666666]=[/COLOR] require([COLOR=#BA2121]'./plant.js'[/COLOR]);
[COLOR=#008000][B]var[/B][/COLOR] plants [COLOR=#666666]=[/COLOR] [[COLOR=#008000][B]new[/B][/COLOR] Plant({
    maxSeeds[COLOR=#666666]:[/COLOR] [COLOR=#666666]5[/COLOR], 
    seedTime[COLOR=#666666]:[/COLOR] [COLOR=#666666]5000[/COLOR],
    [B]place[COLOR=#666666]:[/COLOR] [[COLOR=#666666]500[/COLOR], [COLOR=#666666]500[/COLOR]],
    size[COLOR=#666666]:[/COLOR] [[COLOR=#666666]25[/COLOR], [COLOR=#666666]25[/COLOR]][/B]
})];
[B][COLOR=#008000][B]var[/B][/COLOR] plantPlaces [COLOR=#666666]=[/COLOR] [[COLOR=#BA2121]'500,500'[/COLOR]];[/B]
[B][COLOR=#008000]function[/COLOR] serializePlace(plant) {
    [COLOR=#008000]return[/COLOR] plant.brain.left.memories.place[[COLOR=#666666]0[/COLOR]] [COLOR=#666666]+[/COLOR] [COLOR=#BA2121]','[/COLOR] [COLOR=#666666]+[/COLOR] plant.brain.left.memories.place[[COLOR=#666666]1[/COLOR]];
}[/B]
[COLOR=#008000][B]var[/B][/COLOR] interval [COLOR=#666666]=[/COLOR] setInterval([COLOR=#008000][B]function[/B][/COLOR] () {
    process.stdout.write([COLOR=#BA2121]'\033c'[/COLOR]);
    plants.forEach([COLOR=#008000][B]function[/B][/COLOR] (plant) {
        [COLOR=#008000][B]var[/B][/COLOR] child [COLOR=#666666]=[/COLOR] plant.cycle();
        [COLOR=#008000][B]if[/B][/COLOR] (child [COLOR=#008000][B]instanceof[/B][/COLOR] Plant [B][COLOR=#666666]&&[/COLOR] 
            (plantPlaces.indexOf(serializePlace(child)) [COLOR=#666666]===[/COLOR] [COLOR=#666666]-1[/COLOR])[/B]
        ) {
            plants.push(child);
            [B]plantPlaces.push(serializePlace(child));[/B]
        }
        console.log(plant.brain.left.memories.place);
    });
    console.log([COLOR=#BA2121]"Total plants:"[/COLOR], plants.length);
}, [COLOR=#666666]1000[/COLOR]);

Now when we test, we get a much more manageable outcome. But, since this test made the seedTime only 5 seconds, we still get a lot of plants after running this for only a few minutes...
Code:
[ 500, 500 ]
[ 500, 475 ]
[ 475, 500 ]
[ 500, 525 ]
[ 525, 525 ]
[ 525, 475 ]
[ 525, 450 ]
[ 450, 500 ]
[ 475, 475 ]
....
[ 1025, 425 ]
[ 1050, 500 ]
[ 800, 25 ]
[ 875, 25 ]
[ 1025, 600 ]
[ 275, 900 ]
[ 1025, 650 ]
[ 450, 975 ]
Total plants: 1275
The good news, is that none of the plants are in the same place, and we didn't have to loop to find that out.

In later tutorials, we'll start saving plants to the database, and sending those plants to the client. But wait! We haven't even started on the client yet!

Making games is hard- but I'll cover every bit of my journey in this tutorial. It will take a long time, so only the most committed will follow the entire thing.

Previous (The AI)
Table of Contents (Introduction)
Next (The Client)
 
Last edited:
Back
Top