1
<?php
2
/**
3
 *   File functions:
4
 *   Battle Arena - figth between players and player vs monsters
5
 *
6
 *   @name                 : battle.php
7
 *   @copyright            : (C) 2004,2005,2006 Vallheru Team based on Gamers-Fusion ver 2.5
8
 *   @author               : thindil <thindil@users.sourceforge.net>
9
 *   @author               : Marek 'marq' Chodor <marek.chodor@gmail.com>
10
 *   @author               : eyescream <tduda@users.sourceforge.net>
11
 *   @version              : 1.4a
12
 *   @since                : 26.08.2007
13
 *
14
 */
15
16
//
17
//
18
//       This program is free software; you can redistribute it and/or modify
19
//   it under the terms of the GNU General Public License as published by
20
//   the Free Software Foundation; either version 2 of the License, or
21
//   (at your option) any later version.
22
//
23
//   This program is distributed in the hope that it will be useful,
24
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
25
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
//   GNU General Public License for more details.
27
//
28
//   You should have received a copy of the GNU General Public License
29
//   along with this program; if not, write to the Free Software
30
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31
//
32
// $Id: battle.php 727 2006-10-16 15:48:33Z thindil $
33
34
$title = "Arena Walk";
35
require_once("includes/head.php");
36
require_once("includes/funkcje.php");
37
require_once("class/fight_class.php");
38
require_once("class/monster_class.php");
39
require_once('includes/security.php');
40
41
/**
42
* Get the localization for game
43
*/
44
require_once("languages/".$player -> lang."/battle.php");
45
require_once("languages/".$player -> lang."/funkcje.php");
46
47
global $runda;
48
global $number;
49
global $newdate;
50
global $smarty;
51
global $db;
52
53
if (!isset($_GET['action']) && !isset($_GET['battle']))
54
{
55
    $smarty -> assign(array("Battleinfo" => BATTLE_INFO,
56
                            "Ashowalive" => A_SHOW_ALIVE,
57
                            "Ashowlevel" => A_SHOW_LEVEL,
58
                            "Ashowmonster" => A_SHOW_MONSTER));
59
}
60
61
/**
62
* Show players on this same level which have a player
63
*/
64
if (isset($_GET['action']) && $_GET['action'] == 'showalive')
65
{
66
    $elist = $db -> SelectLimit("SELECT id, user, rank, tribe FROM players WHERE level=".$player -> level." AND hp>0 AND miejsce='".$player -> location."' AND id!=".$player -> id." AND immu='N' AND rasa!='' AND klasa!='' AND rest='N' AND freeze=0 AND age>2", 50);
67
    $arrid = array();
68
    $arrname = array();
69
    $arrrank = array();
70
    $arrtribe = array();
71
    $i = 0;
72
    while (!$elist -> EOF)
73
    {
74
        if ($elist -> fields['rank'] == 'Admin')
75
        {
76
            $arrrank[$i] = R_ADMIN;
77
        }
78
            elseif ($elist -> fields['rank'] == 'Staff')
79
        {
80
            $arrrank[$i] = R_STAFF;
81
        }
82
            elseif ($elist -> fields['rank'] == 'Member')
83
        {
84
            $arrrank[$i] = R_MEMBER;
85
        }
86
            else
87
        {
88
            $arrrank[$i] = $elist -> fields['rank'];
89
        }
90
        $arrid[$i] = $elist -> fields['id'];
91
        $arrname[$i] = $elist -> fields['user'];
92
        $arrtribe[$i] = $elist -> fields['tribe'];
93
        $elist -> MoveNext();
94
        $i = $i + 1;
95
    }
96
    $elist -> Close();
97
    $smarty -> assign ( array("Level" => $player -> level,
98
                              "Enemyid" => $arrid,
99
                              "Enemyname" => $arrname,
100
                              "Enemytribe" => $arrtribe,
101
                              "Enemyrank" => $arrrank,
102
                              "Lid" => L_ID,
103
                              "Showinfo" => SHOW_INFO,
104
                              "Lname" => L_NAME,
105
                              "Lrank" => L_RANK,
106
                              "Lclan" => L_CLAN,
107
                              "Loption" => L_OPTION,
108
                              "Aattack" => A_ATTACK,
109
                              "Orback" => OR_BACK,
110
                              "Bback" => B_BACK));
111
}
112
113
if (isset ($_GET['action']) && $_GET['action'] == 'levellist')
114
{
115
    $smarty -> assign(array(
116
                            "Showall" => SHOW_ALL,
117
                            "Tolevel" => TO_LEVEL,
118
                            "Ago" => A_GO));
119
    if (isset($_GET['step']) && $_GET['step'] == 'go')
120
    {
121
        if (!isset($_POST['slevel']))
122
        {
123
            error(S_LEVEL);
124
        }
125
        if (!isset($_POST['elevel']))
126
        {
127
            error(E_LEVEL);
128
        }
129
        if (!strictInt($_POST['slevel']) || !strictInt($_POST['elevel']))
130
        {
131
            error(ERROR);
132
        }
133
        $elist = $db -> SelectLimit("SELECT id, user, rank, tribe FROM players WHERE level>=".$_POST['slevel']." AND level<=".$_POST['elevel']." AND hp>0 AND miejsce='".$player -> location."' AND id!=".$player -> id." AND immu='N' AND rasa!='' AND klasa!='' AND rest='N' AND freeze=0 AND age>2", 50);
134
        $arrid = array();
135
        $arrname = array();
136
        $arrrank = array();
137
        $arrtribe = array();
138
        $i = 0;
139
        while (!$elist -> EOF)
140
        {
141
            if ($elist -> fields['rank'] == 'Admin')
142
            {
143
                $arrrank[$i] = R_ADMIN;
144
            }
145
                elseif ($elist -> fields['rank'] == 'Staff')
146
            {
147
                $arrrank[$i] = R_STAFF;
148
            }
149
                elseif ($elist -> fields['rank'] == 'Member')
150
            {
151
                $arrrank[$i] = R_MEMBER;
152
            }
153
                else
154
            {
155
                $arrrank[$i] = $elist -> fields['rank'];
156
            }
157
            $arrid[$i] = $elist -> fields['id'];
158
            $arrname[$i] = $elist -> fields['user'];
159
            $arrtribe[$i] = $elist -> fields['tribe'];
160
            $elist -> MoveNext();
161
            $i = $i + 1;
162
        }
163
        $elist -> Close();
164
        $smarty -> assign (array("Enemyid" => $arrid,
165
                                 "Enemyname" => $arrname,
166
                                 "Enemytribe" => $arrtribe,
167
                                 "Enemyrank" => $arrrank,
168
                                 "Lid" => L_ID,
169
                                 "Lname" => L_NAME,
170
                                 "Lrank" => L_RANK,
171
                                 "Lclan" => L_CLAN,
172
                                 "Loption" => L_OPTION,
173
                                 "Aattack" => A_ATTACK));
174
    }
175
}
176
177
/**
178
* Start battle
179
*/
180
181
if (isset($_GET['battle']))
182
{
183
// Protection from attacks immediately after reset.
184
// Will work only with resets at full hour, need to be adapted otherwise.
185
    $arrResets = array( 0,8,10,12,14,16,18,20,22,24);
186
    $intTimestamp = time();  // current time (Unix timestamp)
187
    $year = date("Y");
188
    $month = date("m");
189
    $day = date("d");
190
    $hour = date("H");
191
    $blnTest = true;
192
    for($i = 0; $i < 9; $i++) // count to "number of resets" -1 !
193
        if( $arrResets[$i] <= $hour && $hour < $arrResets[$i+1]) // find between which resets we are
194
        {// reset gap was found
195
            $start = mktime ($arrResets[$i],0,0,$month,$day,$year);
196
            $stop = mktime ($arrResets[$i],5,0,$month,$day,$year);
197
            if( $intTimestamp >= $start && $intTimestamp < $stop)
198
                error(TOO_SOON.' '.$arrResets[$i].':'.'05.');
199
            break;
200
        }
201
202
    global $runda;
203
    global $number;
204
    global $newdate;
205
    global $smarty;
206
    global $db;
207
    
208
    
209
    if (!strictInt($_GET['battle']))
210
    {
211
        error(ERROR);
212
    }
213
214
215
    $attacker = new Fighter($player -> id);
216
    $defender = new Fighter($_GET['battle']);
217
218
219
    if (!$defender -> id)
220
    {
221
        error(NO_PLAYER);
222
    }
223
    if ($defender -> id == $attacker -> id)
224
    {
225
        error(SELF_ATTACK);
226
    }
227
    if ($defender -> hp <= 0)
228
    {
229
        error($defender -> user." ".IS_DEAD);
230
    }
231
    if ($attacker -> hp <= 0)
232
    {
233
        error(YOU_DEAD);
234
    }
235
    if ($player -> energy < 1)
236
    {
237
        error(NO_ENERGY);
238
    }
239
    if ($defender -> tribe == $attacker -> tribe && $defender -> tribe > 0)
240
    {
241
        error(YOUR_CLAN);
242
    }
243
    if ($attacker -> age < 3)
244
    {
245
        error(TOO_YOUNG);
246
    }
247
    if ($defender -> age < 3)
248
    {
249
        error(TOO_YOUNG2);
250
    }
251
    if ($attacker -> clas == '')
252
    {
253
        error(NO_CLASS);
254
    }
255
    if ($defender -> clas == '')
256
    {
257
        error(NO_CLASS2);
258
    }
259
    if ($attacker -> noarrows)
260
    {
261
        error(NO_ARROWS);
262
    }
263
    if (($attacker -> clas == 'Wojownik' || $attacker -> clas == 'Rzemieślnik' || $attacker -> clas == 'Barbarzyńca' || $attacker -> clas == 'Złodziej') && $attacker -> usespell)
264
    {
265
        error(BAD_CLASS);
266
    }
267
268
    $span =  ($attacker -> level - $defender -> level);
269
    if ($span > 0)
270
    {
271
        error(TOO_LOW);
272
    }
273
    if ($attacker -> immunited == 'Y')
274
    {
275
        error(IMMUNITED);
276
    }
277
    if ($defender -> immunited == 'Y')
278
    {
279
        error(IMMUNITED2);
280
    }
281
    if ($attacker -> clas == 'Mag' && $player -> mana == 0 && $attacker -> usespell)
282
    {
283
        error(NO_MANA);
284
    }
285
    if ($player -> location != $defender -> location)
286
    {
287
        error(BAD_LOCATION);
288
    }
289
    if ($defender -> rest == 'Y')
290
    {
291
        error(PLAYER_R);
292
    }
293
    if ($defender -> fight != 0)
294
    {
295
        error(PLAYER_F);
296
    }
297
298
    $smarty -> assign (array("Enemyname" => $defender -> user,
299
                             "Versus" => VERSUS,
300
                             "Action" => ''));
301
    $db -> Execute("UPDATE `players` SET `energy`=`energy`-1 WHERE `id`=".$player -> id);
302
    $smarty -> display ('battle.tpl');
303
    require_once('includes/battle.php');
304
305
    pvpfight($attacker,$defender);
306
    require_once("includes/foot.php");
307
    exit;
308
}
309
310
311
/**
312
* Figth with monsters
313
*/
314
if (isset ($_GET['action']) && $_GET['action'] == 'monster')
315
{
316
    if ($player -> location == 'Lochy')
317
    {
318
        error(ERROR);
319
    }
320
    if (!isset($_GET['fight']) && !isset($_GET['fight1']))
321
    {
322
        $monster = $db -> Execute("SELECT `id`, `name`, `level`, `hp` FROM `monsters` WHERE `location`='".$player -> location."' ORDER BY `level` ASC");
323
        $arrid = array();
324
        $arrname = array();
325
        $arrlevel = array();
326
        $arrhp = array();
327
        $i = 0;
328
        while (!$monster -> EOF)
329
        {
330
            $arrid[$i] = $monster -> fields['id'];
331
            $arrname[$i] = $monster -> fields['name'];
332
            $arrlevel[$i] = $monster -> fields['level'];
333
            $arrhp[$i] = $monster -> fields['hp'];
334
            $monster -> MoveNext();
335
            $i = $i + 1;
336
        }
337
        $monster -> Close();
338
        $smarty -> assign (array("Enemyid" => $arrid,
339
                                 "Enemyname" => $arrname,
340
                                 "Enemylevel" => $arrlevel,
341
                                 "Enemyhp" => $arrhp,
342
                                 "Monsterinfo" => MONSTER_INFO,
343
                                 "Mname" => M_NAME,
344
                                 "Mlevel" => M_LEVEL,
345
                                 "Mhealth" => M_HEALTH,
346
                                 "Mfast" => M_FAST,
347
                                 "Mturn" => M_TURN,
348
                                 "Abattle" => A_BATTLE,
349
                                 "Orback2" => OR_BACK2,
350
                                 "Bback2" => B_BACK2));
351
    }
352
353
        //
354
    // Monsters info
355
    //
356
    if (isset ($_GET['view']))
357
    {
358
        if (!strictInt($_GET['view']))
359
            {
360
                error(ERROR);
361
            }
362
            $monsters = $db -> Execute("SELECT `id`, `name`, `level`, `hp`, `description`, `avatar` FROM `monsters` WHERE `id`=".$_GET['view']);
363
            if (!$monsters -> fields['id'])
364
            {
365
                error(NO_CLAN);
366
            }
367
            $plik = 'images/beasts/'.$monsters -> fields['avatar'];
368
            if (is_file($plik))
369
            {
370
                $arrImageparams = getimagesize($plik);
371
                if ($arrImageparams[0] > 200)
372
                {
373
                    $arrImageparams[0] = 200;
374
                }
375
                if ($arrImageparams[1] > 150)
376
                {
377
                    $arrImageparams[1] = 150;
378
                }
379
                $smarty -> assign ('Avatar', '<center><img src="'.$plik.'" width="'.$arrImageparams[0].'" height="'.$arrImageparams[1].'" /></center><br />');
380
            }
381
            else
382
            {
383
                $smarty -> assign ('Avatar', '<center><img src="images\beasts\default.jpg" width="'.$arrImageparams[0].'" height="'.$arrImageparams[1].'" /></center><br />');
384
            }
385
            $smarty -> assign(array('Id' => $monsters -> fields['id'],
386
            	                    'Name' => $monsters -> fields['name'],
387
                                    'Level' => $monsters -> fields['level'],
388
                                    'Health' => $monsters -> fields['hp'],
389
                                    'Description' => $monsters -> fields['description'],
390
                                    'Tdescription' => T_DESCRIPTION,
391
                                    'Thp' => T_HP,
392
                                    'Tlevel' => T_LEVEL,
393
                                    'Msz_walka' => M_SZ_WALKA,
394
                                    'Mt_walka' => M_T_WALKA,
395
                                    'Aback' => A_BACK,));
396
    }
397
398
    if (isset($_GET['dalej']) || isset($_GET['next']))
399
    {
400
        $smarty -> assign(array("Abattle2" => A_BATTLE2,
401
                                "Witha" => WITH_A,
402
                                "Nend" => N_END));
403
    }
404
    if (isset($_GET['dalej']))
405
    {
406
407
        if (!strictInt($_GET['dalej']))
408
        {
409
            error(ERROR);
410
        }
411
        $en = $db -> Execute("SELECT id, name, location FROM monsters WHERE id=".$_GET['dalej']);
412
        if ($en -> fields['location'] != $player -> location)
413
        {
414
            error(ERROR);
415
        }
416
        $smarty -> assign ( array("Id" => $en -> fields['id'],
417
                                  "Name" => $en -> fields['name'],
418
                                  "Mtimes" => M_TIMES));
419
        $en -> Close();
420
    }
421
    if (isset ($_GET['next']))
422
    {
423
        if (!strictInt($_GET['next']))
424
        {
425
            error(ERROR);
426
        }
427
        $en = $db -> Execute("SELECT id, name, location FROM monsters WHERE id=".$_GET['next']);
428
        if ($en -> fields['location'] != $player -> location)
429
        {
430
            error(ERROR);
431
        }
432
        $smarty -> assign ( array("Id" => $en -> fields['id'],
433
                                  "Name" => $en -> fields['name']));
434
    }
435
    //
436
    //Turn fight with monsters
437
    //
438
    if (isset($_GET['fight1']))
439
    {
440
        global $arrehp;
441
        global $newdate;
442
        require_once("includes/turnfight.php");
443
        if (isset ($_POST['write']) && $_POST['write'] == 'Y') {
444
		$_POST['razy'] = (int)$_POST['razy'];
445
		$_SESSION['amount'] = $_POST['razy'];
446
		if (!isset($_POST['razy']) || $_POST['razy'] == '') {
447
			error(ERROR);
448
			}
449
		if (!strictInt($_GET['fight1']) || !strictInt($_POST['razy'])) {
450
			error(NO_ID);
451
			}
452
		if ($player -> hp <= 0) {
453
			error(NO_HP);
454
			}
455
		if ($_POST['razy'] > 20) {
456
			error(TOO_MUCH_MONSTERS);
457
			}
458
		if ($player -> energy < $_POST['razy'] && !isset($_POST['action'])) {
459
			error(NO_ENERGY2);
460
			}
461
		$attacker = new Fighter($player->id);
462
		$monster = new Monster((int)$_GET['fight1'],1,0);
463
		if (!$monster -> id) {
464
			error(NO_MONSTER);
465
			}
466
		if ($player -> clas == '') {
467
			error(NO_CLASS3);
468
			}
469
		$db -> Execute('UPDATE `players` SET `fight`='.$monster -> id.', `energy` = `energy` - '.$_POST['razy'].' WHERE `id`='.$player -> id);
470
		$_POST['write'] = 'N';
471
472
//prepare session variables for monsters and player
473
		for ($k = 0; $k < $_SESSION['amount']; $k++) {
474
			//each monster identifier
475
//			$strIndex = 'mon'.$k;
476
			$_SESSION['mon'.$k]['id'] = $monster -> id;
477
			//each monster hit points
478
// 			$strIndex = 'monhp'.$k;
479
			$_SESSION['mon'.$k]['hp'] = $monster -> hp;
480
			//each monster action points
481
// 			$strIndex = 'monap'.$k;
482
			if ($attacker -> speed > $monster -> attackspeed) {
483
				$_SESSION['mon'.$k]['ap'] = 1;
484
				}
485
			else {
486
				$_SESSION['mon'.$k]['ap'] = floor($monster -> attackspeed / $attacker -> speed);
487
				if ($_SESSION['mon'.$k]['ap'] > 5) {
488
					$_SESSION['mon'.$k]['ap'] = 5;
489
					}
490
				}
491
			$tmpActionArr[$k][0] = $monster -> speed;
492
			$tmpActionArr[$k][1] = $k;
493
			}
494
		$tmpActionArr[$k][0] = $attacker -> speed;
495
		$tmpActionArr[$k][1] = -1;
496
497
		/**
498
		* function to compare elements of actionArr
499
		*/
500
		function aacmp($a,$b) {
501
			if ($a[0] == $b[0]) return 0;
502
			return ($a[0] > $b[0]) ? -1 : 1;
503
			}
504
505
		usort($tmpActionArr,"aacmp");
506
		for ($k = 0; $k <= $_SESSION['amount']; $k++) {
507
			$actionArr[$k] = $tmpActionArr[$k][1];
508
			}
509
		$_SESSION['actionArr'] = $actionArr;
510
		$_SESSION['exhaust']=0;
511
		if ($attacker -> speed > $monster -> speed) {
512
			$_SESSION['points'] = floor($attacker -> speed / $monster -> speed);
513
			if ($_SESSION['points'] > 5) {
514
				$_SESSION['points'] = 5;
515
				}
516
			}
517
		else {
518
			$_SESSION['points'] = 1;
519
			}
520
		$_SESSION['round']=0;
521
		}
522
	if (isset($monster -> id)) $oppid = $monster -> id;
523
	else $oppid = (int)$_GET['fight1'];
524
525
	turnfight("battle.php?action=monster&fight1=".$oppid);
526
	if (isset($_SESSION['result'])) unset($_SESSION['result']);
527
	require_once("includes/foot.php");
528
	exit;
529
530
    }
531
532
    //
533
    //Fast fight with monsters
534
    //
535
    if (isset($_GET['fight']))
536
    {
537
        global $newdate;
538
	global $db;
539
540
	require_once('includes/battle.php');
541
542
        if (!strictInt($_GET['fight']))
543
        {
544
            error(ERROR);
545
        }
546
        if (!isset($_POST['razy']))
547
        {
548
            $_POST['razy'] = 1;
549
        }
550
        if (!strictInt($_POST['razy']))
551
        {
552
            error(ERROR);
553
        }
554
        if (!isset($_POST['times']))
555
        {
556
            error(ERROR);
557
        }
558
        if (!strictInt($_POST['times']))
559
        {
560
            error(ERROR);
561
        }
562
        if (isset($_SESSION['amount']))
563
        {
564
            error(ERROR);
565
        }
566
        if ($player -> hp <= 0)
567
        {
568
            error(NO_HP);
569
        }
570
        if ($_POST['razy'] > 20)
571
        {
572
            error(TOO_MUCH_MONSTERS);
573
        }
574
        $lostenergy = $_POST['razy'] * $_POST['times'];
575
        if ($player -> energy < $lostenergy)
576
        {
577
            error(NO_ENERGY2);
578
        }
579
580
	$attacker = new Fighter($player->id);
581
	$monster = new Monster((int)$_GET['fight'],$_POST['razy'],0);
582
583
        if (!$monster -> id)
584
        {
585
            error(NO_MONSTER);
586
        }
587
        if ($attacker -> clas == '')
588
        {
589
            error(NO_CLASS3);
590
        }
591
592
	pvmfastfight($attacker,$monster,$_POST['times'],$_POST['razy']);
593
	require_once("includes/foot.php");
594
	exit;
595
    }
596
}
597
598
/**
599
* Initialization of variables
600
*/
601
if (!isset($_GET['battle']))
602
{
603
    $_GET['battle'] = '';
604
}
605
606
if (!isset($_GET['step']))
607
{
608
    $_GET['step'] = '';
609
}
610
611
if (!isset($_GET['fight']))
612
{
613
    $_GET['fight'] = '';
614
}
615
616
if (!isset($_GET['fight1']))
617
{
618
    $_GET['fight1'] = '';
619
}
620
621
if (!isset($_GET['dalej']))
622
{
623
    $_GET['dalej'] = '';
624
}
625
626
if (!isset($_GET['next']))
627
{
628
    $_GET['next'] = '';
629
}
630
631
if (!isset($_GET['action']))
632
{
633
    $_GET['action'] = '';
634
}
635
if (!isset($_GET['view']))
636
{
637
    $_GET['view'] = '';
638
}
639
640
/**
641
* Assign variables and display page
642
*/
643
$smarty -> assign (array("Action" => $_GET['action'],
644
                         "Battle" => $_GET['battle'],
645
                         "Step" => $_GET['step'],
646
                         "Fight" => $_GET['fight'],
647
                         "Fight1" => $_GET['fight1'],
648
                         "Dalej" => $_GET['dalej'],
649
                         "Next" => $_GET['next'],
650
                         "View" => $_GET['view']));
651
$smarty -> display ('battle.tpl');
652
653
require_once("includes/foot.php");
654
?>