1 /* This file is part of KsirK.
2 Copyright (C) 2001-2007 Gael de Chalendar <kleag@free.fr>
3
4 KsirK is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation, either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA
18 */
19
20 /* begin : Sat Sep 1 2001 */
21
22 /**
23 * KGameWindow methods for sprites handling function of the state
24 */
25
26 // application specific includes
27 #include "kgamewin.h"
28 #include "Sprites/backgnd.h"
29 #include "ksirksettings.h"
30 #include "Sprites/animspritesgroup.h"
31 #include "Sprites/cannonsprite.h"
32 #include "Sprites/cavalrysprite.h"
33 #include "Sprites/infantrysprite.h"
34 #include "Sprites/arrowsprite.h"
35 #include "Sprites/skinSpritesData.h"
36 #include "GameLogic/KMessageParts.h"
37 #include "GameLogic/onu.h"
38 #include "fightArena.h"
39
40 // include files for QT
41 #include <QMetaObject>
42 #include <QDir>
43 #include <QPainter>
44 #include <QInputDialog>
45
46 // include files for KDE
47 #include <KMessageBox>
48 #include <KLocalizedString>
49 #include <KConfig>
50 #include <KStandardAction>
51 #include <phonon/mediaobject.h>
52 #include "ksirk_debug.h"
53 #include <KToolBar>
54
55 #include <assert.h>
56
57 namespace Ksirk
58 {
59 using namespace GameLogic;
60
61 /**
62 * Prepares the sprites to be moved : removes the nb necessary sprites from
63 * source, creates the moving sprites and gives them their destination, etc
64 */
initArmiesMovement(unsigned int nbABouger,Country * firstCountry,Country * secondCountry)65 bool KGameWindow::initArmiesMovement(unsigned int nbABouger, Country *firstCountry, Country *secondCountry)
66 {
67 qCDebug(KSIRK_LOG) << "-> " << nbABouger ;
68 KMessageParts messageParts;
69
70 if (firstCountry-> nbArmies() <= nbABouger)
71 {
72 messageParts << I18N_NOOP("Cannot move %1 armies from %2 to %3") << QString::number(nbABouger)
73 << firstCountry->name() << secondCountry->name();
74 broadcastChangeItem(messageParts, ID_STATUS_MSG2, false);
75 return false;
76 }
77 else
78 {
79 messageParts << I18N_NOOP("Moving %1 armies from %2 to %3") << QString::number(nbABouger) << firstCountry->name() << secondCountry->name();
80 broadcastChangeItem(messageParts, ID_STATUS_MSG2);
81 }
82
83 AnimSpritesGroup* newGroup = new AnimSpritesGroup(this,SLOT(slotMovingArmiesArrived(AnimSpritesGroup*)));
84 m_animSpritesGroups.push_back(newGroup);
85 AnimSprite* sprite;
86 if ((firstCountry-> nbArmies() > 10) && (nbABouger == 10)
87 && (nbABouger < firstCountry-> nbArmies()))
88 {
89 sprite = new CannonSprite(m_theWorld->zoom(), backGnd(), 200);
90 firstCountry-> decrNbArmies(10);
91 }
92 else if ((firstCountry-> nbArmies() > 5) && (nbABouger == 5)
93 && (nbABouger < firstCountry-> nbArmies()))
94 {
95 sprite = new CavalrySprite(m_theWorld->zoom(), backGnd(), 200);
96 firstCountry-> decrNbArmies(5);
97 }
98 else if ((firstCountry-> nbArmies() > 1) && (nbABouger == 1)
99 && (nbABouger < firstCountry-> nbArmies()))
100 {
101 sprite = new InfantrySprite(m_theWorld->zoom(), backGnd(), 200);
102 firstCountry-> decrNbArmies();
103 }
104 else
105 {
106 messageParts << I18N_NOOP("Cannot move %1 armies from %2 to %3")
107 << QString::number(nbABouger)
108 << firstCountry->name()
109 << secondCountry->name();
110 broadcastChangeItem(messageParts, ID_STATUS_MSG2, false);
111 return false;
112 }
113 connect(sprite,&AnimSprite::atDestination,this,&KGameWindow::slotMovingArmyArrived);
114 sprite->setupTravel(firstCountry, secondCountry);
115 newGroup->addSprite(sprite);
116 firstCountry-> createArmiesSprites();
117 sprite->setAnimated();
118 // qCDebug(KSIRK_LOG) << "initArmiesMovement returns true";
119 return true;
120 }
121
122
initArmiesMultipleCombat(unsigned int nbA,Country * firstCountry,Country * secondCountry,QPointF dest)123 AnimSprite* KGameWindow::initArmiesMultipleCombat(unsigned int nbA,
124 Country *firstCountry,
125 Country *secondCountry, QPointF dest)
126 {
127 qCDebug(KSIRK_LOG) << "-> " << nbA ;
128 KMessageParts messageParts;
129
130 //AnimSpritesGroup* newGroup = new AnimSpritesGroup(this,SLOT(slotMovingArmiesArrived(AnimSpritesGroup*)));
131 //m_animSpritesGroups.push_back(newGroup);
132 AnimSprite* sprite = 0;
133 if ((!firstCountry->spritesCannons().isEmpty()) && (nbA == 10))
134 {
135 qCDebug(KSIRK_LOG) << "cannon";
136 sprite = new CannonSprite(m_theWorld->zoom(), backGnd(), 200);
137
138 firstCountry->spritesCannons().hideAndRemoveFirst();
139 }
140 else if ((!firstCountry->spritesCavalry().isEmpty()) && (nbA == 5))
141 {
142 qCDebug(KSIRK_LOG) << "cavalry";
143 sprite = new CavalrySprite(m_theWorld->zoom(), backGnd(), 200);
144
145 firstCountry->spritesCavalry().hideAndRemoveFirst();
146 }
147 else if ((!firstCountry->spritesInfantry().isEmpty()) && (nbA == 1))
148 {
149 qCDebug(KSIRK_LOG) << "infantry";
150 sprite = new InfantrySprite(m_theWorld->zoom(), backGnd(), 200);
151
152 firstCountry->spritesInfantry().hideAndRemoveFirst();
153 }
154 // cannot comment out this comment below otherwise will use sprite uninitialized
155 else
156 {
157 messageParts << I18N_NOOP("Cannot move %1 armies from %2 to %3")
158 << QString::number(nbA)
159 << firstCountry->name()
160 << secondCountry->name();
161 broadcastChangeItem(messageParts, ID_STATUS_MSG2, false);
162 return 0;
163 }
164 //connect(sprite,SIGNAL(atDestination(AnimSprite*)),this,SLOT(slotMovingArmyArrived(AnimSprite*)));
165
166 if (backGnd()->bgIsArena())
167 {
168 int relativePosInArena=0;
169
170 if (firstCountry->name() == secondCountry->name())
171 {
172 sprite->setDefendant();
173 relativePosInArena = relativePosInArenaDefense;
174 //relativePosInArenaDefense++;
175 }
176 else
177 {
178 sprite->setAttacker();
179 relativePosInArena = relativePosInArenaAttack;
180 //relativePosInArenaAttack++;
181 }
182
183 qCDebug(KSIRK_LOG) << "****Test relativePos**** " << relativePosInArena;
184 QPointF dep = determinePointDepartArena(firstCountry,relativePosInArena);
185
186 sprite-> setPos(dep);
187
188 qCDebug(KSIRK_LOG) << "****dep point**** " << dep;
189 qCDebug(KSIRK_LOG) << "****dest point**** " << dest;
190 sprite->setupTravel(firstCountry, secondCountry,dep,dest);
191 }
192 else
193 {
194 qCDebug(KSIRK_LOG) << "***************Setup travel normal ************** ";
195 sprite->setupTravel(firstCountry, secondCountry,&dest);
196 }
197
198 qCDebug(KSIRK_LOG) << "-> "<< firstCountry->name() << secondCountry->name();
199 //sprite->setupTravel(firstCountry, secondCountry,&dest);
200 //newGroup->addSprite(sprite);
201 // qCDebug(KSIRK_LOG) << "add a sprite 1";
202 // m_animFighters->addSprite(sprite);
203 //firstCountry-> createArmiesSprites();
204 //sprite->setAnimated();
205 // qCDebug(KSIRK_LOG) << "initArmiesMovement returns true";
206 return sprite;
207 }
208
209
determinePointDepartArena(Country * pays,int relativePos)210 QPointF KGameWindow::determinePointDepartArena(Country *pays, int relativePos)
211 {
212 return( QPointF(pays-> pointInfantry().x()*m_theWorld->zoom(),(pays-> pointInfantry().y()+(1-2*(relativePos%2))*(Sprites::SkinSpritesData::single().intData("infantry-height")+8)*((relativePos+1)/2))*m_theWorld->zoom()));
213 }
214
215
determinePointArrivee(QPointF & pointArriveeAttaquant,QPointF & pointArriveeDefenseur)216 void KGameWindow::determinePointArrivee(
217 QPointF& pointArriveeAttaquant,
218 QPointF& pointArriveeDefenseur)
219 {
220 Country *attackingCountry = firstCountry();
221 Country *defendingCountry = secondCountry();
222 if (attackingCountry == 0 || m_secondCountry == 0)
223 {
224 qCCritical(KSIRK_LOG) << "attackingCountry=" << (void*)attackingCountry << "defendingCountry=" << (void*)defendingCountry;
225 return;
226 }
227
228 // - attacker's flag point
229 qreal pointFlagAttaquantX = attackingCountry-> pointFlag().x()*m_theWorld->zoom();
230
231 // - defender's flag point
232 qreal pointFlagDefenseurX;
233 pointFlagDefenseurX = defendingCountry-> pointFlag().x()*m_theWorld->zoom();
234
235 // - attacker's arrival point (resp. defender's) (left or right of the
236 // defender's flag point depending on the attacker's flag point position)
237 qreal pointArriveeAttaquantX;
238 qreal pointArriveeY;
239 qreal pointArriveeDefenseurX;
240 qreal pointDepartAttaquantX;
241 qreal pointDepartDefenseurX;
242 qreal pointDepartAttaquantY;
243 qreal pointDepartDefenseurY;
244 qreal leftRelativePos;
245
246 if (!attackingCountry->spritesInfantry().isEmpty())
247 {
248 // We must know
249 // - attacker's departure point (pointInfantry)
250 pointDepartAttaquantX = attackingCountry-> pointInfantry().x()*m_theWorld->zoom();
251 pointDepartAttaquantY = attackingCountry-> pointInfantry().y()*m_theWorld->zoom();
252 }
253 else if (!attackingCountry->spritesCavalry().isEmpty())
254 {
255 // We must know
256 // - attacker's departure point (pointCavalry)
257 pointDepartAttaquantX = attackingCountry-> pointCavalry().x()*m_theWorld->zoom();
258 pointDepartAttaquantY = attackingCountry-> pointCavalry().y()*m_theWorld->zoom();
259 }
260 else
261 {
262 // We must know
263 // - attacker's departure point (pointCannon)
264 pointDepartAttaquantX = attackingCountry-> pointCannon().x()*m_theWorld->zoom();
265 pointDepartAttaquantY = attackingCountry-> pointCannon().y()*m_theWorld->zoom();
266 }
267
268 if (!defendingCountry->spritesInfantry().isEmpty())
269 {
270 // - defender's departure point (pointInfantry)
271 pointDepartDefenseurX = defendingCountry-> pointInfantry().x()*m_theWorld->zoom();
272 pointDepartDefenseurY = defendingCountry-> pointInfantry().y()*m_theWorld->zoom();
273
274 }
275 else if (!defendingCountry->spritesCavalry().isEmpty())
276 {
277 // - defender's departure point (pointCavalry)
278 pointDepartDefenseurX = defendingCountry-> pointCavalry().x()* m_theWorld->zoom();
279 pointDepartDefenseurY = defendingCountry-> pointCavalry().y()* m_theWorld->zoom();
280 }
281 else
282 {
283 // - defender's departure point (pointCannon)
284 pointDepartDefenseurX = defendingCountry-> pointCannon().x()* m_theWorld->zoom();
285 pointDepartDefenseurY = defendingCountry-> pointCannon().y()* m_theWorld->zoom();
286 }
287
288 // vertical meet point
289 int diff = Sprites::SkinSpritesData::single().intData("flag-height") - Sprites::SkinSpritesData::single().intData("cannon-height");
290 pointArriveeY = (((defendingCountry-> pointFlag().y() + diff))* m_theWorld->zoom()) ;
291
292 qCDebug(KSIRK_LOG) << "2 " << defendingCountry-> pointFlag().y() << pointArriveeY;
293 if (!attackingCountry->communicateWith(defendingCountry))
294 {
295 qCCritical(KSIRK_LOG) << "Error in KGameWindow::determinePointDepartArena: " << attackingCountry-> name() << " and "
296 << defendingCountry-> name() << " do not communicate!";
297 exit(2);
298 }
299
300 // If the flag of the attacker is to the left of the flag of the defender,
301 // then the arriving point of the attacker is to the left, else it is to
302 // the right
303 // The situation is reversed if the one of the attacker will need to go
304 // through the world limit
305 if (!attackingCountry->spritesInfantry().isEmpty())
306 {
307 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("infantry-width"))*m_theWorld->zoom();
308 }
309 else
310 {
311 if (!attackingCountry->spritesCavalry().isEmpty())
312 {
313 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("cavalry-width"))*m_theWorld->zoom();
314 }
315 else
316 {
317 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("cannon-width"))*m_theWorld->zoom();
318 }
319 }
320
321 qreal rightRelativePos = (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("flag-width"))*m_theWorld->zoom();
322
323 if (!(qAbs(pointFlagAttaquantX-pointFlagDefenseurX) > (backGnd()-> boundingRect().width() / 2)))
324 {
325 if ( pointFlagAttaquantX <= pointFlagDefenseurX )
326 {
327 pointArriveeAttaquantX = pointFlagDefenseurX + leftRelativePos;
328 pointArriveeDefenseurX = pointFlagDefenseurX + rightRelativePos;
329 }
330 else
331 {
332 pointArriveeAttaquantX = pointFlagDefenseurX + rightRelativePos;
333 pointArriveeDefenseurX = pointFlagDefenseurX + leftRelativePos;
334 }
335 }
336 else
337 {
338 if ( pointFlagAttaquantX <= pointFlagDefenseurX )
339 {
340 pointArriveeAttaquantX = pointFlagDefenseurX + rightRelativePos;
341 pointArriveeDefenseurX = pointFlagDefenseurX + leftRelativePos;
342 }
343 else
344 {
345 pointArriveeAttaquantX = pointFlagDefenseurX +leftRelativePos;
346 pointArriveeDefenseurX = pointFlagDefenseurX + rightRelativePos;
347 }
348 }
349
350 pointArriveeAttaquant.setX(pointArriveeAttaquantX);
351 pointArriveeAttaquant.setY(pointArriveeY);
352 pointArriveeDefenseur.setX(pointArriveeDefenseurX);
353 pointArriveeDefenseur.setY(pointArriveeY);
354 qCDebug(KSIRK_LOG) << "3: " << pointArriveeAttaquant << " ; " << pointArriveeDefenseur;
355 }
356
357
358 // point arriveedefenseur inutile
determinePointArriveeForArena(int relative,QPointF & pointArriveeAttaquant,QPointF & pointArriveeDefenseur)359 void KGameWindow::determinePointArriveeForArena(
360 int relative,
361 QPointF& pointArriveeAttaquant,
362 QPointF& pointArriveeDefenseur)
363 {
364 Country *attackingCountry = firstCountry();
365 Country *defendingCountry = secondCountry();
366
367 qCDebug(KSIRK_LOG) << m_firstCountry->name() << "("<<(void*)m_firstCountry<<")"
368 << m_secondCountry->name() << "("<<(void*)m_secondCountry<<")";
369 qCDebug(KSIRK_LOG) << attackingCountry->name() << "("<<(void*)attackingCountry<<")"
370 << defendingCountry->name() << "("<<(void*)defendingCountry<<")"
371 << relative << pointArriveeAttaquant << pointArriveeDefenseur;
372 // - attacker's flag point
373 qreal pointFlagAttaquantX = attackingCountry-> pointFlag().x()*m_theWorld->zoom();
374
375 // in case of arena, the meet will be between the two countries
376 qreal pointFlagDefenseurX = backGnd()-> boundingRect().width() / 2;
377
378 // - attacker's arrival point (resp. defender's) (left or right of the
379 // defender's flag point depending on the attacker's flag point position)
380 qreal pointArriveeAttaquantX;
381 qreal pointArriveeY;
382 qreal pointArriveeDefenseurX;
383 qreal pointDepartAttaquantX;
384 qreal pointDepartDefenseurX;
385 qreal pointDepartAttaquantY;
386 qreal pointDepartDefenseurY;
387 qreal leftRelativePos;
388
389 qCDebug(KSIRK_LOG) << "1" << attackingCountry->spritesInfantry().isEmpty()
390 << attackingCountry->name()
391 << attackingCountry->nbArmies()
392 << attackingCountry->owner()->name()
393 << ((attackingCountry->name()==m_firstCountry->name())?attackingCountry->owner()->getNbAttack():attackingCountry->owner()->getNbDefense());
394
395 if ( attackingCountry->nbArmies()%5 != 0
396 && ((attackingCountry->nbArmies() % 5) >= ((attackingCountry->name()==m_firstCountry->name())?attackingCountry->owner()->getNbAttack():attackingCountry->owner()->getNbDefense())))
397 // if (!attackingCountry->spritesInfantry().isEmpty()
398 // && ((attackingCountry->nbArmies() % 5) >= ((attackingCountry==m_firstCountry)?attackingCountry->owner()->getNbAttack():attackingCountry->owner()->getNbDefense())))
399 {
400 qCDebug(KSIRK_LOG) << "infantry" << attackingCountry->name() << relative;
401 // We must know
402 // - attacker's departure point (pointInfantry)
403 QPointF dep = determinePointDepartArena(attackingCountry, relative);
404 qCDebug(KSIRK_LOG) << "dep=" << dep;
405 pointDepartAttaquantX = dep.x();
406 pointDepartAttaquantY = dep.y();
407 }
408 else if (!attackingCountry->spritesCavalry().isEmpty())
409 {
410 qCDebug(KSIRK_LOG) << "cavalry" << attackingCountry->pointCavalry();
411 // We must know
412 // - attacker's departure point (pointCavalry)
413 pointDepartAttaquantX = attackingCountry-> pointCavalry().x()*m_theWorld->zoom();
414 pointDepartAttaquantY = attackingCountry-> pointCavalry().y()*m_theWorld->zoom();
415 }
416 else
417 {
418 qCDebug(KSIRK_LOG) << "cannon" << attackingCountry->pointCannon();
419 // We must know
420 // - attacker's departure point (pointCannon)
421 pointDepartAttaquantX = attackingCountry-> pointCannon().x()*m_theWorld->zoom();
422 pointDepartAttaquantY = attackingCountry-> pointCannon().y()*m_theWorld->zoom();
423 }
424
425 // problem here: when handling defender, attacker sprites are already moving so
426 // if it has no more infantry sprites, it will not be able to enter this loop
427 // solution: use the number of armies (equivalent to the number of stationary
428 // sprites) and not the number of infantry sprites
429 // defendingCountry->spritesInfantry().isEmpty() === (defendingCountry->nbArmies()%5==0)
430 unsigned int num = 0;
431 if (defendingCountry->name()==m_firstCountry->name())
432 {
433 num = defendingCountry->owner()->getNbAttack();
434 }
435 else
436 {
437 num = defendingCountry->owner()->getNbDefense();
438 }
439 qCDebug(KSIRK_LOG) << "2"
440 << defendingCountry->spritesInfantry().isEmpty()
441 << defendingCountry->name()
442 << defendingCountry->nbArmies()
443 << defendingCountry->owner()->name()
444 << num;
445 if ( defendingCountry->nbArmies()%5 != 0
446 && ((defendingCountry->nbArmies() % 5) >= num)
447 )
448 // if (!defendingCountry->spritesInfantry().isEmpty()
449 // && ((defendingCountry->nbArmies() % 5) >= defendingCountry->owner()->getNbDefense()))
450 {
451 qCDebug(KSIRK_LOG) << "infantry" << defendingCountry->name() << relative;
452 // - defender's departure point (pointInfantry)
453 QPointF dep = determinePointDepartArena(defendingCountry, relative);
454 pointDepartDefenseurX = dep.x();
455 pointDepartDefenseurY = dep.y();
456 qCDebug(KSIRK_LOG) << "dep=" << dep;
457 }
458 else if (!defendingCountry->spritesCavalry().isEmpty())
459 {
460 qCDebug(KSIRK_LOG) << "cavalry" << defendingCountry->pointCavalry();
461 // - defender's departure point (pointCavalry)
462 pointDepartDefenseurX = defendingCountry-> pointCavalry().x()*m_theWorld->zoom();
463 pointDepartDefenseurY = defendingCountry-> pointCavalry().y()*m_theWorld->zoom();
464 }
465 else
466 {
467 qCDebug(KSIRK_LOG) << "cannon" << defendingCountry->pointCannon();
468 // - defender's departure point (pointCannon)
469 pointDepartDefenseurX = defendingCountry-> pointCannon().x()*m_theWorld->zoom();
470 pointDepartDefenseurY = defendingCountry-> pointCannon().y()*m_theWorld->zoom();
471 }
472
473
474 // vertical meet point
475 // in case of arena, the vertical meet will be as soon as it's possible
476 qCDebug(KSIRK_LOG) << "Argh " << pointDepartAttaquantY << pointDepartDefenseurY;
477 pointArriveeY = (pointDepartAttaquantY+pointDepartDefenseurY)/2;
478
479 if (!attackingCountry->communicateWith(defendingCountry))
480 {
481 qCCritical(KSIRK_LOG) << "Error in KGameWindow::determinePointArriveeForArena: " << attackingCountry-> name() << " and "
482 << defendingCountry-> name() << " do not communicate!";
483 exit(2);
484 }
485 // If the flag of the attacker is to the left of the flag of the defender,
486 // then the arriving point of the attacker is to the left, else it is to
487 // the right
488 // The situation is reversed if the one of the attacker will need to go
489 // through the world limit
490
491 qreal factor = 0;
492 if (!attackingCountry->spritesInfantry().isEmpty()
493 && ((attackingCountry->nbArmies() % 5) >= attackingCountry->owner()->getNbAttack()))
494 {
495 factor = Sprites::SkinSpritesData::single().intData("infantry-width");
496 }
497 else if (!attackingCountry->spritesCavalry().isEmpty())
498 {
499 factor = Sprites::SkinSpritesData::single().intData("cavalry-width");
500 }
501 else
502 {
503 factor = Sprites::SkinSpritesData::single().intData("cannon-width");
504 }
505 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + factor)*m_theWorld->zoom();
506
507
508 qreal rightRelativePos = (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("flag-width"))*m_theWorld->zoom();
509
510 //if (!(qAbs(pointFlagAttaquantX-pointFlagDefenseurX) > (backGnd()-> boundingRect().width() / 2)) || backGnd()->bgIsArena())
511 //{
512 if ( pointFlagAttaquantX <= pointFlagDefenseurX )
513 {
514 pointArriveeAttaquantX = pointFlagDefenseurX + leftRelativePos;
515 pointArriveeDefenseurX = pointFlagDefenseurX + rightRelativePos;
516 }
517 else
518 {
519 pointArriveeAttaquantX = pointFlagDefenseurX + rightRelativePos;
520 pointArriveeDefenseurX = pointFlagDefenseurX + leftRelativePos;
521 }
522
523 pointArriveeAttaquant.setX(pointArriveeAttaquantX);
524 pointArriveeAttaquant.setY(pointArriveeY);
525 pointArriveeDefenseur.setX(pointArriveeDefenseurX);
526 pointArriveeDefenseur.setY(pointArriveeY);
527 qCDebug(KSIRK_LOG) << "Done: " << pointArriveeAttaquant << " ; " << pointArriveeDefenseur;
528 }
529
530
initCombatMovement()531 void KGameWindow::initCombatMovement()
532 {
533 qCDebug(KSIRK_LOG) << "1";
534
535 Country *attackingCountry = firstCountry();
536 Country *defendingCountry = secondCountry();
537 if (attackingCountry == 0 || defendingCountry == 0)
538 {
539 qCCritical(KSIRK_LOG) << "attackingCountry=" << (void*)attackingCountry << "defendingCountry=" << (void*)defendingCountry;
540 return;
541 }
542
543 getRightDialog()->close();
544 if (firstCountry() != 0 && firstCountry()->owner() != 0
545 && secondCountry() != 0 && secondCountry()->owner() != 0)
546 {
547 getRightDialog()->displayFightDetails(firstCountry(), secondCountry(),
548 firstCountry()->owner()->getNbAttack(), secondCountry()->owner()->getNbDefense());
549 }
550 centerOnFight(); //center the game on the fight
551 if (isArena()
552 && ! m_automaton->currentPlayer()->isAI()
553 && !m_automaton->currentPlayer()->isVirtual())
554 {
555 qCDebug(KSIRK_LOG) << "Attack with arena";
556 // init and display the arena view
557 showArena();
558 }
559
560 m_animFighters->clear();
561 m_animFighters->changeTarget(this,SLOT(slotMovingFightersArrived(AnimSpritesGroup*)));
562
563 QString sndRoulePath;
564 AnimSprite* defenderSprite = 0;
565 AnimSprite* attackingSprite = 0;
566
567 QPointF pointArriveeAttaquant(0,0);
568 QPointF pointArriveeDefenseur(0,0);
569
570 if (backGnd()->bgIsArena())
571 {
572 determinePointArriveeForArena(0,
573 pointArriveeAttaquant, pointArriveeDefenseur);
574 }
575 else
576 {
577 determinePointArrivee(pointArriveeAttaquant, pointArriveeDefenseur);
578 }
579
580 if (!attackingCountry->spritesInfantry().isEmpty()
581 && ((attackingCountry->nbArmies() % 5) >= attackingCountry->owner()->getNbAttack())
582 && backGnd()->bgIsArena())
583 {
584 qCDebug(KSIRK_LOG) << "**NB-ATTACK**" << attackingCountry->owner()->getNbAttack();
585
586 for (unsigned int i=0; i < attackingCountry->owner()->getNbAttack() ; i++)
587 {
588 attackingSprite = simultaneousAttack(1,KGameWindow::Attack);
589 if (attackingSprite != 0)
590 {
591 qCDebug(KSIRK_LOG) << "add a sprite";
592 m_animFighters->addSprite(attackingSprite);
593 }
594 }
595 nbSpriteAttacking=attackingCountry->owner()->getNbAttack();
596 }
597 else
598 {
599 qCDebug(KSIRK_LOG) << "creating attackingSprite"
600 << attackingCountry->name()
601 << attackingCountry->nbArmies()
602 << attackingCountry->spritesInfantry().size()
603 << attackingCountry->spritesCavalry().size()
604 << attackingCountry->spritesCannons().size();
605 nbSpriteAttacking=1;
606 if ( (attackingCountry->nbArmies() != 0)
607 && attackingCountry->spritesInfantry().isEmpty()
608 && attackingCountry->spritesCavalry().isEmpty()
609 && attackingCountry->spritesCannons().isEmpty())
610 {
611 attackingCountry->createArmiesSprites();
612 }
613 if (!attackingCountry->spritesCavalry().isEmpty())
614 {
615 qCDebug(KSIRK_LOG) << "cavalry" << backGnd()->bgIsArena();
616 if (backGnd()->bgIsArena())
617 {
618 attackingSprite = simultaneousAttack(5,KGameWindow::Attack);
619 }
620 else
621 {
622 attackingSprite = new CavalrySprite(m_theWorld->zoom(), backGnd(), 200);
623
624 attackingCountry->spritesCavalry().hideAndRemoveFirst();
625 }
626 }
627 else if (!attackingCountry->spritesCannons().isEmpty())
628 {
629 qCDebug(KSIRK_LOG) << "cannon" << backGnd()->bgIsArena();
630 if (backGnd()->bgIsArena())
631 {
632 attackingSprite = simultaneousAttack(10,KGameWindow::Attack);
633 }
634 else
635 {
636 attackingSprite = new CannonSprite(m_theWorld->zoom(), backGnd(), 200);
637
638 attackingCountry->spritesCannons().hideAndRemoveFirst();
639 }
640 }
641 else if (!attackingCountry->spritesInfantry().isEmpty())
642 {
643 qCDebug(KSIRK_LOG) << "infantry" << backGnd()->bgIsArena();
644 if (backGnd()->bgIsArena())
645 {
646 attackingSprite = simultaneousAttack(1,KGameWindow::Attack);
647 }
648 else
649 {
650 attackingSprite = new InfantrySprite(m_theWorld->zoom(), backGnd(), 200);
651
652 attackingCountry->spritesInfantry().hideAndRemoveFirst();
653 }
654 }
655 else
656 {
657 qCDebug(KSIRK_LOG) << "No sprite on attacking country!";
658 assert(false);
659 }
660
661 if (attackingSprite == 0)
662 {
663 qCCritical(KSIRK_LOG) << "ERROR: null attackingSprite at " << __FILE__ << ", line " << __LINE__;
664 // return;
665 }
666 else
667 {
668 qCDebug(KSIRK_LOG) << "attackingSprite created";
669
670 // Adding the number of attackers decoration
671 if (attackingCountry->owner()->getNbAttack() == 1)
672 {
673 attackingSprite->addDecoration("mark1", QRect(0,0,10,10));
674 }
675 else if (attackingCountry->owner()->getNbAttack() == 2)
676 {
677 attackingSprite->addDecoration("mark2", QRect(0,0,10,10));
678 }
679 else
680 {
681 attackingSprite->addDecoration("mark3", QRect(0,0,10,10));
682 }
683
684 attackingSprite-> setAttacker();
685 attackingSprite->setupTravel(attackingCountry, defendingCountry, &pointArriveeAttaquant);
686 qCDebug(KSIRK_LOG) << "add a sprite 2";
687 m_animFighters->addSprite(attackingSprite);
688 //(pointDepartAttaquantX <= pointArriveeAttaquantX) ? attackingSprite-> setLookRight() : attackingSprite-> setLookLeft();
689 sndRoulePath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/roll.wav");
690 if (sndRoulePath.isNull())
691 {
692 KMessageBox::error(0, i18n("Sound roule not found - Verify your installation<br>Program cannot continue"), i18n("Error!"));
693 exit(2);
694 }
695 if (KsirkSettings::soundEnabled())
696 {
697 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndRoulePath));
698 m_audioPlayer->play();
699 }
700 }
701 }
702
703 if (!defendingCountry->spritesInfantry().isEmpty()
704 && ((defendingCountry->nbArmies() % 5) >= defendingCountry->owner()->getNbDefense())
705 && backGnd()->bgIsArena())
706 {
707 qCDebug(KSIRK_LOG) << "**NB-DEFENSE**" << defendingCountry->owner()->getNbDefense();
708
709 for (unsigned int i=0;i< defendingCountry->owner()->getNbDefense();i++)
710 {
711 defenderSprite = simultaneousAttack(1,KGameWindow::Defense);
712 if (defenderSprite != 0)
713 {
714 qCDebug(KSIRK_LOG) << "add a sprite";
715 m_animFighters->addSprite(defenderSprite);
716 }
717 }
718 nbSpriteDefending=defendingCountry->owner()->getNbDefense();
719 }
720 else
721 {
722 qCDebug(KSIRK_LOG) << "creating defenderSprite" << defendingCountry->name()
723 << defendingCountry->nbArmies()
724 << defendingCountry->spritesInfantry().size()
725 << defendingCountry->spritesCavalry().size()
726 << defendingCountry->spritesCannons().size();
727 nbSpriteDefending=1;
728 if ( (defendingCountry->nbArmies() != 0)
729 && defendingCountry->spritesInfantry().isEmpty()
730 && defendingCountry->spritesCavalry().isEmpty()
731 && defendingCountry->spritesCannons().isEmpty())
732 {
733 defendingCountry->createArmiesSprites();
734 }
735 if (!defendingCountry->spritesCavalry().isEmpty())
736 {
737 qCDebug(KSIRK_LOG) << "cavalry" << backGnd()->bgIsArena();
738 if (backGnd()->bgIsArena())
739 {
740 defenderSprite = simultaneousAttack(5,KGameWindow::Defense);
741 }
742 else
743 {
744 defenderSprite = new CavalrySprite(m_theWorld->zoom(), backGnd(), 200);
745
746 defendingCountry->spritesCavalry().hideAndRemoveFirst();
747 }
748 }
749 else if (!defendingCountry->spritesCannons().isEmpty())
750 {
751 qCDebug(KSIRK_LOG) << "cannon" << backGnd()->bgIsArena();
752 if (backGnd()->bgIsArena())
753 {
754 defenderSprite = simultaneousAttack(10,KGameWindow::Defense);
755 }
756 else
757 {
758 defenderSprite = new CannonSprite(m_theWorld->zoom(), backGnd(), 200);
759
760 defendingCountry->spritesCannons().hideAndRemoveFirst();
761 }
762 }
763 else if (!defendingCountry->spritesInfantry().isEmpty())
764 {
765 qCDebug(KSIRK_LOG) << "infantry" << backGnd()->bgIsArena();
766 if (backGnd()->bgIsArena())
767 {
768 defenderSprite = simultaneousAttack(1,KGameWindow::Defense);
769 }
770 else
771 {
772 defenderSprite = new InfantrySprite(m_theWorld->zoom(), backGnd(), 200);
773
774 defendingCountry->spritesInfantry().hideAndRemoveFirst();
775 }
776 }
777 else
778 {
779 qCCritical(KSIRK_LOG) << "No sprite on defending country" << defendingCountry->name();
780 return;
781 }
782
783 if (defenderSprite==0)
784 {
785 qCCritical(KSIRK_LOG) << "ERROR: null defenderSprite at " << __FILE__ << ", line " << __LINE__;
786 return;
787 }
788 else
789 {
790 if (defendingCountry->owner()->getNbDefense() == 1)
791 {
792 defenderSprite->addDecoration("mark1", QRect(0,0,10,10));
793 }
794 else if (defendingCountry->owner()->getNbDefense() == 2)
795 {
796 defenderSprite->addDecoration("mark2", QRect(0,0,10,10));
797 }
798
799 defenderSprite-> setDefendant();
800 defenderSprite-> setupTravel(defendingCountry, defendingCountry, &pointArriveeDefenseur);
801 //(pointDepartDefenseurX <= pointArriveeDefenseurX) ? defenderSprite-> setLookRight() : defenderSprite-> setLookLeft();
802 qCDebug(KSIRK_LOG) << "add a sprite 3";
803 m_animFighters->addSprite(defenderSprite);
804 sndRoulePath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/roll.wav");
805 if (sndRoulePath.isNull())
806 {
807 KMessageBox::error(0, i18n("Sound roule not found - Verify your installation<br>Program cannot continue"), i18n("Error!"));
808 exit(2);
809 }
810 if (KsirkSettings::soundEnabled())
811 {
812 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndRoulePath));
813 m_audioPlayer->play();
814 }
815 }
816 }
817 qCDebug(KSIRK_LOG) << "Done";
818 }
819
animCombat()820 void KGameWindow::animCombat()
821 {
822 qCDebug(KSIRK_LOG);
823 m_animFighters->changeTarget(this, SLOT(slotFiringFinished(AnimSpritesGroup*)));
824
825 AnimSpritesGroup::iterator it, it_end;
826 it = m_animFighters->begin(); it_end = m_animFighters->end();
827 for (; it != it_end; it++)
828 {
829 qCDebug(KSIRK_LOG) << "a sprite position: " << (*it)->pos();
830 AnimSprite* sprite = (*it);
831 sprite-> changeSequence("firing");
832
833 qreal firingRelativePos = (Sprites::SkinSpritesData::single().intData("cannon-width") - Sprites::SkinSpritesData::single().intData("firing-width"))*m_theWorld->zoom();
834 if (sprite-> looksToLeft())
835 {
836 sprite-> setPos(
837 sprite-> x() + firingRelativePos,
838 sprite-> y() );
839 }
840 else
841 {
842 sprite-> setPos(
843 sprite-> x() + firingRelativePos,
844 sprite-> y() );
845 }
846 sprite->setAnimated(1);
847
848 QString sndCanonPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/cannon.wav");
849 if (sndCanonPath.isNull())
850 {
851 KMessageBox::error(0,
852 i18n("Sound cannon not found - Verify your installation<br>Program cannot continue"), i18n("Error!"));
853 exit(2);
854 }
855 if (KsirkSettings::soundEnabled())
856 {
857 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCanonPath));
858 m_audioPlayer->play();
859 }
860 }
861 }
862
863 // not currently in use
stopCombat()864 void KGameWindow::stopCombat()
865 {
866 /*
867 // qCDebug(KSIRK_LOG);
868 AnimSpritesGroup::iterator it = m_animFighters->begin();
869 while (it != m_animFighters->end())
870 {
871 CannonSprite* sprite = (CannonSprite*)(*it);
872 // it = m_animFighters.remove(it);
873 sprite-> changeSequence("cannon");
874
875 if (sprite-> looksToLeft())
876 {
877 sprite-> setPos(
878 sprite->x() + (Sprites::SkinSpritesData::single().intData("cannon-width"))*m_theWorld->zoom(),
879 sprite-> y() );
880 }
881 else
882 {
883 // sprite-> setX(sprite-> x());
884 }
885 }*/
886 }
887
888
animExplosion(int who)889 void KGameWindow::animExplosion(int who)
890 {
891 qCDebug(KSIRK_LOG) << who;
892 Country *attackingCountry = firstCountry();
893
894 m_animFighters->changeTarget(this, SLOT(slotExplosionFinished(AnimSpritesGroup*)));
895
896 qCDebug(KSIRK_LOG) << m_animFighters->size() << " fighters";
897 unsigned int toArrive = 0;
898 AnimSpritesGroup::iterator it = m_animFighters->begin();
899 for (;it != m_animFighters->end();it++)
900 {
901 AnimSprite* sprite = (AnimSprite*)(*it);
902 if ( (who == 2) // both are killed
903 || ((who == 0) && (sprite-> isAttacker())) // Attacker is killed
904 || ((who == 1) && (sprite-> isDefendant())) ) // Defender is killed
905 {
906 sprite-> changeSequence("exploding");
907
908 qreal firingRelativePos = (Sprites::SkinSpritesData::single().intData("firing-width") - Sprites::SkinSpritesData::single().intData("exploding-width"))*m_theWorld->zoom();
909 if (sprite-> looksToLeft())
910 {
911 sprite-> setPos(
912 sprite-> x() + firingRelativePos,
913 sprite-> y() );
914 }
915
916 if (sprite->isAttacker())
917 {
918 qCDebug(KSIRK_LOG) << " removing a sprite";
919 //qCDebug(KSIRK_LOG) << "i attack" << i;
920 qCDebug(KSIRK_LOG) << "NKA" << NKA;
921
922 sprite->setAnimated(NKA);
923
924 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
925 if (sndCrashPath.isNull())
926 {
927 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
928 exit(2);
929 }
930 if (KsirkSettings::soundEnabled())
931 {
932 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
933 m_audioPlayer->play();
934 }
935 }
936
937 if (sprite->isDefendant())
938 {
939 qCDebug(KSIRK_LOG) << " removing a sprite";
940 qCDebug(KSIRK_LOG) << "NKD" << NKD;
941
942 sprite->setAnimated(NKD);
943
944 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
945 if (sndCrashPath.isNull())
946 {
947 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
948 exit(2);
949 }
950 if (KsirkSettings::soundEnabled())
951 {
952 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
953 m_audioPlayer->play();
954 }
955 }
956 }
957 else // the sprite is not the one (or one the several) killed
958 {
959 qCDebug(KSIRK_LOG) << " keeping a sprite";
960
961 if ((attackingCountry->nbArmies() % 10) == 0)
962 {
963 sprite-> changeSequence("cannon");
964 }
965 else if ((attackingCountry->nbArmies() % 5) == 0)
966 {
967 sprite-> changeSequence("cavalry");
968 }
969 else
970 {
971 sprite-> changeSequence("infantry");
972 }
973
974 sprite->setStatic();
975 toArrive++;
976 }
977 }
978 for (unsigned int i=0; i < toArrive; i++)
979 {
980 m_animFighters->oneArrived(0);
981 }
982 qCDebug(KSIRK_LOG) << "loop done";
983
984 /*QString sndCrashPath = m_dirs-> findResource("appdata", m_automaton->skin() + "/Sounds/crash.wav");
985 if (sndCrashPath.isNull())
986 {
987 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
988 exit(2);
989 }
990 if (KsirkSettings::soundEnabled())
991 {
992 m_audioPlayer->setCurrentSource(sndCrashPath);
993 m_audioPlayer->play();
994 }*/
995 qCDebug(KSIRK_LOG) << "finished";
996 // m_frame-> initTimer();
997 // qCDebug(KSIRK_LOG)<<"OUT";
998 }
999
1000
animExplosionForArena()1001 void KGameWindow::animExplosionForArena()
1002 {
1003 qCDebug(KSIRK_LOG);
1004
1005 Country *attackingCountry = firstCountry();
1006 Country *defendingCountry = secondCountry();
1007
1008 m_animFighters->changeTarget(this, SLOT(slotExplosionFinished(AnimSpritesGroup*)));
1009
1010 qCDebug(KSIRK_LOG) << "hidden; " << m_animFighters->size() << " fighters";
1011 AnimSpritesGroup::iterator it = m_animFighters->begin();
1012
1013 int nbAttackerSurvivor=attackingCountry->owner()->getNbAttack()-NKA;
1014 int nbDefenderSurvivor=defendingCountry->owner()->getNbDefense()-NKD;
1015
1016 qCDebug(KSIRK_LOG) << "nbAttackerSurvivor" << nbAttackerSurvivor;
1017 qCDebug(KSIRK_LOG) << "nbDefenderSurvivor" << nbDefenderSurvivor;
1018 qCDebug(KSIRK_LOG) << "nbSpriteAttacking" << nbSpriteAttacking;
1019 qCDebug(KSIRK_LOG) << "nbSpriteDefending" << nbSpriteDefending;
1020
1021 for (int nbSpriteTreated=0;
1022 (nbSpriteTreated < nbSpriteAttacking)&&(it!=m_animFighters->end());
1023 it++,nbSpriteTreated++)
1024 {
1025 AnimSprite* sprite = (AnimSprite*)(*it);
1026
1027 qCDebug(KSIRK_LOG) << "nbSpriteTreated" << nbSpriteTreated;
1028
1029 if(dynamic_cast<CannonSprite*>(sprite) != 0)
1030 {
1031 qCDebug(KSIRK_LOG) << "*******CANNON*******";
1032
1033 if (NKA>0)
1034 {
1035 sprite-> changeSequence("exploding");
1036
1037 if (sprite->isAttacker())
1038 {
1039 qCDebug(KSIRK_LOG) << " removing a sprite";
1040 //qCDebug(KSIRK_LOG) << "i attack" << i;
1041 qCDebug(KSIRK_LOG) << "NKA" << NKA;
1042
1043 sprite->setAnimated(NKA);
1044
1045 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
1046 if (sndCrashPath.isNull())
1047 {
1048 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
1049 exit(2);
1050 }
1051 if (KsirkSettings::soundEnabled())
1052 {
1053 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
1054 m_audioPlayer->play();
1055 }
1056 }
1057 }
1058 else
1059 {
1060 sprite-> changeSequence("cannon");
1061
1062 sprite->setStatic();
1063 m_animFighters->oneArrived(0);
1064 }
1065 }
1066 else if(dynamic_cast<CavalrySprite*>(sprite) != NULL)
1067 {
1068 qCDebug(KSIRK_LOG) << "*******CAVALIER*******";
1069
1070 if (NKA>0)
1071 {
1072 sprite-> changeSequence("exploding");
1073
1074 if (sprite->isAttacker())
1075 {
1076 qCDebug(KSIRK_LOG) << " removing a sprite";
1077 //qCDebug(KSIRK_LOG) << "i attack" << i;
1078 qCDebug(KSIRK_LOG) << "NKA" << NKA;
1079
1080 sprite->setAnimated(NKA);
1081
1082 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
1083 if (sndCrashPath.isNull())
1084 {
1085 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
1086 exit(2);
1087 }
1088 if (KsirkSettings::soundEnabled())
1089 {
1090 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
1091 m_audioPlayer->play();
1092 }
1093 }
1094 }
1095 else
1096 {
1097 sprite-> changeSequence("cavalry");
1098
1099 sprite->setStatic();
1100 m_animFighters->oneArrived(0);
1101 }
1102 }
1103 else
1104 {
1105 if (nbAttackerSurvivor<=0)
1106 {
1107 sprite-> changeSequence("exploding");
1108
1109 if (sprite->isAttacker())
1110 {
1111 qCDebug(KSIRK_LOG) << " removing a sprite";
1112 //qCDebug(KSIRK_LOG) << "i attack" << i;
1113 qCDebug(KSIRK_LOG) << "NKA" << NKA;
1114
1115 sprite->setAnimated(1);
1116
1117 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
1118 if (sndCrashPath.isNull())
1119 {
1120 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
1121 exit(2);
1122 }
1123 if (KsirkSettings::soundEnabled())
1124 {
1125 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
1126 m_audioPlayer->play();
1127 }
1128 }
1129 }
1130 else
1131 {
1132 qCDebug(KSIRK_LOG) << " keeping a sprite";
1133 if (sprite->isAttacker())
1134 {
1135 sprite-> changeSequence("infantry");
1136
1137 nbAttackerSurvivor--;
1138 }
1139 sprite->setStatic();
1140 m_animFighters->oneArrived(0);
1141 }
1142 }
1143 }
1144
1145 qCDebug(KSIRK_LOG) << " loop done attack";
1146
1147 for (int nbSpriteTreated=0;
1148 (nbSpriteTreated < nbSpriteDefending)&&(it!=m_animFighters->end());
1149 it++,nbSpriteTreated++)
1150 {
1151 qCDebug(KSIRK_LOG) << "nbSpriteTreated" << nbSpriteTreated;
1152
1153 AnimSprite* sprite = (AnimSprite*)(*it);
1154
1155 if(dynamic_cast<CannonSprite*>(sprite) != NULL)
1156 {
1157 qCDebug(KSIRK_LOG) << "*******CANNON*******";
1158
1159 if (NKD>0)
1160 {
1161 sprite-> changeSequence("exploding");
1162
1163 if (sprite->isDefendant())
1164 {
1165 qCDebug(KSIRK_LOG) << " removing a sprite";
1166 //qCDebug(KSIRK_LOG) << "i attack" << i;
1167 qCDebug(KSIRK_LOG) << "NKD" << NKD;
1168
1169 sprite->setAnimated(NKD);
1170
1171 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
1172 if (sndCrashPath.isNull())
1173 {
1174 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
1175 exit(2);
1176 }
1177 if (KsirkSettings::soundEnabled())
1178 {
1179 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
1180 m_audioPlayer->play();
1181 }
1182 }
1183 }
1184 else
1185 {
1186 sprite-> changeSequence("cannon");
1187
1188 sprite->setStatic();
1189 m_animFighters->oneArrived(0);
1190 }
1191 }
1192 else if(dynamic_cast<CavalrySprite*>(sprite) != NULL)
1193 {
1194 qCDebug(KSIRK_LOG) << "*******CAVALIER*******";
1195
1196 if (NKD>0)
1197 {
1198 sprite-> changeSequence("exploding");
1199
1200 if (sprite->isDefendant())
1201 {
1202 qCDebug(KSIRK_LOG) << " removing a sprite";
1203 //qCDebug(KSIRK_LOG) << "i attack" << i;
1204 qCDebug(KSIRK_LOG) << "NKD" << NKD;
1205
1206 sprite->setAnimated(NKD);
1207
1208 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
1209 if (sndCrashPath.isNull())
1210 {
1211 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
1212 exit(2);
1213 }
1214 if (KsirkSettings::soundEnabled())
1215 {
1216 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
1217 m_audioPlayer->play();
1218 }
1219 }
1220 }
1221 else
1222 {
1223 sprite-> changeSequence("cavalry");
1224
1225 sprite->setStatic();
1226 m_animFighters->oneArrived(0);
1227 }
1228 }
1229 else
1230 {
1231 if (nbDefenderSurvivor<=0)
1232 {
1233 sprite-> changeSequence("exploding");
1234
1235 if (sprite->isDefendant())
1236 {
1237 qCDebug(KSIRK_LOG) << " removing a sprite";
1238 qCDebug(KSIRK_LOG) << "NKD" << NKD;
1239
1240 sprite->setAnimated(1);
1241
1242 QString sndCrashPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/crash.wav");
1243 if (sndCrashPath.isNull())
1244 {
1245 KMessageBox::information(this, i18n("Sound crash not found - Verify your installation\nProgram cannot continue"), i18n("KsirK - Error!"));
1246 exit(2);
1247 }
1248 if (KsirkSettings::soundEnabled())
1249 {
1250 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndCrashPath));
1251 m_audioPlayer->play();
1252 }
1253 }
1254 }
1255 else // the sprite is not the one (or one the several) killed
1256 {
1257 qCDebug(KSIRK_LOG) << " keeping a sprite";
1258
1259 if (sprite->isDefendant())
1260 {
1261 sprite-> changeSequence("infantry");
1262
1263 nbDefenderSurvivor--;
1264 }
1265 sprite->setStatic();
1266 m_animFighters->oneArrived(0);
1267 }
1268 }
1269 }
1270
1271 qCDebug(KSIRK_LOG) << " loop done defense";
1272
1273 qCDebug(KSIRK_LOG) << "finished";
1274 }
1275
stopExplosion()1276 void KGameWindow::stopExplosion()
1277 {
1278 qCDebug(KSIRK_LOG);
1279 m_animFighters->hideAndRemoveAll();
1280 }
1281
initCombatBringBackForArena(Country * attackingCountry,Country * defendingCountry)1282 void KGameWindow::initCombatBringBackForArena(Country *attackingCountry, Country *defendingCountry)
1283 {
1284 qCDebug(KSIRK_LOG);
1285 int who = 0;
1286 if ((NKD != 0)&&(NKA != 0)) who = 2;
1287 else if (NKA != 0) who = 0;
1288 else if (NKD != 0) who = 1;
1289 else KMessageBox::information(0, i18n("Problem: no one destroyed"));
1290
1291 //CannonSprite *newSprite;
1292 AnimSprite* newSprite;
1293
1294 qreal leftRelativePos;
1295 qreal pointDepartAttaquantY;
1296 qreal pointDepartDefenseurY;
1297
1298 qreal flagYDiff = (Sprites::SkinSpritesData::single().intData("flag-height") - Sprites::SkinSpritesData::single().intData("cannon-height"))*m_theWorld->zoom();
1299 qreal pointFlagX = backGnd()->boundingRect().width()/2;
1300
1301 if ((attackingCountry->nbArmies() % 10) == 0)
1302 {
1303 pointDepartAttaquantY = attackingCountry-> pointCannon().y()* m_theWorld->zoom();
1304 }
1305 else
1306 {
1307 if ((attackingCountry->nbArmies() % 5) == 0)
1308 {
1309 pointDepartAttaquantY = attackingCountry-> pointCavalry().y()* m_theWorld->zoom();
1310 }
1311 else
1312 {
1313 pointDepartAttaquantY = attackingCountry-> pointInfantry().y()* m_theWorld->zoom();
1314 }
1315 }
1316
1317 if ((defendingCountry->nbArmies() % 10) == 0)
1318 {
1319 pointDepartDefenseurY = defendingCountry-> pointCannon().y()* m_theWorld->zoom();
1320 }
1321 else
1322 {
1323 if ((defendingCountry->nbArmies() % 5) == 0)
1324 {
1325 pointDepartDefenseurY = defendingCountry-> pointCavalry().y()* m_theWorld->zoom();
1326 }
1327 else
1328 {
1329 pointDepartDefenseurY = defendingCountry-> pointInfantry().y()* m_theWorld->zoom();
1330 }
1331 }
1332
1333 qreal pointArriveeY = (pointDepartAttaquantY+pointDepartDefenseurY)/2;
1334
1335 QPointF start(pointFlagX,pointArriveeY);
1336
1337 qreal rightRelativePos = (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("flag-width"))*m_theWorld->zoom();
1338
1339 if (who == 0) //Attaquant detruit, ramene defenseur
1340 {
1341 if ((defendingCountry->nbArmies() % 10) == 0)
1342 {
1343 newSprite = new CannonSprite(m_theWorld->zoom(), backGnd(), 200);
1344
1345 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("cannon-width"))*m_theWorld->zoom();
1346 }
1347 else
1348 {
1349 if ((defendingCountry->nbArmies() % 5) == 0)
1350 {
1351 newSprite = new CavalrySprite(m_theWorld->zoom(), backGnd(), 200);
1352
1353 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("cavalry-width"))*m_theWorld->zoom();
1354 }
1355 else
1356 {
1357 newSprite = new InfantrySprite(m_theWorld->zoom(), backGnd(), 200);
1358
1359 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("infantry-width"))*m_theWorld->zoom();
1360 }
1361 }
1362
1363 if (backGnd()->bgIsArena() || ((attackingCountry-> pointFlag().x() <= defendingCountry-> pointFlag().x()) && !((qAbs(attackingCountry-> pointFlag().x()-defendingCountry-> pointFlag().x()) > (backGnd()-> boundingRect().width() / 2)) && (attackingCountry->communicateWith(defendingCountry)))))
1364 {
1365 newSprite-> setPos((start)+QPointF(rightRelativePos, flagYDiff));
1366 }
1367 else
1368 {
1369 newSprite-> setPos((start)+QPointF(leftRelativePos, flagYDiff));
1370 }
1371
1372 connect(newSprite,&AnimSprite::atDestination,this,&KGameWindow::slotBring);
1373
1374 QPointF dest;
1375 //int nbPos;
1376 if ((defendingCountry->nbArmies() % 10) == 0)
1377 {
1378 //nbPos = (defendingCountry->nbArmies() / 10);
1379 //dest = QPointF(defendingCountry-> pointCannon().x()*m_theWorld->zoom(),(defendingCountry-> pointCannon().y()+(1-2*(nbPos%2))*(Sprites::SkinSpritesData::single().intData("cannon-height")+8)*((nbPos+1)/2))*m_theWorld->zoom());
1380 dest = QPointF(defendingCountry-> pointCannon().x()*m_theWorld->zoom(),(defendingCountry-> pointCannon().y()+(1-2*(0%2))*(Sprites::SkinSpritesData::single().intData("cannon-height")+8)*((0+1)/2))*m_theWorld->zoom());
1381 }
1382 else
1383 {
1384 if ((defendingCountry->nbArmies() % 5) == 0)
1385 {
1386 //nbPos = (defendingCountry->nbArmies() / 5);
1387 //dest = QPointF(defendingCountry-> pointCavalry().x()*m_theWorld->zoom(),(defendingCountry-> pointCavalry().y()+(1-2*(nbPos%2))*(Sprites::SkinSpritesData::single().intData("cavalry-height")+8)*((nbPos+1)/2))*m_theWorld->zoom());
1388 dest = QPointF(defendingCountry-> pointCavalry().x()*m_theWorld->zoom(),(defendingCountry-> pointCavalry().y()+(1-2*(0%2))*(Sprites::SkinSpritesData::single().intData("cavalry-height")+8)*((0+1)/2))*m_theWorld->zoom());
1389 }
1390 else
1391 {
1392 //nbPos = (defendingCountry->nbArmies()) % 5;
1393
1394 //dest = QPointF(defendingCountry-> pointInfantry().x()*m_theWorld->zoom(),(defendingCountry-> pointInfantry().y()+(1-2*(nbPos%2))*(Sprites::SkinSpritesData::single().intData("infantry-height")+8)*((nbPos+1)/2))*m_theWorld->zoom());
1395 dest = QPointF(defendingCountry-> pointInfantry().x()*m_theWorld->zoom(),(defendingCountry-> pointInfantry().y()+(1-2*(0%2))*(Sprites::SkinSpritesData::single().intData("infantry-height")+8)*((0+1)/2))*m_theWorld->zoom());
1396 }
1397 }
1398
1399 ((AnimSprite*)newSprite)-> setupTravel(attackingCountry, defendingCountry, newSprite-> pos(), dest);
1400
1401 newSprite-> turnTowardDestination();
1402 qCDebug(KSIRK_LOG) << "add a sprite 4";
1403 m_animFighters->addSprite(newSprite);
1404
1405 QString sndRoulePath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/roll.wav");
1406 if (sndRoulePath.isNull())
1407 {
1408 KMessageBox::error(0, i18n("Sound roule not found - Verify your installation<br>Program cannot continue"), i18n("Error!"));
1409 exit(2);
1410 }
1411 if (KsirkSettings::soundEnabled())
1412 {
1413 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndRoulePath));
1414 m_audioPlayer->play();
1415 }
1416 }
1417 else if (who == 1) //Defenseur detruit, ramene Attaquant
1418 {
1419 if ((attackingCountry->nbArmies() % 10) == 0)
1420 {
1421 newSprite = new CannonSprite(m_theWorld->zoom(), backGnd(), 200);
1422
1423 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("cannon-width"))*m_theWorld->zoom();
1424 }
1425 else
1426 {
1427 if ((attackingCountry->nbArmies() % 5) == 0)
1428 {
1429 newSprite = new CavalrySprite(m_theWorld->zoom(), backGnd(), 200);
1430
1431 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("cavalry-width"))*m_theWorld->zoom();
1432 }
1433 else
1434 {
1435 newSprite = new InfantrySprite(m_theWorld->zoom(), backGnd(), 200);
1436
1437 leftRelativePos = - (Sprites::SkinSpritesData::single().intData("width-between-flag-and-fighter") + Sprites::SkinSpritesData::single().intData("infantry-width"))*m_theWorld->zoom();
1438 }
1439 }
1440
1441 if (backGnd()->bgIsArena() || ((attackingCountry-> pointFlag().x() <= defendingCountry-> pointFlag().x()) && !((qAbs(attackingCountry-> pointFlag().x()-defendingCountry-> pointFlag().x()) > (backGnd()-> boundingRect().width() / 2)) && (attackingCountry->communicateWith(defendingCountry)))))
1442 {
1443 newSprite-> setPos((start)+QPointF(leftRelativePos,flagYDiff));
1444 }
1445 else
1446 {
1447 newSprite-> setPos((start)+QPointF(rightRelativePos,flagYDiff));
1448 }
1449
1450 connect(newSprite,&AnimSprite::atDestination,this,&KGameWindow::slotBring);
1451
1452 QPointF dest;
1453 //int nbPos;
1454 if ((attackingCountry->nbArmies() % 10) == 0)
1455 {
1456 //nbPos = (attackingCountry->nbArmies() / 10);
1457 //dest = QPointF(attackingCountry-> pointCannon().x()*m_theWorld->zoom(),(attackingCountry-> pointCannon().y()+(1-2*(nbPos%2))*(Sprites::SkinSpritesData::single().intData("cannon-height")+8)*((nbPos+1)/2))*m_theWorld->zoom());
1458 dest = QPointF(attackingCountry-> pointCannon().x()*m_theWorld->zoom(),(attackingCountry-> pointCannon().y()+(1-2*(0%2))*(Sprites::SkinSpritesData::single().intData("cannon-height")+8)*((0+1)/2))*m_theWorld->zoom());
1459 }
1460 else
1461 {
1462 if ((attackingCountry->nbArmies() % 5) == 0)
1463 {
1464 //nbPos = (attackingCountry->nbArmies() / 5);
1465 //dest = QPointF(attackingCountry-> pointCavalry().x()*m_theWorld->zoom(),(attackingCountry-> pointCavalry().y()+(1-2*(nbPos%2))*(Sprites::SkinSpritesData::single().intData("cavalry-height")+8)*((nbPos+1)/2))*m_theWorld->zoom());
1466 dest = QPointF(attackingCountry-> pointCavalry().x()*m_theWorld->zoom(),(attackingCountry-> pointCavalry().y()+(1-2*(0%2))*(Sprites::SkinSpritesData::single().intData("cavalry-height")+8)*((0+1)/2))*m_theWorld->zoom());
1467 }
1468 else
1469 {
1470 //nbPos = attackingCountry->nbArmies() % 5;
1471 //dest = QPointF(attackingCountry-> pointInfantry().x()*m_theWorld->zoom(),(attackingCountry-> pointInfantry().y()+(1-2*(nbPos%2))*(Sprites::SkinSpritesData::single().intData("infantry-height")+8)*((nbPos+1)/2))*m_theWorld->zoom());
1472 dest = QPointF(attackingCountry-> pointInfantry().x()*m_theWorld->zoom(),(attackingCountry-> pointInfantry().y()+(1-2*(0%2))*(Sprites::SkinSpritesData::single().intData("infantry-height")+8)*((0+1)/2))*m_theWorld->zoom());
1473 }
1474 }
1475
1476 ((AnimSprite*)newSprite)-> setupTravel(defendingCountry, attackingCountry, newSprite-> pos(), dest);
1477
1478 newSprite-> turnTowardDestination();
1479 qCDebug(KSIRK_LOG) << "add a sprite 5";
1480 m_animFighters->addSprite(newSprite);
1481
1482 QString sndRoulePath = QStandardPaths::locate(QStandardPaths::AppDataLocation, m_automaton->skin() + "/Sounds/roll.wav");
1483 if (sndRoulePath.isNull())
1484 {
1485 KMessageBox::error(0, i18n("Sound roule not found - Verify your installation<br>Program cannot continue"), i18n("Error!"));
1486 exit(2);
1487 }
1488 if (KsirkSettings::soundEnabled())
1489 {
1490 m_audioPlayer->setCurrentSource(QUrl::fromLocalFile(sndRoulePath));
1491 m_audioPlayer->play();
1492 }
1493 }
1494 else if (who == 2)
1495 {
1496 } //Attaquant ET Defenseur detruits
1497 else // error
1498 {
1499 qCCritical(KSIRK_LOG) << Q_FUNC_INFO << __FILE__ << __LINE__ << i18n("Bug: who should be 0, 1 or 2.");
1500 exit(1);
1501 }
1502
1503 relativePosInArenaDefense=0;
1504 relativePosInArenaAttack=0;
1505 }
1506
1507 /**
1508 * Disconnects the mouse events signals from their slots to avoid human
1509 * player actions when it is the turn of the AI
1510 */
disconnectMouse()1511 void KGameWindow::disconnectMouse()
1512 {
1513 /* if ( ! disconnect(m_frame, SIGNAL(signalLeftButtonDown(QPoint)),
1514 this, SLOT(slotLeftButtonDown(QPoint))))
1515 qCCritical(KSIRK_LOG) << "cannot connect slotLeftButtonDown !";
1516 if ( ! disconnect(m_frame, SIGNAL(signalLeftButtonUp(QPoint)),
1517 this, SLOT(slotLeftButtonUp(QPoint))))
1518 qCCritical(KSIRK_LOG) << "cannot connect slotLeftButtonUp !";
1519 if ( ! disconnect(m_frame, SIGNAL(signalRightButtonDown(QPoint)),
1520 this, SLOT(slotRightButtonDown(QPoint))))
1521 qCCritical(KSIRK_LOG) << "cannot connect slotRightButtonDown !";*/
1522 }
1523
1524 /**
1525 * Reconnect the mouse events signals to their slots to allow human players to
1526 * play
1527 */
reconnectMouse()1528 void KGameWindow::reconnectMouse()
1529 {
1530 /* if ( ! connect(m_frame, SIGNAL(signalLeftButtonDown(QPoint)),
1531 this, SLOT(slotLeftButtonDown(QPoint))))
1532 qCCritical(KSIRK_LOG) << "cannot connect slotLeftButtonDown !";
1533 if ( ! connect(m_frame, SIGNAL(signalLeftButtonUp(QPoint)),
1534 this, SLOT(slotLeftButtonUp(QPoint))))
1535 qCCritical(KSIRK_LOG) << "cannot connect slotLeftButtonUp !";
1536 if ( ! connect(m_frame, SIGNAL(signalRightButtonDown(QPoint)),
1537 this, SLOT(slotRightButtonDown(QPoint))))
1538 qCCritical(KSIRK_LOG) << "cannot connect slotRightButtonDown !";*/
1539 }
1540
haveAnimFighters() const1541 bool KGameWindow::haveAnimFighters() const
1542 {
1543 return !m_animFighters->empty();
1544 }
1545
updateScrollArrows()1546 void KGameWindow::updateScrollArrows()
1547 {
1548 qCDebug(KSIRK_LOG);
1549 if (m_uparrow != 0)
1550 {
1551 QPointF pos = m_frame->mapToScene(QPoint(m_frame->viewport()->width()/2,0));
1552 pos = pos + QPointF(-(m_uparrow->boundingRect().width()/2),m_uparrow->boundingRect().height());
1553 m_uparrow->setPos(pos);
1554 m_uparrow->setActive(false);
1555 }
1556 if (m_downarrow != 0)
1557 {
1558 QPointF pos = m_frame->mapToScene(QPoint(m_frame->viewport()->width()/2,m_frame->viewport()->height()));
1559 pos = pos - QPointF(m_downarrow->boundingRect().width()/2,m_downarrow->boundingRect().height());
1560 m_downarrow->setPos(pos);
1561 m_downarrow->setActive(false);
1562 }
1563 if (m_leftarrow != 0)
1564 {
1565 QPointF pos = m_frame->mapToScene(QPoint(0,m_frame->viewport()->height()/2));
1566 pos = pos - QPointF(m_downarrow->boundingRect().width()/2,m_downarrow->boundingRect().height());
1567 pos = pos + QPointF(m_leftarrow->boundingRect().width(),-(m_leftarrow->boundingRect().height()/2));
1568 m_leftarrow->setPos(pos);
1569 m_leftarrow->setActive(false);
1570 }
1571 if (m_rightarrow != 0)
1572 {
1573 QPointF pos = m_frame->mapToScene(QPoint(m_frame->viewport()->width(),m_frame->viewport()->height()/2));
1574 pos = pos - QPointF(m_rightarrow->boundingRect().width(),m_rightarrow->boundingRect().height()/2);
1575 m_rightarrow->hide();
1576 m_rightarrow->setPos(pos);
1577 m_rightarrow->show();
1578 m_rightarrow->setActive(false);
1579 }
1580 }
1581
1582 } // closing namespace KsirK
1583