1+ // Globals
2+ var invert = false ;
3+
4+ var main = function ( ) {
5+ // Add a menu item under the map icon on the top toolbar
6+ ui . registerMenuItem ( "Count Tiles" , function ( ) {
7+ count_window ( ) ;
8+ } ) ;
9+ } ;
10+
11+ function calc_tiles ( ) {
12+ // var to return
13+ var numtiles = 0 ;
14+
15+ // Get all settings from the window
16+ var window = ui . getWindow ( "Count Tiles" ) ; var window = ui . getWindow ( "Count Tiles" ) ;
17+ var lands = context . getAllObjects ( "terrain_surface" ) ;
18+ var settings = null ;
19+ var surfaceStyles = [ ]
20+ for ( var i = 0 ; i < lands . length ; i ++ ) {
21+ settings = window . findWidget ( 'landtype' + i ) ;
22+ surfaceStyles [ lands [ i ] . index ] = settings . isChecked
23+ }
24+
25+ // Loop through all tiles except the outer ring of the map as this is just used to show sides.
26+ var tile = null ;
27+ var element = null ;
28+ for ( var i = 1 ; i < ( map . size . x - 1 ) ; i ++ ) { //X
29+ for ( var j = 1 ; j < ( map . size . y - 1 ) ; j ++ ) { //Y
30+ // Check if the type of land is checked
31+ tile = map . getTile ( i , j ) ;
32+ for ( var k = 0 ; k < tile . numElements ; k ++ ) {
33+ element = tile . getElement ( k ) ;
34+ if ( element . type == 'surface' && surfaceStyles [ element . surfaceStyle ] == invert ) {
35+ numtiles ++
36+ }
37+ }
38+ }
39+ }
40+
41+ // Set the value in the window
42+ var text = window . findWidget ( "numtiles" ) ;
43+ text . text = "Number of tiles is: " + numtiles
44+ }
45+
46+ function count_window ( ) {
47+ widgets = [ ]
48+
49+ // Get all land types
50+ var lands = context . getAllObjects ( "terrain_surface" )
51+ var landspercolumn = 27
52+
53+ // Lay-out
54+ widgets . push ( {
55+ type : 'groupbox' ,
56+ name : 'box1' ,
57+ x : 5 ,
58+ y : 20 ,
59+ width : 690 ,
60+ height : 40 ,
61+ text : "Amount of Tiles"
62+ } ) ;
63+
64+ widgets . push ( {
65+ type : 'groupbox' ,
66+ name : 'box1' ,
67+ x : 5 ,
68+ y : 70 ,
69+ width : 690 ,
70+ height : 425 ,
71+ text : "Exclude landtype"
72+ } ) ;
73+
74+ // Num of tiles
75+ widgets . push ( {
76+ type : 'label' ,
77+ name : 'numtiles' ,
78+ x : 15 ,
79+ y : 40 ,
80+ width : 300 ,
81+ height : 20 ,
82+ text : "Number of tiles is: "
83+ } ) ;
84+
85+ widgets . push ( {
86+ type : 'checkbox' ,
87+ name : 'invert' ,
88+ x : 200 ,
89+ y : 38 ,
90+ width : 15 ,
91+ height : 15 ,
92+ isChecked : false ,
93+ text : "" ,
94+ onChange : function onChange ( e ) {
95+ invert = ! invert
96+ calc_tiles ( ) ;
97+ }
98+ } ) ;
99+
100+ widgets . push ( {
101+ type : 'label' ,
102+ name : 'invert_label' ,
103+ x : 213 ,
104+ y : 40 ,
105+ width : 180 ,
106+ height : 20 ,
107+ text : "count excluded landtiles instead"
108+ } ) ;
109+
110+ // Add all the land types
111+ column = 0
112+ row = 0
113+ for ( var i = 0 ; i < lands . length ; i ++ ) {
114+
115+ widgets . push ( {
116+ type : 'checkbox' ,
117+ name : 'landtype' + i ,
118+ x : 30 + ( column * 200 ) ,
119+ y : 85 + ( row * 15 ) ,
120+ width : 15 ,
121+ height : 15 ,
122+ isChecked : false ,
123+ text : "" ,
124+ onChange : function onChange ( e ) {
125+ calc_tiles ( ) ;
126+ }
127+ } ) ;
128+
129+ widgets . push ( {
130+ type : 'label' ,
131+ name : 'landtype_label' + i ,
132+ x : 43 + ( column * 200 ) ,
133+ y : 87 + ( row * 15 ) ,
134+ width : 180 ,
135+ height : 20 ,
136+ text : lands [ i ] . installedObject . name
137+ } ) ;
138+
139+ // Increase the row
140+ row ++
141+
142+ if ( ( i + 1 ) % landspercolumn == 0 ) {
143+ column ++
144+ row = 0
145+ }
146+ }
147+
148+ // Create the window
149+ window = ui . openWindow ( {
150+ classification : 'Count Tiles' ,
151+ title : "Count Tiles 1.0 (by Levis)" ,
152+ width : 700 ,
153+ height : 500 ,
154+ x : 100 ,
155+ y : 100 ,
156+ colours : [ 26 , 26 ] ,
157+ widgets : widgets
158+ } ) ;
159+
160+ // Update the counter
161+ calc_tiles ( )
162+ }
163+
164+ registerPlugin ( {
165+ name : 'TileCounter' ,
166+ version : '1.0' ,
167+ authors : [ 'AutoSysOps (Levis)' ] ,
168+ type : 'remote' ,
169+ licence : 'MIT' ,
170+ targetApiVersion : 34 ,
171+ main : main
172+ } ) ;
0 commit comments