forked from leo7044/CnC_TA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount_Forgotten_Bases_in_Range.user.js
267 lines (252 loc) · 14.4 KB
/
Count_Forgotten_Bases_in_Range.user.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// ==UserScript==
// @name C&C:TA Count Forgotten Bases in Range
// @namespace CountBasesButton
// @description Display the number of forgotten bases in range of selected world object and paste it to chat message
// @downloadURL https://raw.githubusercontent.com/leo7044/CnC_TA/master/Count_Forgotten_Bases_in_Range.user.js
// @updateURL https://raw.githubusercontent.com/leo7044/CnC_TA/master/Count_Forgotten_Bases_in_Range.user.js
// @include http*://prodgame*.alliances.commandandconquer.com/*/index.aspx*
// @include http*://cncapp*.alliances.commandandconquer.com/*/index.aspx*
// @version 1.1.0.2
// @author im not the author of the code - this script is based on Paste Coords Button and Shockr's BaseScanner sripts
// @contributor leo7044 (https://github.com/leo7044)
// ==/UserScript==
(function () {
var CNCTACountBases_main = function () {
try {
function createCountButton() {
console.log('C&C:Tiberium Alliances Count Forgotten Bases in Range: loaded.');
var countButton = {
selectedBase: null,
countBases: function (x, y) {
var levelCount = [];
var count = 0;
var maxAttack = 10;
var world = ClientLib.Data.MainData.GetInstance() .get_World();
for (var scanY = y - 10; scanY <= y + 10; scanY++)
{
for (var scanX = x - 10; scanX <= x + 10; scanX++)
{
var distX = Math.abs(x - scanX);
var distY = Math.abs(y - scanY);
var distance = Math.sqrt((distX * distX) + (distY * distY));
// too far away to scan
if (distance > maxAttack)
{
continue;
}
var object = world.GetObjectFromPosition(scanX, scanY);
// Nothing to scan
if (object === null)
{
continue;
}
// Object isnt a NPC Base / Camp / Outpost
if (object.Type !== ClientLib.Data.WorldSector.ObjectType.NPCBase)
{
continue;
}
if (typeof object.getCampType === 'function' && object.getCampType() === ClientLib.Data.Reports.ENPCCampType.Destroyed)
{
continue;
}
if (typeof object.getLevel !== 'function')
{
countButton._patchClientLib();
}
var level = object.getLevel();
levelCount[level] = (levelCount[level] || 0) + 1;
count++;
}
}
var output = [];
for (var i = 0; i < levelCount.length; i++)
{
var lvl = levelCount[i];
if (lvl !== undefined)
{
output.push(lvl + 'x' + i);
}
}
console.log(x + ':' + y + ' [' + count + ' Bases: ' + output.join(' ') + ']');
countButton.pasteCount(x, y, count, output.join(' '));
},
countSoloBases: function (x, y) {
var count = 0;
var maxAttack = 10;
var world = ClientLib.Data.MainData.GetInstance() .get_World();
for (var scanY = y - 10; scanY <= y + 10; scanY++)
{
for (var scanX = x - 10; scanX <= x + 10; scanX++)
{
var distX = Math.abs(x - scanX);
var distY = Math.abs(y - scanY);
var distance = Math.sqrt((distX * distX) + (distY * distY));
// too far away to scan
if (distance > maxAttack)
{
continue;
}
var object = world.GetObjectFromPosition(scanX, scanY);
// Nothing to scan
if (object === null)
{
continue;
}
// Object isnt a NPC Base / Camp / Outpost
if (object.Type !== ClientLib.Data.WorldSector.ObjectType.NPCBase)
{
continue;
}
if (typeof object.getCampType === 'function' && object.getCampType() === ClientLib.Data.Reports.ENPCCampType.Destroyed)
{
continue;
}
count++;
}
}
return count;
},
count: function () {
if (countButton.selectedBase === null || countButton.selectedBase === undefined) {
return;
}
return countButton.countBases(countButton.selectedBase.get_RawX(), countButton.selectedBase.get_RawY());
},
pasteCount: function (x, y, baseCount, baseData) {
var input = qx.core.Init.getApplication() .getChat() .getChatWidget() .getEditable();
// Input
var dom = input.getContentElement() .getDomElement();
// Input DOM Element
var result = new Array();
result.push(dom.value.substring(0, dom.selectionStart));
// start
result.push('[coords]' + x + ':' + y + '[/coords] [' + baseCount + ' Bases: ' + baseData + ']');
result.push(dom.value.substring(dom.selectionEnd, dom.value.length));
// end
input.setValue(result.join(' '));
},
pasteCoords: function () {
var input = qx.core.Init.getApplication() .getChat() .getChatWidget() .getEditable();
// Input
var dom = input.getContentElement() .getDomElement();
// Input DOM Element
var result = new Array();
result.push(dom.value.substring(0, dom.selectionStart));
// start
result.push('[coords]' + countButton.selectedBase.get_RawX() + ':' + countButton.selectedBase.get_RawY() + '[/coords]');
result.push(dom.value.substring(dom.selectionEnd, dom.value.length));
// end
input.setValue(result.join(' '));
},
_g: function (k, r, q, m) {
var p = [
];
var o = k.toString();
var n = o.replace(/\s/gim, '');
p = n.match(r);
var l;
for (l = 1; l < (m + 1); l++) {
if (p !== null && p[l].length === 6) {
console.debug(q, l, p[l]);
} else {
if (p !== null && p[l].length > 0) {
console.warn(q, l, p[l]);
} else {
console.error('Error - ', q, l, 'not found');
console.warn(q, n);
}
}
}
return p;
},
_patchClientLib: function () {
var proto = ClientLib.Data.WorldSector.WorldObjectNPCBase.prototype;
var re = /100\){0,1};this\.(.{6})=Math.floor.*d\+=f;this\.(.{6})=\(/;
var x = countButton._g(proto.$ctor, re, 'ClientLib.Data.WorldSector.WorldObjectNPCBase', 2);
if (x !== null && x[1].length === 6) {
proto.getLevel = function () {
return this[x[1]];
};
} else {
console.error('Error - ClientLib.Data.WorldSector.WorldObjectNPCBase.Level undefined');
}
}
};
if ( webfrontend.gui.region.RegionCityMenu.prototype.__baseCounterButton_showMenu ){
webfrontend.gui.region.RegionCityMenu.prototype.showMenu = webfrontend.gui.region.RegionCityMenu.prototype.__baseCounterButton_showMenu;
webfrontend.gui.region.RegionCityMenu.prototype.__baseCounterButton_initialized = false;
webfrontend.gui.region.RegionCityMenu.prototype.__baseCounterButton_showMenu = undefined;
};
if (!webfrontend.gui.region.RegionCityMenu.prototype.__countButton_showMenu) {
webfrontend.gui.region.RegionCityMenu.prototype.__countButton_showMenu = webfrontend.gui.region.RegionCityMenu.prototype.showMenu;
webfrontend.gui.region.RegionCityMenu.prototype.showMenu = function (selectedVisObject) {
var self = this;
countButton.selectedBase = selectedVisObject;
if (this.__countButton_initialized != 1) {
this.__countButton_initialized = 1;
this.__coordButton = [];
this.__countButton = [];
this.__countComposite = new qx.ui.container.Composite(new qx.ui.layout.VBox(0)).set({
padding: 2
});
for (var i in this) {
try {
if (this[i] && this[i].basename == "Composite") {
/* var coordbutton = new qx.ui.form.Button("Paste Coords");
coordbutton.addListener("execute", function () {
countButton.pasteCoords();
}); */
var countbutton = new qx.ui.form.Button("Paste Count");
countbutton.addListener("execute", function () {
countButton.count();
});
// this[i].add(coordbutton);
this[i].add(countbutton);
// this.__coordButton.push(coordbutton);
this.__countButton.push(countbutton);
}
} catch (e) {
console.log("buttons ", e);
}
}
}
for (var i = 0; i < self.__countButton.length; ++i) {
self.__countButton[i].setLabel('Paste Count (' + countButton.countSoloBases(countButton.selectedBase.get_RawX(), countButton.selectedBase.get_RawY()) + ')');
}
switch (selectedVisObject.get_VisObjectType()) {
case ClientLib.Vis.VisObject.EObjectType.RegionPointOfInterest:
case ClientLib.Vis.VisObject.EObjectType.RegionRuin:
case ClientLib.Vis.VisObject.EObjectType.RegionHubControl:
case ClientLib.Vis.VisObject.EObjectType.RegionHubServer:
this.add(this.__countComposite);
break;
}
this.__countButton_showMenu(selectedVisObject);
};
}
}
} catch (e) {
console.log('createCountButton: ', e);
}
function CNCTACountBases_checkIfLoaded() {
try {
if (typeof qx !== 'undefined') {
createCountButton();
} else {
window.setTimeout(CNCTACountBases_checkIfLoaded, 1000);
}
} catch (e) {
console.log('CNCTACountBases_checkIfLoaded: ', e);
}
}
window.setTimeout(CNCTACountBases_checkIfLoaded, 1000);
};
try {
var CNCTACountBases = document.createElement('script');
CNCTACountBases.innerHTML = '(' + CNCTACountBases_main.toString() + ')();';
CNCTACountBases.type = 'text/javascript';
document.getElementsByTagName('head') [0].appendChild(CNCTACountBases);
} catch (e) {
console.log('C&C:Tiberium Alliances Count Forgotten Bases in Range: init error: ', e);
}
}) ();