File: fightEngine.cpp

package info (click to toggle)
attal 1.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 4,348 kB
  • ctags: 7,428
  • sloc: cpp: 55,101; sh: 267; ansic: 100; makefile: 54
file content (821 lines) | stat: -rw-r--r-- 18,477 bytes parent folder | download | duplicates (2)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/****************************************************************
**
** Attal : Lords of Doom
**
** fightEngine.cpp
** managing server-side fight
**
** Version : $Id: fightEngine.cpp,v 1.40 2008/03/01 18:59:57 lusum Exp $
**
** Author(s) : Pascal Audoux - Cyrille Verrier - Sardi Carlo
**
** Date : 02/01/2001
**
** Licence :    
**	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, 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.
**
****************************************************************/

#include "fightEngine.h"

// generic include files
#include <stdlib.h>
// include files for QT
// application specific include files
#include "libCommon/attalCommon.h"
#include "libCommon/dataTheme.h"
#include "libCommon/gameData.h"
#include "libCommon/genericCell.h"
#include "libCommon/genericLord.h"
#include "libCommon/genericMapCreature.h"
#include "libCommon/genericPlayer.h"
#include "libCommon/log.h"
#include "libCommon/unit.h"


FightEngine::FightEngine( AttalServer * server )
{
	_server = server;
	_currentUnit = NULL;

	_attackPlayer = NULL;
	_attackLord = NULL;
	_defendPlayer = NULL;
	_defendLord = NULL;
	_defendCell = NULL;
	_analyst = NULL;
	_fake = NULL;
	//_troops = 0;
	_map = NULL;
	_num = 0;
	/* _creatureLord used only in combacts with creatures */
	_creatureLord = new GenericLord();
	_creatureLord->setId( 0 );
	_creatureLord->setCreatureLord( true );
}

FightEngine::~FightEngine()
{
	if(_fake)
		delete _fake;
	if(_analyst)
		delete _analyst;
	if( _map ) {
		delete _map;
	}
	if(_creatureLord)
		delete _creatureLord;

 	_server= NULL;
	_currentUnit = NULL;

	_attackPlayer = NULL;
	_attackLord = NULL;
	_defendPlayer = NULL;
	_defendLord = NULL;
	_defendCell = NULL;
	_analyst = NULL;
	_fake = NULL;
	_map = NULL;
	_hasEnded = false;
	_creatureLord = NULL;
}

void FightEngine::print()
{
	logDD("att play %p", _attackPlayer );
	logDD("att lord %p", _attackLord );
	logDD("att lord cell %p", _attackLord->getCell() );
	logDD("def play %p", _defendPlayer );
	logDD("def lord %p", _defendLord );
	logDD("def lord cell %p", _defendLord->getCell() );
}

void FightEngine::init( GenericPlayer * attackPlayer, GenericLord * attackLord, GenericPlayer * defendPlayer, GenericLord * defendLord )
{
	_result.clear();
	_attackPlayer = attackPlayer;
	_attackLord = attackLord;
	_defendPlayer = defendPlayer;
	_defendLord = defendLord;
	_currentUnit = 0;
	_hasEnded = false;

	_fightCreature = false;
	//signals to clients
	_server->startFight( _attackPlayer, _attackLord, _defendPlayer, _defendLord );

	if( _map ) {
		delete _map;
		_map = 0;
	}

	_map = new GenericFightMap();
	_map->newFightMap( 9, 15 );

	setupUnits();

	newTurn();
}

