File: DrawingUtils.java

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (337 lines) | stat: -rwxr-xr-x 12,687 bytes parent folder | download | duplicates (7)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright Hugh Perkins 2006, 2009
// hughperkins@gmail.com http://manageddreams.com
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
//  more details.
//
// You should have received a copy of the GNU General Public License along
// with this program in the file licence.txt; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
// 1307 USA
// You can find the licence also on the web at:
// http://www.opensource.org/licenses/gpl-license.php
//
// ======================================================================================
//

package hughai.utils;

import java.util.Stack;

import com.springrts.ai.*;
import com.springrts.ai.oo.*;
import com.springrts.ai.oo.clb.*;

import hughai.CSAI;
import hughai.GameAdapter;
import hughai.PlayerObjects;
import hughai.VoiceCommandHandler;
import hughai.basictypes.*;
import hughai.*;
import hughai.mapping.HeightMap;
import hughai.ui.MainUI;
import hughai.unitdata.*;

// methods to draw stuff on the map
public class DrawingUtils
{
   CSAI csai;
   OOAICallback aicallback;
   BuildTable buildTable;
   LogFile logfile;
   Config config;
   //int squaresize;
   PlayerObjects playerObjects;

   final int maxlines;
   int linesDrawn = 0;

   public DrawingUtils( PlayerObjects playerObjects ){
      this.csai = playerObjects.getCSAI();
      this.aicallback = playerObjects.getAicallback();
      this.buildTable = playerObjects.getBuildTable();
      this.logfile = playerObjects.getLogFile();
      this.config = playerObjects.getConfig();

      this.playerObjects = playerObjects;

      maxlines = config.getMaxLinesOnMap();

      csai.registerGameListener( new GameListener() );
      //      csai.RegisterVoiceCommand( "cleanmap", new VoiceCleanMap() );
      csai.RegisterVoiceCommand( "drawpoint", new VoiceDrawPoint() );

      playerObjects.getMainUI().registerButton( "Clean map lines", new CleanMapButton() );
   }

   class CleanMapButton implements MainUI.ButtonHandler {
      @Override
      public void go() {
         CleanDrawing();
      }
   }


   class VoiceDrawPoint implements VoiceCommandHandler {
      @Override
      public void commandReceived( String command, String[] args, int player ) {
         float posx = Float.parseFloat( args[2] );
         float posz = Float.parseFloat( args[3] );
         drawText(new TerrainPos( posx, 0, posz ), "(" + posx + ", " + posz + ")" );;
      }   
   }

   class VoiceCleanMap implements VoiceCommandHandler {
      @Override
      public void commandReceived( String command, String[] args, int player ) {
         CleanDrawing();
      }
   }

   class GameListener extends GameAdapter {
      //      @Override
      //      public void Tick( int frame ) {
      ////         if( frame % ( 150 * 5 ) == 0 ) {
      ////           // csai.sendTextMessage( "" + frame );
      ////           // CleanDrawing();
      ////         }
      //      }
   }

   public void CleanDrawing() {
      csai.sendTextMessage( "cleaning map..." );
      pushCheating();
      setCheating( false );
      int squaresize = playerObjects.getMaps().getMovementMaps().SQUARE_SIZE;
      int mapwidth = aicallback.getMap().getWidth() * squaresize;
      int mapheight = aicallback.getMap().getHeight() * squaresize;
      logfile.WriteLine( "cleandrawing, " + mapwidth + " x " + mapheight );
      for( int z = 0; z < mapheight; z+= 100 ) {
         for( int x = 0; x < mapwidth; x+= 100 ) {
            float elevation = aicallback.getMap().getElevationAt( x, z );
            try {
               aicallback.getMap().getDrawer().deletePointsAndLines(new AIFloat3( x, elevation, z ));
            } catch (CallbackAIException ex) {
               logfile.writeStackTrace(ex);
            }
         }

      }
      popCheating();
      csai.sendTextMessage( " ... done" );
      linesDrawn = 0;
   }

   public void setFigureColor(float r, float g, float b){
      // TODO
      //      csai.handleEngineCommand(
      //            new AddLineDrawAICommand(startpos, endpos));
   }

