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
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/debug-channels.h"
#include "engines/grim/emi/lua_v2.h"
#include "engines/grim/lua/lua.h"
#include "engines/grim/actor.h"
#include "engines/grim/debug.h"
#include "engines/grim/grim.h"
#include "engines/grim/costume.h"
#include "engines/grim/set.h"
#include "engines/grim/emi/emi.h"
#include "engines/grim/emi/costumeemi.h"
#include "engines/grim/emi/skeleton.h"
#include "engines/grim/emi/costume/emichore.h"
#include "engines/grim/emi/costume/emiskel_component.h"
#include "engines/grim/lua/lauxlib.h"
namespace Grim {
void Lua_V2::SetActorLocalAlpha() {
lua_Object actorObj = lua_getparam(1);
lua_Object vertexObj= lua_getparam(2);
lua_Object alphaObj = lua_getparam(3);
// lua_Object unknownObj = lua_getparam(4);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (!lua_isnumber(vertexObj))
return;
if (!lua_isnumber(alphaObj))
return;
int vertex = lua_getnumber(vertexObj);
float alpha = lua_getnumber(alphaObj);
Actor::AlphaMode mode = (Actor::AlphaMode)(int)alpha;
if (mode == Actor::AlphaOff || mode == Actor::AlphaReplace || mode == Actor::AlphaModulate) {
actor->setLocalAlphaMode(vertex, mode);
} else {
actor->setLocalAlpha(vertex, alpha);
}
}
void Lua_V2::SetActorGlobalAlpha() {
lua_Object actorObj = lua_getparam(1);
lua_Object alphaObj = lua_getparam(2);
lua_Object meshObj = lua_getparam(3);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (!lua_isnumber(alphaObj))
return;
const char *mesh = nullptr;
if (lua_isstring(meshObj)) {
mesh = lua_getstring(meshObj);
}
float alpha = lua_getnumber(alphaObj);
if (alpha == Actor::AlphaOff ||
alpha == Actor::AlphaReplace ||
alpha == Actor::AlphaModulate) {
actor->setAlphaMode((Actor::AlphaMode) (int) alpha, mesh);
} else {
actor->setGlobalAlpha(alpha, mesh);
}
}
void Lua_V2::PutActorInOverworld() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
actor->setInOverworld(true);
}
void Lua_V2::RemoveActorFromOverworld() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
actor->setInOverworld(false);
}
void Lua_V2::UnloadActor() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
g_grim->invalidateActiveActorsList();
g_grim->immediatelyRemoveActor(actor);
delete actor;
}
void Lua_V2::SetActorWalkRate() {
lua_Object actorObj = lua_getparam(1);
lua_Object rateObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
if (!lua_isnumber(rateObj))
return;
Actor *actor = getactor(actorObj);
float rate = lua_getnumber(rateObj);
// const below only differ from grim
actor->setWalkRate(rate * 3.279999971389771);
}
void Lua_V2::GetActorWalkRate() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
// const below only differ from grim
lua_pushnumber(actor->getWalkRate() * 0.3048780560493469);
}
void Lua_V2::SetActorTurnRate() {
lua_Object actorObj = lua_getparam(1);
lua_Object rateObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
if (!lua_isnumber(rateObj))
return;
Actor *actor = getactor(actorObj);
float rate = lua_getnumber(rateObj); // FIXME verify negate values of rate
// special handling of value 1 only used for voodoo chair
actor->setTurnRate((rate == 1) ? 100 : rate);
}
void Lua_V2::LockChoreSet() {
lua_Object choreObj = lua_getparam(1);
const char *choreName = lua_getstring(choreObj);
warning("Lua_V2::LockChoreSet: chore: %s", choreName);
}
void Lua_V2::UnlockChoreSet() {
lua_Object choreObj = lua_getparam(1);
const char *choreName = lua_getstring(choreObj);
warning("Lua_V2::UnlockChoreSet: chore: %s", choreName);
}
void Lua_V2::LockChore() {
lua_Object nameObj = lua_getparam(1);
lua_Object filenameObj = lua_getparam(2);
if (!lua_isstring(nameObj) || !lua_isstring(filenameObj)) {
lua_pushnil();
return;
}
const char *name = lua_getstring(nameObj);
const char *filename = lua_getstring(filenameObj);
warning("Lua_V2::LockChore, name: %s, filename: %s", name, filename);
// FIXME: implement missing rest part of code
}
void Lua_V2::UnlockChore() {
lua_Object nameObj = lua_getparam(1);
lua_Object filenameObj = lua_getparam(2);
if (!lua_isstring(nameObj) || !lua_isstring(filenameObj)) {
lua_pushnil();
return;
}
const char *name = lua_getstring(nameObj);
const char *filename = lua_getstring(filenameObj);
warning("Lua_V2::UnlockChore, name: %s, filename: %s", name, filename);
// FIXME: implement missing rest part of code
}
void Lua_V2::IsActorChoring() {
lua_Object actorObj = lua_getparam(1);
bool excludeLoop = getbool(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
const Common::List<Costume *> &costumes = actor->getCostumes();
for (Common::List<Costume *>::const_iterator it = costumes.begin(); it != costumes.end(); ++it) {
Costume *costume = *it;
for (int i = 0; i < costume->getNumChores(); i++) {
int chore = costume->isChoring(i, excludeLoop);
if (chore != -1) {
// Ignore talk chores.
bool isTalk = false;
for (int j = 0; j < 10; j++) {
if (costume == actor->getTalkCostume(j) && actor->getTalkChore(j) == chore) {
isTalk = true;
break;
}
}
if (isTalk)
continue;
lua_pushnumber(chore);
pushbool(true);
return;
}
}
}
lua_pushnil();
}
void Lua_V2::IsChoreValid() {
lua_Object choreObj = lua_getparam(1);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
pushbool(c != nullptr);
} else {
lua_pushnil();
}
}
void Lua_V2::IsChorePlaying() {
lua_Object choreObj = lua_getparam(1);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
pushbool(c->isPlaying() && !c->isPaused());
} else {
lua_pushnil();
}
}
void Lua_V2::IsChoreLooping() {
lua_Object choreObj = lua_getparam(1);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
pushbool(c->isLooping());
} else {
lua_pushnil();
}
}
void Lua_V2::SetChoreLooping() {
lua_Object choreObj = lua_getparam(1);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
c->setLooping(false);
}
lua_pushnil();
}
void Lua_V2::PlayChore() {
lua_Object choreObj = lua_getparam(1);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
c->setPaused(false);
}
}
void Lua_V2::PauseChore() {
lua_Object choreObj = lua_getparam(1);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
c->setPaused(true);
}
}
void Lua_V2::StopChore() {
lua_Object choreObj = lua_getparam(1);
lua_Object fadeTimeObj = lua_getparam(2);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R'))
return;
int chore = lua_getuserdata(choreObj);
float fadeTime = 0.0f;
if (!lua_isnil(fadeTimeObj)) {
if (lua_isnumber(fadeTimeObj))
fadeTime = lua_getnumber(fadeTimeObj);
}
// There are a few cases in the scripts where StopChore is called with an excessively
// large fadeTime value. The original engine ignores fade times greater or equal
// to 0.6 seconds, so we replicate that behavior here.
if (fadeTime >= 0.6f) {
fadeTime = 0.0f;
}
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
c->stop((int)(fadeTime * 1000));
}
}
void Lua_V2::AdvanceChore() {
lua_Object choreObj = lua_getparam(1);
lua_Object timeObj = lua_getparam(2);
if (!lua_isuserdata(choreObj) || lua_tag(choreObj) != MKTAG('C','H','O','R') || !lua_isnumber(timeObj))
return;
int chore = lua_getuserdata(choreObj);
float time = lua_getnumber(timeObj);
Chore *c = EMIChore::getPool().getObject(chore);
if (c) {
if (!c->isPlaying()) {
warning("AdvanceChore() called on stopped chore %s (%s)",
c->getName(), c->getOwner()->getFilename().c_str());
if (c->isLooping()) {
c->getOwner()->playChoreLooping(c->getName());
} else {
c->getOwner()->playChore(c->getName());
}
}
c->advance(time * 1000);
}
}
// TODO: Implement, verify, and rename parameters
void Lua_V2::CompleteChore() {
lua_Object param1 = lua_getparam(1);
lua_Object param2 = lua_getparam(2);
if (!lua_isuserdata(param1) || !lua_isnumber(param2))
error("Lua_V2::CompleteChore - Unknown params");
// Guesswork based on StopChore:
int chore = lua_getuserdata(param1);
float time = lua_getnumber(param2);
error("Lua_V2::CompleteChore(%d, %f) - TODO: Implement opcode", chore, time);
}
void Lua_V2::SetActorSortOrder() {
lua_Object actorObj = lua_getparam(1);
lua_Object orderObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
if (!lua_isnumber(orderObj))
return;
Actor *actor = getactor(actorObj);
int order = (int)lua_getnumber(orderObj);
actor->setSortOrder(order);
g_emi->invalidateSortOrder();
}
void Lua_V2::GetActorSortOrder() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
lua_pushnumber(actor->getEffectiveSortOrder());
}
void Lua_V2::ActorActivateShadow() {
lua_Object actorObj = lua_getparam(1);
lua_Object activeObj = lua_getparam(2);
lua_Object planeObj = lua_getparam(3);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
bool active = (int)lua_getnumber(activeObj) == 1;
const char *plane = nullptr;
if (lua_isstring(planeObj))
plane = lua_getstring(planeObj);
actor->activateShadow(active, plane);
}
void Lua_V2::ActorStopMoving() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
actor->stopWalking();
actor->stopTurning();
warning("Lua_V2::ActorStopMoving, actor: %s", actor->getName().c_str());
// FIXME: Inspect the rest of the code to see if there's anything else missing
}
void Lua_V2::ActorLookAt() {
lua_Object actorObj = lua_getparam(1);
lua_Object xObj = lua_getparam(2);
lua_Object yObj = lua_getparam(3);
lua_Object zObj = lua_getparam(4);
lua_Object rateObj = lua_getparam(5);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A', 'C', 'T', 'R'))
return;
Actor *actor = getactor(actorObj);
if (!actor->getCurrentCostume())
return;
if (lua_isnumber(rateObj))
actor->setLookAtRate(lua_getnumber(rateObj));
// Look at nothing
if (lua_isnil(xObj)) {
if (actor->isLookAtVectorZero())
return;
actor->setLookAtVectorZero();
actor->setLooking(false);
if (lua_isnumber(yObj) && lua_getnumber(yObj) > 0)
actor->setLookAtRate(lua_getnumber(yObj));
return;
} else if (lua_isnumber(xObj)) { // look at xyz
float fY;
float fZ;
float fX = lua_getnumber(xObj);
if (lua_isnumber(yObj))
fY = lua_getnumber(yObj);
else
fY = 0.0f;
if (lua_isnumber(zObj))
fZ = lua_getnumber(zObj);
else
fZ = 0.0f;
Math::Vector3d vector;
vector.set(fX, fY, fZ);
actor->setLookAtVector(vector);
if (lua_isnumber(rateObj))
actor->setLookAtRate(lua_getnumber(rateObj));
} else if (lua_isuserdata(xObj) && lua_tag(xObj) == MKTAG('A', 'C', 'T', 'R')) { // look at another actor
Actor *lookedAct = getactor(xObj);
actor->setLookAtActor(lookedAct);
if (lua_isnumber(yObj))
actor->setLookAtRate(lua_getnumber(yObj));
} else {
return;
}
actor->setLooking(true);
}
void Lua_V2::GetActorWorldPos() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
Math::Vector3d pos = actor->getWorldPos();
lua_pushnumber(pos.x());
lua_pushnumber(pos.y());
lua_pushnumber(pos.z());
}
void Lua_V2::PutActorInSet() {
lua_Object actorObj = lua_getparam(1);
lua_Object setObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!lua_isstring(setObj) && !lua_isnil(setObj)) {
lua_pushnil();
return;
}
const char *set = lua_getstring(setObj);
// FIXME verify adding actor to set
if (!set) {
actor->putInSet("");
lua_pushnil();
} else {
if (!actor->isInSet(set)) {
actor->putInSet(set);
}
lua_pushnumber(1.0);
}
}
void Lua_V2::SetActorRestChore() {
lua_Object actorObj = lua_getparam(1);
lua_Object choreObj = lua_getparam(2);
lua_Object costumeObj = lua_getparam(3);
Costume *costume = nullptr;
int chore = -1;
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
(!lua_isstring(choreObj) && !lua_isnil(choreObj))) {
return;
}
Actor *actor = getactor(actorObj);
setChoreAndCostume(choreObj, costumeObj, actor, costume, chore);
actor->setRestChore(chore, costume);
}
void Lua_V2::SetActorWalkChore() {
lua_Object actorObj = lua_getparam(1);
lua_Object choreObj = lua_getparam(2);
lua_Object costumeObj = lua_getparam(3);
Costume *costume = nullptr;
int chore = -1;
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
(!lua_isstring(choreObj) && !lua_isnil(choreObj))) {
return;
}
Actor *actor = getactor(actorObj);
setChoreAndCostume(choreObj, costumeObj, actor, costume, chore);
actor->setWalkChore(chore, costume);
}
void Lua_V2::SetActorTurnChores() {
lua_Object actorObj = lua_getparam(1);
lua_Object leftChoreObj = lua_getparam(2);
lua_Object rightChoreObj = lua_getparam(3);
lua_Object costumeObj = lua_getparam(4);
Costume *costume;
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
return;
} else if (!lua_isnil(leftChoreObj) && !lua_isstring(leftChoreObj)) {
return;
} else if (!lua_isnil(rightChoreObj) && !lua_isstring(rightChoreObj)) {
return;
}
Actor *actor = getactor(actorObj);
if (!findCostume(costumeObj, actor, &costume))
return;
int leftChore = costume->getChoreId(lua_getstring(leftChoreObj));
int rightChore = costume->getChoreId(lua_getstring(rightChoreObj));
actor->setTurnChores(leftChore, rightChore, costume);
}
void Lua_V2::SetActorTalkChore() {
lua_Object actorObj = lua_getparam(1);
lua_Object indexObj = lua_getparam(2);
lua_Object choreObj = lua_getparam(3);
lua_Object costumeObj = lua_getparam(4);
Costume *costume = nullptr;
int chore = -1;
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
!lua_isnumber(indexObj) ||
(!lua_isstring(choreObj) && !lua_isnil(choreObj))) {
return;
}
int index = (int)lua_getnumber(indexObj);
if (index < 0 || index >= 16)
return;
Actor *actor = getactor(actorObj);
setChoreAndCostume(choreObj, costumeObj, actor, costume, chore);
actor->setTalkChore(index + 1, chore, costume);
}
void Lua_V2::SetActorMumblechore() {
lua_Object actorObj = lua_getparam(1);
lua_Object choreObj = lua_getparam(2);
lua_Object costumeObj = lua_getparam(3);
Costume *costume = nullptr;
int chore = -1;
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R') ||
(!lua_isstring(choreObj) && !lua_isnil(choreObj))) {
return;
}
Actor *actor = getactor(actorObj);
setChoreAndCostume(choreObj, costumeObj, actor, costume, chore);
actor->setMumbleChore(chore, costume);
}
void Lua_V2::GetActorChores() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
const Common::List<Costume *> &costumes = actor->getCostumes();
lua_Object result = lua_createtable();
int count = 0;
for (Common::List<Costume *>::const_iterator it = costumes.begin(); it != costumes.end(); ++it) {
const Common::List<Chore *> &playingChores = (*it)->getPlayingChores();
for (Common::List<Chore *>::const_iterator cit = playingChores.begin(); cit != playingChores.end(); ++cit) {
lua_pushobject(result);
lua_pushnumber(count++);
lua_pushusertag(((EMIChore *)*cit)->getId(), MKTAG('C', 'H', 'O', 'R'));
lua_settable();
}
}
lua_pushobject(result);
lua_pushstring("count");
lua_pushnumber(count);
lua_settable();
lua_pushobject(result);
}
// Helper function, not called from LUA directly
bool Lua_V2::findCostume(lua_Object costumeObj, Actor *actor, Costume **costume) {
*costume = nullptr;
if (lua_isnil(costumeObj)) {
*costume = actor->getCurrentCostume();
} else {
if (lua_isstring(costumeObj)) {
const char *costumeName = lua_getstring(costumeObj);
*costume = actor->findCostume(costumeName);
if (*costume == nullptr) {
actor->pushCostume(costumeName);
*costume = actor->findCostume(costumeName);
}
}
}
return (*costume != nullptr);
}
void Lua_V2::PlayActorChore() {
lua_Object actorObj = lua_getparam(1);
lua_Object choreObj = lua_getparam(2);
lua_Object costumeObj = lua_getparam(3);
lua_Object modeObj = lua_getparam(4);
lua_Object fadeTimeObj = lua_getparam(5);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!lua_isstring(choreObj) || !lua_isstring(costumeObj))
lua_pushnil();
bool mode = false;
float fadeTime = 0.0f;
if (!lua_isnil(modeObj)) {
if (lua_getnumber(modeObj) != 0.0)
mode = true;
}
if (!lua_isnil(fadeTimeObj)) {
if (lua_isnumber(fadeTimeObj))
fadeTime = lua_getnumber(fadeTimeObj);
}
const char *choreName = lua_getstring(choreObj);
Costume *costume;
if (!findCostume(costumeObj, actor, &costume))
return;
EMIChore *chore = (EMIChore *)costume->getChore(choreName);
if (mode) {
costume->playChoreLooping(choreName, (int)(fadeTime * 1000));
} else {
costume->playChore(choreName, (int)(fadeTime * 1000));
}
if (chore) {
lua_pushusertag(chore->getId(), MKTAG('C','H','O','R'));
} else {
lua_pushnil();
}
}
void Lua_V2::StopActorChores() {
lua_Object actorObj = lua_getparam(1);
// Guesswork for boolean parameter
bool ignoreLoopingChores = getbool(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
actor->stopAllChores(ignoreLoopingChores);
}
void Lua_V2::SetActorLighting() {
lua_Object actorObj = lua_getparam(1);
lua_Object lightModeObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (lua_isnil(lightModeObj) || !lua_isnumber(lightModeObj))
return;
int lightMode = (int)lua_getnumber(lightModeObj);
actor->setLightMode((Actor::LightMode)lightMode);
}
void Lua_V2::SetActorCollisionMode() {
lua_Object actorObj = lua_getparam(1);
lua_Object modeObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
assert(actor);
int mode = (int)lua_getnumber(modeObj);
Actor::CollisionMode m;
switch (mode) {
case Actor::CollisionOff:
m = Actor::CollisionOff;
break;
case Actor::CollisionBox:
m = Actor::CollisionBox;
break;
case Actor::CollisionSphere:
m = Actor::CollisionSphere;
break;
default:
warning("Lua_V2::SetActorCollisionMode(): wrong collisionmode: %d, using default 0", mode);
m = Actor::CollisionOff;
}
actor->setCollisionMode(m);
}
void Lua_V2::SetActorCollisionScale() {
lua_Object actorObj = lua_getparam(1);
lua_Object scaleObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
assert(actor);
float scale = lua_getnumber(scaleObj);
actor->setCollisionScale(scale);
}
void Lua_V2::GetActorPuckVector() {
lua_Object actorObj = lua_getparam(1);
lua_Object addObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R')) {
lua_pushnil();
return;
}
Actor *actor = getactor(actorObj);
// Note: The wear chore of dumbshadow.cos is only started from Lua if
// GetActorPuckVector returns a non-nil value. The original engine seems
// to return nil for all actors that have never followed walkboxes.
if (!actor || !actor->hasFollowedBoxes()) {
lua_pushnil();
return;
}
Math::Vector3d result = actor->getPuckVector();
if (!lua_isnil(addObj))
result += actor->getPos();
lua_pushnumber(result.x());
lua_pushnumber(result.y());
lua_pushnumber(result.z());
}
void Lua_V2::SetActorHeadLimits() {
lua_Object actorObj = lua_getparam(1);
lua_Object yawObj = lua_getparam(2);
lua_Object maxPitchObj = lua_getparam(3);
lua_Object minPitchObj = lua_getparam(4);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (lua_isnumber(yawObj) && lua_isnumber(minPitchObj) && lua_isnumber(maxPitchObj)) {
float yaw = lua_getnumber(yawObj);
float maxPitch = lua_getnumber(maxPitchObj);
float minPitch = lua_getnumber(minPitchObj);
actor->setHeadLimits(yaw / 2, maxPitch, -minPitch);
}
}
void Lua_V2::SetActorHead() {
lua_Object actorObj = lua_getparam(1);
lua_Object jointObj = lua_getparam(2);
lua_Object xObj = lua_getparam(3);
lua_Object yObj = lua_getparam(4);
lua_Object zObj = lua_getparam(5);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A', 'C', 'T', 'R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (lua_isstring(jointObj)) {
const char *joint = lua_getstring(jointObj);
Math::Vector3d offset;
offset.x() = lua_getnumber(xObj);
offset.y() = lua_getnumber(yObj);
offset.z() = lua_getnumber(zObj);
actor->setHead(joint, offset);
}
}
void Lua_V2::SetActorFOV() {
lua_Object actorObj = lua_getparam(1);
lua_Object fovObj = lua_getparam(2);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (lua_isnumber(fovObj)) {
float fov = lua_getnumber(fovObj);
// FIXME: implement missing code
//actor->func(fov); // cos(fov * some tuntime val * 0.5)
warning("Lua_V2::SetActorFOV: implement opcode. actor: %s, param: %f", actor->getName().c_str(), fov);
}
}
void Lua_V2::AttachActor() {
// Missing lua parts
lua_Object attachedObj = lua_getparam(1);
lua_Object actorObj = lua_getparam(2);
lua_Object jointObj = lua_getparam(3);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A', 'C', 'T', 'R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
if (!lua_isuserdata(attachedObj) || lua_tag(attachedObj) != MKTAG('A', 'C', 'T', 'R'))
return;
Actor *attached = getactor(attachedObj);
if (!attached)
return;
const char *joint = nullptr;
if (!lua_isnil(jointObj)) {
joint = lua_getstring(jointObj);
}
attached->attachToActor(actor, joint);
Debug::debug(Debug::Actors | Debug::Scripts, "Lua_V2::AttachActor: attaching %s to %s (on %s)", attached->getName().c_str(), actor->getName().c_str(), joint ? joint : "(none)");
g_emi->invalidateSortOrder();
}
void Lua_V2::DetachActor() {
// Missing lua parts
lua_Object attachedObj = lua_getparam(1);
if (!lua_isuserdata(attachedObj) || lua_tag(attachedObj) != MKTAG('A','C','T','R'))
return;
Actor *attached = getactor(attachedObj);
if (!attached)
return;
Debug::debug(Debug::Actors | Debug::Scripts, "Lua_V2::DetachActor: detaching %s from parent actor", attached->getName().c_str());
attached->detach();
g_emi->invalidateSortOrder();
}
void Lua_V2::WalkActorToAvoiding() {
lua_Object actorObj = lua_getparam(1);
lua_Object actor2Obj = lua_getparam(2);
lua_Object xObj = lua_getparam(3);
lua_Object yObj = lua_getparam(4);
lua_Object zObj = lua_getparam(5);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
if (!lua_isuserdata(actor2Obj) || lua_tag(actor2Obj) != MKTAG('A','C','T','R'))
return;
Math::Vector3d destVec;
Actor *actor = getactor(actorObj);
if (!lua_isnumber(xObj)) {
if (!lua_isuserdata(xObj) || lua_tag(xObj) != MKTAG('A','C','T','R'))
return;
Actor *destActor = getactor(xObj);
destVec = destActor->getPos();
} else {
float x = lua_getnumber(xObj);
float y = lua_getnumber(yObj);
float z = lua_getnumber(zObj);
destVec.set(x, y, z);
}
// TODO: Make this actually avoid the second actor
actor->walkTo(destVec);
}
void Lua_V2::WalkActorVector() {
lua_Object actorObj = lua_getparam(1);
// lua_Object xObj = lua_getparam(3);
// lua_Object yObj = lua_getparam(4);
// lua_Object zObj = lua_getparam(5);
// lua_Object param6Obj = lua_getparam(6);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A', 'C', 'T', 'R'))
return;
// Actor *actor = static_cast<Actor *>(lua_getuserdata(actorObj));
Actor *actor2 = getactor(actorObj);
// TODO whole below part need rewrote to much original
float moveHoriz, moveVert;
// Third option is the "left/right" movement
moveHoriz = luaL_check_number(2);
// Fourth Option is the "up/down" movement
moveVert = luaL_check_number(4);
// Get the direction the camera is pointing
Set::Setup *setup = g_grim->getCurrSet()->getCurrSetup();
Math::Vector3d cameraVector(0, 0, 1);
setup->_rot.transform(&cameraVector, false);
// find the angle the camera direction is around the unit circle
Math::Angle cameraYaw = Math::Angle::arcTangent2(cameraVector.x(), cameraVector.z());
// Handle the turning
Math::Vector3d adjustVector(moveHoriz, 0, moveVert);
// find the angle the adjust vector is around the unit circle
Math::Angle adjustYaw = Math::Angle::arcTangent2(adjustVector.x(), adjustVector.z());
Math::Angle yaw = cameraYaw + adjustYaw;
// set the new direction or walk forward
if (actor2->getYaw() != yaw)
actor2->turnTo(0, yaw, 0, true);
actor2->walkForward();
}
void Lua_V2::EnableActorPuck() {
lua_Object actorObj = lua_getparam(1);
if (!lua_isuserdata(actorObj) || lua_tag(actorObj) != MKTAG('A','C','T','R'))
return;
Actor *actor = getactor(actorObj);
if (!actor)
return;
bool enable = getbool(2);
// FIXME: Implement.
warning("Lua_V2::EnableActorPuck: stub, actor: %s enable: %s", actor->getName().c_str(), enable ? "TRUE" : "FALSE");
}
// Helper function, not called from LUA directly
void Lua_V2::setChoreAndCostume(lua_Object choreObj, lua_Object costumeObj, Actor *actor, Costume *&costume, int &chore) {
if (lua_isnil(choreObj)) {
return;
}
if (!findCostume(costumeObj, actor, &costume))
return;
const char *choreStr = lua_getstring(choreObj);
chore = costume->getChoreId(choreStr);
}
} // end of namespace Grim
|