void FightEngine::init( GenericPlayer * attackPlayer, GenericLord * attackLord, GenericFightUnit * defendUnit[MAX_UNIT], GameData * data )
{
	_result.clear();
	_defendExp = 0;
	_attackExp = 0;
	int i;
	_attackPlayer = attackPlayer;
	_attackLord = attackLord;
	_defendPlayer = NULL;
	_defendLord = _creatureLord;
	_fightCreature = true;
	_hasEnded = false;


	//initialisation ai
	if( _analyst ) {
		delete _analyst;
	}
	if( _fake ) {
		delete _fake;
	}
	_fake = new FakeSocket;
	_analyst = new FightAnalyst( data, this );
	_analyst->setSocket( _fake );
	//signals to clients
	_server->startFight( _attackPlayer, _attackLord, defendUnit );

	for( i = 0; i < MAX_UNIT; i++ ) {
		_defendLord->setUnit( i, defendUnit[i] );
	}

	_analyst->initCreatures( _defendLord );
	_fake->sendFightLord( FIGHTER_ATTACK, _attackLord );
	_analyst->handleFightData( _fake );
	for( i = 0; i < MAX_UNIT; i++ ) {
		if( _attackLord->getUnit( i ) ) {
			_fake->sendFightNewUnit( FIGHTER_ATTACK, i, _attackLord->getUnit( i ) );
			_analyst->handleFightData( _fake );
		}
	}
	//end initialisation ai

	if( _map ) {
		delete _map;
		_map = 0;
	}
	_map = new GenericFightMap();
	_map->newFightMap( 9, 15 );

	setupUnits();
	newTurn();
}

void FightEngine::handleFakeSocket( FakeSocket * data )
{
	//_server->copyData( (AttalSocket*) data );
	
	handleSocket( _defendPlayer, data->getData() );
}

void FightEngine::handleSocket( GenericPlayer * player, AttalSocketData data )
{

	_currentData = data;

	if( ( player == _attackPlayer ) || ( player == _defendPlayer ) ) {
		_currentPlayer = player;
		switch( getCla2() ) {
		case C_FIGHT_INIT:
			logEE( "Should not happen FightEngine : FIGHT_INIT" );
			break;
		case C_FIGHT_CREATURE:
			break;
		case C_FIGHT_LORD:
			break;
		case C_FIGHT_CELL:
			break;
		case C_FIGHT_UNIT:
			break;
		case C_FIGHT_MODUNIT:
			break;
		case C_FIGHT_MOVE:
			socketFightMove();
			break;
		case C_FIGHT_ENDMOVE:
			socketFightEndMove();
			break;
		case C_FIGHT_ACTIVE:
			logEE( "Should not happen" );
			break;
		case C_FIGHT_DISTATTACK:
			socketFightDistAttack();
			break;
		case C_FIGHT_WAIT:
			nextUnit( 1 );
			break;
		case C_FIGHT_FLEE:
			socketFightFlee( player );
			break;
		case C_FIGHT_DEFEND:
			if( _currentUnit ) {
				nextUnit( 1 );
				/// XXX: for the moment, "defend" do nothing...
			}
			break;
		case C_FIGHT_DAMAGE:
			break;
		case C_FIGHT_END:
			break;
		default:
			logEE( "Should not happen" );
			break;
		}
	} else {
		logEE( "Should not happen : fight socket from non fighting player" );
	}
}

void FightEngine::socketFightFlee(GenericPlayer * player)
{
	if( player == _attackPlayer ) {
		_result.setDefenseWin();
	} else {
		_result.setAttackWin();
	}
	_result.setFled();
	endFight();
}

void FightEngine::socketFightEndMove()
{
	if( _currentUnit ) {
		postcomputeUnit();

		//if( _troops.remove( _currentUnit ) )
		//	logDD( "Remove current unit" );
		//_troops.last();
		//GenericFightUnit *u = _currentUnit;
		nextUnit( 1 );
	}

}

void FightEngine::socketFightDistAttack()
{
	uchar cla = readChar();
	uchar num = readChar();

	//logDD(" cla %d, num %d",cla,num);
	
	GenericFightUnit * unit =  giveUnit( (CLASS_FIGHTER)cla, num );

	if(_currentUnit->isDistAttack()){
			fightUnit( _currentUnit, unit, ATTACK_SHOOT );
	}
}

