1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
|
/*
* Copyright (C) 1998-1999 Jeffrey S. Freedman
* Copyright (C) 2000-2001 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "Audio.h"
#include "monsters.h"
#include "cheat.h"
#include "chunks.h"
#include "animate.h"
#include "effects.h"
#include "egg.h"
#include "exult.h"
#include "game.h"
#include "gamewin.h"
#include "gamemap.h"
#include "items.h"
#include "npctime.h"
#include "paths.h"
#include "ucmachine.h"
#include "ucsched.h"
#include "ucscriptop.h"
#ifdef USE_EXULTSTUDIO
#include "server.h"
#include "objserial.h"
#include "mouse.h"
#include "servemsg.h"
#endif
using std::cout;
using std::endl;
using std::rand;
using std::ostream;
Egg_object *Egg_object::editing = 0;
/*
* Timer for a missile egg (type-6 egg).
*/
class Missile_launcher : public Time_sensitive
{
Egg_object *egg; // Egg this came from.
int weapon; // Shape for weapon.
int shapenum; // Shape for missile.
int dir; // Direction (0-7). (8==??).
int delay; // Delay (msecs) between launches.
public:
Missile_launcher(Egg_object *e, int weap, int shnum, int di, int del)
: egg(e), weapon(weap), shapenum(shnum), dir(di), delay(del)
{ }
virtual void handle_event(unsigned long curtime, long udata);
};
/*
* Launch a missile.
*/
void Missile_launcher::handle_event
(
unsigned long curtime,
long udata
)
{
Game_window *gwin = Game_window::get_game_window();
Tile_coord src = egg->get_tile();
// Is egg off the screen?
if (!gwin->get_win_tile_rect().has_point(src.tx, src.ty))
return; // Return w'out adding back to queue.
Projectile_effect *proj = 0;
if (dir < 8) // Direction given?
{ // Get adjacent tile in direction.
Tile_coord adj = src.get_neighbor(dir%8);
// Make it go 20 tiles.
int dx = adj.tx - src.tx, dy = adj.ty - src.ty;
Tile_coord dest = src;
dest.tx += 20*dx;
dest.ty += 20*dy;
proj = new Projectile_effect(src, dest, shapenum, weapon);
}
else // Target a party member.
{
Actor *party[9];
int psize = gwin->get_party(party, 1);
int cnt = psize;
int n = rand()%psize; // Pick one at random.
// Find one we can hit.
for (int i = n; !proj && cnt; cnt--, i = (i + 1)%psize)
if (Fast_pathfinder_client::is_straight_path(src,
party[i]->get_tile()))
proj = new Projectile_effect(
src, party[i], shapenum, weapon);
}
if (proj)
gwin->add_effect(proj);
// Add back to queue for next time.
gwin->get_tqueue()->add(curtime + delay, this, udata);
}
/*
* Paint at given spot in world.
*/
void Egglike_game_object::paint
(
Game_window *gwin
)
{
if(gwin->paint_eggs)
Game_object::paint(gwin);
}
/*
* Can this be clicked on?
*/
int Egglike_game_object::is_findable
(
Game_window *gwin
)
{
return gwin->paint_eggs && Ireg_game_object::is_findable(gwin);
}
/*
* Create an egg from IREG data.
*/
Egg_object::Egg_object
(
int shapenum, int framenum,
unsigned int tilex, unsigned int tiley,
unsigned int lft,
unsigned short itype,
unsigned char prob,
short d1, short d2
) : Egglike_game_object(shapenum, framenum, tilex, tiley, lft),
probability(prob), data1(d1), data2(d2),
area(Rectangle(0, 0, 0, 0)), launcher(0)
{
type = itype&0xf;
// Teleport destination?
if (type == teleport && framenum == 6)
type = path; // (Mountains N. of Vesper).
criteria = (itype & (7<<4)) >> 4;
distance = (itype >> 10) & 0x1f;
unsigned char noct = (itype >> 7) & 1;
unsigned char do_once = (itype >> 8) & 1;
// Missile eggs can be rehatched
unsigned char htch = (type == missile) ? 0 : ((itype >> 9) & 1);
solid_area = (criteria == something_on || criteria == cached_in ||
// Teleports need solid area.
type == teleport) ? 1 : 0;
unsigned char ar = (itype >> 15) & 1;
flags = (noct << nocturnal) + (do_once << once) +
(htch << hatched) + (ar << auto_reset);
if (type == usecode || type == teleport || type == path)
set_quality(data1&0xff);
#if 0 /* +++++The old way. */
if (type == path) // Store paths.
Game_window::get_game_window()->add_path_egg(this);
#endif
}
/*
* Init. for a field.
*/
inline void Egg_object::init_field
(
unsigned char ty // Egg (field) type.
)
{
type = ty;
probability = 100;
data1 = data2 = 0;
launcher = 0;
area = Rectangle(0, 0, 0, 0);
criteria = party_footpad;
distance = 0;
solid_area = 0;
flags = (1 << auto_reset);
}
/*
* Create an egg representing a field.
*/
Egg_object::Egg_object
(
int shapenum,
int framenum,
unsigned int tilex, unsigned int tiley,
unsigned int lft,
unsigned char ty // Egg (field) type.
) : Egglike_game_object(shapenum, framenum, tilex, tiley, lft)
{
init_field(ty);
}
/*
* Destructor:
*/
Egg_object::~Egg_object
(
)
{
if (launcher)
{
Game_window::get_game_window()->get_tqueue()->remove(launcher);
delete launcher;
}
}
/*
* Set active area after being added to its chunk.
*/
void Egg_object::set_area
(
)
{
if (!probability || type == path)// No chance of normal activation?
{
area = Rectangle(0, 0, 0, 0);
return;
}
Game_window *gwin = Game_window::get_game_window();
Tile_coord t = get_tile(); // Get absolute tile coords.
switch (criteria) // Set up active area.
{
case cached_in: // Make it really large.
area = Rectangle(t.tx - 32, t.ty - 32, 64, 64);
break;
case avatar_footpad:
case party_footpad:
{
Shape_info& info = gwin->get_info(this);
int xtiles = info.get_3d_xtiles(),
ytiles = info.get_3d_ytiles();
area = Rectangle(t.tx - xtiles + 1, t.ty - ytiles + 1,
xtiles, ytiles);
break;
}
case avatar_far: // Make it 1 tile bigger each dir.
area = Rectangle(t.tx - distance - 1, t.ty - distance - 1,
2*distance + 3, 2*distance + 3);
break;
default:
{
int width = 2*distance;
width++; // Added 8/1/01.
if (distance <= 1) // Small?
{
// More guesswork:
if (criteria == external_criteria)
width += 2;
}
area = Rectangle(t.tx - distance, t.ty - distance,
width, width);
break;
}
}
// Don't go outside the world.
Rectangle world(0, 0, c_num_chunks*c_tiles_per_chunk,
c_num_chunks*c_tiles_per_chunk);
area = area.intersect(world);
}
/*
* Is the egg active when stepping onto a given spot, or placing an obj.
* on the spot?
*/
int Egg_object::is_active
(
Game_object *obj, // Object placed (or Actor).
int tx, int ty, int tz, // Tile stepped onto.
int from_tx, int from_ty // Tile stepped from.
)
{
if ((flags & (1 << (int) hatched)) &&
!(flags & (1 << (int) auto_reset)))
return (0); // For now... Already hatched.
Game_window *gwin = Game_window::get_game_window();
if (flags & (1 << (int) nocturnal))
{ // Nocturnal.
int hour = gwin->get_hour();
if (!(hour >= 9 || hour <= 5))
return (0); // It's not night.
}
if (cheat.in_map_editor())
return 0; // Disable in map-editor.
Egg_criteria cri = (Egg_criteria) get_criteria();
int deltaz = tz - get_lift();
int absdeltaz = deltaz < 0 ? -deltaz : deltaz;
switch (cri)
{
case cached_in: // Anywhere in square.
{
if (obj != gwin->get_main_actor() || !area.has_point(tx, ty))
return 0; // Not in square.
if (!(flags & (1 << (int) hatched)))
return 1; // First time.
// Must have autoreset.
// Just activate when reentering.
return !area.has_point(from_tx, from_ty);
}
case party_near:
case avatar_near: // New tile is in, old is out.
if (obj != gwin->get_main_actor() &&
(cri != party_near || obj->get_party_id() < 0))
return 0;
if (type == teleport) // Teleports: Any tile, exact lift.
return absdeltaz == 0 && area.has_point(tx, ty);
if (!((absdeltaz <= 1 ||
// Using trial&error here:
(Game::get_game_type() == SERPENT_ISLE &&
type != missile) ||
(type == missile && tz/5 == get_lift()/5)) &&
area.has_point(tx, ty) &&
!area.has_point(from_tx, from_ty)))
return 0;
return 1;
case avatar_far: // New tile is outside, old is inside.
{
if (obj != gwin->get_main_actor() || !area.has_point(tx, ty))
return (0);
Rectangle inside(area.x + 1, area.y + 1,
area.w - 2, area.h - 2);
return inside.has_point(from_tx, from_ty) &&
!inside.has_point(tx, ty);
}
case avatar_footpad:
return obj == gwin->get_main_actor() && deltaz == 0 &&
area.has_point(tx, ty);
case party_footpad:
return area.has_point(tx, ty) && deltaz == 0 &&
(obj->get_party_id() >= 0 ||
obj == gwin->get_main_actor());
case something_on:
return obj != gwin->get_main_actor() &&
// Guessing. At SI end, deltaz == -1.
deltaz >= -1 && deltaz <= 3 &&
area.has_point(tx, ty) && obj->get_npc_num() <= 0;
case external_criteria:
default:
return 0;
}
}
/*
* Paint at given spot in world.
*/
void Egg_object::paint
(
Game_window *gwin
)
{
Egglike_game_object::paint(gwin);
// Make sure launcher is active.
if (launcher && !launcher->in_queue())
gwin->get_tqueue()->add(0L, launcher, 0);
}
/*
* Run usecode when double-clicked.
*/
void Egg_object::activate
(
Usecode_machine *umachine,
int /* event */
)
{
#ifdef USE_EXULTSTUDIO
if (client_socket >= 0 && // Talking to ExultStudio?
cheat.in_map_editor())
{
editing = 0;
Tile_coord t = get_tile();
unsigned long addr = (unsigned long) this;
if (Egg_object_out(client_socket, addr, t.tx, t.ty, t.tz,
get_shapenum(), get_framenum(),
type, criteria, probability, distance,
(flags>>nocturnal)&1, (flags>>once)&1,
(flags>>hatched)&1, (flags>>auto_reset)&1,
data1, data2) != -1)
{
cout << "Sent egg data to ExultStudio" << endl;
editing = this;
}
else
cout << "Error sending egg data to ExultStudio" <<endl;
return;
}
#endif
activate(umachine, 0, 0);
}
/*
* Message to update from ExultStudio.
*/
void Egg_object::update_from_studio
(
unsigned char *data,
int datalen
)
{
#ifdef USE_EXULTSTUDIO
unsigned long addr;
int tx, ty, tz;
int shape, frame;
int type;
int criteria;
int probability;
int distance;
bool nocturnal, once, hatched, auto_reset;
int data1, data2;
if (!Egg_object_in(data, datalen, addr, tx, ty, tz, shape, frame,
type, criteria, probability, distance,
nocturnal, once, hatched, auto_reset,
data1, data2))
{
cout << "Error decoding egg" << endl;
return;
}
Egg_object *egg = (Egg_object *) addr;
if (egg && egg != editing)
{
cout << "Egg from ExultStudio is not being edited" << endl;
return;
}
editing = 0;
Game_window *gwin = Game_window::get_game_window();
if (!egg) // Create a new one?
{
int x, y;
if (!Get_click(x, y, Mouse::hand, 0))
{
if (client_socket >= 0)
Exult_server::Send_data(client_socket, Exult_server::cancel);
return;
}
if (shape == -1)
shape = 275; // FOR NOW.
if (frame == -1)
switch (type)
{ // (These aren't perfect.)
case monster: frame = 0; break;
case jukebox: frame = 2; break;
case soundsfx:frame = 1; break;
case voice: frame = 3; break;
case weather: frame = 4; break;
case teleport:frame = 5; break;
case path: frame = 6; break;
default: frame = 7; break;
}
// Create. Gets initialized below.
egg = new Egg_object(shape, frame, 0, 0, 0, 0, 0, 0, 0);
int lift; // Try to drop at increasing hts.
for (lift = 0; lift < 12; lift++)
if (gwin->drop_at_lift(egg, x, y, lift))
break;
if (lift == 12)
{
if (client_socket >= 0)
Exult_server::Send_data(client_socket, Exult_server::cancel);
delete egg;
return;
}
if (client_socket >= 0)
Exult_server::Send_data(client_socket, Exult_server::user_responded);
}
egg->type = type;
if (shape != -1)
egg->set_shape(shape);
if (frame != -1)
egg->set_frame(frame);
gwin->add_dirty(egg);
egg->criteria = criteria&7;
egg->distance = distance&31;
egg->probability = probability;
egg->flags = ((nocturnal?1:0)<<Egg_object::nocturnal) +
((once?1:0)<<Egg_object::once) +
((hatched?1:0)<<Egg_object::hatched) +
((auto_reset?1:0)<<Egg_object::auto_reset);
egg->data1 = data1;
egg->data2 = data2;
Map_chunk *chunk =
gwin->get_chunk_safely(egg->get_cx(), egg->get_cy());
chunk->remove_egg(egg); // Got to add it back.
chunk->add_egg(egg);
cout << "Egg updated" << endl;
#endif
}
/*
* Create a monster nearby.
*/
static void Create_monster
(
Game_window *gwin,
Egg_object *egg,
int shnum, // Monster shape.
Monster_info *inf, // Info.
int sched,
int align
)
{
Tile_coord dest = Map_chunk::find_spot(
egg->get_tile(), 5, shnum, 0, 1);
if (dest.tx != -1)
{
Monster_actor *monster = Monster_actor::create(shnum, dest,
sched, align);
gwin->add_dirty(monster);
gwin->add_nearby_npc(monster);
}
}
/*
* Handle a teleport egg.
*/
void Egg_object::activate_teleport
(
Game_object *obj // Object (actor) that came near it.
)
{
Game_window *gwin = Game_window::get_game_window();
Tile_coord pos(-1, -1, -1); // Get position to jump to.
int qual = get_quality();
if (qual == 255)
{ // Jump to coords.
int schunk = data1 >> 8;
pos = Tile_coord(
(schunk%12)*c_tiles_per_schunk + (data2&0xff),
(schunk/12)*c_tiles_per_schunk + (data2>>8), 0);
}
else
{
Egg_vector vec; // Look for dest. egg (frame == 6).
if (find_nearby_eggs(vec, 275, 256, qual, 6))
{
Egg_object *path = vec[0];
pos = path->get_tile();
}
}
cout << "Should teleport to (" << pos.tx << ", " <<
pos.ty << ')' << endl;
if (pos.tx != -1 && obj && (obj == gwin->get_main_actor() ||
obj->get_party_id() >= 0))
// Teleport everyone!!!
gwin->teleport_party(pos);
// Can keep doing it.
flags &= ~((1 << (int) hatched));
}
/*
* Hatch egg.
*/
void Egg_object::activate
(
Usecode_machine *umachine,
Game_object *obj, // Object (actor) that came near it.
int must // If 1, skip dice roll.
)
{
#ifdef DEBUG
print_debug();
#endif
int roll = must ? 0 : 1 + rand()%100;
if (roll > probability)
return; // Out of luck.
// Flag it as done.
flags |= (1 << (int) hatched);
Game_window *gwin = Game_window::get_game_window();
switch(type)
{
case jukebox:
#ifdef DEBUG
cout << "Audio parameters might be: " << (data1&0xff) << " and " << ((data1>>8)&0x01) << endl;
#endif
Audio::get_ptr()->start_music((data1)&0xff,(data1>>8)&0x01);
break;
case voice:
umachine->do_speech(data1&0xff);
break;
case monster: // Also creates other objects.
{
int shnum = data2&1023;
int frnum = data2>>10;
Monster_info *inf =
gwin->get_info(shnum).get_monster_info();
if (inf)
{ // Armageddon spell cast?
if (gwin->armageddon)
break;
int sched = data1>>8;
int align = data1&3;
int cnt = (data1&0xff)>>2;
if (cnt > 1) // Randomize.
cnt = 1 + (rand()%cnt);
while (cnt--)
Create_monster(gwin, this, shnum, inf,
sched, align);
}
else // Create item.
{
Shape_info& info = gwin->get_info(shnum);
Game_object *nobj =
gwin->create_ireg_object(info,
shnum, frnum, get_tx(),
get_ty(), get_lift());
Map_chunk *chunk =
gwin->get_chunk(get_cx(), get_cy());
if (nobj->is_egg())
chunk->add_egg((Egg_object *) nobj);
else
chunk->add(nobj);
gwin->add_dirty(nobj);
nobj->set_flag(Obj_flags::okay_to_take);
// Objects are created temporary
nobj->set_flag(Obj_flags::is_temporary);
}
break;
}
case usecode:
{ // Data2 is the usecode function.
if (must) // From script? Do immediately.
umachine->call_usecode(data2, this,
Usecode_machine::egg_proximity);
else // Do on next animation frame.
{
Usecode_script *scr = new Usecode_script(this);
scr->add(Ucscript::usecode, data2);
if (flags & (1<<(int)once))
{// Don't remove until done.
scr->add(Ucscript::remove);
flags &= ~(1<<(int)once);
}
scr->start(gwin->get_std_delay());
}
break;
}
case missile:
{
// Get data. Not sure about delay.
int weapon = data1, dir = data2&0xff, delay = data2>>8;
cout << "Missile egg: " << item_names[weapon]
<< endl;
Shape_info& info = gwin->get_info(weapon);
Weapon_info *winf = info.get_weapon_info();
int proj;
if (winf && winf->get_projectile())
proj = winf->get_projectile();
else
proj = 856;// Fireball. Shouldn't get here.
if (!launcher)
launcher = new Missile_launcher(this, weapon,
proj, dir, gwin->get_std_delay()*delay);
if (!launcher->in_queue())
gwin->get_tqueue()->add(0L, launcher, 0);
break;
}
case teleport:
activate_teleport(obj);
break;
case weather:
{
set_weather(gwin, data1&0xff, data1>>8, this);
break;
}
case button: // Set off all in given area.
{
int dist = data1&0xff;
Egg_vector eggs;
find_nearby_eggs(eggs, 275, dist);
for (Egg_vector::const_iterator it = eggs.begin();
it != eggs.end(); ++it)
{
Egg_object *egg = *it;
if (egg != this &&
egg->criteria == external_criteria &&
!(egg->flags & (1 << (int) hatched))) // Experimental attempting to fix problem in Silver Seed
egg->activate(umachine, obj, 0);
}
break;
}
default:
cout << "Egg not actioned" << endl;
}
if (flags & (1 << (int) once))
remove_this(0);
}
/*
* Print debug information.
*/
void Egg_object::print_debug
(
)
{
cout << "Egg type is " << (int) type << ", prob = " <<
(int) probability <<
", distance = " << (int) distance << ", crit = " <<
(int) criteria << ", once = " <<
((flags & (1<<(int)once)) != 0) << ", hatched = " <<
((flags & (1<<(int)hatched)) != 0) <<
", areset = " <<
((flags & (1<<(int)auto_reset)) != 0) << ", data1 = " << data1
<< ", data2 = " << data2 << endl;
}
/*
* Set the weather (static).
*/
void Egg_object::set_weather
(
Game_window *gwin,
int weather, // 0-6.
int len, // In game minutes (I think).
Game_object *egg // Egg this came from, or null.
)
{
if (!len) // Means continuous.
len = 120; // How about a couple game hours?
int cur = gwin->get_weather();
cout << "Current weather is " << cur << "; setting " << weather
<< endl;
switch (weather)
{
case 0: // Back to normal.
gwin->remove_weather_effects();
break;
case 2: // Storm.
if (cur != weather)
gwin->add_effect(new Storm_effect(len, 0, egg));
break;
case 3: // (On Ambrosia).
gwin->remove_weather_effects();
gwin->add_effect(new Sparkle_effect(len, 0, egg));
break;
case 6: // Clouds.
gwin->add_effect(new Clouds_effect(len, 0, egg));
break;
default:
break;
}
}
/*
* Move to a new absolute location. This should work even if the old
* location is invalid (cx=cy=255).
*/
void Egg_object::move
(
int newtx,
int newty,
int newlift
)
{
Game_window *gwin = Game_window::get_game_window();
// Figure new chunk.
int newcx = newtx/c_tiles_per_chunk, newcy = newty/c_tiles_per_chunk;
Map_chunk *newchunk = gwin->get_chunk_safely(newcx, newcy);
if (!newchunk)
return; // Bad loc.
remove_this(1); // Remove from old.
set_lift(newlift); // Set new values.
shape_pos = ((newtx%c_tiles_per_chunk) << 4) + newty%c_tiles_per_chunk;
newchunk->add_egg(this); // Updates cx, cy.
gwin->add_dirty(this); // And repaint new area.
}
/*
* This is needed since it calls remove_egg().
*/
void Egg_object::remove_this
(
int nodel // 1 to not delete.
)
{
Game_window *gwin = Game_window::get_game_window();
if (get_owner()) // Watch for this.
get_owner()->remove(this);
else
{
Map_chunk *chunk = gwin->get_chunk_safely(cx, cy);
if (chunk)
{
gwin->add_dirty(this); // (Make's ::move() simpler.).
chunk->remove_egg(this);
}
}
if (launcher) // Stop missiles.
{
Game_window::get_game_window()->get_tqueue()->remove(launcher);
delete launcher;
launcher = 0;
}
if (!nodel)
gwin->delete_object(this);
}
/*
* Write out.
*/
void Egg_object::write_ireg
(
ostream& out
)
{
unsigned char buf[13]; // 13-byte entry + length-byte.
buf[0] = 12;
uint8 *ptr = &buf[1]; // To avoid confusion about offsets.
write_common_ireg(ptr); // Fill in bytes 1-4.
ptr += 4;
unsigned short tword = type&0xf;// Set up 'type' word.
tword |= ((criteria&7)<<4);
tword |= (((flags>>nocturnal)&1)<<7);
tword |= (((flags>>once)&1)<<8);
tword |= (((flags>>hatched)&1)<<9);
tword |= ((distance&0x1f)<<10);
tword |= (((flags>>auto_reset)&1)<<15);
Write2(ptr, tword);
*ptr++ = probability;
Write2(ptr, data1);
*ptr++ = (get_lift()&15)<<4;
Write2(ptr, data2);
out.write((char*)buf, sizeof(buf));
// Write scheduled usecode.
Game_map::write_scheduled(out, this);
}
/*
* Create from IREG data.
*/
Animated_egg_object::Animated_egg_object
(
int shapenum, int framenum,
unsigned int tilex,
unsigned int tiley, unsigned int lft,
unsigned short itype,
unsigned char prob, short d1, short d2
) : Egg_object(shapenum, framenum,
tilex, tiley, lft, itype, prob, d1, d2)
{
animator = new Frame_animator(this, 1);
}
/*
* Create for fields.
*/
Animated_egg_object::Animated_egg_object
(
int shapenum, int framenum, unsigned int tilex,
unsigned int tiley, unsigned int lft,
unsigned char ty
) : Egg_object(shapenum, framenum, tilex, tiley, lft, ty)
{
int recycle = 0; // Frame to begin new cycles after 1st.
switch (type)
{
case poison_field:
recycle = 6; break;
case sleep_field:
recycle = 1; break;
case fire_field:
recycle = 8; break;
case caltrops_field:
animator = 0; return; // This doesn't get animated.
}
animator = new Field_frame_animator(this, recycle);
}
/*
* Delete.
*/
Animated_egg_object::~Animated_egg_object()
{
delete animator;
}
/*
* Render.
*/
void Animated_egg_object::paint
(
Game_window *gwin
)
{
if (animator)
animator->want_animation(); // Be sure animation is on.
Ireg_game_object::paint(gwin); // Always paint these.
}
/*
* Run usecode when double-clicked or when activated by proximity.
*/
void Animated_egg_object::activate
(
Usecode_machine *umachine,
int event
)
{
Egg_object::activate(umachine, event);
flags &= ~(1 << (int) hatched); // Moongate: reset always.
}
/*
* Apply field.
*
* Output: True to delete field.
*/
bool Field_object::field_effect
(
Actor *actor
)
{
bool del = false; // Only delete poison, sleep fields.
switch (type)
{
case poison_field:
if (rand()%2)
{
actor->set_flag(Obj_flags::poisoned);
del = true;
}
break;
case sleep_field:
if (rand()%2)
{
actor->set_flag(Obj_flags::asleep);
del = true;
}
break;
case fire_field:
// Blue fire (serpent isle)?
if (get_shapenum() == 561)
{
actor->reduce_health(5 + rand()%4);
say(first_ouch, last_ouch);
}
else if (rand()%2)
{
actor->reduce_health(1);
if (rand()%2)
say(first_ouch, last_ouch);
}
// But no sleeping here.
actor->clear_flag(Obj_flags::asleep);
break;
case caltrops_field:
if (actor->get_property(Actor::intelligence)*
(actor->get_flag(Obj_flags::might) ? 2 : 1) < rand()%40)
{
actor->reduce_health(2 + rand()%3);
say(first_ouch, last_ouch);
}
break;
}
return del;
}
/*
* Run usecode when double-clicked or when activated by proximity.
* (Generally, nothing will happen.)
*/
void Field_object::activate
(
Usecode_machine *umachine,
int event
)
{
Ireg_game_object::activate(umachine, event);
}
/*
* Avatar stepped on it.
*/
void Field_object::activate
(
Usecode_machine *umachine,
Game_object *obj, // Object (actor) that came near it.
int /* must */ // If 1, skip dice roll.
)
{
Game_window *gwin = Game_window::get_game_window();
Main_actor *av = gwin->get_main_actor();
if (obj != av && obj->get_party_id() < 0)
return; // Not a party member.
if (field_effect((Actor *) obj))// Apply field.
remove_this(0); // Delete sleep/poison if applied.
}
/*
* Write out. These are stored as normal game objects.
*/
void Field_object::write_ireg
(
ostream& out
)
{
Ireg_game_object::write_ireg(out);
}
/*
* It's a Mirror
*/
Mirror_object::Mirror_object(int shapenum, int framenum, unsigned int tilex,
unsigned int tiley, unsigned int lft) :
Egg_object(shapenum, framenum, tilex, tiley, lft, Egg_object::mirror_object)
{
solid_area = 1;
}
void Mirror_object::activate(Usecode_machine *umachine, int event)
{
Ireg_game_object::activate(umachine, event);
}
void Mirror_object::activate(Usecode_machine *umachine, Game_object *obj, int must)
{
// These are broken, so dont touch
if ((get_framenum()%3) == 2) return;
int wanted_frame = get_framenum()/3;
wanted_frame *= 3;
// Find upperleft or our area
Tile_coord t = get_tile();
// To left or above?
if (get_shapenum()==268) // Left
{
t.tx++;
t.ty--;
}
else // Above
{
t.tx--;
t.ty++;
}
// We just want to know if the area is blocked
int nl = 0;
if (Map_chunk::is_blocked(1, t.tz, t.tx, t.ty, 2, 2, nl, MOVE_WALK, 0))
{
wanted_frame++;
}
// Only if it changed update the shape
if (get_framenum()!=wanted_frame)
{
Game_window *gwin = Game_window::get_game_window();
gwin->add_dirty(this);
set_frame(wanted_frame);
gwin->add_dirty(this);
}
}
// Can it be activated?
int Mirror_object::is_active(Game_object *obj, int tx, int ty, int tz, int from_tx, int from_ty)
{
// These are broken, so dont touch
int frnum = get_framenum();
if (frnum%3 == 2) return 0;
if (frnum >= 3 && GAME_BG) // Demon mirror in FOV.
return 0;
return 1;
}
// Set up active area.
void Mirror_object::set_area()
{
// These are broken, so dont touch
if ((get_framenum()%3) == 2) area = Rectangle(0, 0, 0, 0);
Tile_coord t = get_tile(); // Get absolute tile coords.
// To left or above?
if (get_shapenum()==268) area = Rectangle(t.tx-1, t.ty-3, 6, 6);
else area = Rectangle(t.tx-3 , t.ty-1, 6, 6);
}
void Mirror_object::paint(Game_window *gwin)
{
Ireg_game_object::paint(gwin); // Always paint these.
}
/*
* Write out. These are stored as normal game objects.
*/
void Mirror_object::write_ireg(ostream& out)
{
Ireg_game_object::write_ireg(out);
}
|