   public void AddLine(TerrainPos startpos, TerrainPos endpos ) {
      //      csai.handleEngineCommand(
      //            new CreateLineFigureDrawerAICommand(
      //                  startpos,
      //                  endpos,
      //                  2,
      //                  false,
      //                  200,
      //                  -1,
      //                  -1) );
      //                  AIFloat3 pos1, AIFloat3 pos2, float width, 
      //                  boolean arrow, int lifeTime, int figureGroupId, 
      //                  int ret_newFigureGroupId)
         if( linesDrawn >= maxlines ) {
            csai.sendTextMessage( "DrawingUtils.AddLine: too many lines drawn.  drawing more would crash Spring..." );
            return;
            //         throw new RuntimeException("DrawingUtils.AddLine: too many lines drawn.  drawing more would crash Spring..." );
         }
         pushCheating();
         setCheating( false );
         try {
            aicallback.getMap().getDrawer().addLine(startpos.toAIFloat3(), endpos.toAIFloat3());
         } catch (CallbackAIException ex) {
            logfile.writeStackTrace(ex);
         }
         linesDrawn++;
         popCheating();
   }
   
   Stack<Boolean> cheatingStack = new Stack<Boolean>(); 
   void pushCheating() {
      cheatingStack.push( isCheating() );
   }
   
   boolean isCheating() {
      return playerObjects.getAicallback().getCheats().isEnabled();
   }
   
   void setCheating( boolean cheating ) {
      if( isCheating() != cheating ) {
         playerObjects.getAicallback().getCheats().setEnabled( cheating );
      }
   }
   
   void popCheating() {
      boolean oldCheating = cheatingStack.pop(); 
      setCheating( oldCheating );
   }

   //   public void AddLine(Float3 startpos, Float3 endpos ) {
   //      AddLine(startpos, endpos);
   //   }

   public void DrawUnit( UnitDef toDrawUnitDef, TerrainPos pos, float rotation, int lifeTime, int teamId, boolean transparent, boolean drawBorder, int facing) {

      try {
         aicallback.getMap().getDrawer().drawUnit(toDrawUnitDef, pos.toAIFloat3(), rotation, lifeTime, teamId, transparent, drawBorder, facing);
      } catch (CallbackAIException ex) {
         logfile.writeStackTrace(ex);
      }
   }

   public void DrawUnit( String defname, TerrainPos pos ) {
      DrawUnit( defname, pos, 0, 200, aicallback.getSkirmishAI().getTeamId(), true, false );
   }

   public void DrawUnit( String defname, TerrainPos pos, float rotation, int lifeTime, int teamId, boolean transparent, boolean drawBorder ) {
      if( pos == null ){ throw new RuntimeException( "drawunit: pos was null"); }
      UnitDef toDrawUnitDef = buildTable.getUnitDefByName( defname.toLowerCase() );
      if( toDrawUnitDef == null ){ throw new RuntimeException( "unit " + defname + " doesn't exist."); }

      lifeTime = 200;
      try {
         aicallback.getMap().getDrawer().drawUnit(toDrawUnitDef, pos.toAIFloat3(), rotation, lifeTime, teamId, transparent, drawBorder, 0);
      } catch (CallbackAIException ex) {
         logfile.writeStackTrace(ex);
      }
   }

   public void drawText( TerrainPos pos, String text ) {
      pushCheating();
      setCheating( false );
      try {
         aicallback.getMap().getDrawer().addPoint(pos.toAIFloat3(), text);
      } catch (CallbackAIException ex) {
         logfile.writeStackTrace(ex);
      }
      popCheating();
   }

   public void DrawRectangle(TerrainPos pos, int width, int height )
   {
      //Float3 pos = new Float3(inpos);
      float elevation = aicallback.getMap().getElevationAt(pos.x, pos.z) + 10;
      AddLine(pos.add( new TerrainPos(0f, elevation, 0f)  ),
            pos.add( new TerrainPos(width, elevation, 0) ) );
      AddLine(pos.add( new TerrainPos(width, elevation, 0) ),
            pos.add( new TerrainPos(width, elevation, height) ) );
      AddLine(pos.add( new TerrainPos(width, elevation, height) ),
            pos.add( new TerrainPos(0, elevation, height) ) );
      AddLine(pos.add( new TerrainPos(0, elevation, height) ),
            pos.add( new TerrainPos(0, elevation, 0) ) );
   }