void FightEngine::socketFightMove()
{
	uchar cla = readChar();
	uchar num = readChar();
	int row = readInt();
	int col = readInt();
		
	//logDD("cla %d, num %d , row %d ,col %d ,", cla, num, row, col);

	if(row > 9 || col > 15)
		logEE("bug! cla %d, num %d , row %d ,col %d ,", cla, num, row, col);
	
	handleMove( giveUnit( (CLASS_FIGHTER)cla, num ), _map->at( row, col ) );
}

void FightEngine::setupUnits()
{
	TRACE("FightEngine::setupUnits");

	/// XXX: improve algo for initial placement of units...
	for( int i = 0; i < MAX_UNIT; i++ ) {
		if( _attackLord->getUnit( i ) ) {
			_attackLord->getUnit ( i )->setLookingToRight( true );
			_attackLord->getUnit ( i )->setFightMap ( _map );
			moveUnit( _attackLord->getUnit( i ), _map->at( i, 0 ) );
		}
		if( _defendLord->getUnit( i ) ) {
			_defendLord->getUnit ( i )->setLookingToRight( false );
			_defendLord->getUnit ( i )->setFightMap ( _map );
			moveUnit(  _defendLord->getUnit( i ), _map->at( i, 14 ) );
		}
	}
}

void FightEngine::newTurn()
{
	TRACE("FightEngine::newTurn");

	_currentUnit = 0;

	// initialize values (movement)
	for( int i = 0; i < MAX_UNIT; i++ ) {
		if( _attackLord->getUnit( i ) ) {
			_attackLord->getUnit( i )->setMaxMove();

		}
		if( _defendLord->getUnit( i ) ) {
			_defendLord->getUnit( i )->setMaxMove();
		}
	}

	// setup list of units;
	orderTroops();

	activateUnit( _troops.last() );
}

void FightEngine::endTurn()
{
	TRACE("FightEngine::endTurn");

	computeFightResultStatus();

	if( _result.isFightFinished() ) {
		endFight();
	} else {
		newTurn();
	}
}

void FightEngine::updateUnits()
{

	TRACE("FightEngine::updateUnits");

	GenericFightUnit  * newUnit;
	uint i;

	if(_attackLord) {
		for( i = 0; i < MAX_UNIT; i++) {
			newUnit = giveUnit( FIGHTER_ATTACK, i );
			if(newUnit){
				if(newUnit->getNumber()==0){
					delete newUnit;
					newUnit=NULL;
					_attackLord->setUnit(i,NULL);
				}
			}
		}
	}
	
	if( _defendLord ) {
		for( i = 0; i < MAX_UNIT; i++) {
			newUnit = giveUnit( FIGHTER_DEFENSE, i );
			if(newUnit){
				if(newUnit->getNumber()==0){
					delete newUnit;
					newUnit=NULL;
					_defendLord->setUnit(i,NULL);
				}
			}
		}
	}

}

void FightEngine::endFight()
{
	TRACE("FightEngine::endFight");
	
	updateUnits();
	_server->endFight( _attackPlayer, _defendPlayer, _result );

	if( _fightCreature ) {
		_fake->sendFightEnd( _result );
		_analyst->handleFightData( _fake );
	}

	_currentUnit = 0;
	_hasEnded = true;
	emit sig_endFight( _result );
}

void FightEngine::moveUnit( GenericFightUnit * unit, GenericFightCell * cell )
{
	TRACE("FightEngine::moveUnit");

	if( ! _fightCreature ) {
		_server->moveUnit( _defendPlayer, giveClass( unit ), giveNum( unit ), cell );
	} else {
		_fake->sendFightUnitMove( giveClass( unit ), giveNum( unit ), cell );
		_analyst->handleFightData( _fake );
	}
	_server->moveUnit( _attackPlayer, giveClass( unit ), giveNum( unit ), cell );

	unit->goTo( cell );
}

