-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwork.rooms.js
142 lines (134 loc) · 3.62 KB
/
work.rooms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Work from rooms configuration module
*/
let messages = require('./messages');
let objects = require('./objects');
let path = require('./path');
let rooms = require('./rooms');
/**
* Room-role-planned work : depends on work step
*
* @param creepjs object
* @param creep Creep
*/
module.exports.work = function(creepjs, creep)
{
switch (creep.memory.step) {
case 'spawning': if (!creep.spawning) this.spawning(creepjs, creep); break;
case 'goToStart': this.goToStart (creepjs, creep); break;
case 'goToSource': this.goToSource(creepjs, creep); break;
case 'sourceWork': this.sourceWork(creepjs, creep); break;
case 'goToTarget': this.goToTarget(creepjs, creep); break;
case 'targetWork': this.targetWork(creepjs, creep); break;
default: creepjs.nextStep(creep, 'spawning');
}
};
/**
* #1 : SPAWN
* Called when creep has just spawned
*
* @param creepjs object
* @param creep Creep
*/
module.exports.spawning = function(creepjs, creep)
{
let position = rooms.getRoomPosition(creep.room, creep.memory.room_role);
if (position) {
if (!(position.x - creep.pos.x) && !(position.y - creep.pos.y)) {
creepjs.nextStep(creep, 'sourceWork');
this.sourceWork(creepjs, creep);
}
else {
path.calculate(creep, position, { ignore_creeps: false, source_range: 0 });
creepjs.nextStep(creep, 'goToStart');
creep.memory.source = rooms.get(creep.memory.room, creep.memory.room_role, 'source');
creep.memory.target = rooms.get(creep.memory.room, creep.memory.room_role, 'target');
this.goToStart(creepjs, creep);
}
}
else {
creep.say('no pos');
}
};
/**
* #2 : GO TO START POSITION
* Initial go to start position
*
* @param creepjs object
* @param creep Creep
*/
module.exports.goToStart = function(creepjs, creep)
{
let moved = path.move(creep);
if (moved === path.ARRIVED) {
let room_memory = rooms.get(creep.memory.room, creep.memory.room_role, rooms.MEMORY);
if (room_memory.path) creep.memory.path = room_memory.path;
else delete creep.memory.path;
delete creep.memory.path_step;
creepjs.nextStep(creep, 'sourceWork');
this.sourceWork(creepjs, creep);
}
else if (moved) {
creep.say('m:' + messages.error(moved));
}
};
/**
* #3 : SOURCE WORK
*
* @param creepjs object
* @param creep Creep
*/
module.exports.sourceWork = function(creepjs, creep)
{
let error = creepjs.sourceWork(creep);
if (error === creepjs.NEXT_STEP) creepjs.nextStep(creep, 'goToTarget');
else if (error) creep.say('s:' + messages.error(error));
};
/**
* #4 : GO TO TARGET
*
* @param creepjs object
* @param creep Creep
*/
module.exports.goToTarget = function(creepjs, creep)
{
let moved = creep.memory.path ? path.move(creep) : path.ARRIVED;
if ((moved === path.ARRIVED) || (moved === path.WAYPOINT)) {
creepjs.nextStep(creep, 'targetWork');
this.targetWork(creepjs, creep);
}
else if (moved) {
creep.say('m:' + messages.error(moved));
}
};
/**
* #5 : TARGET WORK
*
* @param creepjs object
* @param creep Creep
*/
module.exports.targetWork = function(creepjs, creep)
{
let error = creepjs.targetWork(creep);
if (error === creepjs.NEXT_STEP) creepjs.nextStep(creep, 'goToSource');
else if (error) creep.say('t:' + messages.error(error));
};
/**
* #6 : GO TO SOURCE
* Go to source step
*
* @param creepjs
* @param creep Creep
*/
module.exports.goToSource = function(creepjs, creep)
{
let moved = creep.memory.path ? path.move(creep) : path.ARRIVED;
if (moved === path.ARRIVED) {
delete creep.memory.path_step;
creepjs.nextStep(creep, 'sourceWork');
this.sourceWork(creepjs, creep);
}
else if (moved) {
creep.say('m:' + messages.error(moved));
}
};