   public void DrawCircle( TerrainPos pos, double radius )
   {
      TerrainPos lastpos = null;

      for( int angle = 0; angle <= 360; angle += 20 )
      {
         int x = (int)( (double)radius * Math.cos ( (double)angle * Math.PI / 180 ) );
         int y = (int)( (double)radius * Math.sin ( (double)angle * Math.PI / 180 ) );
         TerrainPos thispos = new TerrainPos( x, 0, y ).add( pos );
         if( lastpos != null )
         {
            AddLine( thispos,lastpos );
         }
         lastpos = thispos;
      }
   }

   // returns figure group
   public void DrawMap(int[][] map )
   {
      Map gamemap = aicallback.getMap();
      int thismapwidth = map.length;
      int thismapheight = map[0].length;
      int gamemapwidth = gamemap.getWidth();
      int gamemapheight = gamemap.getHeight();
      int multiplier = ( gamemapwidth / thismapwidth ) * 8;

      int figuregroup = 0;
      int granularity = config.getMapDrawGranularity();
      int skip = ( granularity * thismapwidth ) / gamemapwidth; // so, if they are the same, skip will be equal to granularity
      // if thismapwidith is half gamemapwidth,  then skip will be granularity / 2
      logfile.WriteLine( "drawingUtils.drawmap granularity: " + granularity 
            + " skip: " + skip + " mapwidth: " + thismapwidth + " / " + gamemapwidth
            + " x " + thismapheight + " / " + gamemapheight );
      if( skip < 1 ) { skip = 1; }
      HeightMap heightMap = playerObjects.getMaps().getHeightMap();
//      float[][] heightMapArray = heightMap.GetHeightMap();
      for( int z = 0; z < thismapheight; z += skip )
      {
         for (int x = 0; x < thismapwidth; x+= skip )
         {
            //            float elevation = gamemap.getElevationAt( x *multiplier, y * multiplier ) + 10;
//            float elevation = heightMapArray[x][z];
            float elevation = heightMap.getElevationAt( 
                  new HeightMap.HeightMapPos( x, z ) );
            if (x < (thismapwidth - skip) &&
                  map[x][z] != map[x + skip][z] )
            {
               AddLine(new TerrainPos((x + skip) * multiplier, elevation, z * multiplier),
                     new TerrainPos((x + skip) * multiplier, elevation, (z + skip) * multiplier) );
            }
            if (z < (thismapheight - skip) &&
                  map[x][z] != map[x][z + skip] )
            {
               AddLine(new TerrainPos(x * multiplier, elevation, (z + skip) * multiplier),
                     new TerrainPos((x + skip) * multiplier, elevation, (z + skip) * multiplier) );
            }
         }
      }
   }
   // returns figure group
   public void DrawMap(boolean[][] map)
   {
      Map gamemap = aicallback.getMap();
      int thismapwidth = map.length;
      int thismapheight = map[0].length;
      int multiplier = ( gamemap.getWidth() / thismapwidth ) * 8;

      int figuregroup = 0;
      for (int y = 1; y < thismapheight - 1; y++)
         //      for (int y = 0; y < thismapheight; y++)
      {
         for (int x = 1; x < thismapwidth - 1; x++)
            //         for (int x = 0; x < thismapwidth; x++)
         {
            if (x < (thismapwidth - 2) &&
                  //            if (x < (thismapwidth - 1) &&
                  map[x][y] != map[x + 1][y])
            {
               float elevation = gamemap.getElevationAt(x * multiplier, y * multiplier) + 10;
               AddLine(new TerrainPos((x + 1) * multiplier, elevation, y * multiplier),
                     new TerrainPos((x + 1) * multiplier, elevation, (y + 1) * multiplier) );
            }
            if (y < (thismapheight - 2) &&
                  //            if (y < (thismapheight - 1) &&
                  map[x][y] != map[x][y + 1])
            {
               float elevation = gamemap.getElevationAt(x * multiplier, y * multiplier) + 10;
               AddLine(new TerrainPos(x * multiplier, elevation, (y + 1) * multiplier),
                     new TerrainPos((x + 1) * multiplier, elevation, (y + 1) * multiplier) );
            }
         }
      }
   }
}