void FightEngine::fightUnit( GenericFightUnit * unitAtt, GenericFightUnit * unitDef, CLASS_ATTACK_TYPE attackType )
{
	TRACE("FightEngine::fightUnit");

	long int damage, bonus;

	if (unitAtt->canAttack()) {
		if( giveClass( unitAtt ) == giveClass( unitDef ) ) {
			logEE( "Allied units can't fight eachother !" );
			return;
		}

		if( unitAtt->getAttack() > 0 ) {
			damage = ( ( rand() % unitAtt->getAttack() ) + 1 ) * unitAtt->getNumber();
		} else {
			damage = unitAtt->getNumber();
		}

		GenericLord * lordAtt = giveLord( unitAtt );
		GenericLord * lordDef = giveLord( unitDef ); 
		GenericPlayer * playAtt = lordAtt->getOwner();
		GenericPlayer * playDef = lordDef->getOwner();
		QString playAttName = playAtt ? playAtt->getConnectionName() : "server";
		QString playDefName = playDef ? playDef->getConnectionName() : "server";

		bonus = lordAtt->getCharac( ATTACK ) - lordDef->getCharac( DEFENSE );
		bonus *= lordAtt->getCharac( ATTACK ) > lordDef->getCharac( DEFENSE )  ? 5 : -2;

		long luck = 0;
		if( unitAtt->getLuck() != 0 ) {
			if( unitAtt->getLuck() > 0 ) {
				luck = ( rand() % unitAtt->getLuck() );
			} else {
				luck = - ( rand() % unitAtt->getLuck() );
			}
		}
		bonus += ( luck * 5 );

		bonus = ( bonus > 400 ) ? 400 : ( bonus < -100 ) ? -100 : bonus;

		damage += (damage * bonus) / 100;

		damage = ( damage >= 0 ) ? damage : 0;

		QString msg;
		int oldNum = unitDef->getNumber();
		//logDD("hit server");
		unitDef->hit( damage );

		QString attName = unitAtt->getCreature()->getName();
		QString defName = unitDef->getCreature()->getName();

		msg.sprintf( "%s ( %s ) hit %s ( %s ) : %ld damages %li creatures died", qPrintable( attName ), qPrintable(playAttName), qPrintable( defName ), qPrintable( playDefName), damage,oldNum - unitDef->getNumber() );
		logDD("%s" , qPrintable(msg));
		_server->sendFightMessage( msg, _attackPlayer, _defendPlayer );

		// manage experience earned
		uint exp = ( oldNum - unitDef->getNumber() ) * unitDef->getMaxHealth();
		if( lordDef == _attackLord ) {
			_defendExp += exp;
		} else {
			_attackExp += exp;
		}

		//_server->updateUnit( _attackPlayer, giveClass( unitDef ), giveNum( unitDef ), unitDef );
		//_server->updateUnit( _defendPlayer, giveClass( unitDef ), giveNum( unitDef ), unitDef );
		_server->damageUnit( _attackPlayer,
				giveClass ( unitAtt ),
				giveNum ( unitAtt ),
				attackType,
				giveClass( unitDef ),
				giveNum( unitDef ),
				damage );
		if( ! _fightCreature ) {
			_server->damageUnit( _defendPlayer,
					giveClass ( unitAtt ),
					giveNum ( unitAtt ),
					attackType,
					giveClass( unitDef ),
					giveNum( unitDef ),
					damage ); 
		} else {
			_fake->sendFightDamageUnit
				( giveClass ( unitAtt ),
					giveNum ( unitAtt ),
					attackType,
					giveClass( unitDef ),
					giveNum( unitDef ),
					damage );
			_analyst->handleFightData( _fake );
		}
		unitAtt->finishAttack();
	}
}

