-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadventure.inc.php
executable file
·347 lines (313 loc) · 11.2 KB
/
adventure.inc.php
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
<?php
include_once("conn.inc");
//LAST UPDATED 12-10-05
/*******
Adventure object
Users get assigned an adventure object, which determines if they are going to fight
a monster or participate in an encounter. Encounters could be traps or random treasures.
Fights are obviously fights with monsters.
*******/
class adventure
{
var $location = 1;
var $type = 1;
var $monster = array();
var $myAdventure = array();
var $resource = "";
var $completed = false;
var $experience = 0;
var $gold = 0;
var $item = 0;
var $lastMonsterDamage=0;
function adventure($adventureIn, $levelMax)
{
$adventureIn = str_replace("_"," ",$adventureIn);
$query = "SELECT name,location,level FROM places WHERE name='{$adventureIn}'";
$result = mysql_query($query) or die ("error loading location from database");
$row = mysql_fetch_array( $result );
$this->location = $row['location'];
$this->type = $this->rolldice(1,2);
switch ($this->type)
{
case 1:
//go on an adventure
$this->newAdventure($levelMax-5);
break;
case 2:
//fight a monster
$this->monster($levelMax);
break;
}
}
function showAdventure()
{
$toReturn;
if ( !$this->isCompleted() )
{
switch ($this->type)
{
case 1:
$toReturn .= "<strong>" . $this->myAdventure['name'] . "</strong> <br />";
$toReturn .= "<img src=\"{$this->resource}\" /><br />";
$toReturn .= $this->myAdventure['description'];
break;
case 2:
$toReturn .= "There is a {$this->monster['monstername']} for you to do glorious battle with!<br/>";
$toReturn .= "<img src=\"{$this->resource}\" /><br />";
break;
}
}
else
{
switch ($this->type)
{
case 1:
$toReturn .= "<strong>" . $this->myAdventure['name'] . "</strong>";
$toReturn .= "<img src=\"{$this->resource}\" /><br />";
$toReturn .= $this->myAdventure['description'];
break;
case 2:
$toReturn = "You defeated the monster!<br/>";
break;
}
}
return $toReturn;
}
function complete()
{
$this->completed = true;
}
function fail()
{
$this->completed = true;
/* if ($this->type == 1) {
$this->myAdventure['rewardItemID'] = 0;
}
if ($this->type == 2) {
$this->monster['item'] = 0;
$this->monster['gold'] = 0;
}
*/
$this->item = 0;
$this->gold = 0;
$this->experience = 0;
}
function getReward()
{
$reward = array();
$reward['experience'] = $this->experience;
$reward['gold'] = $this->gold;
$reward['item'] = $this->item;
return $reward;
}
function newAdventure($userlevel)
{
//Select all adventures for this location
$query = "SELECT adventureID,name,description,testAgainst,testValue,completeText,failText,rewardExperience,rewardItemID,location,level FROM adventures WHERE location={$this->location}";
$result = mysql_query($query) or die ("error finding an adventure");
//Select a single one to use
$num = mysql_num_rows($result);
if ($num > 1 && $num != 0) {
//If there is more than one adventure for this location in the database, we need to randomly pick one of them
//Roll a random number between zero and the number of adventures-1. mysql_data_seek will move the pointer for
//the next data in the result array to be whatever the die roll is, and then we will fetch the associative array.
$newnum = $this->rolldice(0, $num-1);
mysql_data_seek($result, $newnum);
$keeper = mysql_fetch_assoc($result);
} elseif ($num != 0) {
$keeper = mysql_fetch_assoc($result);
}
$this->myAdventure = $keeper;
/*
$levelDiff = $this->myAdventure['level'] - $userlevel;
$levelMod = $levelDiff * 10;
$this->experience = max(1, $this->myAdventure['rewardExperience'] + $levelMod);
*/
$baseExp = $this->myAdventure['rewardExperience'];
$levelMod = ($this->myAdventure['level'] - $userlevel) * 2;
$this->experience = max(1, ($baseExp + $levelMod));
$this->experience = 10;
if ($this->myAdventure["rewardItemID"] > 0) {
$this->item = $this->myAdventure["rewardItemID"];
}
$this->resource = "images/adventures/{$this->myAdventure['adventureID']}.gif";
}
function monster($max)
{
//create a new monster
$query = "SELECT monstername,level,hp,str,dex,gold,item, itemChance FROM monsters WHERE location={$this->location} ORDER BY level";
$result = mysql_query($query) or die ("error loading monster from database");
//select a monster to use if more than one is found
if (mysql_num_rows($result)>1)
{
while ( $row = mysql_fetch_array( $result ) )
{
$keeper=$row;
$roll = $this->rolldice(1,3);
//users have a better chance of hitting a lower level monster
if ($roll < 3)
break;
}
}
else
{
$keeper = mysql_fetch_array($result);
}
$this->monster = $keeper;
//experience = 10 + the difference in the monsters level and the player's level
//So a monster with a higher level than the player will give the player more exp
//and a monster with a lower level than the player will give the player less exp
//Max is user level + 5
//$this->experience = max( 1, (10 + ( $this->monster['level'] - ( $max-5 ) ) ) );
$baseExp = 10;
$levelMod = ( $this->monster['level'] - ($max-5) ) * 2;
$this->experience = max(1, $baseExp + $levelMod);
//Determine if user will recieve an item from this monster
//Roll against itemChance; if lessthan or equal to itemchance, then you get the item
//Maybe someday luck will play a factor in this...
$roll = $this->rolldice(1,100);
if ($roll < $this->monster['itemChance'] || $roll == $this->monster['itemChance']) {
$this->item=$this->monster['item'];
} else {
$this->item = 0;
}
$this->gold = $this->monster['gold'];
$this->resource = "images/monsters/" . str_replace(" ", "_", $this->monster['monstername']) . ".gif";
}
function testAdventure($userstats)
{
$msg = array();
switch ($this->myAdventure['testAgainst']) {
case 'dex':
if ($userstats['dex'] > $this->myAdventure['testValue']) {
$this->complete();
$msg["response"] = "<strong id=\"response\">" . $this->myAdventure["completeText"] . "</strong>";
} else {
$this->fail();
$msg["response"] = "<strong id=\"response\">" . $this->myAdventure["failtext"] . "</strong>";
}
break;
case 'str':
if ($userstats['str'] > $this->myAdventure['testValue']) {
$this->complete();
$msg["response"] = "<strong id=\"response\">" . $this->myAdventure["completeText"] . "</strong>";
} else {
$this->fail();
$msg["response"] = "<strong id=\"response\">" . $this->myAdventure["failtext"] . "</strong>";
}
break;
default:
$this->complete();
$msg["response"] = "<strong id=\"response\">" . $this->myAdventure["completeText"] . "</strong>";
break;
}
return $msg;
}
function performCommand($command,$userstats)
{
$msg = array();
switch ($this->type)
{
case 1:
break; //case 1
case 2:
switch ($command)
{
case "Attack":
$msg['message'] = $this->attackMonster($userstats);
if ($this->isCompleted() == false) {
$msg['response'] = "<strong id=\"response\">" . $this->monsterAttacks($userstats) . "</strong>";
$msg ['damage'] = $this->lastMonsterDamage;
}
break; //case "Attack"
case "Run":
$msg['message'] = $this->tryToRun($userstats);
if ($this->isCompleted() == false) {
$msg['response'] = "<strong id=\"response\">" . $this->monsterAttacks($userstats) . "</strong>";
$msg ['damage'] = $this->lastMonsterDamage;
}
break; //case "Run"
}
break; //case 2
}
return $msg;
}
function tryToRun ($userstats)
{
$msg = "";
//try to run
$udex = $userstats['dex'];
$mdex = $this->monster['dex'];
if ( $udex > $mdex ) $bonus = $udex - $mdex; else $bonus = 0;
$roll = $this->rolldice(1,6) + $bonus;
if ( $roll > 4 )
{
$msg .= "You ran, you coward.";
$this->fail();
}
else
{
$msg .= "You failed to run!";
}
return $msg;
}
function monsterAttacks ($userstats)
{
$msg = "";
if ($this->rollToHit( $this->monster['dex'], $userstats['dex'] ) )
{
$this->lastMonsterDamage = $this->rollDamage( $this->monster['str'], $userstats['str'] );
$msg .= "<br/>The {$this->monster['monstername']} hits for {$this->lastMonsterDamage} damage!";
} else $msg .= "<br/>The {$this->monster['monstername']} misses!";
return $msg;
}
function attackMonster($userstats)
{
$msg .= "";
//attack the monster
for ($classAttacks = $userstats['attacks']; $classAttacks > 0 && $this->isCompleted() == false; $classAttacks--)
{
//if the user manages to hit the monster, roll for damage
if( $this->rollToHit( $userstats['dex'], $this->monster['dex'] ) )
{
$damage = $this->rollDamage( $userstats['str'],$this->monster['str'] );
$this->monster['hp'] -= $damage;
$msg .= "<br/>You hit for {$damage} damage!";
if ($this->monster['hp'] <= 0)
{
$msg .= "<br/>You killed your enemy!";
$this->complete();
}
}
else $msg .= "<br/>You miss!";
}
return $msg;
}
function rollToHit( $aDex, $dDex )
{
//aDex = attacker's dexterity, dDex = defender's dexterity
//boolean
if ( $aDex > $dDex ) $bonus = $aDex - $dDex; else $bonus = 0;
$roll = $this->rolldice(1,6) + $bonus;
if ($roll > 4) return true; else return false;
}
function rollDamage( $aStr, $dStr )
{
$dammin = max( $aStr - $dStr, 1);
$damage = $this->rolldice( $dammin, $aStr );
return $damage;
}
function isCompleted()
{
return $this->completed;
}
function rolldice($low,$high)
{
return round(rand($low,$high));
}
function getType()
{
return $this->type;
}
}
?>