asc /source/dialogs/attackpanel.cpp

Language C++ Lines 381
MD5 Hash 3482209104de6171e42175c38d41bf2a Estimated Cost $7,178 (why?)
Repository https://bitbucket.org/gcubar/asc View Raw File View Project SPDX
  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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <pgimage.h>
#include <pgtooltiphelp.h>

#include "attackpanel.h"

#include "../paradialog.h"
#include "../attack.h"
#include "../explosivemines.h"
#include "../iconrepository.h"

#include "../soundList.h"
#include "../gamemap.h"
#include "../events.h"
#include "../gameoptions.h"
#include "../windowing.h"
#include "../sdl/graphicsqueue.h"

#define USE_COLOR_CONSTANTS
#include <pgcolors.h>


class GetAttackerImage : public FightVisitor {
   private:
      const SDL_Rect dst;
      Surface& surf;

      void paint( UnitAttacksSomething& battle )
      {
         battle.getAttackingUnit()->typ->paint( surf, SPoint( dst.x, dst.y ), battle.getAttackingUnit()->getOwningPlayer().getPlayerColor() );
      }
      
   public:
      GetAttackerImage( tfight& battle, Surface& surface, const SDL_Rect& dest ) : dst ( dest ), surf( surface )
      {
         battle.visit( *this );
      };
      
      void visit( tunitattacksobject& battle )  { paint(battle); };
      void visit( tunitattacksbuilding& battle )  { paint(battle); };
      void visit( tunitattacksunit& battle )  { paint(battle); };
      
      void visit( tmineattacksunit& battle )
      {
         Mine* m = battle.getFirstMine();
         if ( m )
            m->paint( surf, SPoint( dst.x, dst.y ) );
      }
};

class GetTargetImage : public FightVisitor {
   private:
      const SDL_Rect dst;
      Surface& surf;
   public:
      GetTargetImage( tfight& battle, Surface& surface, const SDL_Rect& dest ) : dst ( dest ), surf( surface )
      {
         battle.visit( *this );
      };

      
      void visit( tunitattacksobject& battle )
      {
         battle.getTarget()->typ->display( surf, SPoint( dst.x, dst.y ) );
      };
      
      void visit( tmineattacksunit& battle )
      {
         battle.getTarget()->typ->paint( surf, SPoint( dst.x, dst.y ), battle.getTarget()->getOwningPlayer().getPlayerColor() );
      }
      
      void visit( tunitattacksbuilding& battle )
      {
         battle.getTarget()->paintSingleField( surf, SPoint(dst.x, dst.y), battle.getTarget()->typ->entry );
      };
      
      void visit( tunitattacksunit& battle )
      {
         battle.getTarget()->typ->paint( surf, SPoint( dst.x, dst.y ), battle.getTarget()->getOwningPlayer().getPlayerColor() );
      };
};



class AttackPanel : public Panel {
       tfight& engine;
       int attacker_exp;
       int defender_exp;
   public:
      AttackPanel ( tfight& engine_ ) ;
      void setBarGraphValue( const ASCString& widgetName, float fraction ) { Panel::setBargraphValue( widgetName, fraction ); };
      void setLabelText ( const ASCString& widgetName, int i ) { Panel::setLabelText ( widgetName, i ); };
      void setLabelText ( const ASCString& widgetName, const ASCString& i ) { Panel::setLabelText ( widgetName, i ); };
      void dispValue ( const ASCString& name, float value, float maxvalue, PG_Color color );
      void setBarGraphColor( const ASCString& widgetName, PG_Color color ) { Panel::setBarGraphColor( widgetName, color ); };
      void painter ( const PG_Rect &src, const ASCString& name, const PG_Rect &dst);
   private:
      void registerSpecialDisplay( const ASCString& name );
};



AttackPanel::AttackPanel ( tfight& engine_ ) : Panel( PG_Application::GetWidgetById(ASC_PG_App::mainScreenID), PG_Rect(0,0,170,200), "Attack" ), engine( engine_ )
{
   registerSpecialDisplay( "attacker_unit_pic" );
   registerSpecialDisplay( "defender_unit_pic" );
   registerSpecialDisplay( "attacker_unitexp" );
   registerSpecialDisplay( "defender_unitexp" );
   registerSpecialDisplay( "attacker_level" );
   registerSpecialDisplay( "defender_level" );
   registerSpecialDisplay( "attacker_weaponsymbol" );
   registerSpecialDisplay( "defender_weaponsymbol" );
   attacker_exp = engine.av.experience;
   defender_exp = engine.dv.experience;
}