void FightEngine::nextUnit( int remove )
{
	//this function search next unit in _troops list and remove used unit
	GenericFightUnit * unit;
	int pos;
	bool found = false;

	computeFightResultStatus();

	//update units before end of fight
	if( _result.isFightFinished() ) {
		endFight();
		return;
	}

	//remove current unit from list
	if( remove ) {
		pos = _troops.indexOf( _currentUnit );
		_troops.removeAt( pos );
		if( pos == _troops.count() ) {
			pos = 0;
		}

		if( _troops.count() > 0 ) {
			_troops.at( pos );
		}
	}

	for ( int i = 0;  i < _troops.count(); i++  ) {
		unit = _troops.at(i);
		if( !unit ) {
			unit = _troops.last();
		}

		if( unit->getNumber() <= 0 ) {
			pos = _troops.indexOf( unit );
			_troops.removeAt( pos );
			if( pos == _troops.count() ) {
				pos = 0;
			}
			if( _troops.count() > 0 ) {
				_troops.at( pos );
			}
		} else {
			found = true;
		}
		if ( found ) {
			break;
		}
	}
	
	if( found ) {
		activateUnit( unit );
	} else {
		endTurn();
	}
}

void FightEngine::activateUnit( GenericFightUnit * unit )
{
	_currentUnit = unit;
	_map->initPath( _currentUnit );
	_currentUnit->activateAttack();

	precomputeUnit();
	_server->activateUnit( _attackPlayer, giveClass( _currentUnit ), giveNum( _currentUnit ) );
	if( _defendPlayer ) {
		_server->activateUnit( _defendPlayer, giveClass( _currentUnit ), giveNum( _currentUnit ) );
	} else {
		_fake->sendFightActivate( giveClass( _currentUnit ), giveNum( _currentUnit ) );
		_analyst->handleFightData( _fake );
	}
}

void FightEngine::computeFightResultStatus()
{
	int sumAtt = 0, sumDef = 0;

	for( int i = 0; i < MAX_UNIT; i++ ) {
		GenericFightUnit * unit = _attackLord->getUnit( i );
		if( unit ) {
			sumAtt += unit->getNumber();
		}

		unit = _defendLord->getUnit( i );
		if( unit ) {
			sumDef += unit->getNumber();
		}
	}

	if( sumAtt <= 0 ) {
		_result.setDefenseWin();
		//_server->sendLordRemove( _attackLord );
		//XXX: Remove lord from server memory
	}
	if( sumDef <= 0 ) {
		_result.setAttackWin();
		//_server->sendLordRemove( _defendLord );
		//XXX: Remove lord from server memory
	}
}

void FightEngine::orderTroops()
{
	GenericFightUnit * unit;

	_troops.clear();
	for( int i = 0; i < MAX_UNIT; i++ ) {
		unit = _attackLord->getUnit( i );
		if( unit && ( unit->getNumber() > 0 ) ) {
			_troops.add( unit );
		}
		unit = _defendLord->getUnit( i );
		if( unit && ( unit->getNumber() > 0 ) ) {
			_troops.add( unit );
		}
	}
}


int FightEngine::giveNum( GenericFightUnit * unit )
{
	for( int i = 0; i < MAX_UNIT; i++ ) {
		if( ( _attackLord->getUnit( i ) == unit ) || ( _defendLord->getUnit( i ) == unit ) ) {
			return i;
		}
	}

	logEE( "Should not happen" );
	return -1;
}

CLASS_FIGHTER FightEngine::giveClass( GenericFightUnit * unit )
{
	for( int i = 0; i < MAX_UNIT; i++ ) {
		if( _attackLord->getUnit( i ) == unit ) {
			return FIGHTER_ATTACK;
		}
		if( _defendLord->getUnit( i ) == unit ) {
			return FIGHTER_DEFENSE;
		}
	}

	logEE( "Should not happen" );
	return FIGHTER_ATTACK;
}

GenericFightUnit * FightEngine::giveUnit( CLASS_FIGHTER fighter, int num )
{
	GenericFightUnit * unit;

	if( fighter == FIGHTER_ATTACK ) {
		unit = _attackLord->getUnit( num );
	} else {
		unit = _defendLord->getUnit( num );
	}

	return unit;
}

