Agruement transfer limits?



  • Hi,

    I'm having some issue with transferring arguments through modules. I have done some C programming in the past, but never JavaScript, so I maybe doing some simple mistake.

     

    In main I call the role.builder module

     

    var breedCreep = require('breed.creep');

    var roleMiner = require('role.miner');
    var roleRunner = require('role.runner');
    var roleUpgrader = require('role.upgrader');
    var roleBuilder = require('role.builder');
    var roleRepairer = require('role.repairer');

    module.exports.loop = function ()
    {
        
        //Clear dead creeps from memory
        for(var name in Memory.creeps)
           {
                if(!Game.creeps[name])
                {
                    delete Memory.creeps[name];
                    console.log('Clearing non-existing creep memory:', name);
                }
            }
        for(var name in Memory.structures)
        {
            if(!Game.structures[name])
            {
                delete Memory.structures[name];
                console.log('Clearing non-existing stucture memory: ', name);
            }
        }
        
        //Run creep roles.
        for(var name in Game.creeps)
            {
                var creep = Game.creeps[name];
                if(creep.memory.role == 'miner')
                {
                    roleMiner.run(creep);
                }
                if(creep.memory.role == 'runner')
                {
                    roleRunner.run(creep);
                }
                if(creep.memory.role == 'upgrader')
                {
                    roleUpgrader.run(creep);
                }
                if(creep.memory.role == 'builder')
                {
                    roleBuilder.run(creep);
                }
            }
        
        breedCreep.run();
        roleRepairer.run();
        
    }

     

    Then in the role.builder module I call role.repairer and pass the creep argument.

     

    var roleRepairer = require('role.repairer');

    var roleBuilder = {

        /** @param {Creep} creep **/
        run: function(creep)
        {

            if(creep.memory.building && creep.carry.energy == 0)
            {
                creep.memory.building = false;
            }
            if(!creep.memory.building && creep.carry.energy == creep.carryCapacity)
            {
                creep.memory.building = true;
            }

            if(creep.memory.building)
            {
                var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
                if(targets.length)
                {
                    if(creep.build(targets[0]) == ERR_NOT_IN_RANGE)
                    {
                        creep.moveTo(targets[0]);
                    }
                }
                else
                {
                    roleRepairer.run(creep);
                }
            }
            else
            {
                var containers = creep.room.find(FIND_STRUCTURES,
                {
                    filter: (structure) =>
                    {
                        return(structure.structureType == STRUCTURE_CONTAINER) &&
                            structure.store.energy > 0;
                    }
                });
                
                 if(containers.length > 0)
                {
                    if(containers[0].store.energy > 0)
                    {
                        if(containers[0].transfer(creep, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
                        {
                            creep.moveTo(containers[0]);
                        }
                    }
                }
                else
                {
                    var sources = creep.room.find(FIND_SOURCES);
                    if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE)
                    {
                        creep.moveTo(sources[0]);
                    }
                }
            }
        }
    };

    module.exports = roleBuilder;

     

    No issues with the role.builder. Here is role.repairer

     

    var roleRepairer = {

        run: function(creep)
        {

            var roadDamageThreshold = 3000;
            var containerDamageThreshold = 200000;
            var rampartDamageThreshold = 200000;
            
            var roadsToFix = 0;
            var containersToFix = 0;
            var rampartsToFix;
            var wallsToFix = 0;

            var damagedStructures = Game.rooms.E3S31.find(FIND_STRUCTURES,
            {
                filter: (structure) =>
                {
                    return(structure.structureType == STRUCTURE_ROAD ||
                        structure.structureType == STRUCTURE_CONTAINER ||
                        structure.structureType == STRUCTURE_RAMPART ||
                        structure.structureType == STRUCTURE_WALL) &&
                        structure.hits < structure.hitsMax;
                }
            });
            
            for(i = 0; i < damagedStructures.length; i++)
            {
                switch(damagedStructures[i].structureType)
                {
                    case STRUCTURE_ROAD:
                        if(damagedStructures[i].hits < roadDamageThreshold)
                        {
                            roadsToFix += damagedStructures[i];
                        }
                        break;
                        
                    case STRUCTURE_CONTAINER:
                        if(damagedStructures[i].hits < containerDamageThreshold)
                        {
                            containersToFix += damagedStructures[i];
                        }
                        break;
                    
                    case STRUCTURE_RAMPART:
                        if(damagedStructures[i].hits < rampartDamageThreshold)
                        {
                            rampartsToFix = damagedStructures[i];
                        }
                        break;
                        
                    case STRUCTURE_WALL:
                        break;
                }
            }
            console.log(creep.name + ': task is ' + creep.memory.task);
            if(creep.memory.task == 'unassigned')
            {
                if(roadsToFix > 0)
                {
                    creep.memory.task = 'roads';
                    if(creep.repair(roadsToFix[0]) == ERR_NOT_IN_RANGE)
                    {
                        creep.moveTo(roadsToFix[0]);
                    }
                }
                else if (containersToFix > 0)
                {
                    creep.memory.task = 'containers';
                    if(creep.repair(containersToFix[0]) == ERR_NOT_IN_RANGE)
                    {
                        creep.moveTo(containersToFix[0]);
                    }
                }
                else if (rampartsToFix > 0)
                {
                    console.log('Fixing?');
                    creep.memory.task = 'ramparts';
                    if(creep.repair(rampartsToFix[0]) == ERR_NOT_IN_RANGE)
                    {
                        creep.moveTo(rampartsToFix[0]);
                    }
                }
            }
            else
            {
                if(task == 'roads')
                {
                    if(roadsToFix > 0)
                    {
                        if(creep.repair(roadsToFix[0]) == ERR_NOT_IN_RANGE)
                        {
                            creep.moveTo(roadsToFix[0]);
                        }
                    }
                    else
                    {
                        creep.memory.task = 'unassigned';
                    }
                }
                else if(task == 'containers')
                {
                    if(containersToFix > 0)
                    {
                        if(creep.repair(containersToFix[0]) == ERR_NOT_IN_RANGE)
                        {
                            creep.moveTo(containersToFix[0]);
                        }
                    }
                    else
                    {
                        creep.memory.task = 'unassigned';
                    }
                }
                else if (task == 'ramparts')
                {
                    if(rampartsToFix > 0)
                    {
                        if(creep.repair(rampartsToFix[0]) == ERR_NOT_IN_RANGE)
                        {
                            creep.moveTo(rampartsToFix[0]);
                        }
                    }
                    else
                    {
                        creep.memory.task = 'unassigned';
                    }
                }
                else if (task == 'walls')
                {
                    
                }
            }
            if(roadsToFix > 0)
            {
                
            }
        }
    };

    module.exports = roleRepairer;

     

    This is where the problem seems to be. The creep argument causes errors.

     

    [11:05:52 PM]
    Builder32: task is unassigned
    [11:05:52 PM]
    Builder33: task is unassigned
    [11:05:52 PM]
    role.repairer:56
    console.log(creep.name + ': task is ' + creep.memory.task);
    ^
    TypeError: Cannot read property 'name' of undefined
    at Object.roleRepairer.run (role.repairer:56:26)
    at Object.module.exports.loop (main:53:18)
    at __mainLoop:1:52

     

    The error says it can't read the property, but in the 2 console lines above it is shows that it does. This happens with and property of creep, including creep.memory

    Am I missing something?

    Thanks Koraka

     Edit: I don't know how to put the code into a box to make it easier to read.



  • Nevermind, I found the second roleRepairer.run(); call in main.