Line data Source code
1 : // Returns an array of commands suitable for ProcessCommand() based on the rally point data.
2 : // This assumes that the rally point has a valid position.
3 : function GetRallyPointCommands(cmpRallyPoint, spawnedEnts)
4 : {
5 0 : let data = cmpRallyPoint.GetData();
6 0 : let rallyPos = cmpRallyPoint.GetPositions();
7 0 : let ret = [];
8 0 : for (let i = 0; i < rallyPos.length; ++i)
9 : {
10 : // Look and see if there is a command in the rally point data, otherwise just walk there.
11 0 : let command = data[i] && data[i].command ? data[i].command : "walk";
12 :
13 : // If a target was set and the target no longer exists, or no longer
14 : // has a valid position, then just walk to the rally point.
15 0 : if (data[i] && data[i].target)
16 : {
17 0 : let cmpPosition = Engine.QueryInterface(data[i].target, IID_Position);
18 0 : if (!cmpPosition || !cmpPosition.IsInWorld())
19 : {
20 0 : if (command == "gather")
21 0 : command = "gather-near-position";
22 0 : else if (command == "collect-treasure")
23 0 : command = "collect-treasure-near-position";
24 0 : else if (command == "attack")
25 0 : command = "attack-walk";
26 : else
27 0 : command = "walk";
28 : }
29 : }
30 :
31 0 : switch (command)
32 : {
33 : case "gather":
34 0 : ret.push({
35 : "type": "gather",
36 : "entities": spawnedEnts,
37 : "target": data[i].target,
38 : "queued": true
39 : });
40 0 : break;
41 : case "gather-near-position":
42 0 : ret.push({
43 : "type": "gather-near-position",
44 : "entities": spawnedEnts,
45 : "x": rallyPos[i].x,
46 : "z": rallyPos[i].z,
47 : "resourceType": data[i].resourceType,
48 : "resourceTemplate": data[i].resourceTemplate,
49 : "queued": true
50 : });
51 0 : break;
52 : case "repair":
53 : case "build":
54 0 : ret.push({
55 : "type": "repair",
56 : "entities": spawnedEnts,
57 : "target": data[i].target,
58 : "queued": true,
59 : "autocontinue": i == rallyPos.length - 1
60 : });
61 0 : break;
62 : case "occupy-turret":
63 0 : ret.push({
64 : "type": "occupy-turret",
65 : "entities": spawnedEnts,
66 : "target": data[i].target,
67 : "queued": true
68 : });
69 0 : break;
70 : case "garrison":
71 0 : ret.push({
72 : "type": "garrison",
73 : "entities": spawnedEnts,
74 : "target": data[i].target,
75 : "queued": true
76 : });
77 0 : break;
78 : case "attack-walk":
79 0 : ret.push({
80 : "type": "attack-walk",
81 : "entities": spawnedEnts,
82 : "x": rallyPos[i].x,
83 : "z": rallyPos[i].z,
84 : "targetClasses": data[i].targetClasses,
85 : "queued": true
86 : });
87 0 : break;
88 : case "patrol":
89 0 : ret.push({
90 : "type": "patrol",
91 : "entities": spawnedEnts,
92 : "x": rallyPos[i].x,
93 : "z": rallyPos[i].z,
94 : "target": data[i].target,
95 : "targetClasses": data[i].targetClasses,
96 : "queued": true
97 : });
98 0 : break;
99 : case "attack":
100 0 : ret.push({
101 : "type": "attack",
102 : "entities": spawnedEnts,
103 : "target": data[i].target,
104 : "allowCapture": data[i].allowCapture,
105 : "queued": true,
106 : });
107 0 : break;
108 : case "trade":
109 0 : ret.push({
110 : "type": "setup-trade-route",
111 : "entities": spawnedEnts,
112 : "source": data[i].source,
113 : "target": data[i].target,
114 : "route": undefined,
115 : "queued": true
116 : });
117 0 : break;
118 : case "collect-treasure":
119 0 : ret.push({
120 : "type": "collect-treasure",
121 : "entities": spawnedEnts,
122 : "target": data[i].target,
123 : "queued": true
124 : });
125 0 : break;
126 : case "collect-treasure-near-position":
127 0 : ret.push({
128 : "type": "collect-treasure-near-position",
129 : "entities": spawnedEnts,
130 : "x": rallyPos[i].x,
131 : "z": rallyPos[i].z,
132 : "queued": true
133 : });
134 0 : break;
135 : default:
136 0 : ret.push({
137 : "type": "walk",
138 : "entities": spawnedEnts,
139 : "x": rallyPos[i].x,
140 : "z": rallyPos[i].z,
141 : "queued": true
142 : });
143 0 : break;
144 : }
145 : }
146 :
147 : // special case: trade route with waypoints
148 : // (we do not modify the RallyPoint before, as we want it to be displayed with all way-points)
149 0 : if (ret.length > 1 && ret[ret.length-1].type == "setup-trade-route")
150 : {
151 0 : let route = [];
152 0 : let waypoints = ret.length - 1;
153 :
154 0 : for (let i = 0; i < waypoints; ++i)
155 : {
156 0 : if (ret[i].type != "walk")
157 : {
158 0 : route = undefined;
159 0 : break;
160 : }
161 0 : route.push({ "x": ret[i].x, "z": ret[i].z });
162 : }
163 :
164 0 : if (route && route.length > 0)
165 : {
166 0 : ret.splice(0, waypoints);
167 0 : ret[0].route = route;
168 : }
169 : }
170 :
171 0 : return ret;
172 : }
173 :
174 0 : Engine.RegisterGlobal("GetRallyPointCommands", GetRallyPointCommands);
|