GenericLord * FightEngine::giveLord( GenericFightUnit * unit )
{
	GenericLord * ret;

	if( giveClass( unit ) == FIGHTER_ATTACK ) {
		ret = _attackLord;
	} else {
		ret = _defendLord;
	}
	
	return ret;
}

void FightEngine::handleMove( GenericFightUnit * unit, GenericFightCell * cell )
{
	
	TRACE("FightEngine::handleMove cell access %d, row %d , col %d", cell->getAccess(), cell->getRow(), cell->getCol());

	if( unit != _currentUnit ) {
		logEE( "The unit moving is not active !!" );
		return;
	}
    
	GenericFightCell* neib = NULL;

	// 2 cell unit test  
	if ( unit->getCreature()->getSize () == 2 ){

		neib = _map->getHeadCell( cell , unit->isLookingToRight() );

		if( ! _map->areNeighbours( unit->getCell(), cell ) &&  ! _map->areNeighbours( neib, cell )) {
			logEE( "The dest cell is not a neighbour cell of the unit" );
			printStatus( unit , cell );
			return;
		}
	} else {
	// 1 cell unit test
		if( ! _map->areNeighbours( unit->getCell(), cell ) ) {
			logEE( "The dest cell is not a neighbour cell of the unit" );
			printStatus( unit , cell );
			return;
		}
	}

	switch( cell->getAccess() ) {
	case AttalCommon::UNKNOWN_ACCESS:
		logEE( "Should not happen (UNKNOWN_ACCESS)" );
		break;
	case AttalCommon::NEAR_FREE:
		TRACE("NEAR_FREE");
		moveUnit( unit, cell );
		break;
	case AttalCommon::NEAR_OCCUPIED: 
		{
			TRACE("NEAR_OCCUPIED");
			GenericFightUnit * cellUnit = cell->getUnit();
			if( cellUnit ){
				if( cellUnit != unit){
					fightUnit( unit, cellUnit, ATTACK_FIGHT );
				} else if( neib ) {
					cellUnit = neib->getUnit();
					if( cellUnit ){
						if( cellUnit != unit){
							fightUnit( unit, cellUnit, ATTACK_FIGHT );
						}
					}
				}
			} else {
				logEE("NEAR_OCCUPIED Signal error, correct bug \n");
			}
		}
		break;
	case AttalCommon::FAR_FREE:
		logEE( "Should not happen (AttalCommon::FAR_FREE)" );
		printStatus( unit , cell );
		break;
	case AttalCommon::FAR_OCCUPIED:
		logEE( "Should not happen (FAR_OCCUPIED)" );
		printStatus( unit , cell );
		break;
	case AttalCommon::NONE:
		logEE( "Should not happen (NONE)" );
		printStatus( unit , cell );
		break;
	default:
		logEE( "Should not happen" );
		printStatus( unit , cell );
		break;
	}
}

uint FightEngine::getExperience( GenericLord * lord )
{
	/// XXX: we could have 'lord' in parameter and know the winner...
	uint ret = 0;

	if( lord == _attackLord ) {
		ret = _attackExp;
	} else if( lord == _defendLord ) {
		ret = _defendExp;
	}

	return ret;
}

void FightEngine::printStatus( GenericFightUnit * unit, GenericFightCell * cell )
{
#ifdef QT_DEBUG
	unit->display();
#endif
	logEE( "start <> dest");
	logEE( "%d-%d <> %d-%d", unit->getCell()->getRow(),
					unit->getCell()->getCol(),
					cell->getRow(),
					cell->getCol() 
					);

}

uchar FightEngine::getCla1()
{
	return _currentData.getCla1();
}

uchar FightEngine::getCla2()
{
	return _currentData.getCla2();
}

uchar FightEngine::getCla3()
{
	return _currentData.getCla3();
}

int FightEngine::readInt()
{
	return _currentData.readInt();
}

unsigned char FightEngine::readChar()
{
	return _currentData.readChar();
}