void AttackPanel::registerSpecialDisplay( const ASCString& name )
{
   SpecialDisplayWidget* sdw = dynamic_cast<SpecialDisplayWidget*>( FindChild( name, true ) );
   if ( sdw )
      sdw->display.connect( SigC::slot( *this, &AttackPanel::painter ));
}


void AttackPanel::painter ( const PG_Rect &src, const ASCString& name, const PG_Rect &dst)
{
   Surface s = Surface::Wrap( PG_Application::GetScreen() );

   if ( name  == "attacker_unit_pic" ) {
      GetAttackerImage( engine, s, dst );
      return;
   }
   if ( name  == "defender_unit_pic" ) {
      GetTargetImage( engine, s, dst );
      return;
   }
   if ( name  == "attacker_unitexp" ) {
      s.Blit( IconRepository::getIcon("experience" + ASCString::toString(attacker_exp) + ".png"), SPoint(dst.x, dst.y) );
      return;
   }
   if ( name  == "defender_unitexp" ) {
      s.Blit( IconRepository::getIcon("experience" + ASCString::toString(defender_exp) + ".png"), SPoint(dst.x, dst.y) );
      return;
   }
   if ( name  == "attacker_level" && engine.av.height ) {
      s.Blit( IconRepository::getIcon("height-a" + ASCString::toString(getFirstBit(engine.av.height)) + ".png"), SPoint(dst.x, dst.y) );
      return;
   }
   if ( name  == "defender_level" && engine.dv.height ) {
      s.Blit( IconRepository::getIcon("height-a" + ASCString::toString(getFirstBit(engine.dv.height)) + ".png"), SPoint(dst.x, dst.y) );
      return;
   }

   if ( name  == "attacker_weaponsymbol" && engine.av.weapontype >= 0  ) {
      s.Blit( IconRepository::getIcon(SingleWeapon::getIconFileName( engine.av.weapontype ) + "-small.png"), SPoint(dst.x, dst.y) );
      return;
   }
   if ( name  == "defender_weaponsymbol" && engine.dv.weapontype >= 0  ) {
      s.Blit( IconRepository::getIcon(SingleWeapon::getIconFileName( engine.dv.weapontype ) + "-small.png"), SPoint(dst.x, dst.y) );
      return;
   }

}


void AttackPanel::dispValue ( const ASCString& name, float value, float maxvalue, PG_Color color )
{
   if ( value > 0 ) {
      setBarGraphColor( name + "bar", color );
      if ( value > maxvalue )
         setBarGraphValue( name + "bar", 1 );
      else
         setBarGraphValue( name + "bar", value / maxvalue );
   } else {
      setBarGraphColor( name + "bar", PG_Colormap::yellow );
      if ( value < -maxvalue )
         setBarGraphValue( name + "bar", 1 );
      else
         setBarGraphValue( name + "bar", -value / maxvalue );
   }
   ASCString s;
   s.format ( "%d", int(value * 100) );
   setLabelText( name, s );
}



class BattleSoundPlayer : public FightVisitor {
   private:
      bool first;
      tfight& the_battle;
            
      void play( UnitAttacksSomething& battle )
      {
         SoundList::getInstance().playSound ( SoundList::shooting,
                                              battle.getAttackingUnit()->getWeapon(battle.av.weapnum)->getScalarWeaponType(),
                                              false,
                                              battle.getAttackingUnit()->getWeapon(battle.av.weapnum)->soundLabel );
      };
      
   public:
      BattleSoundPlayer( tfight& battle ) : first ( true ), the_battle(battle)
      {
         battle.visit( *this );
      };

      void playEnd()
      {
         first = false;
         the_battle.visit( *this );
      }
      
      void visit( tunitattacksobject& battle )
      {
         if ( first )
            play( battle );
      };
      
      void visit( tunitattacksbuilding& battle )
      {
         if ( first )
            play( battle );
         else
            if ( battle.dv.damage >= 100 )
               SoundList::getInstance().playSound( SoundList::buildingCollapses );
      };
      
      void visit( tunitattacksunit& battle )
      {
         if ( first )
            play( battle );
         else {
            if ( battle.av.damage >= 100  )
               SoundList::getInstance().playSound( SoundList::unitExplodes , 0, false, battle.getAttackingUnit()->typ->killSoundLabel );
            
            if ( battle.dv.damage >= 100 )
               SoundList::getInstance().playSound( SoundList::unitExplodes , 0, false, battle.getTarget()->typ->killSoundLabel );

         }
      };
      
      void visit( tmineattacksunit& battle )
      {
         if ( first )
            SoundList::getInstance().playSound( SoundList::shooting , 1 );
         else
            if ( battle.dv.damage >= 100 )
               SoundList::getInstance().playSound( SoundList::unitExplodes , 0, false, battle.getTarget()->typ->killSoundLabel );
      }
      
};

