· 10 years ago · Sep 23, 2016, 05:26 PM
1-------------------------MAIN-------------------------
2var creepFunctions = require('manager.CreepFunctions');
3
4module.exports.loop = function () {
5 CheckForDeath();
6
7 creepFunctions.CreateCreepIfPossible();
8
9 creepFunctions.DoCreepWork();
10}
11
12function CheckForDeath()
13{
14 if(!Game.creeps[Memory.oldestCreepName])
15 {
16 delete Memory.creeps[Memory.oldestCreepName];
17
18 //9007...991 = max var value.
19 var newOldestAge = 9007199254740991;
20 var newOldestName = "";
21 for(var name in Memory.creeps)
22 {
23 if(Memory.creeps[name].spawnTime < newOldestAge)
24 {
25 newOldestAge = Memory.creeps[name].spawnTime;
26 newOldestName = name;
27 }
28 }
29 Memory.oldestCreepName = newOldestName;
30 }
31}
32
33-------------------------manager.CreepFunctions-------------------------
34var roleHarvester = require('role.Harvester');
35var roleUpgrader = require('role.Upgrader');
36
37/*
38TABLE OF CONTENTS:
39 CreateCreepIfPossible
40 - Sees if a creep can be made. If it can, create a creep.
41
42 MakeUniqueCreep (Helper Function)
43 - Makes a creep with a unique name. Assigns role.
44
45 GetNumberOfWorkers (Helper Function)
46 - Gets the number of workers under a designated role.
47
48 DoCreepWork
49 - Makes the creeps do their assigned tasks.
50
51*/
52
53var numTypes = 3;
54var numCreepsWanted = 5;
55
56var creepRoles = {
57 HARVESTER : 'H',
58 BUILDER : 'B',
59 UPGRADER : 'U'
60};
61
62var componentCosts = {
63 WORK : 100,
64 CARRY : 50,
65 MOVE : 50
66};
67
68//TODO: Make this function better by not having else and if checks. Rather, have a single for loop.
69//ALSO: If there is spare energy, we should put that into making buildings.
70//ALSO: Make builders. DUH.
71function CreateCreepIfPossible()
72{
73 for(var name in Game.spawns)
74 {
75 if(Game.spawns[name].spawning == null
76 && Game.spawns[name].energy >= 300)
77 {
78 var notSpawning = true;
79 //Create harvesters
80 var numHarvesters = GetNumberOfWorkers(creepRoles.HARVESTER);
81 if(numHarvesters < numCreepsWanted && notSpawning)
82 {
83 MakeUniqueCreep(creepRoles.HARVESTER, numHarvesters, name);
84 notSpawning = false;
85 }
86
87 //Create Upgraders
88 var numUpgraders = GetNumberOfWorkers(creepRoles.UPGRADER);
89 if(numUpgraders < numCreepsWanted && notSpawning)
90 {
91 MakeUniqueCreep(creepRoles.UPGRADER, numUpgraders, name);
92 notSpawning = false;
93 }
94 }
95 }
96}
97
98function MakeUniqueCreep(roleType, currentNum, spawnerName)
99{
100 var wentToZero = false;
101 var newName = roleType + currentNum.toString();
102
103 //while the creep with x name exists, create a new name.
104 while(Game.creeps[newName])
105 {
106 newName = roleType;
107 currentNum++;
108 //Start at 0 for naming conventions if the name didn't work initially.
109 if(!wentToZero)
110 {
111 currentNum=0;
112 wentToZero=true;
113 }
114 newName+=currentNum;
115 }
116 //Create the creep, store memory.
117 Game.spawns[spawnerName].createCreep([WORK, WORK, MOVE, CARRY], newName);
118 Memory.creeps[newName].role = roleType;
119 Memory.creeps[newName].spawnTime = Game.time;
120}
121
122function GetNumberOfWorkers(roleType)
123{
124 var total = 0;
125 //Check "Game" as memory may have a dead creep.
126 for(var name in Game.creeps)
127 {
128 if(Memory.creeps[name].role == roleType)
129 {
130 total++;
131 }
132 }
133 return total;
134}
135
136function DoCreepWork()
137{
138 for(var name in Game.creeps)
139 {
140 creep = Game.creeps[name];
141
142 if(Memory.creeps[name].role == creepRoles.HARVESTER)
143 {
144 roleHarvester.run(creep);
145 }
146 else if(Memory.creeps[name].role == creepRoles.UPGRADER)
147 {
148 roleUpgrader.run(creep);
149 }
150 }
151}
152
153module.exports =
154{
155 CreateCreepIfPossible,
156 DoCreepWork
157};