| 1 | package Games::EternalLands::Client; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp qw(carp cluck); | ||
| 5 | use IO::Socket; | ||
| 6 | use POSIX; | ||
| 7 | use YAML; | ||
| 8 | use Games::EternalLands::Map; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Carp; | ||
| 11 | use Tie::Cache; | ||
| 12 | |||
| 13 | our $VERSION = "0.04"; | ||
| 14 | |||
| 15 | # use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET); | ||
| 16 | |||
| 17 | # use Module qw(:DEFAULT :T2 !B3 A3); | ||
| 18 | |||
| 19 | |||
| 20 | use Games::EternalLands::Constants qw(:ServerCommands :ClientCommands :Debug :Stats :TypeContainers); | ||
| 21 | |||
| 22 | my $MAXBAGS = 200; | ||
| 23 | my $ITEMS_PER_BAG = 50; | ||
| 24 | |||
| 25 | ################################################ | ||
| 26 | |||
| 27 | |||
| 28 | ################################################ | ||
| 29 | |||
| 30 | sub Log | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | my @now = localtime(time); | ||
| 34 | my $nowStr = sprintf("%4d-%02d-%02d %02d:%02d", $now[5]+1900,$now[4],$now[3], $now[2],$now[1]); | ||
| 35 | |||
| 36 | defined($_[0]) || cluck("Argument to Log() is not defined !"); | ||
| 37 | print STDERR "[",$nowStr,"] ",$_[0],"\n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub setDebug | ||
| 41 | { | ||
| 42 | my $self = shift; | ||
| 43 | my ($val) = @_; | ||
| 44 | |||
| 45 | $self->{'debug'} = $val; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub myLocation | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | my $me = $self->{'me'}; | ||
| 52 | |||
| 53 | my $map = $self->crntMap(); | ||
| 54 | if (wantarray) { | ||
| 55 | return defined($me) ? ($map,$me->{'xpos'},$me->{'ypos'}) : (undef,undef,undef); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | return defined($me) ? [$map,$me->{'xpos'},$me->{'ypos'}] : undef; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub emptyEquipSlot | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | my ($start) = @_; | ||
| 66 | |||
| 67 | for(my $i=36; $i<44; $i++) { | ||
| 68 | if (!defined($self->{'invByPos'}->{$i})) { | ||
| 69 | return $i; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | return -1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub emptyInvSlot | ||
| 76 | { | ||
| 77 | my $self = shift; | ||
| 78 | my ($start) = @_; | ||
| 79 | |||
| 80 | for(my $i=0; $i<36; $i++) { | ||
| 81 | if (!defined($self->{'invByPos'}->{$i})) { | ||
| 82 | return $i; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | return -1; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub moveInvItem | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | my ($from,$to) = @_; | ||
| 92 | |||
| 93 | if (!defined($self->{'invByPos'}->{$from})) { | ||
| 94 | $self->Log("Can't move empty inventory slot $from\n"); | ||
| 95 | return undef; | ||
| 96 | } | ||
| 97 | if (defined($self->{'invByPos'}->{$to})) { | ||
| 98 | $self->Log("Can't move item to used inventory slot $to\n"); | ||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | my $item = $self->{'invByPos'}->{$from}->{'name'} || "unkown"; | ||
| 102 | $self->Log("Moving $item from slot $from to slot $to"); | ||
| 103 | |||
| 104 | $self->send($MOVE_INVENTORY_ITEM,pack('CC',$from,$to)); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub equipItem | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | my ($name,$multi) = @_; | ||
| 111 | |||
| 112 | if (!defined($multi)) { | ||
| 113 | foreach my $p (36 .. 43) { | ||
| 114 | if (exists $self->{'invByPos'}->{$p}) { | ||
| 115 | my $item = $self->{'invByPos'}->{$p}->{'name'}; | ||
| 116 | ($item =~ m/^$name$/i) && return 1; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | my @pList = (); | ||
| 122 | foreach my $p (1 .. 35) { | ||
| 123 | if (exists $self->{'invByPos'}->{$p}) { | ||
| 124 | my $item = $self->{'invByPos'}->{$p}->{'name'}; | ||
| 125 | ($item =~ m/^$name$/i) && push(@pList,$p); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | if ($#pList == -1) { | ||
| 130 | $self->Log("Do not have a $name to equip"); | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | my $to = $self->emptyEquipSlot(); | ||
| 134 | if ($to == -1) { | ||
| 135 | $self->Log("No free inventory position to equip $name to"); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | $self->moveInvItem($pList[0],$to); | ||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub getPlayerInfo | ||
| 143 | { | ||
| 144 | my $self = shift; | ||
| 145 | my ($id) = @_; | ||
| 146 | |||
| 147 | $self->send($GET_PLAYER_INFO,pack('L',$id)); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub haveItem | ||
| 151 | { | ||
| 152 | my $self = shift; | ||
| 153 | my ($name) = @_; | ||
| 154 | my @pList = keys(%{$self->{'invByName'}->{lc($name)}}); | ||
| 155 | |||
| 156 | foreach my $p (@pList) { | ||
| 157 | defined($p) && return $self->{'invByPos'}->{$p}; | ||
| 158 | } | ||
| 159 | return undef; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub unEquipAll | ||
| 163 | { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my @equippedSlots; | ||
| 167 | for(my $pos=36; $pos<44; $pos++) { | ||
| 168 | if (defined($self->{'invByPos'}->{$pos})) { | ||
| 169 | push(@equippedSlots,$pos); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | ($#equippedSlots >= 0) || return 0; | ||
| 173 | |||
| 174 | my $to = 0; | ||
| 175 | foreach my $from (@equippedSlots) { | ||
| 176 | while(defined($self->{'invByPos'}->{$to})) { | ||
| 177 | $to++; | ||
| 178 | } | ||
| 179 | ($to < 36) || last; | ||
| 180 | $self->moveInvItem($from,$to); | ||
| 181 | } | ||
| 182 | if ($to >= 36) { | ||
| 183 | $self->Log("not enough spare slots to un-equip everything"); | ||
| 184 | return 0; | ||
| 185 | } | ||
| 186 | return 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub unEquipItem | ||
| 190 | { | ||
| 191 | my $self = shift; | ||
| 192 | my ($name) = @_; | ||
| 193 | |||
| 194 | my @pList = keys(%{$self->{'invByName'}->{lc($name)}}); | ||
| 195 | |||
| 196 | my $from = -1; | ||
| 197 | foreach my $p (@pList) { | ||
| 198 | ($p >= 36) || next; | ||
| 199 | $from = $p; | ||
| 200 | last; | ||
| 201 | } | ||
| 202 | if ($from == -1) { | ||
| 203 | return 1; | ||
| 204 | } | ||
| 205 | my $to = $self->emptyInvSlot(); | ||
| 206 | if ($to == -1) { | ||
| 207 | $self->Log("No free inventory position to un-equip $name to"); | ||
| 208 | return undef; | ||
| 209 | } | ||
| 210 | $self->moveInvItem($from,$to); | ||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub sitDown | ||
| 215 | { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | $self->send($SIT_DOWN,pack('C',1)); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub standUp | ||
| 222 | { | ||
| 223 | my $self = shift; | ||
| 224 | |||
| 225 | $self->send($SIT_DOWN,pack('C',0)); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub packetAsHex | ||
| 229 | { | ||
| 230 | my $self = shift; | ||
| 231 | my ($pkt) = @_; | ||
| 232 | my @bytes; | ||
| 233 | |||
| 234 | my $n = length($pkt); | ||
| 235 | for(my $i=0; $i<$n; $i++) { | ||
| 236 | my $ch = substr($pkt,$i,1); | ||
| 237 | push(@bytes,sprintf("%2X", ord($ch))); | ||
| 238 | } | ||
| 239 | return join(" ",@bytes); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub send | ||
| 243 | { | ||
| 244 | my $self = shift; | ||
| 245 | my ($cmd,$data) = @_; | ||
| 246 | |||
| 247 | if ($cmd eq $MOVE_TO or $cmd eq $ATTACK_SOMEONE) { | ||
| 248 | if ($self->{'lastMove'}+1 > time()) { | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | $self->{'lastMove'} = time(); | ||
| 252 | } | ||
| 253 | ($self->{'debug'} & $DEBUG_TYPES) && $self->Log("Sending: ".$ServerCommandsByID{$cmd}); | ||
| 254 | |||
| 255 | my $len = pack('v',length($data)+1); | ||
| 256 | my $buf = $cmd.$len.$data; | ||
| 257 | |||
| 258 | ($self->{'debug'} & $DEBUG_PACKETS) && $self->Log("Sending: ".$self->packetAsHex($buf)); | ||
| 259 | |||
| 260 | $self->{'nSentPackets'} += 1; | ||
| 261 | my $ok = send($self->{'socket'},$buf,0); | ||
| 262 | |||
| 263 | return $ok | ||
| 264 | } | ||
| 265 | |||
| 266 | sub lookAtMapObject | ||
| 267 | { | ||
| 268 | my $self = shift; | ||
| 269 | my ($id) = @_; | ||
| 270 | |||
| 271 | $self->send($LOOK_AT_MAP_OBJECT,pack('L',$id)); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub sendPM | ||
| 275 | { | ||
| 276 | my $self = shift; | ||
| 277 | my ($user,$msg) = @_; | ||
| 278 | |||
| 279 | push(@{$self->{'pmQueue'}}, "$user $msg"); | ||
| 280 | } | ||
| 281 | |||
| 282 | sub dropItem | ||
| 283 | { | ||
| 284 | my $self = shift; | ||
| 285 | my ($qty,$name) = @_; | ||
| 286 | |||
| 287 | my $onHand = $self->qtyOnHand($name); | ||
| 288 | if ($onHand <= 0) { | ||
| 289 | $self->Log("Don't have any $name to drop"); | ||
| 290 | return 0; | ||
| 291 | } | ||
| 292 | if ($qty > $onHand) { | ||
| 293 | $qty = $onHand; | ||
| 294 | $self->Log("Only have any $qty $name to drop"); | ||
| 295 | } | ||
| 296 | my $dropped = 0; | ||
| 297 | my $inv = $self->{'invByName'}->{lc($name)}; | ||
| 298 | foreach my $item (values %$inv) { | ||
| 299 | my $pos = $item->{'pos'}; | ||
| 300 | my $n = $item->{'quantity'}; | ||
| 301 | my $drop = ($qty > $n) ? $n : $qty; | ||
| 302 | $self->send($DROP_ITEM,pack('CV',$pos,$drop)); | ||
| 303 | $dropped += $drop; | ||
| 304 | ($dropped >= $qty) || last; | ||
| 305 | } | ||
| 306 | |||
| 307 | return $qty; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub dropAll | ||
| 311 | { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | my $me = $self->{'me'}; | ||
| 315 | my $x = $self->{'me'}->{'xpos'} || "unkown"; | ||
| 316 | my $y = $self->{'me'}->{'ypos'} || "unkown"; | ||
| 317 | |||
| 318 | my @items = keys(%{$self->{'invByPos'}}); | ||
| 319 | foreach my $i (@items) { | ||
| 320 | my $pos = $self->{'invByPos'}->{$i}->{'pos'}; | ||
| 321 | my $qty = $self->{'invByPos'}->{$i}->{'quantity'}; | ||
| 322 | my $name = $self->{'invByPos'}->{$i}->{'name'} || "'object with no name'"; | ||
| 323 | $self->Log("Dropping $qty $name at ($x,$y)"); | ||
| 324 | $self->send($DROP_ITEM,pack('CV',$pos,$qty)); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | sub putInStorage | ||
| 329 | { | ||
| 330 | my $self = shift; | ||
| 331 | my ($qty,$name) = @_; | ||
| 332 | |||
| 333 | my $onHand = $self->qtyOnHand($name); | ||
| 334 | if ($onHand <= 0) { | ||
| 335 | $self->Log("Don't have any $name to put in storage"); | ||
| 336 | return 0; | ||
| 337 | } | ||
| 338 | if ($qty > $onHand) { | ||
| 339 | $qty = $onHand; | ||
| 340 | $self->Log("Only have any $qty $name to put in storage"); | ||
| 341 | } | ||
| 342 | my $stored = 0; | ||
| 343 | my $inv = $self->{'invByName'}->{lc($name)}; | ||
| 344 | foreach my $item (values %$inv) { | ||
| 345 | my $pos = $item->{'pos'}; | ||
| 346 | my $n = $item->{'quantity'}; | ||
| 347 | my $sto = ($qty > $n) ? $n : $qty; | ||
| 348 | $self->Log("Putting $sto $name in stoarge"); | ||
| 349 | $self->send($DEPOSITE_ITEM,pack('CV',$pos,$sto)); | ||
| 350 | $stored += $sto; | ||
| 351 | ($stored >= $qty) || last; | ||
| 352 | } | ||
| 353 | return $qty; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub tradeObject | ||
| 357 | { | ||
| 358 | my $self = shift; | ||
| 359 | my ($qty,$name) = @_; | ||
| 360 | |||
| 361 | my $onHand = $self->qtyOnHand($name); | ||
| 362 | if ($onHand <= 0) { | ||
| 363 | $self->Log("I don't have any $name to put up for trade"); | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | if ($onHand < $qty) { | ||
| 367 | $qty = $onHand; | ||
| 368 | $self->Log("I only have $qty $name to put up for trade"); | ||
| 369 | } | ||
| 370 | my $traded = 0; | ||
| 371 | my $inv = $self->{'invByName'}->{lc($name)}; | ||
| 372 | foreach my $item (values %$inv) { | ||
| 373 | my $pos = $item->{'pos'}; | ||
| 374 | my $n = $item->{'quantity'}; | ||
| 375 | my $trade = ($qty > $n) ? $n : $qty; | ||
| 376 | $self->send($PUT_OBJECT_ON_TRADE,pack('CCV',1,$pos,$trade)); | ||
| 377 | $traded += $trade; | ||
| 378 | ($traded >= $qty) || last; | ||
| 379 | } | ||
| 380 | return $qty; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub useInventoryItem | ||
| 384 | { | ||
| 385 | my $self = shift; | ||
| 386 | my ($name) = @_; | ||
| 387 | |||
| 388 | $name = lc($name); | ||
| 389 | if (!exists $self->{'invByName'}->{$name}) { | ||
| 390 | $self->Log("I don't have a $name to use"); | ||
| 391 | return undef; | ||
| 392 | } | ||
| 393 | my @items = keys(%{$self->{'invByName'}->{$name}}); | ||
| 394 | if ($#items < 0) { | ||
| 395 | $self->Log("No $name at any position . . ."); | ||
| 396 | return undef; | ||
| 397 | } | ||
| 398 | my $item = $self->{'invByName'}->{$name}->{$items[0]}; | ||
| 399 | if ($item->{'cooldown'} > time()) { | ||
| 400 | $self->Log("$name has not cooled down yet"); | ||
| 401 | return undef; | ||
| 402 | } | ||
| 403 | $self->send($USE_INVENTORY_ITEM,pack('v',$item->{'pos'})); | ||
| 404 | |||
| 405 | foreach my $item (values %{$self->{'invByPos'}}) { | ||
| 406 | $item->{'cooldown'} = time()+1; | ||
| 407 | } | ||
| 408 | return 1; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub attackActor | ||
| 412 | { | ||
| 413 | my $self = shift; | ||
| 414 | my ($id) = @_; | ||
| 415 | |||
| 416 | $self->send($ATTACK_SOMEONE,pack('L',$id)); | ||
| 417 | $self->{'path'} = undef; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub harvest | ||
| 421 | { | ||
| 422 | my $self = shift; | ||
| 423 | my ($id) = @_; | ||
| 424 | |||
| 425 | $self->send($HARVEST, pack('v',$id)); | ||
| 426 | } | ||
| 427 | |||
| 428 | sub isDead | ||
| 429 | { | ||
| 430 | my $self = shift; | ||
| 431 | my ($actor) = @_; | ||
| 432 | |||
| 433 | return $actor->{'dead'}; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub locateMe | ||
| 437 | { | ||
| 438 | my $self = shift; | ||
| 439 | |||
| 440 | $self->{'locateMe'} = undef; | ||
| 441 | $self->send($LOCATE_ME,""); | ||
| 442 | } | ||
| 443 | |||
| 444 | sub keepAlive | ||
| 445 | { | ||
| 446 | my $self = shift; | ||
| 447 | my ($force) = @_; | ||
| 448 | |||
| 449 | my $currentTime = time(); | ||
| 450 | my $nextHeartbeatTime = $self->{'lastHeartbeatTime'} + $self->{'heartbeatTimer'}; | ||
| 451 | if (($currentTime >= $nextHeartbeatTime) || ($force)) { | ||
| 452 | $self->send($HEART_BEAT,""); | ||
| 453 | $self->{'lastHeartbeatTime'} = time(); | ||
| 454 | } | ||
| 455 | if (($self->{'canTrade'}) && ($self->{'msgInterval'} >= 15)) { | ||
| 456 | my $nextMsgTime = $self->{'lastMsgAt'} + $self->{'msgInterval'} * 60; | ||
| 457 | if ($currentTime > $nextMsgTime) { | ||
| 458 | $self->{'lastMsgAt'} = $currentTime; | ||
| 459 | $self->Advertise(); | ||
| 460 | } | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | # unpack the items list from the pack sent by | ||
| 465 | # the server in to a hash | ||
| 466 | sub getItemsList | ||
| 467 | { | ||
| 468 | my $self = shift; | ||
| 469 | my ($data) = @_; | ||
| 470 | my %items; | ||
| 471 | |||
| 472 | my $nItems = unpack('C', substr($data,0,1)); | ||
| 473 | for(my $i=0; $i<$nItems; $i++) { | ||
| 474 | my $item = { | ||
| 475 | 'image' => unpack('v', substr($data,$i*8+1,2)), | ||
| 476 | 'quantity' => unpack('V', substr($data,$i*8+1+2,4)), | ||
| 477 | 'pos' => unpack('C', substr($data,$i*8+1+6,1)), | ||
| 478 | 'flags' => unpack('C', substr($data,$i*8+1+7,1)), | ||
| 479 | }; | ||
| 480 | $items{$item->{'pos'}} = $item; | ||
| 481 | } | ||
| 482 | return \%items; | ||
| 483 | } | ||
| 484 | |||
| 485 | sub deleteItem | ||
| 486 | { | ||
| 487 | my $self = shift; | ||
| 488 | my ($pos) = @_; | ||
| 489 | |||
| 490 | if (!exists $self->{'invByPos'}->{$pos}) { | ||
| 491 | $self->Log("Can't delete something I don't have !"); | ||
| 492 | return; | ||
| 493 | } | ||
| 494 | my $name = $self->{'invByPos'}->{$pos}->{'name'}; | ||
| 495 | delete $self->{'invByPos'}->{$pos}; | ||
| 496 | if (!defined($name)) { | ||
| 497 | $self->Log("Deleteing an item with no name !"); | ||
| 498 | return; | ||
| 499 | } | ||
| 500 | if (!exists $self->{'invByName'}->{$name}) { | ||
| 501 | $self->Log("Deleting an item with an unkown name !"); | ||
| 502 | return; | ||
| 503 | } | ||
| 504 | delete $self->{'invByName'}->{$name}->{$pos}; | ||
| 505 | if (keys(%{$self->{'invByName'}->{$name}}) == 0) { | ||
| 506 | delete $self->{'invByName'}->{$name}; | ||
| 507 | $self->Log("I no longer have any $name"); | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | sub Say | ||
| 512 | { | ||
| 513 | my $self = shift; | ||
| 514 | my ($msg) = @_; | ||
| 515 | |||
| 516 | $self->send($RAW_TEXT,$msg); | ||
| 517 | if ($msg =~ m/^\#beam me/i) { | ||
| 518 | print STDERR "Beaming may screw x,y locations up !\n"; | ||
| 519 | # $self->{'me'}->{'xpos'} = undef; | ||
| 520 | # $self->{'me'}->{'ypos'} = undef; | ||
| 521 | } | ||
| 522 | ($self->{'debug'} & $DEBUG_TEXT) && | ||
| 523 | $self->Log("I said '$msg'"); | ||
| 524 | } | ||
| 525 | |||
| 526 | sub LogTrade | ||
| 527 | { | ||
| 528 | my $self = shift; | ||
| 529 | |||
| 530 | my $trader = $self->{'tradeWith'}; | ||
| 531 | foreach my $pos (keys %{$self->{'thereTrades'}}) { | ||
| 532 | my $name = $self->{'thereTrades'}->{$pos}->{'name'}; | ||
| 533 | my $qty = $self->{'thereTrades'}->{$pos}->{'quantity'}; | ||
| 534 | $self->Log("$trader gave me $qty '".$name."'"); | ||
| 535 | } | ||
| 536 | foreach my $pos (keys %{$self->{'myTrades'}}) { | ||
| 537 | my $name = $self->{'myTrades'}->{$pos}->{'name'}; | ||
| 538 | my $qty = $self->{'myTrades'}->{$pos}->{'quantity'}; | ||
| 539 | $self->Log("I gave $trader $qty '".$name."'"); | ||
| 540 | } | ||
| 541 | $self->Log("Trade with '$trader' complete"); | ||
| 542 | } | ||
| 543 | |||
| 544 | sub getActors($) | ||
| 545 | { | ||
| 546 | my $self = shift; | ||
| 547 | |||
| 548 | my @actors = values %{$self->{'actorsByID'}}; | ||
| 549 | |||
| 550 | return wantarray ? @actors : \@actors; | ||
| 551 | } | ||
| 552 | |||
| 553 | sub actorsPosition | ||
| 554 | { | ||
| 555 | my $self = shift; | ||
| 556 | my ($id) = @_; | ||
| 557 | |||
| 558 | my $actor = $self->{'actorsByID'}->{$id}; | ||
| 559 | return ($actor->{'xpos'},$actor->{'ypos'}); | ||
| 560 | } | ||
| 561 | |||
| 562 | sub moveTo | ||
| 563 | { | ||
| 564 | my $self = shift; | ||
| 565 | my ($x,$y) = @_; | ||
| 566 | |||
| 567 | ($x =~ m/^\d+$/) || confess "x='$x' which is not numeric"; | ||
| 568 | ($y =~ m/^\d+$/) || confess "y='$y' which is not numeric"; | ||
| 569 | |||
| 570 | $self->{'me'}->{'lastMoved'} = time(); | ||
| 571 | |||
| 572 | $self->send($MOVE_TO,pack('vv',$x,$y)); | ||
| 573 | } | ||
| 574 | |||
| 575 | ########################################################################### | ||
| 576 | # Bag handling functions # | ||
| 577 | ########################################################################### | ||
| 578 | |||
| 579 | sub addBag | ||
| 580 | { | ||
| 581 | my $self = shift; | ||
| 582 | my ($id,$x,$y,$z) = @_; | ||
| 583 | |||
| 584 | if ($id >= $MAXBAGS) { | ||
| 585 | $self->Log("Bad bag ID $id at ($x,$y)"); | ||
| 586 | return undef; | ||
| 587 | } | ||
| 588 | if (defined($self->{'bagsByID'}->{$id})) { | ||
| 589 | $self->Log("Bag($id) already exists! this should not happen"); | ||
| 590 | } | ||
| 591 | my $bag = { | ||
| 592 | 'bagX' => $x, | ||
| 593 | 'bagY' => $y, | ||
| 594 | 'bagZ' => $z, | ||
| 595 | 'bagID' => $id, | ||
| 596 | }; | ||
| 597 | $self->{'bagsByID'}->{$id} = $bag; | ||
| 598 | #$self->Log("Bag($id) at ($x,$y,$z)"); | ||
| 599 | |||
| 600 | return $bag; | ||
| 601 | } | ||
| 602 | |||
| 603 | sub getBagByLocation | ||
| 604 | { | ||
| 605 | my $self = shift; | ||
| 606 | my ($x,$y) = @_; | ||
| 607 | |||
| 608 | foreach my $id (keys %{$self->{'bagsByID'}}) { | ||
| 609 | ($self->{'bagsByID'}->{$id}->{'bagX'} == $x) || next; | ||
| 610 | ($self->{'bagsByID'}->{$id}->{'bagY'} == $y) || next; | ||
| 611 | return $id; | ||
| 612 | } | ||
| 613 | return undef; | ||
| 614 | } | ||
| 615 | |||
| 616 | sub getBagByID | ||
| 617 | { | ||
| 618 | my $self = shift; | ||
| 619 | my ($id) = @_; | ||
| 620 | (exists $self->{'bagsByID'}->{$id}) || return undef; | ||
| 621 | return $self->{'bagsByID'}->{$id}; | ||
| 622 | } | ||
| 623 | |||
| 624 | sub openBag | ||
| 625 | { | ||
| 626 | my $self = shift; | ||
| 627 | my ($id) = @_; | ||
| 628 | |||
| 629 | my $bag = $self->{'bagsByID'}->{$id}; | ||
| 630 | if (!defined($bag)) { | ||
| 631 | $self->Log("Opening non existant bag $id, this should not happen"); | ||
| 632 | return undef; | ||
| 633 | } | ||
| 634 | my $me = $self->{'me'}; | ||
| 635 | if (($bag->{'bagX'} != $me->{'xpos'}) or ($bag->{'bagY'} != $me->{'ypos'})) { | ||
| 636 | $self->Log("Can't open bag $id because I am not on it"); | ||
| 637 | return undef; | ||
| 638 | } | ||
| 639 | if (defined($bag->{'items'})) { | ||
| 640 | delete $bag->{'items'}; | ||
| 641 | } | ||
| 642 | push(@{$self->{'groundItems'}}, $bag); | ||
| 643 | $self->send($INSPECT_BAG,pack('C',$id)); | ||
| 644 | ($self->{'debug'} & $DEBUG_BAGS) && | ||
| 645 | $self->Log("Inspecting bag $id"); | ||
| 646 | |||
| 647 | return $bag; | ||
| 648 | } | ||
| 649 | |||
| 650 | sub nearestBag($$) | ||
| 651 | { | ||
| 652 | my $self = shift; | ||
| 653 | my ($all) = @_; | ||
| 654 | |||
| 655 | my $closest = undef; | ||
| 656 | my $dist = 100000; | ||
| 657 | |||
| 658 | foreach my $bagID (keys %{$self->{'bagsByID'}}) { | ||
| 659 | my $bag = $self->{'bagsByID'}->{$bagID}; | ||
| 660 | if (!defined($bag->{'lookedAt'}) || $all) { | ||
| 661 | my $d = $self->distanceTo($bag->{'bagX'},$bag->{'bagY'}); | ||
| 662 | if ($d < $dist) { | ||
| 663 | $dist = $d; | ||
| 664 | $closest = $bag; | ||
| 665 | } | ||
| 666 | } | ||
| 667 | } | ||
| 668 | if ($self->{'debug'} & $DEBUG_BAGS) { | ||
| 669 | my $id = $closest->{'bagID'}; | ||
| 670 | my ($x,$y) = ($closest->{'bagX'},$closest->{'bagY'}); | ||
| 671 | $self->Log("Nearest bag($id) is at ($x,$y), distance=$dist"); | ||
| 672 | } | ||
| 673 | return ($closest,$dist); | ||
| 674 | } | ||
| 675 | |||
| 676 | sub pickUp($$$) | ||
| 677 | { | ||
| 678 | my $self = shift; | ||
| 679 | my ($bag,$pos,$qty) = @_; | ||
| 680 | |||
| 681 | my $item = $bag->{'items'}->{$pos}; | ||
| 682 | if (!defined($item)) { | ||
| 683 | $self->Log("Picking up item from bad position in bag"); | ||
| 684 | return undef; | ||
| 685 | } | ||
| 686 | if ($item->{'quantity'} < $qty) { | ||
| 687 | $self->Log("Picking up more than in the bag"); | ||
| 688 | $qty = $item->{'quantity'}; | ||
| 689 | } | ||
| 690 | $self->send($PICK_UP_ITEM, pack('CV',$pos,$qty)); | ||
| 691 | $item->{'pickUp'} = $qty; | ||
| 692 | push(@{$self->{'pickUpQueue'}}, $item); | ||
| 693 | } | ||
| 694 | |||
| 695 | ########################################################################### | ||
| 696 | # # | ||
| 697 | ########################################################################### | ||
| 698 | |||
| 699 | sub distanceTo | ||
| 700 | { | ||
| 701 | my $self = shift; | ||
| 702 | my ($toX,$toY) = @_; | ||
| 703 | my $fromX = $self->{'me'}->{'xpos'}; | ||
| 704 | my $fromY = $self->{'me'}->{'ypos'}; | ||
| 705 | |||
| 706 | return $self->{'Map'}->distance($fromX,$fromY,$toX,$toY); | ||
| 707 | } | ||
| 708 | |||
| 709 | sub getStat | ||
| 710 | { | ||
| 711 | my $self = shift; | ||
| 712 | my ($stat) = @_; | ||
| 713 | my @stat = (); | ||
| 714 | if (exists $self->{'stats'}->{$stat}) { | ||
| 715 | @stat = @{$self->{'stats'}->{$stat}}; | ||
| 716 | } | ||
| 717 | return wantarray ? @stat : $stat[0]; | ||
| 718 | } | ||
| 719 | |||
| 720 | sub useMapObject | ||
| 721 | { | ||
| 722 | my $self = shift; | ||
| 723 | my ($objID) = @_; | ||
| 724 | |||
| 725 | $self->send($USE_MAP_OBJECT,pack('Vl',$objID,-1)); | ||
| 726 | } | ||
| 727 | |||
| 728 | sub touchPlayer | ||
| 729 | { | ||
| 730 | my $self = shift; | ||
| 731 | my ($name) = @_; | ||
| 732 | |||
| 733 | $name = lc($name); | ||
| 734 | if (!defined($self->{'actorsByName'}->{$name})) { | ||
| 735 | $self->Log("Not touching unkown player $name"); | ||
| 736 | return; | ||
| 737 | } | ||
| 738 | my $id = $self->{'actorsByName'}->{$name}->{'id'}; | ||
| 739 | |||
| 740 | $self->{'crntNPC'} = $id; | ||
| 741 | $self->send($TOUCH_PLAYER,pack('l',$id)); | ||
| 742 | $self->{'NPCchat'}->{$id}->{'waiting'} = time(); | ||
| 743 | delete $self->{'NPCchat'}->{$id}->{'options'}; | ||
| 744 | delete $self->{'NPCchat'}->{$id}->{'text'}; | ||
| 745 | delete $self->{'NPCchat'}->{$id}->{'name'}; | ||
| 746 | } | ||
| 747 | |||
| 748 | sub respondToNPC | ||
| 749 | { | ||
| 750 | my $self = shift; | ||
| 751 | my ($opt) = @_; | ||
| 752 | |||
| 753 | my $id = $opt->{'id'}; | ||
| 754 | $self->{'crntNPC'} = $id; | ||
| 755 | $self->send($RESPOND_TO_NPC,pack('vv',$opt->{'actor'},$id)); | ||
| 756 | $self->{'NPCchat'}->{$id}->{'waiting'} = time(); | ||
| 757 | |||
| 758 | } | ||
| 759 | |||
| 760 | ########################################################### | ||
| 761 | # MISCELLANEOUS CALLBACKS # | ||
| 762 | ########################################################### | ||
| 763 | |||
| 764 | sub LOG_IN_OK | ||
| 765 | { | ||
| 766 | my $self = shift; | ||
| 767 | my ($type,$len,$data) = @_; | ||
| 768 | |||
| 769 | $self->{'loggedIn'} = 1; | ||
| 770 | } | ||
| 771 | |||
| 772 | sub LOG_IN_NOT_OK | ||
| 773 | { | ||
| 774 | my $self = shift; | ||
| 775 | my ($type,$len,$data) = @_; | ||
| 776 | |||
| 777 | $self->{'loggedIn'} = 0; | ||
| 778 | $self->{'failedLogins'} += 1; | ||
| 779 | } | ||
| 780 | |||
| 781 | sub PING_REQUEST | ||
| 782 | { | ||
| 783 | my $self = shift; | ||
| 784 | my ($type,$len,$data) = @_; | ||
| 785 | |||
| 786 | $self->send($PING_RESPONSE,$data); | ||
| 787 | } | ||
| 788 | |||
| 789 | sub NEW_MINUTE | ||
| 790 | { | ||
| 791 | my $self = shift; | ||
| 792 | my ($type,$len,$data) = @_; | ||
| 793 | |||
| 794 | $self->{'gminute'} = unpack('v',$data); | ||
| 795 | if ($self->{'gminute'} % 60 == 0) { | ||
| 796 | $self->{'canHarvExp'} = 1; | ||
| 797 | } | ||
| 798 | } | ||
| 799 | |||
| 800 | sub STORAGE_LIST | ||
| 801 | { | ||
| 802 | my $self = shift; | ||
| 803 | my ($type,$len,$data) = @_; | ||
| 804 | |||
| 805 | my @items = unpack('C(CZ*)*',$data); # How can you not love perl ! | ||
| 806 | my $n = shift @items; | ||
| 807 | for(my $i=0; $i<$n; $i++) { | ||
| 808 | my $id = shift(@items); | ||
| 809 | my $name = shift(@items); | ||
| 810 | $self->{'stoCategories'}->{$name} = $id; | ||
| 811 | } | ||
| 812 | $self->{'waitingForStorage'} = 0; | ||
| 813 | } | ||
| 814 | |||
| 815 | sub STORAGE_ITEMS | ||
| 816 | { | ||
| 817 | my $self = shift; | ||
| 818 | my ($type,$len,$data) = @_; | ||
| 819 | |||
| 820 | my $n = unpack('C',substr($data,0,1)); | ||
| 821 | if ($n == 255) { | ||
| 822 | return; | ||
| 823 | } | ||
| 824 | |||
| 825 | my $catgry = unpack('C',substr($data,1,1)); | ||
| 826 | |||
| 827 | my @items = unpack("CC(vVC)$n", $data); | ||
| 828 | } | ||
| 829 | |||
| 830 | |||
| 831 | sub GET_ITEMS_COOLDOWN | ||
| 832 | { | ||
| 833 | my $self = shift; | ||
| 834 | my ($type,$len,$data) = @_; | ||
| 835 | |||
| 836 | my @v = unpack('(Cvv)*',$data); | ||
| 837 | my $n = ($#v+1)/3; | ||
| 838 | |||
| 839 | foreach my $item (values %{$self->{'invByPos'}}) { | ||
| 840 | $item->{'cooldown'} = 0; | ||
| 841 | } | ||
| 842 | for(my $i=0; $i<$n; $i++) { | ||
| 843 | my ($pos,$max,$cool) = @v[$i*3 .. $i*3+2]; | ||
| 844 | $self->{'invByPos'}->{$pos}->{'cooldown'} = time()+$cool+1; | ||
| 845 | } | ||
| 846 | } | ||
| 847 | |||
| 848 | sub RAW_TEXT | ||
| 849 | { | ||
| 850 | my $self = shift; | ||
| 851 | my ($type,$len,$data) = @_; | ||
| 852 | |||
| 853 | my $text = substr($data,2); | ||
| 854 | |||
| 855 | ($self->{'debug'} & $DEBUG_TEXT) && $self->Log("RAW TEXT: $text"); | ||
| 856 | |||
| 857 | if (($text =~ m/\s*(\w+) wants to trade with you/) && $self->{'canTrade'}) { | ||
| 858 | my $name = lc($1); | ||
| 859 | $self->Log("Trade request from '".$name."'"); | ||
| 860 | my $actor = $self->{'actorsByName'}->{$name}; | ||
| 861 | if (!defined($actor)) { | ||
| 862 | $self->sendPM($name, "Sorry, I can't get your actor ID, this should not happen . . ."); | ||
| 863 | $self->sendPM($name, "Please notify the owner of this bot"); | ||
| 864 | return; | ||
| 865 | } | ||
| 866 | $self->send($TRADE_WITH,pack('V',$actor->{'id'})); | ||
| 867 | } | ||
| 868 | elsif ($text =~ m/^\[PM from (\w+): (.*)\]/) { | ||
| 869 | $self->Log("$1 said '".$2."'"); | ||
| 870 | if($self->can("handlePM")) { | ||
| 871 | $self->handlePM($1,$2); | ||
| 872 | } | ||
| 873 | } | ||
| 874 | elsif ($text =~ m/^You are in (.*\S)\s+\[(.+)\]/) { | ||
| 875 | $self->{'locateMe'} = "$1 [$2]"; | ||
| 876 | if (defined($self->{'locReply'})) { | ||
| 877 | $self->sendPM($self->{'locReply'},"$1 at $2"); | ||
| 878 | $self->{'locReply'} = undef; | ||
| 879 | } | ||
| 880 | } | ||
| 881 | elsif ($text =~ m/^Items you have in your storage:/) { | ||
| 882 | my @lines = split("\n", $text); | ||
| 883 | if (my $user = $self->{'tellSTO'}) { | ||
| 884 | foreach my $line (@lines) { | ||
| 885 | ($line =~ m/$self->{'stoRE'}/i) && $self->sendPM($user,$line); | ||
| 886 | } | ||
| 887 | } | ||
| 888 | } | ||
| 889 | elsif ($text =~ m/^Your harvesting experience limit for this hour expired/) { | ||
| 890 | $self->{'canHarvExp'} = 0 | ||
| 891 | } | ||
| 892 | elsif ($text =~ m/^Today is a special day/) { | ||
| 893 | my @lines = split("\n",$text); | ||
| 894 | $self->{'specialDay'} = $lines[1]; | ||
| 895 | $self->Log("Special Day: ".$self->{'specialDay'}); | ||
| 896 | } | ||
| 897 | elsif ($text =~ m/^Day ends/) { | ||
| 898 | $self->{'specialDay'} = "Just an ordinary day"; | ||
| 899 | } | ||
| 900 | |||
| 901 | return $text; | ||
| 902 | } | ||
| 903 | |||
| 904 | ########################################################################## | ||
| 905 | # ACTOR RELATED CALLBACKS # | ||
| 906 | ########################################################################## | ||
| 907 | |||
| 908 | sub decodeTitle | ||
| 909 | { | ||
| 910 | my ($title) = @_; | ||
| 911 | |||
| 912 | my ($name,$nameC,$guild,$guildC) = ("","","",""); | ||
| 913 | my ($i,$j,$k); | ||
| 914 | my $len = length($title); | ||
| 915 | |||
| 916 | for($i=0; $i <= $len-1; $i++) { | ||
| 917 | (ord(substr($title,$i)) >= 127) || last; | ||
| 918 | } | ||
| 919 | for($j=$i; $j <= $len-1; $j++) { | ||
| 920 | (ord(substr($title,$j,1)) < 127) || last; | ||
| 921 | } | ||
| 922 | if ($j <= $len-1) { | ||
| 923 | for($k=$j; $k <= $len-1; $k++) { | ||
| 924 | (ord(substr($title,$k,1)) >= 127) || last; | ||
| 925 | } | ||
| 926 | $guildC = ord(substr($title,$j,$k-$j)); | ||
| 927 | $guild = substr($title,$k); | ||
| 928 | } | ||
| 929 | $nameC = ord(substr($title,0,$i)); | ||
| 930 | $name = substr($title,$i,$j-$i); | ||
| 931 | |||
| 932 | return ([$nameC,$name],[$guildC,$guild]); | ||
| 933 | } | ||
| 934 | |||
| 935 | sub ADD_NEW_ACTOR | ||
| 936 | { | ||
| 937 | my $self = shift; | ||
| 938 | my ($type,$len,$data) = @_; | ||
| 939 | |||
| 940 | my $actor; | ||
| 941 | my $id = unpack('v', substr($data,0,2)); | ||
| 942 | $actor->{'id'} = $id; | ||
| 943 | $actor->{'xpos'} = unpack('v', substr($data,2,2)) & 0x7FF; | ||
| 944 | $actor->{'ypos'} = unpack('v', substr($data,4,2)) & 0x7FF; | ||
| 945 | $actor->{'zpos'} = unpack('v', substr($data,6,2)); | ||
| 946 | $actor->{'prevX'} = $actor->{'xpos'}; | ||
| 947 | $actor->{'prevY'} = $actor->{'ypos'}; | ||
| 948 | $actor->{'zrot'} = unpack('v', substr($data,8,2)); | ||
| 949 | $actor->{'bufs'} = 0; # ignore bufs at the moment | ||
| 950 | $actor->{'type'} = ord(substr($data,10,1)); | ||
| 951 | $actor->{'frame'} = substr($data,11,1); | ||
| 952 | $actor->{'stats'}->{'mp'} = [unpack('v', substr($data,14,2)),unpack('v', substr($data,12,2))]; | ||
| 953 | $actor->{'kind'} = ord(substr($data,16,1)); | ||
| 954 | my ($name,$guild) = decodeTitle(unpack('Z*',substr($data,17,13))); | ||
| 955 | $actor->{'nColour'} = $name->[0]; | ||
| 956 | $actor->{'name'} = lc($name->[1]); | ||
| 957 | $actor->{'gColour'} = $guild->[0]; | ||
| 958 | $actor->{'guild'} = $guild->[1]; | ||
| 959 | $actor->{'lastMoved'} = time(); | ||
| 960 | $actor->{'inCombat'} = 0; | ||
| 961 | $actor->{'map'} = $self->crntMap(); | ||
| 962 | |||
| 963 | $self->{'actorsByID'}->{$id} = $actor; | ||
| 964 | $self->{'actorsByName'}->{$actor->{'name'}} = $actor; # Assumes unique names . . . | ||
| 965 | if ($self->{'my_id'} == $id) { | ||
| 966 | $self->{'me'} = $self->{'actorsByID'}->{$id}; | ||
| 967 | } | ||
| 968 | |||
| 969 | return $actor; | ||
| 970 | } | ||
| 971 | |||
| 972 | sub ADD_NEW_ENHANCED_ACTOR | ||
| 973 | { | ||
| 974 | my $self = shift; | ||
| 975 | my ($type,$len,$data) = @_; | ||
| 976 | |||
| 977 | my $actor; | ||
| 978 | my $id = unpack('v', substr($data,0,2)); | ||
| 979 | $actor->{'id'} = $id; | ||
| 980 | $actor->{'xpos'} = unpack('v', substr($data,2,2)) & 0x7FF; | ||
| 981 | $actor->{'ypos'} = unpack('v', substr($data,4,2)) & 0x7FF; | ||
| 982 | $actor->{'zpos'} = unpack('v', substr($data,6,2)); | ||
| 983 | $actor->{'prevX'} = $actor->{'xpos'}; | ||
| 984 | $actor->{'prevY'} = $actor->{'ypos'}; | ||
| 985 | $actor->{'zrot'} = unpack('v', substr($data,8,2)); | ||
| 986 | $actor->{'bufs'} = 0; # ignore bufs at the moment | ||
| 987 | $actor->{'type'} = ord(substr($data,10,1)); | ||
| 988 | $actor->{'frame'} = substr($data,11,1); | ||
| 989 | $actor->{'skin'} = substr($data,12,1); | ||
| 990 | $actor->{'hair'} = substr($data,13,1); | ||
| 991 | $actor->{'shirt'} = substr($data,14,1); | ||
| 992 | $actor->{'pants'} = substr($data,15,1); | ||
| 993 | $actor->{'boots'} = substr($data,16,1); | ||
| 994 | $actor->{'head'} = substr($data,17,1); | ||
| 995 | $actor->{'shield'} = substr($data,18,1); | ||
| 996 | $actor->{'weapon'} = substr($data,19,1); | ||
| 997 | $actor->{'cape'} = substr($data,20,1); | ||
| 998 | $actor->{'helmet'} = substr($data,21,1); | ||
| 999 | $actor->{'stats'}->{'mp'} = [unpack('v', substr($data,25,2)),unpack('v', substr($data,23,2))]; | ||
| 1000 | $actor->{'kind'} = ord(substr($data,27,1)); | ||
| 1001 | my ($name,$guild) = decodeTitle(unpack('Z*',substr($data,28,13))); | ||
| 1002 | $actor->{'nColour'} = $name->[0]; | ||
| 1003 | $actor->{'name'} = lc($name->[1]); | ||
| 1004 | $actor->{'gColour'} = $guild->[0]; | ||
| 1005 | $actor->{'guild'} = $guild->[1]; | ||
| 1006 | $actor->{'lastMoved'} = time(); | ||
| 1007 | $actor->{'inCombat'} = 0; | ||
| 1008 | $actor->{'map'} = $self->crntMap(); | ||
| 1009 | |||
| 1010 | $self->{'actorsByID'}->{$id} = $actor; | ||
| 1011 | $self->{'actorsByName'}->{$actor->{'name'}} = $actor; # Assumes unique names . . . | ||
| 1012 | if ($self->{'my_id'} == $id) { | ||
| 1013 | $self->{'me'} = $self->{'actorsByID'}->{$id}; | ||
| 1014 | } | ||
| 1015 | defined($self->{'map'}) && $self->{'map'}->setOccupied($actor); | ||
| 1016 | |||
| 1017 | return $actor; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | sub KILL_ALL_ACTORS | ||
| 1021 | { | ||
| 1022 | my $self = shift; | ||
| 1023 | my ($type,$len,$data) = @_; | ||
| 1024 | $self->{'actorsByID'} = {}; | ||
| 1025 | $self->{'actorsByName'} = {}; | ||
| 1026 | $self->{'me'} = undef; | ||
| 1027 | $self->{'path'} = undef; | ||
| 1028 | defined($self->{'map'}) && $self->{'map'}->setAllVacant(); | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | sub REMOVE_ACTOR | ||
| 1032 | { | ||
| 1033 | my $self = shift; | ||
| 1034 | my ($type,$len,$data) = @_; | ||
| 1035 | |||
| 1036 | my $id = unpack('v', $data); | ||
| 1037 | my $actor = $self->{'actorsByID'}->{$id}; | ||
| 1038 | if (defined($actor)) { | ||
| 1039 | my $name = $actor->{'name'}; | ||
| 1040 | if (defined($self->{'actorsByName'}->{$name})) { | ||
| 1041 | if ($self->{'actorsByName'}->{$name}->{'id'} == $id) { | ||
| 1042 | delete $self->{'actorsByName'}->{$name}; | ||
| 1043 | } | ||
| 1044 | } | ||
| 1045 | delete $self->{'actorsByID'}->{$id}; | ||
| 1046 | defined($self->{'map'}) && $self->{'map'}->setVacant($actor); | ||
| 1047 | } | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | sub ADD_ACTOR_COMMAND | ||
| 1051 | { | ||
| 1052 | my $self = shift; | ||
| 1053 | my ($type,$len,$data) = @_; | ||
| 1054 | |||
| 1055 | my %moveXY = ( | ||
| 1056 | "n" => [ 0, 1], "ne" => [ 1, 1], | ||
| 1057 | "e" => [ 1, 0], "se" => [ 1,-1], | ||
| 1058 | "s" => [ 0,-1], "sw" => [ -1,-1], | ||
| 1059 | "w" => [-1, 0], "nw" => [ -1, 1], | ||
| 1060 | ); | ||
| 1061 | |||
| 1062 | my $actorID = unpack('v', substr($data,0,2)); | ||
| 1063 | my $cmd = substr($data,2,1); | ||
| 1064 | my $actor = $self->{'actorsByID'}->{$actorID}; | ||
| 1065 | my $cmdStr = $ActorCommandsByID{$cmd}; | ||
| 1066 | my $name = "Unknown actor"; | ||
| 1067 | if (defined($actor)) { | ||
| 1068 | $name = $actor->{'name'}; | ||
| 1069 | if ($cmdStr =~ m/^move_(\w+)/) { | ||
| 1070 | $actor->{'prevX'} = $actor->{'xpos'}; | ||
| 1071 | $actor->{'prevY'} = $actor->{'ypos'}; | ||
| 1072 | defined($self->{'map'}) && $self->{'map'}->setVacant($actor); | ||
| 1073 | $actor->{'xpos'} += $moveXY{$1}->[0]; | ||
| 1074 | $actor->{'ypos'} += $moveXY{$1}->[1]; | ||
| 1075 | defined($self->{'map'}) && $self->{'map'}->setOccupied($actor); | ||
| 1076 | $actor->{'lastMoved'} = time(); | ||
| 1077 | } | ||
| 1078 | elsif ($cmdStr =~ m/^die.*/) { | ||
| 1079 | $actor->{'dead'} = 1; | ||
| 1080 | $actor->{'inCombat'} = 0; | ||
| 1081 | } | ||
| 1082 | elsif ($cmdStr =~ m/^enter_combat.*/) { | ||
| 1083 | $actor->{'inCombat'} = 1; | ||
| 1084 | } | ||
| 1085 | elsif ($cmdStr =~ m/^leave_combat.*/) { | ||
| 1086 | $actor->{'inCombat'} = 0; | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | ($self->{'debug'} & $DEBUG_TYPES) && | ||
| 1090 | $self->Log("Actor=$name Command=$cmdStr"); | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | sub GET_ACTOR_DAMAGE | ||
| 1094 | { | ||
| 1095 | my $self = shift; | ||
| 1096 | my ($type,$len,$data) = @_; | ||
| 1097 | |||
| 1098 | my $id = unpack('v',substr($data,0,2)); | ||
| 1099 | my $dmg = unpack('v',substr($data,2,2)); | ||
| 1100 | my $actor = $self->{'actorsByID'}->{$id}; | ||
| 1101 | if (!defined($actor)) { | ||
| 1102 | $self->Log("Damage to unkown actor($id)"); | ||
| 1103 | return undef; | ||
| 1104 | } | ||
| 1105 | $actor->{'stats'}->{'mp'} -= $dmg; | ||
| 1106 | |||
| 1107 | return $actor; | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | sub GET_ACTOR_HEAL | ||
| 1111 | { | ||
| 1112 | my $self = shift; | ||
| 1113 | my ($type,$len,$data) = @_; | ||
| 1114 | |||
| 1115 | my $id = unpack('v',substr($data,0,2)); | ||
| 1116 | my $dmg = unpack('v',substr($data,2,2)); | ||
| 1117 | my $actor = $self->{'actorsByID'}->{$id}; | ||
| 1118 | if (!defined($actor)) { | ||
| 1119 | $self->Log("Healing unkown actor($id)"); | ||
| 1120 | return undef; | ||
| 1121 | } | ||
| 1122 | $actor->{'stats'}->{'mp'} += $dmg; | ||
| 1123 | |||
| 1124 | return $actor; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | sub SEND_NPC_INFO | ||
| 1128 | { | ||
| 1129 | my $self = shift; | ||
| 1130 | my ($type,$len,$data) = @_; | ||
| 1131 | |||
| 1132 | my $id = $self->{'crntNPC'}; | ||
| 1133 | my $name = unpack('Z*',$data); | ||
| 1134 | $self->{'NPCchat'}->{$id}->{'name'} = $name; | ||
| 1135 | $self->{'NPCchat'}->{$id}->{'waiting'} = time(); | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | sub NPC_TEXT | ||
| 1139 | { | ||
| 1140 | my $self = shift; | ||
| 1141 | my ($type,$len,$data) = @_; | ||
| 1142 | my $txt; | ||
| 1143 | |||
| 1144 | my $id = $self->{'crntNPC'}; | ||
| 1145 | my ($byte1,$byte2) = unpack('CC',$data); | ||
| 1146 | if ($byte1 > 127 and $byte2 > 127) { | ||
| 1147 | # Questlog addition (don't handle it at the moment) | ||
| 1148 | $txt = substr($data,2); } | ||
| 1149 | else { | ||
| 1150 | $txt = substr($data,1); | ||
| 1151 | } | ||
| 1152 | $self->{'NPCchat'}->{$id}->{'text'} = $txt; | ||
| 1153 | $self->{'NPCchat'}->{$id}->{'waiting'} = time(); | ||
| 1154 | return $txt | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | sub NPC_OPTIONS_LIST | ||
| 1158 | { | ||
| 1159 | my $self = shift; | ||
| 1160 | my ($type,$len,$data) = @_; | ||
| 1161 | my $offset=0; | ||
| 1162 | my %options; | ||
| 1163 | |||
| 1164 | my $fromID = $self->{'crntNPC'}; | ||
| 1165 | for(my $i=0;$i<20;$i++) { | ||
| 1166 | if ($offset + 3 > $len) { | ||
| 1167 | last; | ||
| 1168 | } | ||
| 1169 | my $n = unpack('v',substr($data,$offset,2)); | ||
| 1170 | if ($offset + 3 + $n + 2 + 2 > $len) { | ||
| 1171 | last; | ||
| 1172 | } | ||
| 1173 | my $response = lc(substr($data,$offset+2,$n-1)); | ||
| 1174 | my $id = unpack('v',substr($data,$offset+2+$n)); | ||
| 1175 | my $toActor = unpack('v',substr($data,$offset+2+2+$n)); | ||
| 1176 | $options{$response} = { | ||
| 1177 | 'id' => $id, | ||
| 1178 | 'actor' => $toActor, | ||
| 1179 | }; | ||
| 1180 | $offset += $n+2+2+2; | ||
| 1181 | ($self->{'debug'} & $DEBUG_TEXT) && $self->Log("NPC Option($toActor): $id: $response"); | ||
| 1182 | } | ||
| 1183 | $self->{'NPCchat'}->{$fromID}->{'options'} = \%options; | ||
| 1184 | delete $self->{'NPCchat'}->{$fromID}->{'waiting'}; | ||
| 1185 | return \%options; | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | ########################################################################## | ||
| 1189 | # CALLBACKS ABOUT THIS CLIENT # | ||
| 1190 | ########################################################################## | ||
| 1191 | |||
| 1192 | sub TELEPORT_OUT | ||
| 1193 | { | ||
| 1194 | my $self = shift; | ||
| 1195 | my ($type,$len,$data) = @_; | ||
| 1196 | } | ||
| 1197 | |||
| 1198 | sub TELEPORT_IN | ||
| 1199 | { | ||
| 1200 | my $self = shift; | ||
| 1201 | my ($type,$len,$data) = @_; | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | sub GET_TELEPORTERS_LIST | ||
| 1205 | { | ||
| 1206 | my $self = shift; | ||
| 1207 | my ($type,$len,$data) = @_; | ||
| 1208 | |||
| 1209 | my $n = unpack('c',substr($data,0,2)); | ||
| 1210 | for(my $i=0; $i<$n; $i++) { | ||
| 1211 | my ($x,$y,$type) = unpack('vvC',substr($data,$i*5+2,5)); | ||
| 1212 | $self->Log("TELEPORTER at ($x,$y)"); | ||
| 1213 | } | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | sub getMap | ||
| 1217 | { | ||
| 1218 | my $self = shift; | ||
| 1219 | my ($name) = @_; | ||
| 1220 | |||
| 1221 | $name = "/maps/".$name.".elm"; | ||
| 1222 | my $map = $self->{'mapCache'}->{$name}; | ||
| 1223 | if (!defined($map)) { | ||
| 1224 | $map = Games::EternalLands::Map->new($name,$self->{'elDir'}); | ||
| 1225 | $self->{'mapCache'}->{$name} = $map; | ||
| 1226 | } | ||
| 1227 | return $map; | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | sub CHANGE_MAP | ||
| 1231 | { | ||
| 1232 | my $self = shift; | ||
| 1233 | my ($type,$len,$data) = @_; | ||
| 1234 | |||
| 1235 | $self->{'path'} = undef; | ||
| 1236 | $self->{'bagsByID'} = {}; | ||
| 1237 | if (defined($self->{'elDir'})) { | ||
| 1238 | my $name = unpack('Z*',$data); | ||
| 1239 | |||
| 1240 | if (!defined($self->{'mapCache'}->{$name})) { | ||
| 1241 | my @harvestables; | ||
| 1242 | my @entrable; | ||
| 1243 | my $map = Games::EternalLands::Map->new($name,$self->{'elDir'}); | ||
| 1244 | my @objects = $map->objects(); | ||
| 1245 | foreach my $id (@objects) { | ||
| 1246 | my $fname = $map->{'3dByID'}->{$id}->{'file_name'}; | ||
| 1247 | $fname =~ s%.*/([^/]+\.e3d)$%$1%; | ||
| 1248 | if (defined($self->{'harvestableTypes'}->{$fname})) { | ||
| 1249 | push(@harvestables,$id); | ||
| 1250 | } | ||
| 1251 | if (defined($self->{'entrableTypes'}->{$fname})) { | ||
| 1252 | push(@entrable,$id); | ||
| 1253 | } | ||
| 1254 | delete $self->{'Map'}->{'3dByID'}->{$id}->{'file_name'}; | ||
| 1255 | } | ||
| 1256 | $map->{'entrable'} = \@entrable; | ||
| 1257 | $map->{'harvestables'} = \@harvestables; | ||
| 1258 | $self->{'mapCache'}->{$name} = $map; | ||
| 1259 | } | ||
| 1260 | $self->{'Map'} = $self->{'mapCache'}->{$name}; | ||
| 1261 | } | ||
| 1262 | $self->locateMe(); | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | =begin | ||
| 1266 | $VAR1 = [ | ||
| 1267 | 10, 10, phy | ||
| 1268 | 14, 14, coo | ||
| 1269 | 4, 4, rea | ||
| 1270 | 4, 4, wil | ||
| 1271 | 4, 4, ins | ||
| 1272 | 4, 4, vit | ||
| 1273 | 0-11 | ||
| 1274 | |||
| 1275 | 0, 0, nexus | ||
| 1276 | 0, 0, | ||
| 1277 | 0, 0, | ||
| 1278 | 0, 0, | ||
| 1279 | 0, 0, | ||
| 1280 | 0, 0, | ||
| 1281 | 12-23 | ||
| 1282 | |||
| 1283 | 24 0, 0, man | ||
| 1284 | 6 16, 16, har | ||
| 1285 | 8 0, 0, alc | ||
| 1286 | 30 16, 23, oa | ||
| 1287 | 2 19, 19, att | ||
| 1288 | 4 17, 17, def | ||
| 1289 | 6 0, 0, mag | ||
| 1290 | 8 0, 0, pot | ||
| 1291 | 24-39 | ||
| 1292 | |||
| 1293 | 240, 240, carry | ||
| 1294 | 28, 35, mp | ||
| 1295 | 32, 32, ep | ||
| 1296 | 40-45 | ||
| 1297 | |||
| 1298 | 65506, | ||
| 1299 | 250, | ||
| 1300 | 0, | ||
| 1301 | 9175040, | ||
| 1302 | 933232640, | ||
| 1303 | 1184235520, | ||
| 1304 | 0, | ||
| 1305 | 9175040, | ||
| 1306 | 562888704, | ||
| 1307 | 1099759617, | ||
| 1308 | 2420834305, | ||
| 1309 | 2601713664, | ||
| 1310 | 1503789056, | ||
| 1311 | 1539506176, | ||
| 1312 | 0, | ||
| 1313 | 9175040, | ||
| 1314 | 0, | ||
| 1315 | 9175040, | ||
| 1316 | 0, | ||
| 1317 | 65535, | ||
| 1318 | 250, | ||
| 1319 | 0, | ||
| 1320 | 0, | ||
| 1321 | 0, | ||
| 1322 | 0, | ||
| 1323 | 140, | ||
| 1324 | 0, | ||
| 1325 | 0 | ||
| 1326 | ]; | ||
| 1327 | =cut | ||
| 1328 | |||
| 1329 | sub HERE_YOUR_STATS | ||
| 1330 | { | ||
| 1331 | my $self = shift; | ||
| 1332 | my ($type,$len,$data) = @_; | ||
| 1333 | |||
| 1334 | my @v = unpack('v[48]V[17]v[7]',$data); | ||
| 1335 | $self->{'stats'} = { | ||
| 1336 | 'phy' => [$v[0],$v[1]], | ||
| 1337 | 'coo' => [$v[2],$v[3]], | ||
| 1338 | 'rea' => [$v[4],$v[5]], | ||
| 1339 | 'wil' => [$v[6],$v[7]], | ||
| 1340 | 'ins' => [$v[8],$v[9]], | ||
| 1341 | 'phy' => [$v[10],$v[11]], | ||
| 1342 | }; | ||
| 1343 | $self->{'nexus'} = { | ||
| 1344 | 'human' => [$v[12],$v[13]], | ||
| 1345 | 'animal' => [$v[14],$v[15]], | ||
| 1346 | 'vegetal' => [$v[16],$v[17]], | ||
| 1347 | 'inorganic' => [$v[18],$v[19]], | ||
| 1348 | 'artificial' => [$v[20],$v[21]], | ||
| 1349 | 'magic' => [$v[22],$v[23]], | ||
| 1350 | }; | ||
| 1351 | $self->{'skills'} = { | ||
| 1352 | 'man' => [$v[24],$v[25]], | ||
| 1353 | 'har' => [$v[26],$v[27]], | ||
| 1354 | 'alc' => [$v[28],$v[29]], | ||
| 1355 | 'oa' => [$v[30],$v[31]], | ||
| 1356 | 'att' => [$v[32],$v[33]], | ||
| 1357 | 'def' => [$v[34],$v[35]], | ||
| 1358 | 'mag' => [$v[36],$v[37]], | ||
| 1359 | 'pot' => [$v[38],$v[39]], | ||
| 1360 | 'sum' => [$v[66],$v[67]], | ||
| 1361 | 'cra' => [$v[70],$v[71]], | ||
| 1362 | }; | ||
| 1363 | $self->{'stats'} = { | ||
| 1364 | 'carry' => [$v[40],$v[41]], | ||
| 1365 | 'mp' => [$v[42],$v[43]], | ||
| 1366 | 'ep' => [$v[44],$v[45]], | ||
| 1367 | 'food' => [$v[46],45], | ||
| 1368 | }; | ||
| 1369 | $self->{'research'} = { | ||
| 1370 | 'completed' => $v[47], | ||
| 1371 | 'researching' => $v[81], | ||
| 1372 | 'total' => $v[82], | ||
| 1373 | }; | ||
| 1374 | |||
| 1375 | $self->{'experience'} = { | ||
| 1376 | 'man' => [$v[49],$v[50]], | ||
| 1377 | 'har' => [$v[51],$v[52]], | ||
| 1378 | 'alc' => [$v[53],$v[54]], | ||
| 1379 | 'oa' => [$v[55],$v[56]], | ||
| 1380 | 'att' => [$v[57],$v[58]], | ||
| 1381 | 'def' => [$v[59],$v[60]], | ||
| 1382 | 'mag' => [$v[61],$v[62]], | ||
| 1383 | 'pot' => [$v[63],$v[64]], | ||
| 1384 | 'sum' => [$v[69],$v[70]], | ||
| 1385 | 'cra' => [$v[73],$v[74]], | ||
| 1386 | }; | ||
| 1387 | =begin | ||
| 1388 | print STDERR Dumper(\@v); | ||
| 1389 | print STDERR "stats=",Dumper($self->{'stats'}); | ||
| 1390 | print STDERR "nexus=",Dumper($self->{'nexus'}); | ||
| 1391 | print STDERR "skills=",Dumper($self->{'skills'}); | ||
| 1392 | print STDERR "research=",Dumper($self->{'research'}); | ||
| 1393 | print STDERR "experience=",Dumper($self->{'experience'}); | ||
| 1394 | exit 1; | ||
| 1395 | =cut | ||
| 1396 | |||
| 1397 | } | ||
| 1398 | |||
| 1399 | sub SEND_PARTIAL_STAT | ||
| 1400 | { | ||
| 1401 | my $self = shift; | ||
| 1402 | my ($type,$len,$data) = @_; | ||
| 1403 | |||
| 1404 | my $n = $len/5; | ||
| 1405 | for(my $i=0; $i<$n; $i++) { | ||
| 1406 | my $stat = substr($data,$i*5+0,1); | ||
| 1407 | my $value = unpack('l', substr($data,$i*5+1,4)); | ||
| 1408 | |||
| 1409 | if ($stat eq $FOOD_LEV) { | ||
| 1410 | $self->{'stats'}->{'food'}->[0] = $value; } | ||
| 1411 | elsif ($stat eq $MAT_POINT_CUR) { | ||
| 1412 | $self->{'stats'}->{'mp'}->[0] = $value; } | ||
| 1413 | elsif ($stat eq $MAT_POINT_BASE) { | ||
| 1414 | $self->{'stats'}->{'mp'}->[1] = $value; } | ||
| 1415 | elsif ($stat eq $ETH_POINT_CUR) { | ||
| 1416 | $self->{'stats'}->{'ep'}->[0] = $value; } | ||
| 1417 | elsif ($stat eq $ETH_POINT_BASE) { | ||
| 1418 | $self->{'stats'}->{'ep'}->[1] = $value; } | ||
| 1419 | elsif ($stat eq $CARRY_WGHT_CUR) { | ||
| 1420 | $self->{'stats'}->{'carry'}->[0] = $value; } | ||
| 1421 | elsif ($stat eq $CARRY_WGHT_BASE) { | ||
| 1422 | $self->{'stats'}->{'carry'}->[1] = $value; } | ||
| 1423 | elsif ($stat eq $DEF_EXP) { | ||
| 1424 | $self->{'experience'}->{'def'}->[0] = $value; } | ||
| 1425 | elsif ($stat eq $DEF_EXP_NEXT) { | ||
| 1426 | $self->{'experience'}->{'def'}->[1] = $value; } | ||
| 1427 | elsif ($stat eq $ATT_EXP) { | ||
| 1428 | $self->{'experience'}->{'att'}->[0] = $value; } | ||
| 1429 | elsif ($stat eq $ATT_EXP_NEXT) { | ||
| 1430 | $self->{'experience'}->{'att'}->[1] = $value; } | ||
| 1431 | elsif ($stat eq $HARV_EXP) { | ||
| 1432 | $self->{'experience'}->{'har'}->[0] = $value; } | ||
| 1433 | elsif ($stat eq $HARV_EXP_NEXT) { | ||
| 1434 | $self->{'experience'}->{'har'}->[1] = $value; } | ||
| 1435 | elsif ($stat eq $HARV_S_CUR) { | ||
| 1436 | $self->{'skills'}->{'har'}->[0] = $value; } | ||
| 1437 | elsif ($stat eq $HARV_S_BASE) { | ||
| 1438 | $self->{'skills'}->{'har'}->[1] = $value; } | ||
| 1439 | elsif ($stat eq $ATT_S_CUR) { | ||
| 1440 | $self->{'skills'}->{'att'}->[0] = $value; } | ||
| 1441 | elsif ($stat eq $ATT_S_BASE) { | ||
| 1442 | $self->{'skills'}->{'att'}->[1] = $value; } | ||
| 1443 | elsif ($stat eq $DEF_S_CUR) { | ||
| 1444 | $self->{'skills'}->{'def'}->[0] = $value; } | ||
| 1445 | elsif ($stat eq $DEF_S_BASE) { | ||
| 1446 | $self->{'skills'}->{'def'}->[1] = $value; } | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | sub YOU_ARE | ||
| 1451 | { | ||
| 1452 | my $self = shift; | ||
| 1453 | my ($type,$len,$data) = @_; | ||
| 1454 | |||
| 1455 | $self->{'my_id'} = unpack('v', $data); | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | ################################################################ | ||
| 1459 | # TRADE RELATED CALLBACKS # | ||
| 1460 | ################################################################ | ||
| 1461 | |||
| 1462 | sub GET_YOUR_TRADEOBJECTS | ||
| 1463 | { | ||
| 1464 | my $self = shift; | ||
| 1465 | my ($type,$len,$data) = @_; | ||
| 1466 | } | ||
| 1467 | |||
| 1468 | sub GET_TRADE_PARTNER_NAME | ||
| 1469 | { | ||
| 1470 | my $self = shift; | ||
| 1471 | my ($type,$len,$data) = @_; | ||
| 1472 | |||
| 1473 | my $partner = substr($data,1); | ||
| 1474 | $self->{'tradeWith'} = $partner; | ||
| 1475 | |||
| 1476 | $self->sendPM($self->{'tradeWith'},"please pm with what you wish to buy or sell"); | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | # Blindly trust our trade partner, when they accept so | ||
| 1480 | # do we (but see Bot.pm) | ||
| 1481 | sub GET_TRADE_ACCEPT | ||
| 1482 | { | ||
| 1483 | my $self = shift; | ||
| 1484 | my ($type,$len,$data) = @_; | ||
| 1485 | |||
| 1486 | my $who = unpack('C', $data); | ||
| 1487 | if ($who) { | ||
| 1488 | $self->{'tradeAccepted'} += 1; | ||
| 1489 | my @accepted = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); | ||
| 1490 | foreach my $item (@{$self->{'??'}}) { | ||
| 1491 | my $pos = $item->{'pos'}; | ||
| 1492 | $accepted[$pos] = $item->{'type'}; | ||
| 1493 | } | ||
| 1494 | $data = pack('CCCCCCCCCCCCCCCC',@accepted); | ||
| 1495 | $self->send($ACCEPT_TRADE, $data); | ||
| 1496 | } | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | sub GET_TRADE_REJECT | ||
| 1500 | { | ||
| 1501 | my $self = shift; | ||
| 1502 | my ($type,$len,$data) = @_; | ||
| 1503 | |||
| 1504 | my $who = unpack('C', $data); | ||
| 1505 | if ($who) { | ||
| 1506 | $self->{'tradeAccepted'} = 0; | ||
| 1507 | } | ||
| 1508 | } | ||
| 1509 | |||
| 1510 | # Called when an object is removed from the trade window. | ||
| 1511 | # We only deal with objects our trade partner removed | ||
| 1512 | # as we should know the state of our own trade objects | ||
| 1513 | # We send a LOOK_AT_TRADE_ITEM to the server so that | ||
| 1514 | # we can get the description for the object | ||
| 1515 | sub REMOVE_TRADE_OBJECT | ||
| 1516 | { | ||
| 1517 | my $self = shift; | ||
| 1518 | my ($type,$len,$data) = @_; | ||
| 1519 | |||
| 1520 | my $pos = unpack('C', substr($data,4,1)); | ||
| 1521 | my $who = unpack('C', substr($data,5,1)); | ||
| 1522 | my $qty = unpack('V', substr($data,0,4)), | ||
| 1523 | |||
| 1524 | my $trades; | ||
| 1525 | if ($who) { # Trade partner removed object | ||
| 1526 | $trades = $self->{'thereTrades'}; } | ||
| 1527 | else { | ||
| 1528 | $trades = $self->{'myTrades'}; | ||
| 1529 | } | ||
| 1530 | my $item = $trades->{$pos}; | ||
| 1531 | if (!defined($item)) { | ||
| 1532 | $self->Log("removing unknown item from trade - this should not happen"); | ||
| 1533 | return; | ||
| 1534 | } | ||
| 1535 | if ($item->{'quantity'} == $qty) { | ||
| 1536 | delete $trades->{$pos}; | ||
| 1537 | } | ||
| 1538 | elsif ($item->{'quantity'} < $qty) { | ||
| 1539 | $self->Log("removing more from trade than is in the trade - this should not happen"); | ||
| 1540 | } | ||
| 1541 | else { | ||
| 1542 | $item->{'quantity'} -= $qty; | ||
| 1543 | } | ||
| 1544 | } | ||
| 1545 | |||
| 1546 | sub GET_TRADE_OBJECT | ||
| 1547 | { | ||
| 1548 | my $self = shift; | ||
| 1549 | my ($type,$len,$data) = @_; | ||
| 1550 | |||
| 1551 | my $qty = unpack('V', substr($data,2,4)), | ||
| 1552 | my $pos = unpack('C', substr($data,7,1)); | ||
| 1553 | my $who = unpack('C', substr($data,8,1)); | ||
| 1554 | my $trades = ($who) ? $self->{'thereTrades'} : $self->{'myTrades'}; | ||
| 1555 | |||
| 1556 | if (defined($trades->{$pos})) { | ||
| 1557 | $trades->{$pos}->{'quantity'} += $qty; } | ||
| 1558 | else { | ||
| 1559 | $trades->{$pos} = { | ||
| 1560 | 'pos' => $pos, | ||
| 1561 | 'image' => unpack('v', substr($data,1,2)), | ||
| 1562 | 'quantity' => $qty, | ||
| 1563 | 'type' => unpack('C', substr($data,6,1)), | ||
| 1564 | }; | ||
| 1565 | $self->send($LOOK_AT_TRADE_ITEM, pack('CC',$pos,$who)); | ||
| 1566 | push(@{$self->{'lookAtQueue'}}, [$GET_TRADE_OBJECT,$trades->{$pos}]); | ||
| 1567 | } | ||
| 1568 | } | ||
| 1569 | |||
| 1570 | sub GET_TRADE_EXIT | ||
| 1571 | { | ||
| 1572 | my $self = shift; | ||
| 1573 | |||
| 1574 | if ($self->{'tradeAccepted'} == 2) { | ||
| 1575 | $self->sendPM($self->{'tradeWith'}," Thanks"); | ||
| 1576 | } | ||
| 1577 | $self->LogTrade(); | ||
| 1578 | |||
| 1579 | |||
| 1580 | $self->{'tradeWith'} = undef; | ||
| 1581 | $self->{'thereTrades'} = {}; | ||
| 1582 | $self->{'myTrades'} = {}; | ||
| 1583 | $self->{'tradeOk'} = 0; | ||
| 1584 | $self->{'tradeAccepted'} = 0; | ||
| 1585 | } | ||
| 1586 | |||
| 1587 | ################################################################ | ||
| 1588 | # INVENTORY RELATED CALLBACKS # | ||
| 1589 | ################################################################ | ||
| 1590 | |||
| 1591 | # decode the message from the server that tells us what is in | ||
| 1592 | # our inventory. | ||
| 1593 | # create a a hash of these objects by inventory position | ||
| 1594 | # Send LOOK_AT_INVENTORY_ITEM for each item so that we can | ||
| 1595 | # build a 'byName' hash of these items as well | ||
| 1596 | sub HERE_YOUR_INVENTORY | ||
| 1597 | { | ||
| 1598 | my $self = shift; | ||
| 1599 | |||
| 1600 | my ($type,$len,$data) = @_; | ||
| 1601 | |||
| 1602 | $self->{'nCarry'} = 0; | ||
| 1603 | $self->{'nEquip'} = 0; | ||
| 1604 | $self->{'invByPos'} = $self->getItemsList($data); | ||
| 1605 | |||
| 1606 | my @posList = sort (keys %{$self->{'invByPos'}}); | ||
| 1607 | foreach my $pos (@posList) { | ||
| 1608 | $self->send($LOOK_AT_INVENTORY_ITEM, pack('C',$pos)); | ||
| 1609 | push(@{$self->{'lookAtQueue'}}, [$HERE_YOUR_INVENTORY,$self->{'invByPos'}->{$pos}]); | ||
| 1610 | if ($pos < 36) {$self->{'nCarry'} += 1;} else {$self->{'nEquip'} += 1;} | ||
| 1611 | } | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | sub REMOVE_ITEM_FROM_INVENTORY | ||
| 1615 | { | ||
| 1616 | my $self = shift; | ||
| 1617 | my ($type,$len,$data) = @_; | ||
| 1618 | |||
| 1619 | my $pos = unpack('C',$data); | ||
| 1620 | $self->deleteItem($pos); | ||
| 1621 | if ($pos < 36) {$self->{'nCarry'} -= 1;} else {$self->{'nEquip'} -= 1;} | ||
| 1622 | } | ||
| 1623 | |||
| 1624 | sub GET_NEW_INVENTORY_ITEM | ||
| 1625 | { | ||
| 1626 | my $self = shift; | ||
| 1627 | my ($type,$len,$data) = @_; | ||
| 1628 | |||
| 1629 | my $pos = unpack('C', substr($data,6,1)); | ||
| 1630 | my $newitem = { | ||
| 1631 | 'image' => unpack('v', substr($data,0,2)), | ||
| 1632 | 'quantity' => unpack('V', substr($data,2,4)), | ||
| 1633 | 'pos' => $pos, | ||
| 1634 | 'flags' => unpack('C', substr($data,7,1)), | ||
| 1635 | }; | ||
| 1636 | if ($newitem->{'quantity'} == 0) { | ||
| 1637 | $self->deleteItem($pos); | ||
| 1638 | if ($pos < 36) {$self->{'nCarry'} -= 1;} else {$self->{'nEquip'} -= 1;} | ||
| 1639 | } | ||
| 1640 | else { | ||
| 1641 | my $item = $self->{'invByPos'}->{$pos}; | ||
| 1642 | if (defined($item) and defined($item->{'name'})) { | ||
| 1643 | $item->{'quantity'} = $newitem->{'quantity'}; | ||
| 1644 | $self->Log("I now have $item->{'quantity'} $item->{'name'}"); | ||
| 1645 | } | ||
| 1646 | else { | ||
| 1647 | $self->{'invByPos'}->{$pos} = $newitem; | ||
| 1648 | $data = pack('C',$pos); | ||
| 1649 | $self->send($LOOK_AT_INVENTORY_ITEM,$data); | ||
| 1650 | push(@{$self->{'lookAtQueue'}}, [$GET_NEW_INVENTORY_ITEM,$newitem]); | ||
| 1651 | if ($pos < 36) {$self->{'nCarry'} += 1;} else {$self->{'nEquip'} += 1;} | ||
| 1652 | } | ||
| 1653 | } | ||
| 1654 | |||
| 1655 | return $newitem; | ||
| 1656 | } | ||
| 1657 | |||
| 1658 | sub INVENTORY_ITEM_TEXT | ||
| 1659 | { | ||
| 1660 | my $self = shift; | ||
| 1661 | my ($type,$len,$data) = @_; | ||
| 1662 | |||
| 1663 | my ($name,$desc,$weight); | ||
| 1664 | |||
| 1665 | ($desc,$weight) = split("\n",$data); | ||
| 1666 | ($name,$desc) = split(" - ",$desc); | ||
| 1667 | ($weight) = ($weight =~ m/weight:\s+(\d+)\s*emu/i); | ||
| 1668 | $name = lc(substr($name,1)); | ||
| 1669 | |||
| 1670 | my $q = shift(@{$self->{'lookAtQueue'}}); | ||
| 1671 | if (defined($q)) { | ||
| 1672 | my $type = $q->[0]; | ||
| 1673 | my $item = $q->[1]; | ||
| 1674 | $item->{'name'} = $name; | ||
| 1675 | $item->{'desc'} = $desc; | ||
| 1676 | $item->{'weight'} = $weight; | ||
| 1677 | if ($type eq $HERE_YOUR_INVENTORY or $type eq $GET_NEW_INVENTORY_ITEM) { | ||
| 1678 | $self->{'invByName'}->{lc($name)}->{$item->{'pos'}} = $item; | ||
| 1679 | } | ||
| 1680 | ($self->{'debug'} & $DEBUG_TEXT) && | ||
| 1681 | $self->Log("Item: $item->{'quantity'} $name"); | ||
| 1682 | } | ||
| 1683 | else { | ||
| 1684 | $self->Log("Looking at item '$name' for no reason !"); | ||
| 1685 | } | ||
| 1686 | } | ||
| 1687 | |||
| 1688 | sub HERE_YOUR_GROUND_ITEMS | ||
| 1689 | { | ||
| 1690 | my $self = shift; | ||
| 1691 | my ($type,$len,$data) = @_; | ||
| 1692 | |||
| 1693 | my $bag = $self->{'groundItems'}->[0]; | ||
| 1694 | |||
| 1695 | my $numItems = unpack('C',$data); | ||
| 1696 | if ($numItems > $ITEMS_PER_BAG) { | ||
| 1697 | $self->Log("Too many items in bag: $numItems"); | ||
| 1698 | return undef; | ||
| 1699 | } | ||
| 1700 | for(my $i=0;$i<$numItems;$i++) { | ||
| 1701 | my $offset = $i*7+1; | ||
| 1702 | my $image = unpack('v', substr($data,$offset,2)); | ||
| 1703 | my $qty = unpack('L',substr($data,$offset+2,4)); | ||
| 1704 | my $pos = unpack('C',substr($data,$offset+6,1)); | ||
| 1705 | my $item = { | ||
| 1706 | 'pos' => $pos, | ||
| 1707 | 'quantity' => $qty, | ||
| 1708 | 'image' => $image, | ||
| 1709 | 'bag' => $bag, | ||
| 1710 | }; | ||
| 1711 | $bag->{'items'}->{$pos} = $item; | ||
| 1712 | $self->send($LOOK_AT_GROUND_ITEM,pack('C',$pos)); | ||
| 1713 | push(@{$self->{'lookAtQueue'}}, [$LOOK_AT_GROUND_ITEM,$item]); | ||
| 1714 | } | ||
| 1715 | return $bag; | ||
| 1716 | } | ||
| 1717 | |||
| 1718 | ########################################################### | ||
| 1719 | # BAGS REALTED CALLBACKS # | ||
| 1720 | ########################################################### | ||
| 1721 | |||
| 1722 | sub GET_NEW_BAG | ||
| 1723 | { | ||
| 1724 | my $self = shift; | ||
| 1725 | my ($type,$len,$data) = @_; | ||
| 1726 | |||
| 1727 | my $x = unpack('v', substr($data,0,2)), | ||
| 1728 | my $y = unpack('v', substr($data,+2,2)), | ||
| 1729 | my $z = 0, #BUG | ||
| 1730 | my $id = unpack('C', substr($data,4,1)); | ||
| 1731 | my $bag = $self->addBag($id,$x,$y,$z); | ||
| 1732 | |||
| 1733 | return $bag; | ||
| 1734 | } | ||
| 1735 | |||
| 1736 | sub GET_BAGS_LIST | ||
| 1737 | { | ||
| 1738 | my $self = shift; | ||
| 1739 | my ($type,$len,$data) = @_; | ||
| 1740 | my @bags = (); | ||
| 1741 | |||
| 1742 | my $numBags = unpack('C',substr($data,0,1)); | ||
| 1743 | if ($numBags > $MAXBAGS) { | ||
| 1744 | $self->Log("Bad number of bags in list: $numBags"); | ||
| 1745 | return \@bags; | ||
| 1746 | } | ||
| 1747 | for(my $i=0; $i<$numBags; $i++) { | ||
| 1748 | my $offset = $i*5+1; | ||
| 1749 | my $x = unpack('v', substr($data,$offset,2)); | ||
| 1750 | my $y = unpack('v', substr($data,$offset+2,2)); | ||
| 1751 | my $z = 0; | ||
| 1752 | my $id = unpack('C', substr($data,$offset+4,1)); | ||
| 1753 | my $bag = $self->addBag($id,$x,$y,$z); | ||
| 1754 | if (defined($bag)) { | ||
| 1755 | push(@bags, $bag); | ||
| 1756 | } | ||
| 1757 | } | ||
| 1758 | return \@bags; | ||
| 1759 | } | ||
| 1760 | |||
| 1761 | sub DESTROY_BAG | ||
| 1762 | { | ||
| 1763 | my $self = shift; | ||
| 1764 | my ($type,$len,$data) = @_; | ||
| 1765 | |||
| 1766 | my $bagID = unpack('C', substr($data,0,1)); | ||
| 1767 | if (defined($self->{'bagsByID'}->{$bagID})) { | ||
| 1768 | delete $self->{'bagsByID'}->{$bagID}; } | ||
| 1769 | else { | ||
| 1770 | $self->Log("Destroying uknown bag $bagID"); | ||
| 1771 | } | ||
| 1772 | } | ||
| 1773 | |||
| 1774 | sub CLOSE_BAG | ||
| 1775 | { | ||
| 1776 | my $self = shift; | ||
| 1777 | my ($type,$len,$data) = @_; | ||
| 1778 | |||
| 1779 | my $bag = shift(@{$self->{'groundItems'}}); | ||
| 1780 | if ($self->{'debug'} & $DEBUG_BAGS) { | ||
| 1781 | $self->Log("Closed bag ".$bag->{'bagID'}); | ||
| 1782 | } | ||
| 1783 | } | ||
| 1784 | |||
| 1785 | ########################################################### | ||
| 1786 | |||
| 1787 | sub processArgs | ||
| 1788 | { | ||
| 1789 | my $self = shift; | ||
| 1790 | my @args = @_; | ||
| 1791 | my @notUsed; | ||
| 1792 | |||
| 1793 | while(my $arg = shift @args) { | ||
| 1794 | if ($arg eq '-server') { | ||
| 1795 | $self->{'server'} = shift @args; } | ||
| 1796 | elsif ($arg eq '-port') { | ||
| 1797 | $self->{'port'} = shift @args; } | ||
| 1798 | elsif ($arg eq '-elDir') { | ||
| 1799 | $self->{'elDir'} = shift @args; } | ||
| 1800 | elsif ($arg eq '-debug') { | ||
| 1801 | $self->{'debug'} |= shift @args; } | ||
| 1802 | else { | ||
| 1803 | push(@notUsed, $arg); | ||
| 1804 | } | ||
| 1805 | } | ||
| 1806 | return @notUsed; | ||
| 1807 | } | ||
| 1808 | |||
| 1809 | sub new | ||
| 1810 | { | ||
| 1811 | my $class = shift; | ||
| 1812 | my $self = {}; | ||
| 1813 | bless($self, $class); | ||
| 1814 | |||
| 1815 | $self->{'debug'} = 0; | ||
| 1816 | $self->{'server'} = undef; | ||
| 1817 | $self->{'port'} = undef; | ||
| 1818 | $self->{'username'} = undef; | ||
| 1819 | $self->{'socket'} = undef; | ||
| 1820 | $self->{'lastHeartbeatTime'} = 0; | ||
| 1821 | $self->{'heartbeatTimer'} = 25; | ||
| 1822 | $self->{'connected'} = 0; | ||
| 1823 | $self->{'loggedIn'} = 0; | ||
| 1824 | $self->{'failedLogins'} = 0; | ||
| 1825 | $self->{'buffer'} = ""; | ||
| 1826 | $self->{'packets'} = []; | ||
| 1827 | $self->{'nRcvdPackets'} = 0; | ||
| 1828 | $self->{'nSentPackets'} = 0; | ||
| 1829 | $self->{'Map'} = undef; | ||
| 1830 | $self->{'myd_id'} = -1; | ||
| 1831 | $self->{'me'} = undef; | ||
| 1832 | $self->{'actorsByID'} = undef; | ||
| 1833 | $self->{'actorsByName'} = undef; | ||
| 1834 | $self->{'elDir'} = undef; | ||
| 1835 | $self->{'bagsByID'} = {}; | ||
| 1836 | $self->{'groundItems'} = []; | ||
| 1837 | $self->{'lastMove'} = 0; | ||
| 1838 | $self->{'locName'} = "Lost"; | ||
| 1839 | $self->{'locPos'} = "0,0"; | ||
| 1840 | $self->{'lastMoved'} = time(); | ||
| 1841 | $self->{'specialDay'} = "Just an ordinary day"; | ||
| 1842 | |||
| 1843 | $self->{'itemsToSell'} = {}; | ||
| 1844 | $self->{'itemsToBuy'} = {}; | ||
| 1845 | $self->{'lastMsgAt'} = time(); # no msg on startup | ||
| 1846 | $self->{'msgInterval'} = 20; # minutes | ||
| 1847 | |||
| 1848 | $self->{'canTrade'} = 1; | ||
| 1849 | $self->{'myTrades'} = {}; | ||
| 1850 | $self->{'thereTrades'} = {}; | ||
| 1851 | $self->{'tradeAccepted'} = 0; | ||
| 1852 | |||
| 1853 | $self->{'invByPos'} = {}; | ||
| 1854 | $self->{'invByName'} = {}; | ||
| 1855 | $self->{'lookAtQueue'} = (); # FIFO of objects we have asked to look at | ||
| 1856 | $self->{'pmQueue'} = (); # FIFO of objects we have asked to look at | ||
| 1857 | |||
| 1858 | tie(%{$self->{'mapCache'}},'Tie::Cache',{MaxCount=>8}); | ||
| 1859 | |||
| 1860 | @_ = $self->processArgs(@_); | ||
| 1861 | |||
| 1862 | my $elDir = $self->{'elDir'}; | ||
| 1863 | if (!defined($elDir)) { | ||
| 1864 | $self->Log("elDir not defined - no map oriented functionaility"); | ||
| 1865 | return $self; | ||
| 1866 | } | ||
| 1867 | |||
| 1868 | open(FP, $elDir."/harvestable.lst") || die $elDir."/harvestable.lst"; | ||
| 1869 | while(<FP>) { | ||
| 1870 | $_ =~ s/\r\n$//; | ||
| 1871 | $self->{'harvestableTypes'}->{$_} = 1; | ||
| 1872 | } | ||
| 1873 | close(FP); | ||
| 1874 | |||
| 1875 | open(FP, $elDir."/entrable.lst") || die $elDir."/el/entrable.lst"; | ||
| 1876 | while(<FP>) { | ||
| 1877 | $_ =~ s/\r\n$//; | ||
| 1878 | $_ =~ s%^.*/%%; | ||
| 1879 | $self->{'entrableTypes'}->{$_} = 1; | ||
| 1880 | } | ||
| 1881 | close(FP); | ||
| 1882 | |||
| 1883 | return $self; | ||
| 1884 | } | ||
| 1885 | |||
| 1886 | sub connect | ||
| 1887 | { | ||
| 1888 | my $self = shift; | ||
| 1889 | |||
| 1890 | while(@_) { | ||
| 1891 | my $arg = shift; | ||
| 1892 | if ($arg eq '-server') { | ||
| 1893 | $self->{'server'} = shift; | ||
| 1894 | } | ||
| 1895 | elsif ($arg eq '-port') { | ||
| 1896 | $self->{'port'} = shift; | ||
| 1897 | } | ||
| 1898 | } | ||
| 1899 | |||
| 1900 | defined($self->{'server'}) || die "server must be defined"; | ||
| 1901 | defined($self->{'port'}) || die "port must be defined"; | ||
| 1902 | |||
| 1903 | $self->{'socket'} = IO::Socket::INET->new(Proto => 'tcp', | ||
| 1904 | Blocking => 1, | ||
| 1905 | PeerAddr => $self->{'server'}, | ||
| 1906 | PeerPort => $self->{'port'}); | ||
| 1907 | |||
| 1908 | if (!defined($self->{'socket'})) { | ||
| 1909 | $self->Log("Failed to create socket: $!"); | ||
| 1910 | return 0; | ||
| 1911 | } | ||
| 1912 | |||
| 1913 | $self->{'connected'} = 1; | ||
| 1914 | my ($type,$len,$packet) = $self->NextPacket(); | ||
| 1915 | $self->Dispatch($type,$len,$packet); | ||
| 1916 | $self->keepAlive(1); | ||
| 1917 | |||
| 1918 | return 1; | ||
| 1919 | } | ||
| 1920 | |||
| 1921 | sub disconnect | ||
| 1922 | { | ||
| 1923 | my $self = shift; | ||
| 1924 | |||
| 1925 | close($self->{'socket'}); | ||
| 1926 | $self->{'connected'} = 0; | ||
| 1927 | $self->{'socket'} = undef; | ||
| 1928 | } | ||
| 1929 | |||
| 1930 | sub login | ||
| 1931 | { | ||
| 1932 | my $self = shift; | ||
| 1933 | |||
| 1934 | my ($user,$pass); | ||
| 1935 | if (@_) { | ||
| 1936 | ($user,$pass) = @_; | ||
| 1937 | } | ||
| 1938 | if (!defined($user) || !defined($pass)) { | ||
| 1939 | $self->Log("User and password must be passed"); | ||
| 1940 | return 0; | ||
| 1941 | } | ||
| 1942 | |||
| 1943 | $self->{'loginFailed'} = 0; | ||
| 1944 | $self->send($LOG_IN, sprintf("%s %s%c",$user,$pass,0)); | ||
| 1945 | |||
| 1946 | while (!$self->{'loggedIn'} && !$self->{'failedLogins'}) { | ||
| 1947 | my ($type,$len,$packet) = $self->NextPacket(); | ||
| 1948 | $self->Dispatch($type,$len,$packet); | ||
| 1949 | } | ||
| 1950 | if ($self->{'loggedIn'}) { | ||
| 1951 | $self->locateMe(); | ||
| 1952 | while(!defined($self->{'me'})) { | ||
| 1953 | my ($type,$len,$packet) = $self->NextPacket(); | ||
| 1954 | $self->Dispatch($type,$len,$packet); | ||
| 1955 | } | ||
| 1956 | } | ||
| 1957 | return $self->{'loggedIn'}; | ||
| 1958 | } | ||
| 1959 | |||
| 1960 | sub splitPackets | ||
| 1961 | { | ||
| 1962 | my $self = shift; | ||
| 1963 | |||
| 1964 | my $found; | ||
| 1965 | do { | ||
| 1966 | $found = 0; | ||
| 1967 | if (length($self->{'buffer'}) > 3) { | ||
| 1968 | my $len = unpack('v',substr($self->{'buffer'},1,2))-1; | ||
| 1969 | if (length($self->{'buffer'}) >= $len+2) { | ||
| 1970 | my $type = substr($self->{'buffer'},0,1); | ||
| 1971 | my $data = substr($self->{'buffer'},3,$len); | ||
| 1972 | push(@{$self->{'packets'}},[$type,$len,$data]); | ||
| 1973 | $self->{'buffer'} = substr($self->{'buffer'},$len+3); | ||
| 1974 | $found = 1; | ||
| 1975 | ($self->{'debug'} & $DEBUG_PACKETS) && | ||
| 1976 | $self->Log("Read Data: ".$self->packetAsHex($len.$type.$data)); | ||
| 1977 | ($self->{'debug'} & $DEBUG_TYPES) && | ||
| 1978 | $self->Log("Read packet '".$ClientCommandsByID{$type}."'"); | ||
| 1979 | } | ||
| 1980 | } | ||
| 1981 | } while($found); | ||
| 1982 | } | ||
| 1983 | |||
| 1984 | sub readBuffer | ||
| 1985 | { | ||
| 1986 | my $self = shift; | ||
| 1987 | |||
| 1988 | my $n = ($#{$self->{'pmQueue'}} > 9) ? 9 : $#{$self->{'pmQueue'}}; | ||
| 1989 | while($n >= 0) { | ||
| 1990 | my $msg = shift(@{$self->{'pmQueue'}}); | ||
| 1991 | $self->send($SEND_PM,$msg); | ||
| 1992 | $n--; | ||
| 1993 | } | ||
| 1994 | |||
| 1995 | my $rin = ""; my $rout; | ||
| 1996 | vec($rin, fileno($self->{'socket'}), 1) = 1; | ||
| 1997 | my $nfound = select($rout=$rin, undef, undef, 0.34); | ||
| 1998 | if ($nfound) { | ||
| 1999 | my $buf; | ||
| 2000 | my $ok = recv($self->{'socket'},$buf,1024,0); | ||
| 2001 | if (!defined($ok)) { | ||
| 2002 | $self->Log("recv() failed !"); | ||
| 2003 | return undef; | ||
| 2004 | } | ||
| 2005 | my $n = length($buf); | ||
| 2006 | if ($n > 0) { | ||
| 2007 | $self->{'buffer'} .= $buf; | ||
| 2008 | my $nBuf = length($self->{'buffer'}); | ||
| 2009 | } | ||
| 2010 | return 1; | ||
| 2011 | } | ||
| 2012 | return 0; | ||
| 2013 | } | ||
| 2014 | |||
| 2015 | sub NextPacket | ||
| 2016 | { | ||
| 2017 | my $self = shift; | ||
| 2018 | |||
| 2019 | $self->keepAlive(0); | ||
| 2020 | if ($#{$self->{'packets'}} == -1) { | ||
| 2021 | $self->readBuffer(); | ||
| 2022 | $self->splitPackets(); | ||
| 2023 | } | ||
| 2024 | if ($#{$self->{'packets'}} >= 0) { | ||
| 2025 | my $next = shift @{$self->{'packets'}}; | ||
| 2026 | my ($type,$len,$data) = @{$next}; | ||
| 2027 | (return $type,$len,$data); | ||
| 2028 | } | ||
| 2029 | return (undef,undef,undef); | ||
| 2030 | } | ||
| 2031 | |||
| 2032 | sub Dispatch | ||
| 2033 | { | ||
| 2034 | my $self = shift; | ||
| 2035 | my ($type,$len,$data) = @_; | ||
| 2036 | my $ret = undef; | ||
| 2037 | |||
| 2038 | defined($type) || return undef; | ||
| 2039 | |||
| 2040 | $self->{'nRcvdPackets'} += 1; | ||
| 2041 | |||
| 2042 | my $typeStr = $ClientCommandsByID{$type}; | ||
| 2043 | defined($typeStr) || confess "bad packet type in Dispatch"; | ||
| 2044 | my $fn = $self->can($typeStr); | ||
| 2045 | if (defined($fn)) { | ||
| 2046 | if ($self->{'debug'} & $DEBUG_TYPES) { | ||
| 2047 | my $n = $self->{'nRcvdPackets'}; | ||
| 2048 | $self->Log("Dispatching packet($n) '".$typeStr."'"); | ||
| 2049 | } | ||
| 2050 | $ret = &{$fn}($self,$type,$len,$data); | ||
| 2051 | } | ||
| 2052 | else { | ||
| 2053 | ($self->{'debug'} & $DEBUG_TYPES) && | ||
| 2054 | $self->Log("Unhandled packet '".$typeStr."'"); | ||
| 2055 | } | ||
| 2056 | $self->keepAlive(0); | ||
| 2057 | |||
| 2058 | return $ret; | ||
| 2059 | } | ||
| 2060 | |||
| 2061 | sub crntMap | ||
| 2062 | { | ||
| 2063 | my $self = shift; | ||
| 2064 | |||
| 2065 | return $self->{'Map'}->{'name'}; | ||
| 2066 | } | ||
| 2067 | |||
| 2068 | return 1; | ||
| 2069 |