const int maxdefenseshown = 2;
const int maxattackshown = 2;

/**
  Waits the given time, but still updates the screen.
  These is of no use to the user, but are needed by the video recorder
*/
void waitWithUpdate( int millisecs ) 
{
   int frameDelay;
   if ( CGameOptions::Instance()->video.ascframeratelimit  > 0 )
      frameDelay = 100 / CGameOptions::Instance()->video.ascframeratelimit ;
   else
      frameDelay = 10;
   
   int t = ticker;
   do {
      int t2 = ticker;
      do {
         releasetimeslice();
      } while ( t2 + frameDelay > ticker );
      postScreenUpdate( PG_Application::GetScreen() );
   } while ( t + millisecs/20 > ticker ); 
}


void showAttackAnimation( tfight& battle, GameMap* actmap, int ad, int dd )
{

   BattleSoundPlayer bsp( battle );
   
   auto_ptr<AttackPanel> at ( new AttackPanel(battle));

   float avd = float( 100 - battle.av.damage )/100;
   float dvd = float( 100 - battle.dv.damage )/100;

   PG_Color attackingColor = lighten_Color( actmap->player[battle.getAttackingPlayer()].getColor(), 22 );
   PG_Color defendingColor = lighten_Color( actmap->player[battle.getDefendingPlayer()].getColor(), 22 );

   at->setBarGraphValue( "attacker_unitstatusbar", avd );
   at->setBarGraphColor( "attacker_unitstatusbar", attackingColor );

   at->setBarGraphValue( "defender_unitstatusbar", dvd );
   at->setBarGraphColor( "defender_unitstatusbar", defendingColor );

   at->setLabelText( "attacker_unitstatus", 100 - battle.av.damage );
   at->setLabelText( "defender_unitstatus", 100 - battle.dv.damage );



   at->setBarGraphValue( "attacker_hemmingbar", (battle.dv.hemming -1) / 1.4 );
   at->setBarGraphColor( "attacker_hemmingbar", attackingColor );
   at->setBarGraphValue( "defender_hemmingbar", (battle.av.hemming -1) / 1.4 );
   at->setBarGraphColor( "defender_hemmingbar", defendingColor );

   at->setLabelText( "defender_hemming", "-" );
   at->setLabelText( "attacker_hemming", int((battle.dv.hemming-1) * 100 ));

   at->dispValue( "attacker_attackbonus", battle.strength_attackbonus(battle.av.attackbonus), maxattackshown, attackingColor );
   at->dispValue( "defender_attackbonus", battle.strength_attackbonus(battle.dv.attackbonus), maxattackshown, defendingColor );


   at->dispValue( "attacker_defencebonus", battle.defense_defensebonus(battle.av.defensebonus), maxattackshown, attackingColor );
   at->dispValue( "defender_defencebonus", battle.defense_defensebonus(battle.dv.defensebonus), maxattackshown, defendingColor );


   battle.calc();
   at->Show();


   int time1 = CGameOptions::Instance()->attackspeed1;
   if ( time1 <= 0 )
      time1 = 30;

   int time2 = CGameOptions::Instance()->attackspeed2;
   if ( time2 <= 0 )
      time2 = 50;

   int time3 = CGameOptions::Instance()->attackspeed3;
   if ( time3 <= 0 )
      time3 = 30;

   waitWithUpdate( time1*10 );



   if ( ad != -1 )
      battle.av.damage = ad;

   if ( dd != -1 )
      battle.dv.damage = dd;


   float avd2 = float( 100 - battle.av.damage )/100;
   float dvd2 = float( 100 - battle.dv.damage )/100;

   int starttime = ticker;
   while ( ticker < starttime + time2 ) {
      float p = float(ticker - starttime ) / time2;

      at->setBarGraphValue( "attacker_unitstatusbar", avd + (avd2-avd) * p );
      at->setBarGraphValue( "defender_unitstatusbar", dvd + (dvd2-dvd) * p );

      at->setLabelText( "attacker_unitstatus", int( 100.0 * (avd + (avd2-avd) * p )) );
      at->setLabelText( "defender_unitstatus", int( 100.0 * (dvd + (dvd2-dvd) * p )) );
      at->Update();

      releasetimeslice();
   }

   at->setBarGraphValue( "attacker_unitstatusbar", avd2 );
   at->setBarGraphValue( "defender_unitstatusbar", dvd2 );
   at->Update();

   bsp.playEnd();
   
   waitWithUpdate( time3*10 );
}
Back to Top