| 1 | package Games::EternalLands::Bot; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp qw(confess cluck carp); | ||
| 5 | use IO::Socket; | ||
| 6 | use POSIX; | ||
| 7 | use YAML; | ||
| 8 | use Games::EternalLands::Client; | ||
| 9 | use Games::EternalLands::MapHelper ':all'; | ||
| 10 | use Data::Dumper; | ||
| 11 | use vars qw(@ISA); | ||
| 12 | |||
| 13 | @ISA = qw(Games::EternalLands::Client); | ||
| 14 | |||
| 15 | use Games::EternalLands::Constants qw(:Debug :TypeContainers); | ||
| 16 | |||
| 17 | our $VERSION = '0.04'; | ||
| 18 | |||
| 19 | my $ACTOR_TYPE_NPC = 2; | ||
| 20 | |||
| 21 | ################################################ | ||
| 22 | |||
| 23 | |||
| 24 | ################################################ | ||
| 25 | |||
| 26 | sub getHeight | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my ($x,$y) = @_; | ||
| 30 | |||
| 31 | my $hMap = $self->{'Map'}->{'hMap'}; | ||
| 32 | my $wdth = $self->{'Map'}->{'width'}; | ||
| 33 | my $hght = $self->{'Map'}->{'height'}; | ||
| 34 | |||
| 35 | return Games::EternalLands::MapHelper::getZ($hMap,$wdth,$hght,$x,$y); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub randomLocation | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | my ($x,$y,$h); | ||
| 43 | my $width = $self->{'Map'}->{'width'}; | ||
| 44 | my $height = $self->{'Map'}->{'height'}; | ||
| 45 | do { | ||
| 46 | $x = int(rand($width)); | ||
| 47 | $y = int(rand($height)); | ||
| 48 | $h = $self->getHeight($x,$y); | ||
| 49 | } while ($h == 0); | ||
| 50 | |||
| 51 | return [$x,$y]; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub send | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | my ($cmd,$data) = @_; | ||
| 58 | |||
| 59 | if (defined($ActiveCommands{$cmd})) { | ||
| 60 | $self->{'lastUsedMapObject'} = undef; | ||
| 61 | } | ||
| 62 | $self->SUPER::send($cmd,$data); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub saveKnowledge | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | if (defined($self->{'knowledgeFile'})) { | ||
| 70 | my $tmp = $self->{'knowledgeFile'}.".tmp"; | ||
| 71 | YAML::DumpFile($tmp, $self->{'knowledge'}); | ||
| 72 | rename($tmp,$self->{'knowledgeFile'}); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub rememberActor($$) | ||
| 77 | { | ||
| 78 | my $self = shift; | ||
| 79 | my ($new) = @_; | ||
| 80 | |||
| 81 | my $name = lc($new->{'name'}); | ||
| 82 | if (!defined($name)) { | ||
| 83 | confess "Actors name is not defined"; | ||
| 84 | return undef; | ||
| 85 | } | ||
| 86 | |||
| 87 | $self->{'knowledge'}->{'actors'}->{$name} = { | ||
| 88 | 'name' => $name, | ||
| 89 | 'map' => $new->{'map'}, | ||
| 90 | 'x' => $new->{'xpos'}, | ||
| 91 | 'y' => $new->{'ypos'}, | ||
| 92 | }; | ||
| 93 | $self->saveKnowledge(); | ||
| 94 | |||
| 95 | if ($self->{'debug'} & $DEBUG_TEXT) { | ||
| 96 | my $name = $new->{'name'} || 'undef'; | ||
| 97 | my $map = $new->{'map'} || 'undef'; | ||
| 98 | my $x = $new->{'xpos'} || 'undef'; | ||
| 99 | my $y = $new->{'ypos'} || 'undef'; | ||
| 100 | $self->Log("Remembering Actor '$name' at $map($x,$y)"); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub getNPCLocation | ||
| 106 | { | ||
| 107 | my $self = shift; | ||
| 108 | my ($name) = @_; | ||
| 109 | |||
| 110 | my $npc = $self->{'knowledge'}->{'actors'}->{lc($name)}; | ||
| 111 | |||
| 112 | if (wantarray) { | ||
| 113 | return defined($npc) ? ($npc->{'map'},$npc->{'x'},$npc->{'y'}) : (undef,undef,undef); | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | return defined($npc) ? [$npc->{'map'},$npc->{'x'},$npc->{'y'}] : undef; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub rememberExit($$) | ||
| 121 | { | ||
| 122 | my $self = shift; | ||
| 123 | my ($new) = @_; | ||
| 124 | |||
| 125 | my $id = $new->{'id'}; | ||
| 126 | if (!defined($id)) { | ||
| 127 | confess("exit id is not defined !"); | ||
| 128 | return undef; | ||
| 129 | } | ||
| 130 | my $from = $new->{'from'}; | ||
| 131 | if (!defined($from)) { | ||
| 132 | confess("exit from is not defined !"); | ||
| 133 | return undef; | ||
| 134 | } | ||
| 135 | |||
| 136 | my $exit = $self->{'knowledge'}->{'exitsByMap'}->{$from}->{$id} || {}; | ||
| 137 | map {$exit->{$_} = $new->{$_}} (keys %$new); | ||
| 138 | $self->{'knowledge'}->{'exitsByMap'}->{$from}->{$id} = $exit; | ||
| 139 | |||
| 140 | if ($self->{'debug'} & $DEBUG_TEXT) { | ||
| 141 | my $to = $exit->{'to'} || 'undef'; | ||
| 142 | my $x = $exit->{'x'} || 'undef'; | ||
| 143 | my $y = $exit->{'y'} || 'undef'; | ||
| 144 | $self->Log("Remembering exit($id) from $from to $to($x,$y)"); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | sub rememberHarvest($$) | ||
| 149 | { | ||
| 150 | my $self = shift; | ||
| 151 | my ($new) = @_; | ||
| 152 | |||
| 153 | my $id = $new->{'id'}; | ||
| 154 | if (!defined($id)) { | ||
| 155 | confess "id of harvest is not defined"; | ||
| 156 | return undef; | ||
| 157 | } | ||
| 158 | my $map = $new->{'map'}; | ||
| 159 | if (!defined($map)) { | ||
| 160 | confess "map of harvest is not defined"; | ||
| 161 | return undef; | ||
| 162 | } | ||
| 163 | |||
| 164 | my $harv = $self->{'knowledge'}->{'harvByMap'}->{$map}->{'byID'}->{$id} || {}; | ||
| 165 | map {$harv->{$_} = $new->{$_}} (keys %$new); | ||
| 166 | $self->{'knowledge'}->{'harvByMap'}->{$map}->{'byID'}->{$id} = $harv; | ||
| 167 | |||
| 168 | if (my $name = $new->{'name'}) { | ||
| 169 | $name =~ s/\.\s*$//; | ||
| 170 | $new->{'name'} = lc($name); | ||
| 171 | |||
| 172 | my $found = 0; | ||
| 173 | my $hList = $self->{'knowledge'}->{'harvByMap'}->{$map}->{'byName'}->{$name} || []; | ||
| 174 | foreach my $h (@{$hList}) { | ||
| 175 | if ($h eq $harv) { | ||
| 176 | $found = $1; | ||
| 177 | last; | ||
| 178 | } | ||
| 179 | }; | ||
| 180 | if (! $found) { | ||
| 181 | push(@{$hList},$harv); | ||
| 182 | $self->{'knowledge'}->{'harvByMap'}->{$map}->{'byName'}->{$name} = $hList; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | if ($self->{'debug'} & $DEBUG_TEXT) { | ||
| 187 | my $name = $harv->{'name'} || "'unkown'"; | ||
| 188 | $self->Log("Remembering harvest($id) on $map is a $name"); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub getExitDetails($$$) | ||
| 193 | { | ||
| 194 | my $self = shift; | ||
| 195 | my ($map,$id) = @_; | ||
| 196 | |||
| 197 | if (!defined($map)) { | ||
| 198 | $map = $self->crntMap(); | ||
| 199 | } | ||
| 200 | |||
| 201 | (exists $self->{'knowledge'}->{'exitsByMap'}->{$map}) || return undef; | ||
| 202 | return $self->{'knowledge'}->{'exitsByMap'}->{$map}->{$id}; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub connectedMaps | ||
| 206 | { | ||
| 207 | my $self = shift; | ||
| 208 | my ($map) = @_; | ||
| 209 | |||
| 210 | if (!defined($map)) { | ||
| 211 | $map = $self->crntMap(); | ||
| 212 | } | ||
| 213 | my %maps; | ||
| 214 | my $exits = $self->{'knowledge'}->{'exitsByMap'}->{$map}; | ||
| 215 | if (defined($exits)) { | ||
| 216 | foreach my $e (values %$exits) { | ||
| 217 | if (defined($e->{'to'})) { | ||
| 218 | $maps{$e->{'to'}} = 1; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | my @maps = keys(%maps); | ||
| 223 | if (wantarray) { | ||
| 224 | return @maps; | ||
| 225 | } | ||
| 226 | return ($#maps >= 0) ? \@maps : undef; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub knownMaps | ||
| 230 | { | ||
| 231 | my $self = shift; | ||
| 232 | |||
| 233 | my %Maps; | ||
| 234 | |||
| 235 | foreach my $m (keys %{$self->{'knowledge'}->{'exitsByMap'}}) { | ||
| 236 | $Maps{$m} = 1 | ||
| 237 | } | ||
| 238 | foreach my $m (keys %{$self->{'knowledge'}->{'harvByMap'}}) { | ||
| 239 | $Maps{$m} = 1 | ||
| 240 | } | ||
| 241 | return (keys %Maps); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub allHarvestables | ||
| 245 | { | ||
| 246 | my $self = shift; | ||
| 247 | my ($map) = @_; | ||
| 248 | |||
| 249 | if (!defined($map)) { | ||
| 250 | $map = $self->crntMap(); | ||
| 251 | } | ||
| 252 | if (my $byID = $self->{'knowledge'}->{'harvByMap'}->{$map}->{'byID'}) { | ||
| 253 | return keys(%$byID); | ||
| 254 | } | ||
| 255 | return wantarray ? () : undef; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub findHarvest | ||
| 259 | { | ||
| 260 | my $self = shift; | ||
| 261 | my ($map,$name) = @_; | ||
| 262 | |||
| 263 | if (!defined($map)) { | ||
| 264 | $map = $self->crntMap(); | ||
| 265 | } | ||
| 266 | |||
| 267 | my $hList = undef; | ||
| 268 | my $byMap = $self->{'knowledge'}->{'harvByMap'}; | ||
| 269 | if (exists $byMap->{$map}->{'byName'}->{$name}) { | ||
| 270 | $hList = $byMap->{$map}->{'byName'}->{$name}; | ||
| 271 | } | ||
| 272 | if (wantarray) { | ||
| 273 | return defined($hList) ? @$hList : (); | ||
| 274 | } | ||
| 275 | return $hList; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub eatThese | ||
| 279 | { | ||
| 280 | my $self = shift; | ||
| 281 | my @eatList = @_; | ||
| 282 | |||
| 283 | $self->{'eatList'} = \@eatList; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub eatSomething | ||
| 287 | { | ||
| 288 | my $self = shift; | ||
| 289 | my ($what) = @_; | ||
| 290 | |||
| 291 | my $eatList = defined($what) ? $what : $self->{'eatList'}; | ||
| 292 | if ($self->{'lastEat'} < time()-90) { | ||
| 293 | $self->{'lastEat'} = time(); | ||
| 294 | foreach my $what (@$eatList) { | ||
| 295 | if ($self->qtyOnHand(lc($what)) > 0) { | ||
| 296 | $self->Log("Eating $what"); | ||
| 297 | $self->useInventoryItem(lc($what)); | ||
| 298 | return 1; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 | return 0; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub findExit | ||
| 306 | { | ||
| 307 | my $self = shift; | ||
| 308 | my ($map,$fn) = @_; | ||
| 309 | |||
| 310 | my @mList = defined($map) ? ($map) : $self->knownMaps(); | ||
| 311 | |||
| 312 | my @result; | ||
| 313 | foreach my $m (@mList) { | ||
| 314 | my $byMap = $self->{'knowledge'}->{'exitsByMap'}; | ||
| 315 | if (exists $byMap->{$map}) { | ||
| 316 | foreach my $e (keys %{$byMap->{$map}}) { | ||
| 317 | if (defined($fn)) { | ||
| 318 | &{$fn}($byMap->{$map}->{$e}) && push(@result,$byMap->{$map}->{$e}); | ||
| 319 | } | ||
| 320 | else { | ||
| 321 | push(@result,$byMap->{$map}->{$e}); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | return wantarray ? @result : \@result; | ||
| 327 | } | ||
| 328 | |||
| 329 | sub harvest | ||
| 330 | { | ||
| 331 | my $self = shift; | ||
| 332 | my ($id) = @_; | ||
| 333 | |||
| 334 | $self->SUPER::harvest($id); | ||
| 335 | $self->{'harvesting'} = { | ||
| 336 | 'map' => $self->crntMap(), | ||
| 337 | 'id' => $id, | ||
| 338 | }; | ||
| 339 | } | ||
| 340 | |||
| 341 | sub getHarvestables | ||
| 342 | { | ||
| 343 | my $self = shift; | ||
| 344 | |||
| 345 | my @tmp = (); | ||
| 346 | my $h = $self->{'Map'}->{'harvestables'}; | ||
| 347 | |||
| 348 | return wantarray ? @$h : $h; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub ADD_NEW_ACTOR | ||
| 352 | { | ||
| 353 | my $self = shift; | ||
| 354 | my ($type,$len,$data) = @_; | ||
| 355 | |||
| 356 | my $actor = $self->SUPER::ADD_NEW_ACTOR($type,$len,$data); | ||
| 357 | if ($actor->{'kind'} eq $ACTOR_TYPE_NPC) { | ||
| 358 | $self->rememberActor($actor); | ||
| 359 | } | ||
| 360 | elsif ($actor->{'id'} == $self->{'my_id'}) { | ||
| 361 | if (my $exit = $self->{'lastUsedMapObject'}) { | ||
| 362 | $exit->{'toX'} = $actor->{'xpos'}; | ||
| 363 | $exit->{'toY'} = $actor->{'ypos'}; | ||
| 364 | $self->rememberExit($exit); | ||
| 365 | $self->{'lastUsedMapObject'} = undef; | ||
| 366 | $self->saveKnowledge(); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | sub ADD_NEW_ENHANCED_ACTOR | ||
| 372 | { | ||
| 373 | my $self = shift; | ||
| 374 | my ($type,$len,$data) = @_; | ||
| 375 | |||
| 376 | my $actor = $self->SUPER::ADD_NEW_ENHANCED_ACTOR($type,$len,$data); | ||
| 377 | if ($actor->{'kind'} eq $ACTOR_TYPE_NPC) { | ||
| 378 | $self->rememberActor($actor); | ||
| 379 | } | ||
| 380 | elsif ($actor->{'id'} == $self->{'my_id'}) { | ||
| 381 | if (my $exit = $self->{'lastUsedMapObject'}) { | ||
| 382 | $exit->{'toX'} = $actor->{'xpos'}; | ||
| 383 | $exit->{'toY'} = $actor->{'ypos'}; | ||
| 384 | $self->rememberExit($exit); | ||
| 385 | $self->{'lastUsedMapObject'} = undef; | ||
| 386 | $self->saveKnowledge(); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | # Try to kil the actor specified | ||
| 392 | # Returns 1 - actor has been killed | ||
| 393 | # 0 - actor escaped us | ||
| 394 | # undef - we are trying to kill it | ||
| 395 | sub killActor | ||
| 396 | { | ||
| 397 | my $self = shift; | ||
| 398 | my ($id) = @_; | ||
| 399 | |||
| 400 | my $actor = $self->{'actorsByID'}->{$id}; | ||
| 401 | defined($actor) || return 0; | ||
| 402 | |||
| 403 | my $me = $self->{'me'}; | ||
| 404 | |||
| 405 | my $dist = $self->distanceTo($actor->{'xpos'},$actor->{'ypos'}); | ||
| 406 | if ($dist >= 4) { | ||
| 407 | ($actor->{'inCombat'}) && return 0; # too far away for us to be attacking it | ||
| 408 | $self->moveCloseTo([$actor->{'xpos'},$actor->{'ypos'}],2); | ||
| 409 | return undef; | ||
| 410 | } | ||
| 411 | if ($me->{'inCombat'} || $actor->{'inCombat'}) { | ||
| 412 | return undef; | ||
| 413 | } | ||
| 414 | if ($actor->{'dead'} || ($actor->{'stats'}->{'mp'}->[0] <= 0)) { | ||
| 415 | return 1; | ||
| 416 | } | ||
| 417 | $self->attackActor($actor->{'id'}); | ||
| 418 | return undef; | ||
| 419 | } | ||
| 420 | |||
| 421 | sub RAW_TEXT | ||
| 422 | { | ||
| 423 | my $self = shift; | ||
| 424 | my ($type,$len,$data) = @_; | ||
| 425 | |||
| 426 | my $text = $self->SUPER::RAW_TEXT($type,$len,$data); | ||
| 427 | if ($text =~ m/^You need to wear a\s+(\S.*\S)\s+in order to harvest this item/) { | ||
| 428 | $self->{'harvesting'}->{'wear'} = $1; | ||
| 429 | $self->rememberHarvest($self->{'harvesting'}); | ||
| 430 | $self->saveKnowledge(); | ||
| 431 | } | ||
| 432 | elsif ($text =~ m/^You started to harvest\s+(\S.*\S)$/) { | ||
| 433 | my $name = lc($1); $name =~ s/\.$//; | ||
| 434 | $self->{'harvesting'}->{'name'} = $name; | ||
| 435 | $self->rememberHarvest($self->{'harvesting'}); | ||
| 436 | $self->saveKnowledge(); | ||
| 437 | } | ||
| 438 | elsif ($text =~ m/^You stopped harvesting/) { | ||
| 439 | $self->{'harvesting'} = undef; | ||
| 440 | } | ||
| 441 | elsif ($text =~ m/^Go to the doorstep.*click.*to get/i) { | ||
| 442 | if (my $exit = $self->{'lastUsedMapObject'}) { | ||
| 443 | $exit->{'msg'} = $text; | ||
| 444 | $self->rememberExit($exit); | ||
| 445 | $self->{'lastUsedMapObject'} = undef; | ||
| 446 | $self->saveKnowledge(); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | elsif ($text =~ m/The door is locked/i) { | ||
| 450 | if (my $exit = $self->{'lastUsedMapObject'}) { | ||
| 451 | $exit->{'msg'} = $text; | ||
| 452 | $self->rememberExit($exit); | ||
| 453 | $self->{'lastUsedMapObject'} = undef; | ||
| 454 | $self->saveKnowledge(); | ||
| 455 | } | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | ########################################################### | ||
| 460 | # | ||
| 461 | ########################################################### | ||
| 462 | |||
| 463 | sub Advertise | ||
| 464 | { | ||
| 465 | my $self = shift; | ||
| 466 | |||
| 467 | my $toSell = $self->{'itemsToSell'}; | ||
| 468 | my @forSale = (keys %{$toSell}); | ||
| 469 | @forSale = sort {$toSell->{$a}->[2] <=> $toSell->{$b}->[2]} @forSale; | ||
| 470 | my $item = $forSale[0]; | ||
| 471 | my $qty = $self->qtyInStock($item); | ||
| 472 | my $price = $toSell->{$item}->[1]; | ||
| 473 | |||
| 474 | if ($qty > 0) { | ||
| 475 | $self->Say('@@3 I am selling '."$qty $item for $price"."gc each"); | ||
| 476 | } | ||
| 477 | |||
| 478 | $toSell->{$item}->[2] = time(); | ||
| 479 | if (defined($self->{'sellingFile'})) { | ||
| 480 | YAML::DumpFile($self->{'sellingFile'}, $self->{'itemsToSell'}); | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | sub handleHelp | ||
| 485 | { | ||
| 486 | my $self = shift; | ||
| 487 | my ($user) = @_; | ||
| 488 | |||
| 489 | my $help = $self->isAdmin($user) ? $self->{'adminhelp'} : $self->{'help'}; | ||
| 490 | if (defined($help)) { | ||
| 491 | foreach my $line (@{$help}) { | ||
| 492 | $self->sendPM($user,"$line"); | ||
| 493 | } | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | sub handleDump | ||
| 498 | { | ||
| 499 | my $self = shift; | ||
| 500 | my ($user) = @_; | ||
| 501 | |||
| 502 | print STDERR Dumper($self); | ||
| 503 | } | ||
| 504 | |||
| 505 | sub handleInv | ||
| 506 | { | ||
| 507 | my $self = shift; | ||
| 508 | my ($user,$item_re) = @_; | ||
| 509 | |||
| 510 | my @items = keys %{$self->{'itemsToSell'}}; | ||
| 511 | if (defined($item_re)) { | ||
| 512 | @items = grep(/$item_re/, @items); | ||
| 513 | } | ||
| 514 | my $n = 0; | ||
| 515 | foreach my $name (@items) { | ||
| 516 | my $onHand = $self->qtyOnHand($name); | ||
| 517 | if ($onHand > 0) { | ||
| 518 | my $qty = $self->qtyInStock($name); | ||
| 519 | if ($qty > 0) { | ||
| 520 | my $price = $self->{'itemsToSell'}->{$name}->[1]; | ||
| 521 | $self->sendPM($user,"$qty $name at ".$price."gc each"); | ||
| 522 | $n++; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | if ($n == 0) { | ||
| 527 | $self->sendPM($user,"I am not selling anything at the moment"); | ||
| 528 | } | ||
| 529 | } | ||
| 530 | |||
| 531 | sub handleListWant | ||
| 532 | { | ||
| 533 | my $self = shift; | ||
| 534 | my ($user) = @_; | ||
| 535 | |||
| 536 | foreach my $name (keys %{$self->{'IWant'}}) { | ||
| 537 | my $qty = $self->{'IWant'}->{$name}; | ||
| 538 | $self->sendPM($user,"I want $qty $name"); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | sub handleListBuySell | ||
| 543 | { | ||
| 544 | my $self = shift; | ||
| 545 | my ($user,$action) = @_; | ||
| 546 | |||
| 547 | my $list = ($action eq "sell") ? $self->{'itemsToSell'} : $self->{'itemsToBuy'}; | ||
| 548 | |||
| 549 | my @items = keys %{$list}; | ||
| 550 | foreach my $name (@items) { | ||
| 551 | my $qty = $list->{$name}->[0]; | ||
| 552 | my $price = $list->{$name}->[1]; | ||
| 553 | $self->sendPM($user,"$qty $name at ".$price."gc each"); | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 | sub handleDoNotSell | ||
| 558 | { | ||
| 559 | my $self = shift; | ||
| 560 | my ($user,$item) = @_; | ||
| 561 | |||
| 562 | if (defined($self->{'itemsToSell'}->{$item})) { | ||
| 563 | undef $self->{'itemsToSell'}->{$item}; | ||
| 564 | if (defined($self->{'sellingFile'})) { | ||
| 565 | YAML::DumpFile($self->{'sellingFile'}, $self->{'itemsToSell'}); | ||
| 566 | } | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | sub handleUpdateSell | ||
| 571 | { | ||
| 572 | my $self = shift; | ||
| 573 | my ($user,$qty,$item,$price) = @_; | ||
| 574 | $self->{'itemsToSell'}->{$item} = [$qty,$price,0]; | ||
| 575 | if (defined($self->{'sellingFile'})) { | ||
| 576 | YAML::DumpFile($self->{'sellingFile'}, $self->{'itemsToSell'}); | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | sub handleUpdateBuy | ||
| 581 | { | ||
| 582 | my $self = shift; | ||
| 583 | my ($user,$qty,$item,$price) = @_; | ||
| 584 | $self->{'itemsToBuy'}->{$item} = [$qty,$price,0]; | ||
| 585 | if (defined($self->{'buyingFile'})) { | ||
| 586 | YAML::DumpFile($self->{'buyingFile'}, $self->{'itemsToBuy'}); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | sub handleBuy | ||
| 591 | { | ||
| 592 | my $self = shift; | ||
| 593 | my ($user,$qty,$name) = @_; | ||
| 594 | |||
| 595 | $self->tradeUserOk($user) || return; | ||
| 596 | |||
| 597 | $name =~ s/\s{2,}/ /g; | ||
| 598 | my $sell = $self->{'itemsToSell'}->{$name}; | ||
| 599 | if (!defined($sell)) { | ||
| 600 | $self->sendPM($user,"Sorry, I don't have any $name"); | ||
| 601 | return; | ||
| 602 | } | ||
| 603 | my $nSell = $self->qtyInStock($name); | ||
| 604 | if ($nSell < $qty) { | ||
| 605 | $self->sendPM($user,"Sorry, I only have ".$nSell." $name"); | ||
| 606 | return; | ||
| 607 | } | ||
| 608 | my $price = ceil($qty * $sell->[1]); | ||
| 609 | $self->sendPM($user,"$qty $name will cost you ".$price."gc"); | ||
| 610 | $self->{'IWant'}->{'gold coins'} += $price; | ||
| 611 | $self->{'mySells'}->{$name} += $qty; | ||
| 612 | |||
| 613 | $self->tradeObject($qty,$name); | ||
| 614 | } | ||
| 615 | |||
| 616 | sub handleSell | ||
| 617 | { | ||
| 618 | my $self = shift; | ||
| 619 | my ($user,$qty,$name) = @_; | ||
| 620 | |||
| 621 | $self->tradeUserOk($user) || return; | ||
| 622 | |||
| 623 | $name =~ s/\s{2,}/ /g; | ||
| 624 | my $buy = $self->{'itemsToBuy'}->{$name}; | ||
| 625 | if (!defined($buy)) { | ||
| 626 | $self->sendPM($user,"Sorry, I am not buying $name"); | ||
| 627 | return; | ||
| 628 | } | ||
| 629 | my $nBuy = $self->qtyToBuy($name); | ||
| 630 | if ($nBuy < $qty) { | ||
| 631 | $self->sendPM($user,"Sorry, I am only buying ".$nBuy." $name"); | ||
| 632 | return; | ||
| 633 | } | ||
| 634 | my $price = floor($qty * $buy->[1]); | ||
| 635 | $self->sendPM($user,"I will pay $price"."gc for $qty $name"); | ||
| 636 | $self->{'IWant'}->{$name} += $qty; | ||
| 637 | $self->{'myBuys'}->{$name} += $qty; | ||
| 638 | |||
| 639 | $self->tradeObject($price,'gold coins'); | ||
| 640 | } | ||
| 641 | |||
| 642 | sub adminCmds | ||
| 643 | { | ||
| 644 | my $self = shift; | ||
| 645 | my ($user,$msg) = @_; | ||
| 646 | |||
| 647 | if ($msg =~ m/^\s*list\s+stock\s*$/i) { # | ||
| 648 | $self->handleListStock($user); | ||
| 649 | } | ||
| 650 | elsif ($msg =~ m/^\s*list\s+sells{0,1}\s*$/i) { # | ||
| 651 | $self->handleListBuySell($user,"sell"); | ||
| 652 | } | ||
| 653 | elsif ($msg =~ m/^\s*list\s+buys{0,1}\s*$/i) { # | ||
| 654 | $self->handleListBuySell($user,"buy"); | ||
| 655 | } | ||
| 656 | elsif ($msg =~ m/^\s*tradeing\s+(on|off)/i) { # | ||
| 657 | $self->{'canTrade'} = ($1 eq 'on'); | ||
| 658 | $self->sendPM($user,"Trading is now $1"); | ||
| 659 | } | ||
| 660 | elsif ($msg =~ m/^\s*list\s+wants{0,1}\s*$/i) { # | ||
| 661 | $self->handleListWant($user); | ||
| 662 | } | ||
| 663 | elsif ($msg =~ m/give\s+me\s+(\d+)\s+(\w.*\S)\s*$/i) { | ||
| 664 | $self->handleGiveMe($user,$1,lc($2)); | ||
| 665 | } | ||
| 666 | elsif ($msg =~ m/^\s*do\s+not\s+sell\s+(\w.*\w)\s*$/i) { | ||
| 667 | $self->handleDoNotSell($user,$1); | ||
| 668 | } | ||
| 669 | elsif ($msg =~ m/^\s*do\s+not\s+buy\s+(\w.*\w)\s*$/i) { | ||
| 670 | $self->handleDoNotBuy($user,$1); | ||
| 671 | } | ||
| 672 | elsif ($msg =~ m/\s*sell\s+(\d+)\s+(\w.*\S)\s+(for|at)\s+(\d+)gc*/i) { | ||
| 673 | $self->handleUpdateSell($user,$1,$2,$4); | ||
| 674 | } | ||
| 675 | elsif ($msg =~ m/\s*buy\s+(\d+)\s+(\w.*\S)\s+(for|at)\s+(\d+)gc*/i) { | ||
| 676 | $self->handleUpdateBuy($user,$1,$2,$4); | ||
| 677 | } | ||
| 678 | elsif ($msg =~ m/^\s*move\s+to\s+(\d+)\,(\d+)\s*/) { | ||
| 679 | $self->moveTo([$1,$2]); | ||
| 680 | } | ||
| 681 | elsif ($msg =~ m/\s*say\s(\S.*\S)\s*/) { | ||
| 682 | $self->Say($1); | ||
| 683 | } | ||
| 684 | elsif ($msg =~ m/\s*stats\s*$/) { | ||
| 685 | $self->handleStats($user); | ||
| 686 | } | ||
| 687 | elsif ($msg =~ m/\s*touch\s+player\s+(\d+)\s*$/) { | ||
| 688 | $self->handleTouchPlayer($1); | ||
| 689 | } | ||
| 690 | elsif ($msg =~ m/\s*respond\s+to\s+(\d+)\s+with\s+(\d+)\s*$/) { | ||
| 691 | $self->respondToNPC($1,$2); | ||
| 692 | } | ||
| 693 | elsif ($msg =~ m/\s*dump\s+(.*\S)\s*$/) { | ||
| 694 | $self->handleDump($user,$1); | ||
| 695 | } | ||
| 696 | elsif ($msg =~ m/^\s*sto\s*$/) { | ||
| 697 | $self->handleSto($user,'.*'); | ||
| 698 | } | ||
| 699 | elsif ($msg =~ m/^\s*sto\s+(.+)$/) { | ||
| 700 | $self->handleSto($user,$1); | ||
| 701 | } | ||
| 702 | else { | ||
| 703 | return 0; | ||
| 704 | } | ||
| 705 | return 1; | ||
| 706 | } | ||
| 707 | |||
| 708 | sub handlePM | ||
| 709 | { | ||
| 710 | my $self = shift; | ||
| 711 | my ($user,$msg) = @_; | ||
| 712 | |||
| 713 | my $canTrade = $self->{'canTrade'}; | ||
| 714 | |||
| 715 | if ($self->isAdmin($user)) { | ||
| 716 | $self->adminCmds($user,$msg) && return; | ||
| 717 | } | ||
| 718 | if ($msg =~ m/^\s*loc\s*$/i) { | ||
| 719 | $self->{'locReply'} = $user; | ||
| 720 | $self->locateMe(); | ||
| 721 | } | ||
| 722 | elsif ($msg =~ m/^\s*owner\s*$/i) { | ||
| 723 | $self->sendPM($user,$self->{'owner'}); | ||
| 724 | } | ||
| 725 | elsif (($canTrade) && ($msg =~ m/^\s*buy\s+(\d+)\s+(\w.*\w)\s*$/i)) { | ||
| 726 | $self->handleBuy($user,$1,lc($2)); | ||
| 727 | } | ||
| 728 | elsif (($canTrade) && ($msg =~ m/^\s*sell\s+(\d+)\s+(\w.*\w)\s*$/i)) { | ||
| 729 | $self->handleSell($user,$1,lc($2)); | ||
| 730 | } | ||
| 731 | elsif (($canTrade) && ($msg =~ m/^\s*donate\s+(\d+)\s+(\w.*\w)\s*$/i)) { | ||
| 732 | if ($self->tradeUserOk($user)) { | ||
| 733 | $self->{'IWant'}->{$2} += $1; | ||
| 734 | } | ||
| 735 | } | ||
| 736 | elsif (($canTrade) && ($msg =~ m/^\s*wanted\s*$/i)) { | ||
| 737 | $self->handleWanted($user,undef); | ||
| 738 | } | ||
| 739 | elsif (($canTrade) && ($msg =~ m/^\s*inv\s+(\w|\w.*\w)\s*$/i)) { | ||
| 740 | $self->handleInv($user,$1); | ||
| 741 | } | ||
| 742 | elsif (($canTrade) && ($msg =~ m/^\s*inv\s*$/i)) { | ||
| 743 | $self->handleInv($user,undef); | ||
| 744 | } | ||
| 745 | elsif ($msg =~ m/^\s*help\s*$/i) { | ||
| 746 | $self->handleHelp($user); | ||
| 747 | } | ||
| 748 | else { | ||
| 749 | $self->sendPM($user,"Sorry, I don't understand."); | ||
| 750 | $self->sendPM($user,"PM me with HELP for a list of commands"); | ||
| 751 | } | ||
| 752 | } | ||
| 753 | |||
| 754 | ########################################################################################## | ||
| 755 | # Path finding/following functions | ||
| 756 | ########################################################################################## | ||
| 757 | |||
| 758 | sub distance($$$$) | ||
| 759 | { | ||
| 760 | my $self = shift; | ||
| 761 | my ($x1,$y1,$x2,$y2) = @_; | ||
| 762 | |||
| 763 | my $x = ($x1 > $x2) ? $x1-$x2 : $x2-$x1; | ||
| 764 | my $y = ($y1 > $y2) ? $y1-$y2 : $y2-$y1; | ||
| 765 | |||
| 766 | return ($x > $y) ? $x : $y; | ||
| 767 | } | ||
| 768 | |||
| 769 | sub distanceToObject | ||
| 770 | { | ||
| 771 | my $self = shift; | ||
| 772 | my ($id) = @_; | ||
| 773 | |||
| 774 | my ($x,$y) = $self->getObjectLocation($id); | ||
| 775 | |||
| 776 | return $self->distanceTo($x,$y); | ||
| 777 | } | ||
| 778 | |||
| 779 | sub pathDesc | ||
| 780 | { | ||
| 781 | my ($path) = @_; | ||
| 782 | |||
| 783 | my @tmp = (); | ||
| 784 | foreach my $p (@$path) { | ||
| 785 | my $str = (ref($p) eq "ARRAY") ? "($p->[0],$p->[1])": "($p)"; | ||
| 786 | push(@tmp, $str); | ||
| 787 | } | ||
| 788 | my $pathStr = join(' ==> ',@tmp); | ||
| 789 | return $pathStr; | ||
| 790 | } | ||
| 791 | |||
| 792 | sub getAllExits | ||
| 793 | { | ||
| 794 | my $self = shift; | ||
| 795 | my ($map) = @_; | ||
| 796 | |||
| 797 | my @exits = (); | ||
| 798 | my $exits = $self->{'knowledge'}->{'exitsByMap'}->{$map}; | ||
| 799 | if (defined($exits)) { | ||
| 800 | @exits = keys(%$exits); | ||
| 801 | } | ||
| 802 | return wantarray ? @exits : \@exits; | ||
| 803 | } | ||
| 804 | |||
| 805 | # $from = { | ||
| 806 | # 'map' => ? | ||
| 807 | # 'loc' => [x,y] | ||
| 808 | # | ||
| 809 | # } | ||
| 810 | # | ||
| 811 | # | ||
| 812 | |||
| 813 | sub exitOk | ||
| 814 | { | ||
| 815 | my ($exit) = @_; | ||
| 816 | |||
| 817 | defined($exit->{'to'}) || return 0; | ||
| 818 | ($exit->{'to'} ne 'undef') || return 0; | ||
| 819 | defined($exit->{'toX'}) || return 0; | ||
| 820 | defined($exit->{'toY'}) || return 0; | ||
| 821 | return 1; | ||
| 822 | } | ||
| 823 | |||
| 824 | sub findPathToMap | ||
| 825 | { | ||
| 826 | my $self = shift; | ||
| 827 | my ($from,$to,$delta) = @_; | ||
| 828 | |||
| 829 | my %Visited; | ||
| 830 | my @queue; | ||
| 831 | |||
| 832 | my $id = "MAP,$from->[0],$from->[1],$from->[2]"; | ||
| 833 | my ($crntID,$crntCost,$parent) = (undef,undef,undef); | ||
| 834 | push(@queue, [$id,0,"NONE"]); # [id,cost,parent] | ||
| 835 | L: while(my $next = shift(@queue)) { | ||
| 836 | my ($crntMap,$crntX,$crntY); | ||
| 837 | ($crntID,$crntCost,$parent) = @$next; | ||
| 838 | #print STDERR "Now at $crntID, cost=$crntCost parent=$parent\n"; | ||
| 839 | my $exitID = undef; | ||
| 840 | if ($crntID =~ m/^MAP\,(.+)\,(\d+)\,(\d+)/) { | ||
| 841 | ($crntMap,$crntX,$crntY) = ($1,$2,$3); | ||
| 842 | } | ||
| 843 | elsif ($crntID =~ m/^EXIT\,(.+)\,(\d+)/) { | ||
| 844 | my $e = $self->getExitDetails($1,$2); | ||
| 845 | ($crntMap,$crntX,$crntY) = ($1,$e->{'fromX'},$e->{'fromY'}); | ||
| 846 | $exitID = $2; | ||
| 847 | } | ||
| 848 | else { | ||
| 849 | carp("Can't decode my location"); | ||
| 850 | return undef; | ||
| 851 | } | ||
| 852 | my $map = $self->getMap($crntMap); | ||
| 853 | $Visited{$crntID} = $parent; | ||
| 854 | if (defined($exitID)) { # we are at an exit | ||
| 855 | my $exit = $self->getExitDetails($crntMap,$exitID); | ||
| 856 | if (defined($exit->{'to'}) and defined($exit->{'toX'}) and defined($exit->{'toY'})) { | ||
| 857 | my $nextID = "MAP,$exit->{'to'},$exit->{'toX'},$exit->{'toY'}"; | ||
| 858 | if (!defined($Visited{$nextID})) { | ||
| 859 | push(@queue, [$nextID,$crntCost+10,$crntID]); | ||
| 860 | #print STDERR "We are at an exit - adding $nextID, cost=$crntCost+10, parent=$crntID to queue\n"; | ||
| 861 | } | ||
| 862 | } | ||
| 863 | } | ||
| 864 | elsif ($crntMap eq $to->[0]) { | ||
| 865 | #print STDERR "We are on the desination map\n"; | ||
| 866 | my $path = $self->doPathFind($map,$crntX,$crntY,$to->[1],$to->[2],$delta); | ||
| 867 | if (defined($path)) { | ||
| 868 | #print STDERR "Destination is reachable\n"; | ||
| 869 | my $n = $#{$path}; | ||
| 870 | if ($n < $delta) { | ||
| 871 | #print STDERR "We have arrived\n"; | ||
| 872 | last L; | ||
| 873 | } | ||
| 874 | my $nextID = "MAP,$to->[0],$path->[$n]->[0],$path->[$n]->[1]"; | ||
| 875 | if (!defined($Visited{$nextID})) { | ||
| 876 | my $cost = $crntCost+$n+1; | ||
| 877 | push(@queue, [$nextID,$cost,$crntID]); | ||
| 878 | #print STDERR "adding $nextID, cost=$cost, parent=$crntID to queue\n"; | ||
| 879 | } | ||
| 880 | } | ||
| 881 | else { | ||
| 882 | #print STDERR "Dstination not reachble - exit map\n"; | ||
| 883 | my @exits = $self->getAllExits($crntMap); | ||
| 884 | @exits = map {$self->getExitDetails($crntMap,$_)} @exits; | ||
| 885 | @exits = grep { exitOk($_) } @exits; | ||
| 886 | foreach my $e (@exits) { | ||
| 887 | my $nextID = "EXIT,$e->{'from'},$e->{'id'}"; | ||
| 888 | if (!defined($Visited{$nextID})) { | ||
| 889 | my $path = $self->doPathFind($map,$crntX,$crntY,$e->{'fromX'},$e->{'fromY'},10); | ||
| 890 | if (defined($path)) { | ||
| 891 | my $cost = $crntCost + $#{$path}+1; | ||
| 892 | push(@queue, [$nextID,$cost,$crntID]); | ||
| 893 | #print STDERR "Adding exit $nextID, cost=$cost, parent=$crntID to queue\n"; | ||
| 894 | } | ||
| 895 | } | ||
| 896 | } | ||
| 897 | } | ||
| 898 | } | ||
| 899 | else { | ||
| 900 | #print STDERR "Just arrived on a map\n"; | ||
| 901 | my @exits = $self->getAllExits($crntMap); | ||
| 902 | @exits = map {$self->getExitDetails($crntMap,$_)} @exits; | ||
| 903 | @exits = grep { exitOk($_) } @exits; | ||
| 904 | foreach my $e (@exits) { | ||
| 905 | my $nextID = "EXIT,$e->{'from'},$e->{'id'}"; | ||
| 906 | if (!defined($Visited{$nextID})) { | ||
| 907 | my $path = $self->doPathFind($map,$crntX,$crntY,$e->{'fromX'},$e->{'fromY'},10); | ||
| 908 | if (defined($path)) { | ||
| 909 | my $cost = $crntCost + $#{$path}+1; | ||
| 910 | push(@queue, [$nextID,$cost,$crntID]); | ||
| 911 | #print STDERR "Adding exit $nextID, cost=$cost, parent=$crntID to queue\n"; | ||
| 912 | } | ||
| 913 | } | ||
| 914 | } | ||
| 915 | } | ||
| 916 | @queue = sort { $a->[1] <=> $b->[1] } @queue; | ||
| 917 | } | ||
| 918 | my @path; | ||
| 919 | while($crntID ne "NONE") { | ||
| 920 | unshift(@path,$crntID); | ||
| 921 | $crntID = $Visited{$crntID}; | ||
| 922 | } | ||
| 923 | return \@path; | ||
| 924 | } | ||
| 925 | |||
| 926 | # Take path returned by the findPath* calls and | ||
| 927 | # 1. convert from "x,y" to [x,y] | ||
| 928 | # 2. chop it up in to a series of steps, where | ||
| 929 | # each step is <= 6 | ||
| 930 | sub shortenPath | ||
| 931 | { | ||
| 932 | my $self = shift; | ||
| 933 | my ($path) = @_; | ||
| 934 | |||
| 935 | my @path; | ||
| 936 | my ($newX,$newY) = (-1,-1); | ||
| 937 | my ($pathX,$pathY) = (-1,-1); | ||
| 938 | |||
| 939 | my $p1 = shift @$path; | ||
| 940 | if (defined($p1)) { | ||
| 941 | ($newX,$newY) = ($pathX,$pathY) = @$p1; | ||
| 942 | foreach my $p (@{$path}) { | ||
| 943 | ($newX,$newY) = @$p; | ||
| 944 | if ($self->distance($pathX,$pathY,$newX,$newY) > 6) { | ||
| 945 | push(@path,[$newX,$newY]); | ||
| 946 | ($pathX,$pathY) = ($newX,$newY); | ||
| 947 | } | ||
| 948 | } | ||
| 949 | if ($newX != $pathX or $newY != $pathY) { | ||
| 950 | push(@path,[$newX,$newY]); | ||
| 951 | } | ||
| 952 | } | ||
| 953 | ($self->{'debug'} & $DEBUG_PATH) && | ||
| 954 | $self->Log("Shortened path: ".pathDesc(\@path)); | ||
| 955 | |||
| 956 | return \@path; | ||
| 957 | } | ||
| 958 | |||
| 959 | sub doPathFind | ||
| 960 | { | ||
| 961 | my $self = shift; | ||
| 962 | my ($map,$fromX,$fromY,$toX,$toY,$delta) = @_; | ||
| 963 | |||
| 964 | defined($fromX) || cluck("fromX is not defined"); | ||
| 965 | defined($fromY) || cluck("fromY is not defined"); | ||
| 966 | defined($toX) || cluck("toX is not defined"); | ||
| 967 | defined($toY) || cluck("toY is not defined"); | ||
| 968 | ($toX =~ m/^\d+$/) || cluck("toX='$toX' which is not numeric"); | ||
| 969 | ($toY =~ m/^\d+$/) || cluck("toY='$toY' which is not numeric"); | ||
| 970 | ($toX =~ m/^\d+$/) || cluck("toX='$toX' which is not numeric"); | ||
| 971 | ($toY =~ m/^\d+$/) || cluck("toY='$toY' which is not numeric"); | ||
| 972 | |||
| 973 | ($self->{'debug'} & $DEBUG_PATH) && | ||
| 974 | $self->Log("findPath from ($fromX,$fromY) to ($toX,$toY) (delta=$delta)"); | ||
| 975 | my $hMap = $map->{'hMap'}; | ||
| 976 | my $wdth = $map->{'width'}; | ||
| 977 | my $hght = $map->{'height'}; | ||
| 978 | my $path = findPathFromTo($hMap,$wdth,$hght,$fromX,$fromY,$toX,$toY,$delta); | ||
| 979 | |||
| 980 | return $path | ||
| 981 | } | ||
| 982 | |||
| 983 | sub findPathClose($$$$) | ||
| 984 | { | ||
| 985 | my $self = shift; | ||
| 986 | my ($from,$to,$delta) = @_; | ||
| 987 | |||
| 988 | my $path = $self->doPathFind($self->{'Map'},$from->[0],$from->[1],$to->[0],$to->[1],$delta); | ||
| 989 | if (defined($path)) { | ||
| 990 | ($self->{'debug'} & $DEBUG_PATH) && | ||
| 991 | $self->Log("Detailed path: ".pathDesc($path)); | ||
| 992 | $path = $self->shortenPath($path); | ||
| 993 | } | ||
| 994 | return $path; | ||
| 995 | } | ||
| 996 | |||
| 997 | sub findPath($$$) | ||
| 998 | { | ||
| 999 | my $self = shift; | ||
| 1000 | my ($from,$to) = @_; | ||
| 1001 | |||
| 1002 | return $self->findPathClose($from,$to,0); | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | sub stop | ||
| 1006 | { | ||
| 1007 | my $self = shift; | ||
| 1008 | |||
| 1009 | $self->{'path'} = undef; | ||
| 1010 | $self->{'destination'} = [-1-1,0]; | ||
| 1011 | |||
| 1012 | return 1; | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | sub moveCloseTo($$$) | ||
| 1016 | { | ||
| 1017 | my $self = shift; | ||
| 1018 | my ($to,$delta) = @_; | ||
| 1019 | |||
| 1020 | my $me = $self->{'me'}; | ||
| 1021 | if (!defined($me)) { | ||
| 1022 | $self->Log("Can't move - 'me' is not defined"); | ||
| 1023 | return undef; | ||
| 1024 | } | ||
| 1025 | my ($fromX,$fromY) = ($me->{'xpos'},$me->{'ypos'}); | ||
| 1026 | if (!defined($fromX) or !defined($fromY)) { | ||
| 1027 | $self->Log("Can't move - my location is not defined"); | ||
| 1028 | return undef; | ||
| 1029 | } | ||
| 1030 | my $path = $self->findPathClose([$fromX,$fromY],$to,$delta); | ||
| 1031 | $self->followPath($path); | ||
| 1032 | |||
| 1033 | return $path; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | sub moveTo($$) | ||
| 1037 | { | ||
| 1038 | my $self = shift; | ||
| 1039 | my ($to) = @_; | ||
| 1040 | |||
| 1041 | return $self->moveCloseTo($to,0); | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | sub followPath($$) | ||
| 1045 | { | ||
| 1046 | my $self = shift; | ||
| 1047 | my ($path) = @_; | ||
| 1048 | |||
| 1049 | if (!defined($path)) { | ||
| 1050 | $self->Log("Not following undefined path !"); | ||
| 1051 | return undef; | ||
| 1052 | } | ||
| 1053 | my $n = $#{$path}; | ||
| 1054 | if ($n < 0) { | ||
| 1055 | $self->Log("Not following empty path !"); | ||
| 1056 | return undef; | ||
| 1057 | } | ||
| 1058 | $self->{'path'} = $path; | ||
| 1059 | my ($x,$y) = ($path->[$n]->[0],$path->[$n]->[1]); | ||
| 1060 | $self->{'destination'} = [$x,$y,0]; | ||
| 1061 | $self->SUPER::moveTo($path->[0]->[0],$path->[0]->[1]); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | ########################################################################################## | ||
| 1065 | # | ||
| 1066 | ########################################################################################## | ||
| 1067 | |||
| 1068 | sub useMapObject | ||
| 1069 | { | ||
| 1070 | my $self = shift; | ||
| 1071 | my ($id) = @_; | ||
| 1072 | |||
| 1073 | if (my $prev = $self->{'lastUsedMapObject'}) { | ||
| 1074 | my ($x,$y) = ($prev->{'x'},$prev->{'y'}); | ||
| 1075 | my $to = $prev->{'to'}; | ||
| 1076 | if (!defined($x) || !defined($y) || !defined($to)) { | ||
| 1077 | $prev->{'timedOut'} = 1; | ||
| 1078 | } | ||
| 1079 | $self->rememberExit($prev); | ||
| 1080 | $self->saveKnowledge(); | ||
| 1081 | } | ||
| 1082 | $self->{'timeLastObjectUsed'} = time(); | ||
| 1083 | $self->{'lastUsedMapObject'} = { | ||
| 1084 | 'from' => $self->crntMap(), | ||
| 1085 | 'id' => $id, | ||
| 1086 | }; | ||
| 1087 | $self->SUPER::useMapObject($id); | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | sub getObjectLocation | ||
| 1091 | { | ||
| 1092 | my $self = shift; | ||
| 1093 | my ($id) = @_; | ||
| 1094 | |||
| 1095 | my $obj = $self->{'Map'}->{'3dByID'}->{$id}; | ||
| 1096 | my ($x,$y) = (int($obj->{'x_pos'}*2),int($obj->{'y_pos'}*2)); | ||
| 1097 | |||
| 1098 | return wantarray ? ($x,$y) : [$x,$y]; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub CHANGE_MAP($$$$) | ||
| 1102 | { | ||
| 1103 | my $self = shift; | ||
| 1104 | my ($type,$len,$data) = @_; | ||
| 1105 | |||
| 1106 | $self->SUPER::CHANGE_MAP($type,$len,$data); | ||
| 1107 | |||
| 1108 | my $map = $self->crntMap(); | ||
| 1109 | if (my $exit = $self->{'lastUsedMapObject'}) { | ||
| 1110 | $exit->{'to'} = $map; | ||
| 1111 | $self->rememberExit($exit); | ||
| 1112 | } | ||
| 1113 | my $exits = $self->{'Map'}->{'entrable'}; | ||
| 1114 | foreach my $id (@$exits) { | ||
| 1115 | if (!defined($self->getExitDetails($map,$id))) { | ||
| 1116 | my ($x,$y) = $self->getObjectLocation($id); | ||
| 1117 | $self->rememberExit({'id'=>$id,'from'=>$map,'fromX'=>$x,'fromY'=>$y}); | ||
| 1118 | } | ||
| 1119 | } | ||
| 1120 | my $harv = $self->{'Map'}->{'harvestables'}; | ||
| 1121 | foreach my $h (@$harv) { | ||
| 1122 | if (!defined($self->{'knowledge'}->{'harvByMap'}->{$map}->{'byID'}->{$h})) { | ||
| 1123 | my ($x,$y) = $self->getObjectLocation($h); | ||
| 1124 | $self->rememberHarvest({map=>$map, x=>$x, y=>$y, id=>$h}); | ||
| 1125 | } | ||
| 1126 | } | ||
| 1127 | $self->saveKnowledge(); | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | sub sellToNPC($$$) | ||
| 1131 | { | ||
| 1132 | my $self = shift; | ||
| 1133 | my ($qty,$item) = @_; | ||
| 1134 | |||
| 1135 | my $crntNPC = $self->{'crntNPC'}; | ||
| 1136 | (defined $crntNPC) || return 0; | ||
| 1137 | (exists $self->{'NPCchat'}->{$crntNPC}) || return undef; | ||
| 1138 | (exists $self->{'NPCchat'}->{$crntNPC}->{'waiting'}) && return undef; | ||
| 1139 | |||
| 1140 | my $opts = $self->{'NPCchat'}->{$crntNPC}->{'options'}; | ||
| 1141 | defined($opts) || return undef; | ||
| 1142 | |||
| 1143 | if (defined($opts->{'sell'})) { | ||
| 1144 | $self->respondToNPC($opts->{'sell'}); | ||
| 1145 | return undef; | ||
| 1146 | } | ||
| 1147 | elsif (defined($opts->{$item})) { | ||
| 1148 | $self->respondToNPC($opts->{$item}); | ||
| 1149 | return undef; | ||
| 1150 | } | ||
| 1151 | else { | ||
| 1152 | my $sellQty = 0; | ||
| 1153 | foreach my $o (keys %{$opts}) { | ||
| 1154 | if (($o =~ m/^\d+$/) and ($o > $sellQty and $o <= $qty)) { | ||
| 1155 | $sellQty = $o; | ||
| 1156 | } | ||
| 1157 | } | ||
| 1158 | ($sellQty > 0) || return 0; | ||
| 1159 | $self->respondToNPC($opts->{$sellQty}); | ||
| 1160 | return $sellQty; | ||
| 1161 | } | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | sub openStorage($) | ||
| 1165 | { | ||
| 1166 | my $self = shift; | ||
| 1167 | |||
| 1168 | my $crntNPC = $self->{'crntNPC'}; | ||
| 1169 | (defined $crntNPC) || return 0; | ||
| 1170 | (exists $self->{'NPCchat'}->{$crntNPC}) || return 0; | ||
| 1171 | my $opts = $self->{'NPCchat'}->{$crntNPC}->{'options'}; | ||
| 1172 | (defined $opts) || return undef; | ||
| 1173 | |||
| 1174 | if (exists $opts->{'open storage'}) { | ||
| 1175 | $self->respondToNPC($opts->{'open storage'}); | ||
| 1176 | $self->{'waitingForStorage'} = 1; | ||
| 1177 | return undef; | ||
| 1178 | } | ||
| 1179 | return (! $self->{'waitingForStorage'}); | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | sub nextMove($) | ||
| 1183 | { | ||
| 1184 | my $self = shift; | ||
| 1185 | |||
| 1186 | my $path = $self->{'path'}; | ||
| 1187 | if (!defined($path)) { | ||
| 1188 | return undef; | ||
| 1189 | } | ||
| 1190 | if ($#{$path} == -1) { | ||
| 1191 | $self->{'path'} = undef; | ||
| 1192 | $self->{'destination'} = [-1,-1,0]; | ||
| 1193 | return undef; | ||
| 1194 | } | ||
| 1195 | my $me = $self->{'me'}; | ||
| 1196 | if (!defined($me)) { | ||
| 1197 | return undef; | ||
| 1198 | } | ||
| 1199 | my $next = $path->[0]; | ||
| 1200 | my $d = $self->distanceTo($next->[0],$next->[1]); | ||
| 1201 | ($self->{'debug'} & $DEBUG_PATH) && | ||
| 1202 | $self->Log("Next location ($next->[0],$next->[1]), distance=$d"); | ||
| 1203 | if ($d <= 1) { | ||
| 1204 | shift (@{$self->{'path'}}); | ||
| 1205 | if ($next = $path->[0]) { | ||
| 1206 | ($self->{'debug'} & $DEBUG_PATH) && | ||
| 1207 | $self->Log("Asking server to move me to ($next->[0],$next->[1])"); | ||
| 1208 | $self->SUPER::moveTo($next->[0],$next->[1]); | ||
| 1209 | } | ||
| 1210 | } | ||
| 1211 | elsif ($me->{'lastMoved'}+2 < time()) { | ||
| 1212 | my ($x,$y,$delta) = @{$self->{'destination'}}; | ||
| 1213 | ($self->{'debug'} & $DEBUG_PATH) && | ||
| 1214 | $self->Log("Timeout on moving - calling moveCloseTo([$x,$y],$delta)"); | ||
| 1215 | $self->moveCloseTo([$x,$y],$delta); | ||
| 1216 | } | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | sub Dispatch($$$$) | ||
| 1220 | { | ||
| 1221 | my $self = shift; | ||
| 1222 | my ($type,$len,$data) = @_; | ||
| 1223 | |||
| 1224 | my $ret; | ||
| 1225 | if (defined($type)) { | ||
| 1226 | $ret = $self->SUPER::Dispatch($type,$len,$data); | ||
| 1227 | } | ||
| 1228 | if (defined($self->{'path'})) { | ||
| 1229 | $self->nextMove($self->{'path'}); | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | return $ret; | ||
| 1233 | } | ||
| 1234 | |||
| 1235 | ################################################################ | ||
| 1236 | # TRADE RELATED CALLBACKS # | ||
| 1237 | ################################################################ | ||
| 1238 | |||
| 1239 | sub GET_TRADE_ACCEPT | ||
| 1240 | { | ||
| 1241 | my $self = shift; | ||
| 1242 | my ($type,$len,$data) = @_; | ||
| 1243 | |||
| 1244 | my $who = unpack('C', $data); | ||
| 1245 | if ($who) { | ||
| 1246 | $self->{'tradeOk'} = $self->chkTrade($self->{'thereTrades'},$self->{'IWant'}); | ||
| 1247 | if ($self->{'tradeOk'}) { | ||
| 1248 | $self->SUPER::GET_TRADE_ACCEPT($type,$len,$data); | ||
| 1249 | } | ||
| 1250 | } | ||
| 1251 | } | ||
| 1252 | |||
| 1253 | ################################################################ | ||
| 1254 | # INVENTORY RELATED CALLBACKS # | ||
| 1255 | ################################################################ | ||
| 1256 | |||
| 1257 | sub readHelp | ||
| 1258 | { | ||
| 1259 | my $self = shift; | ||
| 1260 | my ($fname) = @_; | ||
| 1261 | my @lines = (); | ||
| 1262 | |||
| 1263 | if (defined($fname)) { | ||
| 1264 | if (open(FP,$fname)) { | ||
| 1265 | while(<FP>) { | ||
| 1266 | chomp $_; | ||
| 1267 | push(@lines, $_); | ||
| 1268 | } | ||
| 1269 | } | ||
| 1270 | else { | ||
| 1271 | $self->Log("Coud not open $fname for reading\n"); | ||
| 1272 | } | ||
| 1273 | } | ||
| 1274 | return \@lines; | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | sub new | ||
| 1278 | { | ||
| 1279 | my $class = shift; | ||
| 1280 | my ($self) = Games::EternalLands::Client->new(@_); | ||
| 1281 | bless($self, $class); | ||
| 1282 | |||
| 1283 | $self->{'admin'} = []; | ||
| 1284 | |||
| 1285 | $self->{'helpFile'} = undef; | ||
| 1286 | $self->{'adminHelpFile'} = undef; | ||
| 1287 | $self->{'help'} = undef; | ||
| 1288 | $self->{'adminhelp'} = undef; | ||
| 1289 | $self->{'owner'} = "No owner defined"; | ||
| 1290 | $self->{'location'} = "No location defined"; | ||
| 1291 | |||
| 1292 | $self->{'buyingFile'} = undef; | ||
| 1293 | $self->{'sellingFile'} = undef; | ||
| 1294 | $self->{'itemsToSell'} = {}; | ||
| 1295 | $self->{'itemsToBuy'} = {}; | ||
| 1296 | $self->{'lastMsgAt'} = time(); # no msg on startup | ||
| 1297 | $self->{'msgInterval'} = 20; # minutes | ||
| 1298 | $self->{'pmHandlers'} = {}; | ||
| 1299 | $self->{'destination'} = [-1,-1,0]; | ||
| 1300 | |||
| 1301 | $self->{'IWant'} = {}; | ||
| 1302 | $self->{'tradingWith'} = undef; | ||
| 1303 | $self->{'myTrades'} = {}; | ||
| 1304 | $self->{'mySells'} = {}; | ||
| 1305 | $self->{'thereTrades'} = {}; | ||
| 1306 | $self->{'tradeOk'} = 0; | ||
| 1307 | $self->{'tradeAccepted'} = 0; | ||
| 1308 | $self->{'stoByID'} = {}; | ||
| 1309 | $self->{'stoByName'} = {}; | ||
| 1310 | $self->{'waitingForStorage'} = 0; | ||
| 1311 | $self->{'eatList'} = []; | ||
| 1312 | $self->{'lastEat'} = 0; | ||
| 1313 | |||
| 1314 | $self->{'pktMemory'} = []; | ||
| 1315 | $self->{'memoryRegex'} = '^NO$'; | ||
| 1316 | $self->{'maxPktMemory'} = 0; | ||
| 1317 | |||
| 1318 | @_ = $self->processArgs(@_); | ||
| 1319 | |||
| 1320 | $self->{'help'} = $self->readHelp($self->{'helpFile'}); | ||
| 1321 | $self->{'adminhelp'} = $self->readHelp($self->{'adminHelpFile'}); | ||
| 1322 | |||
| 1323 | if (defined($self->{'sellingFile'}) and (-e $self->{'sellingFile'})) { | ||
| 1324 | $self->{'itemsToSell'} = YAML::LoadFile($self->{'sellingFile'}); | ||
| 1325 | } | ||
| 1326 | if (defined($self->{'buyingFile'}) and (-e $self->{'buyingFile'})) { | ||
| 1327 | $self->{'itemsToBuy'} = YAML::LoadFile($self->{'buyingFile'}); | ||
| 1328 | } | ||
| 1329 | if (defined($self->{'knowledgeFile'}) and (-e $self->{'knowledgeFile'})) { | ||
| 1330 | $self->{'knowledge'} = YAML::LoadFile($self->{'knowledgeFile'}); | ||
| 1331 | } | ||
| 1332 | |||
| 1333 | return $self; | ||
| 1334 | } | ||
| 1335 | |||
| 1336 | sub setMemory | ||
| 1337 | { | ||
| 1338 | my $self = shift; | ||
| 1339 | my ($max,$regex) = @_; | ||
| 1340 | |||
| 1341 | $self->{'memoryRegex'} = $regex; | ||
| 1342 | $self->{'maxPktMemory'} = $max; | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | sub forget | ||
| 1346 | { | ||
| 1347 | my $self = shift; | ||
| 1348 | |||
| 1349 | $self->{'pktMemory'} = []; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | sub inspectBag | ||
| 1353 | { | ||
| 1354 | my $self = shift; | ||
| 1355 | my ($bagID) = @_; | ||
| 1356 | |||
| 1357 | my $bag = $self->{'bagsByID'}->{$bagID}; | ||
| 1358 | if (!defined($bag)) { | ||
| 1359 | $self->Log("Inspecting non existant bag $bagID"); | ||
| 1360 | return {}; | ||
| 1361 | } | ||
| 1362 | my $items = $bag->{'items'}; | ||
| 1363 | if (!defined($items)) { | ||
| 1364 | return undef; | ||
| 1365 | } | ||
| 1366 | foreach my $pos (keys %$items) { | ||
| 1367 | if (!defined($items->{$pos}->{'name'})) { | ||
| 1368 | return undef; | ||
| 1369 | } | ||
| 1370 | } | ||
| 1371 | return $items; | ||
| 1372 | } | ||
| 1373 | |||
| 1374 | sub pickUp | ||
| 1375 | { | ||
| 1376 | my $self = shift; | ||
| 1377 | my ($bagID,$pickup) = @_; | ||
| 1378 | |||
| 1379 | my $bag = $self->{'bagsByID'}->{$bagID}; | ||
| 1380 | if (!defined($bag)) { | ||
| 1381 | $self->Log("Picking up from non existsant bag($bagID)"); | ||
| 1382 | return 0; | ||
| 1383 | } | ||
| 1384 | if (!defined($bag->{'items'})) { | ||
| 1385 | $self->Log("Picking up from empty bag($bagID)"); | ||
| 1386 | return 0; | ||
| 1387 | } | ||
| 1388 | foreach my $pos (keys %{$bag->{'items'}}) { | ||
| 1389 | my $name = $bag->{'items'}->{$pos}->{'name'}; | ||
| 1390 | if (!defined($name)) { | ||
| 1391 | $self->Log("item in bag with no name !"); | ||
| 1392 | next; | ||
| 1393 | } | ||
| 1394 | my $qty = defined($pickup) ? $pickup->{$name} : 100000; | ||
| 1395 | if (defined($qty)) { | ||
| 1396 | my $inBag = $bag->{'items'}->{$pos}->{'quantity'}; | ||
| 1397 | if ($inBag < $qty) { | ||
| 1398 | $qty = $inBag; | ||
| 1399 | } | ||
| 1400 | $self->SUPER::pickUp($bag,$pos,$qty); | ||
| 1401 | } | ||
| 1402 | } | ||
| 1403 | return 1; | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | sub contains | ||
| 1407 | { | ||
| 1408 | my $self = shift; | ||
| 1409 | my ($hash,$item) = @_; | ||
| 1410 | |||
| 1411 | foreach my $pos (keys %{$hash}) { | ||
| 1412 | if ($hash->{$pos}->{'name'} eq $item) { | ||
| 1413 | return $hash->{$pos}; | ||
| 1414 | } | ||
| 1415 | } | ||
| 1416 | return undef; | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | sub isAdmin | ||
| 1420 | { | ||
| 1421 | my $self = shift; | ||
| 1422 | my ($user) = @_; | ||
| 1423 | |||
| 1424 | foreach my $admin (@{$self->{'admins'}}) { | ||
| 1425 | if ($user eq $admin) { | ||
| 1426 | return 1; | ||
| 1427 | } | ||
| 1428 | } | ||
| 1429 | return 0; | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | sub tradeUserOk | ||
| 1433 | { | ||
| 1434 | my $self = shift; | ||
| 1435 | my ($user) = @_; | ||
| 1436 | |||
| 1437 | my $tradeWith = $self->{'tradeWith'}; | ||
| 1438 | if (!defined($tradeWith)) { | ||
| 1439 | $self->sendPM($user,"Start trading with me before asking for items."); | ||
| 1440 | return 0 ; | ||
| 1441 | } | ||
| 1442 | if ($tradeWith ne $user) { | ||
| 1443 | $self->sendPM($user,"Sorry, I am already trading with someone else."); | ||
| 1444 | $self->sendPM($user,"Please try again in while."); | ||
| 1445 | return 0; | ||
| 1446 | } | ||
| 1447 | return 1; | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | |||
| 1451 | ########################################################### | ||
| 1452 | # | ||
| 1453 | ########################################################### | ||
| 1454 | |||
| 1455 | sub invIsComplete | ||
| 1456 | { | ||
| 1457 | my $self = shift; | ||
| 1458 | foreach my $pos (keys %{$self->{'invByPos'}}) { | ||
| 1459 | if (!defined($self->{'invByPos'}->{$pos}->{'name'})) { | ||
| 1460 | return 0; } | ||
| 1461 | } | ||
| 1462 | return 1; | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | sub qtyToBuy | ||
| 1466 | { | ||
| 1467 | my $self = shift; | ||
| 1468 | my ($name) = @_; | ||
| 1469 | |||
| 1470 | my $qty = $self->{'itemsToBuy'}->{$name}->[0]; | ||
| 1471 | my $price = $self->{'itemsToBuy'}->{$name}->[1]; | ||
| 1472 | my $gc = $self->qtyOnHand('gold coins'); | ||
| 1473 | |||
| 1474 | return (floor($price * $qty) > $gc) ? floor($gc/$price) : $qty; | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | sub qtyInStock | ||
| 1478 | { | ||
| 1479 | my $self = shift; | ||
| 1480 | my ($name) = @_; | ||
| 1481 | |||
| 1482 | my $onHand = $self->qtyOnHand($name); | ||
| 1483 | if ($onHand <= 0) { | ||
| 1484 | return 0; | ||
| 1485 | } | ||
| 1486 | if (!defined($self->{'itemsToSell'}->{$name})) { | ||
| 1487 | return 0; | ||
| 1488 | } | ||
| 1489 | my $toSell = $self->{'itemsToSell'}->{$name}->[0] || 0; | ||
| 1490 | if (defined($self->{'myTrades'}->{$name})) { | ||
| 1491 | $toSell -= $self->{'myTrades'}->{$name}->{'quantity'}; | ||
| 1492 | $onHand -= $self->{'myTrades'}->{$name}->{'quantity'}; | ||
| 1493 | } | ||
| 1494 | return ($toSell > $onHand) ? $onHand : $toSell; | ||
| 1495 | } | ||
| 1496 | |||
| 1497 | sub qtyOnHand | ||
| 1498 | { | ||
| 1499 | my $self = shift; | ||
| 1500 | my ($name) = @_; | ||
| 1501 | |||
| 1502 | if (my $byName = $self->{'invByName'}->{lc($name)}) { | ||
| 1503 | my $qty = 0; | ||
| 1504 | foreach my $pos (values %$byName) { | ||
| 1505 | $qty += $pos->{'quantity'}; | ||
| 1506 | } | ||
| 1507 | return $qty; | ||
| 1508 | } | ||
| 1509 | return 0; | ||
| 1510 | } | ||
| 1511 | |||
| 1512 | sub chkTrade | ||
| 1513 | { | ||
| 1514 | my $self = shift; | ||
| 1515 | my ($trades,$wants) = @_; | ||
| 1516 | |||
| 1517 | |||
| 1518 | my $user = $self->{'tradeWith'}; | ||
| 1519 | my %thereTrades = %{$self->{'thereTrades'}}; # Copy so we can modify it | ||
| 1520 | my $IWant = $self->{'IWant'}; | ||
| 1521 | |||
| 1522 | # check if what we want matches what we were given | ||
| 1523 | my $tradeOk = 1; | ||
| 1524 | foreach my $want (keys %{$IWant}) { | ||
| 1525 | my $qty = $IWant->{$want}; | ||
| 1526 | my $item = $self->contains(\%thereTrades,$want); | ||
| 1527 | if (defined($item)) { | ||
| 1528 | $qty -= $item->{'quantity'}; | ||
| 1529 | delete $thereTrades{$item->{'pos'}}; | ||
| 1530 | } | ||
| 1531 | if ($qty > 0) { | ||
| 1532 | $self->sendPM($user,"I still need $qty more $want"); | ||
| 1533 | $tradeOk = 0; | ||
| 1534 | } | ||
| 1535 | elsif ($qty < 0) { | ||
| 1536 | $qty *= -1; | ||
| 1537 | $self->sendPM($user,"you have given me $qty too many $want"); | ||
| 1538 | $tradeOk = 0; | ||
| 1539 | } | ||
| 1540 | } | ||
| 1541 | foreach my $pos (keys %thereTrades) { | ||
| 1542 | my $name = $thereTrades{$pos}->{'name'}; | ||
| 1543 | $self->sendPM($user,"$name is not something you are giving/selling to me"); | ||
| 1544 | $tradeOk = 0; | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | return $tradeOk; | ||
| 1548 | } | ||
| 1549 | |||
| 1550 | sub Say | ||
| 1551 | { | ||
| 1552 | my $self = shift; | ||
| 1553 | my ($msg) = @_; | ||
| 1554 | |||
| 1555 | $self->SUPER::Say($msg); | ||
| 1556 | if ($msg =~ m/^\#beam me/i) { | ||
| 1557 | $self->{'lastUsedMapObject'} = undef; | ||
| 1558 | $self->{'path'} = undef; | ||
| 1559 | } | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | sub handleWanted | ||
| 1563 | { | ||
| 1564 | my $self = shift; | ||
| 1565 | my ($user,$item_re) = @_; | ||
| 1566 | |||
| 1567 | my @items = keys %{$self->{'itemsToBuy'}}; | ||
| 1568 | if (defined($item_re)) { | ||
| 1569 | @items = grep(/$item_re/, @items); | ||
| 1570 | } | ||
| 1571 | my $n = 0; | ||
| 1572 | foreach my $name (@items) { | ||
| 1573 | my $qty = $self->qtyToBuy($name); | ||
| 1574 | if ($qty > 0) { | ||
| 1575 | my $price = $self->{'itemsToBuy'}->{$name}->[1]; | ||
| 1576 | $self->sendPM($user,"$qty $name at ".$price."gc each"); | ||
| 1577 | $n++; | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | if ($n == 0) { | ||
| 1581 | $self->sendPM($user,"I am not buying anything at the moment"); | ||
| 1582 | } | ||
| 1583 | } | ||
| 1584 | |||
| 1585 | sub handleListStock | ||
| 1586 | { | ||
| 1587 | my $self = shift; | ||
| 1588 | my ($user) = @_; | ||
| 1589 | |||
| 1590 | foreach my $name (keys %{$self->{'invByName'}}) { | ||
| 1591 | my $qty = $self->qtyOnHand($name); | ||
| 1592 | $self->sendPM($user,"I have $qty $name"); | ||
| 1593 | } | ||
| 1594 | } | ||
| 1595 | |||
| 1596 | sub handleDoNotBuy | ||
| 1597 | { | ||
| 1598 | my $self = shift; | ||
| 1599 | my ($user,$item) = @_; | ||
| 1600 | |||
| 1601 | if (defined($self->{'itemsToBuy'}->{$item})) { | ||
| 1602 | undef $self->{'itemsToBuy'}->{$item}; | ||
| 1603 | if (defined($self->{'buyingFile'})) { | ||
| 1604 | YAML::DumpFile($self->{'buyingFile'}, $self->{'itemsToBuy'}); | ||
| 1605 | } | ||
| 1606 | } | ||
| 1607 | } | ||
| 1608 | |||
| 1609 | sub handleGiveMe | ||
| 1610 | { | ||
| 1611 | my $self = shift; | ||
| 1612 | my ($user,$qty,$name) = @_; | ||
| 1613 | |||
| 1614 | $self->tradeUserOk($user) || return; | ||
| 1615 | |||
| 1616 | my $give = $self->qtyOnHand($name); | ||
| 1617 | if ($give <= 0) { | ||
| 1618 | $self->sendPM($user,"Sorry, I don't have any $name"); | ||
| 1619 | } | ||
| 1620 | else { | ||
| 1621 | if ($qty > $give) { | ||
| 1622 | $self->sendPM($user,"Sorry, I only have ".$give->{'quantity'}." $name"); | ||
| 1623 | } | ||
| 1624 | else { | ||
| 1625 | $self->tradeObject($qty,$name); | ||
| 1626 | } | ||
| 1627 | } | ||
| 1628 | } | ||
| 1629 | |||
| 1630 | sub handleStats | ||
| 1631 | { | ||
| 1632 | my $self = shift; | ||
| 1633 | |||
| 1634 | my ($user) = @_; | ||
| 1635 | |||
| 1636 | foreach my $stat ('har','att','def','alc','mag','pot','sum','oa') { | ||
| 1637 | my $xp_cur = $self->{'experience'}->{$stat}->[0]; | ||
| 1638 | my $xp_base = $self->{'experience'}->{$stat}->[1]; | ||
| 1639 | my $cur = $self->{'skills'}->{$stat}->[0]; | ||
| 1640 | my $base = $self->{'skills'}->{$stat}->[1]; | ||
| 1641 | my $msg = sprintf("| %3s: %2d/%2d %d/%d",$stat,$cur,$base,$xp_cur,$xp_base); | ||
| 1642 | $self->sendPM($user,$msg); | ||
| 1643 | } | ||
| 1644 | foreach my $stat ('mp','ep','food','carry') { | ||
| 1645 | my $cur = $self->{'stats'}->{$stat}->[0]; | ||
| 1646 | my $base = $self->{'stats'}->{$stat}->[1]; | ||
| 1647 | my $msg = sprintf("| %5s: %2d/%2d", $stat, $cur, $base); | ||
| 1648 | $self->sendPM($user,$msg); | ||
| 1649 | } | ||
| 1650 | $self->sendPM($user,"Filled Inventory Slots: ".$self->{"nCarry"}); | ||
| 1651 | $self->sendPM($user,"Filled Equipment Slots: ".$self->{"nEquip"}); | ||
| 1652 | } | ||
| 1653 | |||
| 1654 | sub handleSto | ||
| 1655 | { | ||
| 1656 | my $self = shift; | ||
| 1657 | my ($user,$re) = @_; | ||
| 1658 | |||
| 1659 | $self->Say('#sto '); | ||
| 1660 | $self->{'tellSTO'} = $user; | ||
| 1661 | $self->{'stoRE'} = $re; | ||
| 1662 | } | ||
| 1663 | |||
| 1664 | sub distanceTo | ||
| 1665 | { | ||
| 1666 | my $self = shift; | ||
| 1667 | my ($toX,$toY) = @_; | ||
| 1668 | |||
| 1669 | defined($toX) || confess "x not defined in distanceTo(x,y)"; | ||
| 1670 | defined($toX) || confess "y not defined in distanceTo(x,y)"; | ||
| 1671 | |||
| 1672 | my $me = $self->{'me'}; | ||
| 1673 | my ($fromX,$fromY) = ($me->{'xpos'},$me->{'ypos'}); | ||
| 1674 | |||
| 1675 | (defined($fromX) && defined($fromY)) || confess "my location not defined in distanceTo()"; | ||
| 1676 | |||
| 1677 | my ($x,$y) = (abs($fromX-$toX),abs($fromY-$toY)); | ||
| 1678 | my $d = ($x > $y) ? $x : $y; | ||
| 1679 | |||
| 1680 | return $d; | ||
| 1681 | } | ||
| 1682 | |||
| 1683 | sub arrived | ||
| 1684 | { | ||
| 1685 | my $self = shift; | ||
| 1686 | |||
| 1687 | return (!defined($self->{'path'}) and !defined($self->{'pathState'})); | ||
| 1688 | } | ||
| 1689 | |||
| 1690 | sub NextPacket | ||
| 1691 | { | ||
| 1692 | my ($self) = shift; | ||
| 1693 | |||
| 1694 | my ($type,$len,$data) = $self->SUPER::NextPacket(); | ||
| 1695 | |||
| 1696 | if ($self->{'debug'} & $DEBUG_PATH) { | ||
| 1697 | my ($map,$x,$y) = $self->myLocation(); | ||
| 1698 | my $loc = (defined($x) and defined($y)) ? "($x,$y)" : "(undef,undef)"; | ||
| 1699 | $self->Log("I am on '$map' at $loc"); | ||
| 1700 | } | ||
| 1701 | my $food = $self->{'stats'}->{'food'}->[0]; | ||
| 1702 | if (defined($food) and $food < 5) { | ||
| 1703 | $self->eatSomething(undef); | ||
| 1704 | } | ||
| 1705 | if (my $prev = $self->{'lastUsedMapObject'}) { | ||
| 1706 | if ($self->{'timeLastObjectUsed'} < time()-40) { | ||
| 1707 | my ($x,$y) = ($prev->{'x'},$prev->{'y'}); | ||
| 1708 | my $to = $prev->{'to'}; | ||
| 1709 | if (!defined($x) || !defined($y) || !defined($to)) { | ||
| 1710 | $prev->{'timedOut'} = 1; | ||
| 1711 | } | ||
| 1712 | $self->rememberExit($prev); | ||
| 1713 | $self->{'lastUsedMapObject'} = undef; | ||
| 1714 | $self->saveKnowledge(); | ||
| 1715 | } | ||
| 1716 | } | ||
| 1717 | return ($type,$len,$data); | ||
| 1718 | |||
| 1719 | } | ||
| 1720 | |||
| 1721 | ################################################################ | ||
| 1722 | # TRADE RELATED CALLBACKS # | ||
| 1723 | ################################################################ | ||
| 1724 | |||
| 1725 | sub GET_TRADE_EXIT | ||
| 1726 | { | ||
| 1727 | my $self = shift; | ||
| 1728 | |||
| 1729 | if ($self->{'tradeAccepted'} == 2) { | ||
| 1730 | foreach my $name (keys %{$self->{'mySells'}}) { | ||
| 1731 | $self->{'itemsToSell'}->{$name}->[0] -= $self->{'mySells'}->{$name}; | ||
| 1732 | } | ||
| 1733 | foreach my $name (keys %{$self->{'myBuys'}}) { | ||
| 1734 | $self->{'itemsToBuy'}->{$name}->[0] -= $self->{'myBuys'}->{$name}; | ||
| 1735 | } | ||
| 1736 | } | ||
| 1737 | $self->{'IWant'} = {}; | ||
| 1738 | $self->{'mySells'} = {}; | ||
| 1739 | $self->{'myBuys'} = {}; | ||
| 1740 | |||
| 1741 | if (defined($self->{'sellingFile'})) { | ||
| 1742 | YAML::DumpFile($self->{'sellingFile'}, $self->{'itemsToSell'}); | ||
| 1743 | } | ||
| 1744 | $self->SUPER::GET_TRADE_EXIT(@_); | ||
| 1745 | } | ||
| 1746 | |||
| 1747 | ################################################################ | ||
| 1748 | # INVENTORY RELATED CALLBACKS # | ||
| 1749 | ################################################################ | ||
| 1750 | |||
| 1751 | sub processArgs | ||
| 1752 | { | ||
| 1753 | my $self = shift; | ||
| 1754 | my @args = @_; | ||
| 1755 | my @notUsed = (); | ||
| 1756 | |||
| 1757 | while(my $arg = shift @args) { | ||
| 1758 | if ($arg eq '-server') { | ||
| 1759 | $self->{'server'} = shift @args; } | ||
| 1760 | elsif ($arg eq '-port') { | ||
| 1761 | $self->{'port'} = shift @args; } | ||
| 1762 | elsif ($arg eq '-admins') { | ||
| 1763 | my $admins = shift @args; | ||
| 1764 | my @admins = split(',',$admins); | ||
| 1765 | $self->{'admins'} = \@admins; | ||
| 1766 | } | ||
| 1767 | elsif ($arg eq '-knowledgeFile') { | ||
| 1768 | $self->{'knowledgeFile'} = shift @args; } | ||
| 1769 | elsif ($arg eq '-buyingFile') { | ||
| 1770 | $self->{'buyingFile'} = shift @args; } | ||
| 1771 | elsif ($arg eq '-sellingFile') { | ||
| 1772 | $self->{'sellingFile'} = shift @args; } | ||
| 1773 | elsif ($arg eq '-helpFile') { | ||
| 1774 | $self->{'helpFile'} = shift @args; } | ||
| 1775 | elsif ($arg eq '-adminHelpFile') { | ||
| 1776 | $self->{'adminHelpFile'} = shift @args; } | ||
| 1777 | elsif ($arg eq '-msgInterval') { | ||
| 1778 | $self->{'msgInterval'} = shift @args; } | ||
| 1779 | elsif ($arg eq '-owner') { | ||
| 1780 | $self->{'owner'} = shift @args; } | ||
| 1781 | elsif ($arg eq '-location') { | ||
| 1782 | $self->{'location'} = shift @args; } | ||
| 1783 | else { | ||
| 1784 | push(@notUsed,$arg); | ||
| 1785 | } | ||
| 1786 | } | ||
| 1787 | return @notUsed; | ||
| 1788 | } | ||
| 1789 | |||
| 1790 | return 1; |