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
|
#include "StdAfx.h"
#include <fstream>
#include <stdexcept>
#include "mmgr.h"
#include "creg/VarTypes.h"
#include "ExplosionGenerator.h"
#include "FileSystem/FileHandler.h"
#include "Game/Camera.h"
#include "LogOutput.h"
#include "Map/Ground.h"
#include "ConfigHandler.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GroundFlash.h"
#include "Rendering/Textures/ColorMap.h"
#include "ProjectileHandler.h"
#include "Unsynced/BubbleProjectile.h"
#include "Unsynced/DirtProjectile.h"
#include "Unsynced/ExploSpikeProjectile.h"
#include "Unsynced/HeatCloudProjectile.h"
#include "Unsynced/SmokeProjectile2.h"
#include "Unsynced/SpherePartProjectile.h"
#include "Unsynced/WakeProjectile.h"
#include "Unsynced/WreckProjectile.h"
#include <assert.h>
#include "GlobalUnsynced.h"
#include "Exceptions.h"
#include <boost/cstdint.hpp>
using std::min;
using std::max;
CR_BIND_DERIVED_INTERFACE(CExpGenSpawnable, CWorldObject);
CR_REG_METADATA(CExpGenSpawnable,
);
CExplosionGeneratorHandler* explGenHandler = NULL;
// -------------------------------------------------------------------------------
// ClassAliasList: Finds C++ classes with class aliases
// -------------------------------------------------------------------------------
ClassAliasList::ClassAliasList() {}
void ClassAliasList::Load(const LuaTable& aliasTable)
{
map<string, string> aliasList;
aliasTable.GetMap(aliasList);
aliases.insert(aliasList.begin(), aliasList.end());
}
creg::Class* ClassAliasList::GetClass(const string& name)
{
string n = name;
for (;;) {
map<string,string>::iterator i = aliases.find(n);
if (i == aliases.end()) {
break;
}
n = i->second;
}
creg::Class *cls = creg::System::GetClass(n);
if (!cls) {
throw content_error("Unknown class: " + name);
}
return cls;
}
string ClassAliasList::FindAlias(const string& className)
{
for (map<string,string>::iterator i = aliases.begin(); i != aliases.end(); ++i)
if (i->second == className) return i->first;
return className;
}
// -------------------------------------------------------------------------------
// Explosion generator handler: loads and stores a list of explosion generators
// -------------------------------------------------------------------------------
CExplosionGeneratorHandler::CExplosionGeneratorHandler()
: luaParser("gamedata/explosions.lua",
SPRING_VFS_MOD_BASE, SPRING_VFS_ZIP)
{
LuaParser aliasParser("gamedata/explosion_alias.lua",
SPRING_VFS_MOD_BASE, SPRING_VFS_ZIP);
if (!aliasParser.Execute()) {
logOutput.Print(aliasParser.GetErrorLog());
} else {
const LuaTable root = aliasParser.GetRoot();
projectileClasses.Load(root.SubTable("projectiles"));
generatorClasses.Load(root.SubTable("generators"));
}
if (!luaParser.Execute()) {
logOutput.Print(luaParser.GetErrorLog());
} else {
luaTable = luaParser.GetRoot();
}
}
CExplosionGenerator* CExplosionGeneratorHandler::LoadGenerator(const string& tag)
{
string klass;
string::size_type seppos = tag.find(':');
if (seppos == string::npos) {
klass = tag;
} else {
klass = tag.substr(0, seppos);
}
creg::Class *cls = generatorClasses.GetClass(klass);
if (!cls->IsSubclassOf(CExplosionGenerator::StaticClass())) {
throw content_error(klass + " is not a subclass of CExplosionGenerator");
}
CExplosionGenerator* eg = (CExplosionGenerator *)cls->CreateInstance();
if (seppos != string::npos) {
eg->Load(this, tag.substr(seppos + 1));
}
return eg;
}
// -------------------------------------------------------------------------------
// Base explosion generator class
// -------------------------------------------------------------------------------
CR_BIND_INTERFACE(CExplosionGenerator);
CExplosionGenerator::CExplosionGenerator()
{}
CExplosionGenerator::~CExplosionGenerator()
{}
// -------------------------------------------------------------------------------
// Default explosion generator: everything is calculated from damage and radius
// -------------------------------------------------------------------------------
CR_BIND_DERIVED(CStdExplosionGenerator, CExplosionGenerator, );
CStdExplosionGenerator::CStdExplosionGenerator()
{}
CStdExplosionGenerator::~CStdExplosionGenerator()
{}
void CStdExplosionGenerator::Load(CExplosionGeneratorHandler *h, const string& tag)
{
}
void CStdExplosionGenerator::Explosion(const float3 &pos, float damage,
float radius, CUnit *owner,float gfxMod,
CUnit *hit, const float3 &dir)
{
float h2 = ground->GetHeight2(pos.x, pos.z);
float height = pos.y - h2;
if (height < 0.0f) {
height = 0.0f;
}
bool waterExplosion = (h2 < -3.0f);
bool uwExplosion = (pos.y < -15.0f);
bool airExplosion = (pos.y - max(0.0f, h2) > 20.0f);
damage = damage / 20.0f;
// limit the visual effects based on the radius
if (damage > radius * 1.5f)
damage = radius * 1.5f;
damage *= gfxMod;
float3 camVect = camera->pos - pos;
float camLength = camVect.Length();
float moveLength = radius * 0.03f;
if (camLength > 0.0f) { camVect /= camLength; }
if (camLength < moveLength + 2) { moveLength = camLength - 2; }
const float3 npos = pos + camVect * moveLength;
new CHeatCloudProjectile(npos, float3(0.0f, 0.3f, 0.0f), 8 + sqrt(damage) * 0.5f, 7 + damage * 2.8f, owner);
if (ph->particleSaturation < 1.0f) {
// turn off lots of graphic only particles when we have more particles than we want
float smokeDamage = damage;
if (uwExplosion)
smokeDamage *= 0.3f;
if (airExplosion || waterExplosion)
smokeDamage *= 0.6f;
float invSqrtsmokeDamage = 1.0f;
if (smokeDamage > 0.0f) {
invSqrtsmokeDamage /= (sqrt(smokeDamage) * 0.35f);
}
for (int a = 0; a < smokeDamage * 0.6f; ++a) {
const float3 speed(
(-0.1f + gu->usRandFloat() * 0.2f),
( 0.1f + gu->usRandFloat() * 0.3f) * invSqrtsmokeDamage,
(-0.1f + gu->usRandFloat() * 0.2f)
);
const float h = ground->GetApproximateHeight(npos.x, npos.z);
const float time = (40 + sqrt(smokeDamage) * 15) * (0.8f + gu->usRandFloat() * 0.7f);
float3 npos(pos + gu->usRandVector() * (smokeDamage * 1.0f));
if (npos.y < h)
npos.y = h;
new CSmokeProjectile2(pos, npos, speed, time, sqrt(smokeDamage) *4, 0.4f, owner, 0.6f);
}
if (!airExplosion && !uwExplosion && !waterExplosion) {
const int numDirt = (int) min(20.0f, damage * 0.8f);
const float3 color(0.15f, 0.1f, 0.05f);
for (int a = 0; a < numDirt; ++a) {
float3 speed(
(0.5f - gu->usRandFloat()) * 1.5f,
1.7f + gu->usRandFloat() * 1.6f,
(0.5f - gu->usRandFloat()) * 1.5f
);
speed *= (0.7f + min(30.0f, damage) / 30.0f);
const float3 npos(
pos.x - (0.5f - gu->usRandFloat()) * (radius * 0.6f),
pos.y - 2.0f - damage * 0.2f,
pos.z - (0.5f - gu->usRandFloat()) * (radius * 0.6f)
);
new CDirtProjectile(npos, speed, 90 + damage * 2, 2.0f + sqrt(damage) * 1.5f, 0.4f, 0.999f, owner, color);
}
}
if (!airExplosion && !uwExplosion && waterExplosion) {
int numDirt=(int)min(40.f,damage*0.8f);
float3 color(1,1,1);
for (int a=0;a<numDirt;++a) {
float3 speed((0.5f-gu->usRandFloat())*0.2f,a*0.1f+gu->usRandFloat()*0.8f,(0.5f-gu->usRandFloat())*0.2f);
speed*=0.7f+min((float)30,damage)/30;
float3 npos(pos.x-(0.5f-gu->usRandFloat())*(radius*0.2f),pos.y-2.0f-sqrt(damage)*2.0f,pos.z-(0.5f-gu->usRandFloat())*(radius*0.2f));
new CDirtProjectile(npos,speed,90+damage*2,2.0f+sqrt(damage)*2.0f,0.3f,0.99f,owner,color);
}
}
if (damage>=20 && !uwExplosion && !airExplosion) {
int numDebris=gu->usRandInt()%6;
if (numDebris>0)
numDebris+=3+(int)(damage*0.04f);
for (int a=0;a<numDebris;++a) {
float3 speed;
if (height<4)
speed=float3((0.5f-gu->usRandFloat())*2.0f,1.8f+gu->usRandFloat()*1.8f,(0.5f-gu->usRandFloat())*2.0f);
else
speed=float3(gu->usRandVector()*2);
speed*=0.7f+min((float)30,damage)/23;
float3 npos(pos.x-(0.5f-gu->usRandFloat())*(radius*1),pos.y,pos.z-(0.5f-gu->usRandFloat())*(radius*1));
new CWreckProjectile(npos,speed,90+damage*2,owner);
}
}
if (uwExplosion) {
int numBubbles=(int)(damage*0.7f);
for (int a=0;a<numBubbles;++a) {
new CBubbleProjectile(pos+gu->usRandVector()*radius*0.5f,gu->usRandVector()*0.2f+float3(0,0.2f,0),damage*2+gu->usRandFloat()*damage,1+gu->usRandFloat()*2,0.02f,owner,0.5f+gu->usRandFloat()*0.3f);
}
}
if (waterExplosion && !uwExplosion && !airExplosion) {
int numWake=(int)(damage*0.5f);
for (int a=0;a<numWake;++a) {
new CWakeProjectile(pos+gu->usRandVector()*radius*0.2f,gu->usRandVector()*radius*0.003f,sqrt(damage)*4,damage*0.03f,owner,0.3f+gu->usRandFloat()*0.2f,0.8f/(sqrt(damage)*3+50+gu->usRandFloat()*90),1);
}
}
if (radius>10 && damage>4) {
int numSpike=(int)sqrt(damage)+8;
for (int a=0;a<numSpike;++a) {
float3 speed=gu->usRandVector();
speed.Normalize();
speed*=(8+damage*3.0f)/(9+sqrt(damage)*0.7f)*0.35f;
if (!airExplosion && !waterExplosion && speed.y<0)
speed.y=-speed.y;
new CExploSpikeProjectile(pos+speed,speed*(0.9f+gu->usRandFloat()*0.4f),radius*0.1f,radius*0.1f,0.6f,0.8f/(8+sqrt(damage)),owner);
}
}
}
if (radius > 20 && damage > 6 && height < radius * 0.7f) {
float modSize=max(radius,damage*2);
float circleAlpha=0;
float circleGrowth=0;
float ttl=8+sqrt(damage)*0.8f;
if (radius>40 && damage>12) {
circleAlpha=min(0.5f,damage*0.01f);
circleGrowth=(8+damage*2.5f)/(9+sqrt(damage)*0.7f)*0.55f;
}
float flashSize=modSize;
float flashAlpha=min(0.8f,damage*0.01f);
new CStandardGroundFlash(pos,circleAlpha,flashAlpha,flashSize,circleGrowth,ttl);
}
if (radius > 40 && damage > 12) {
CSpherePartProjectile::CreateSphere(pos,min(0.7f,damage*0.02f),5+(int)(sqrt(damage)*0.7f),(8+damage*2.5f)/(9+sqrt(damage)*0.7f)*0.5f,owner);
}
}
// -------------------------------------------------------------------------------
// CCustomExplosionGenerator: Uses explosion info from a TDF file
// -------------------------------------------------------------------------------
CR_BIND_DERIVED(CCustomExplosionGenerator, CStdExplosionGenerator, );
enum
{
SPW_WATER = 1,
SPW_GROUND = 2,
SPW_AIR = 4,
SPW_UNDERWATER = 8,
SPW_UNIT = 16, // only execute when the explosion hits a unit
SPW_NO_UNIT = 32, // only execute when the explosion doesn't hit a unit (environment)
SPW_SYNCED = 64, // spawn this projectile even if particleSaturation > 1
};
CCustomExplosionGenerator::CCustomExplosionGenerator()
{
currentCEG = cachedCEGs.end();
}
CCustomExplosionGenerator::~CCustomExplosionGenerator()
{
}
#define OP_END 0
#define OP_STOREI 1 // int
#define OP_STOREF 2 // float
#define OP_STOREC 3 // char
#define OP_ADD 4
#define OP_RAND 5
#define OP_DAMAGE 6
#define OP_INDEX 7
#define OP_LOADP 8 // load a void* into the pointer register
#define OP_STOREP 9 // store the pointer register into a void*
#define OP_DIR 10 // store the float3 direction
#define OP_SAWTOOTH 11 // Performs a modulo to create a sawtooth wave
#define OP_DISCRETE 12 // Floors the value to a multiple of its parameter
#define OP_SINE 13 // Uses val as the phase of a sine wave
#define OP_YANK 14 // Moves the input value into a buffer, returns zero
#define OP_MULTIPLY 15 // Multiplies with buffer value
#define OP_ADDBUFF 16 // Adds buffer value
#define OP_POW 17 // Power with code as exponent
#define OP_POWBUFF 18 // Power with buffer as exponent
void CCustomExplosionGenerator::ExecuteExplosionCode(const char* code, float damage, char* instance, int spawnIndex, const float3 &dir)
{
float val = 0.0f;
void* ptr = NULL;
float buffer[16];
for (;;) {
switch (*(code++)) {
case OP_END: {
return;
}
case OP_STOREI: {
boost::uint16_t offset = *(boost::uint16_t*) code;
code += 2;
*(int*) (instance + offset) = (int) val;
val = 0.0f;
break;
}
case OP_STOREF: {
boost::uint16_t offset = *(boost::uint16_t*) code;
code += 2;
*(float*) (instance + offset) = val;
val = 0.0f;
break;
}
case OP_STOREC: {
boost::uint16_t offset = *(boost::uint16_t*) code;
code += 2;
*(unsigned char*) (instance + offset) = (int) val;
val = 0.0f;
break;
}
case OP_ADD: {
val += *(float*) code;
code += 4;
break;
}
case OP_RAND: {
val += gu->usRandFloat() * (*(float*) code);
code += 4;
break;
}
case OP_DAMAGE: {
val += damage * (*(float*) code);
code += 4;
break;
}
case OP_INDEX: {
val += spawnIndex * (*(float*) code);
code += 4;
break;
}
case OP_LOADP: {
ptr = *(void**) code;
code += sizeof(void*);
break;
}
case OP_STOREP: {
boost::uint16_t offset = *(boost::uint16_t*) code;
code += 2;
*(void**) (instance + offset) = ptr;
ptr = NULL;
break;
}
case OP_DIR: {
boost::uint16_t offset = *(boost::uint16_t*) code;
code += 2;
*(float3*) (instance + offset) = dir;
break;
}
case OP_SAWTOOTH: {
// this translates to modulo except it works with floats
val -= (*(float*) code) * floor(val / (*(float*) code));
code += 4;
break;
}
case OP_DISCRETE: {
val = (*(float*) code) * floor(val / (*(float*) code));
code += 4;
break;
}
case OP_SINE: {
val = (*(float*) code) * sin(val);
code += 4;
break;
}
case OP_YANK: {
buffer[(*(int*) code)] = val;
val = 0;
code += 4;
break;
}
case OP_MULTIPLY: {
val *= buffer[(*(int*) code)];
code += 4;
break;
}
case OP_ADDBUFF: {
val += buffer[(*(int*) code)];
code += 4;
break;
}
case OP_POW: {
val = pow(val, (*(float*) code));
code += 4;
break;
}
case OP_POWBUFF: {
val = pow(val, buffer[(*(int*) code)]);
code += 4;
break;
}
default: {
assert(false);
break;
}
}
}
}
void CCustomExplosionGenerator::ParseExplosionCode(
CCustomExplosionGenerator::ProjectileSpawnInfo* psi,
int offset, boost::shared_ptr<creg::IType> type, const string& script, string& code)
{
string::size_type end = script.find(';', 0);
string vastr = script.substr(0, end);
if (vastr == "dir") {//first see if we can match any keywords
//if the user uses a keyword assume he knows that it is put on the right datatype for now
code += OP_DIR;
boost::uint16_t ofs = offset;
code.append((char*) &ofs, (char*) &ofs + 2);
}
else if (dynamic_cast<creg::BasicType*>(type.get())) {
creg::BasicType *bt = (creg::BasicType*)type.get();
if (bt->id != creg::crInt && bt->id != creg::crFloat && bt->id != creg::crUChar && bt->id != creg::crBool) {
throw content_error("Projectile properties other than int, float and uchar, are not supported (" + script + ")");
return;
}
int p = 0;
while (p < script.length()) {
char opcode;
char c;
do { c = script[p++]; } while(c == ' ');
bool useInt=false;
if (c == 'i') opcode = OP_INDEX;
else if (c == 'r') opcode = OP_RAND;
else if (c == 'd') opcode = OP_DAMAGE;
else if (c == 'm') opcode = OP_SAWTOOTH;
else if (c == 'k') opcode = OP_DISCRETE;
else if (c == 's') opcode = OP_SINE;
else if (c == 'y') {opcode = OP_YANK; useInt=true;}
else if (c == 'x') {opcode = OP_MULTIPLY; useInt=true;}
else if (c == 'a') {opcode = OP_ADDBUFF; useInt=true;}
else if (c == 'p') opcode = OP_POW;
else if (c == 'q') {opcode = OP_POWBUFF; useInt=true;}
else if (isdigit(c) || c == '.' || c == '-') { opcode = OP_ADD; p--; }
else throw content_error("Explosion script error: \"" + script + "\" : \'" + string(1, c) + "\' is unknown opcode.");
char* endp;
if(!useInt) {
float v = (float)strtod(&script[p], &endp);
p += endp - &script[p];
code += opcode;
code.append((char*) &v, ((char*) &v) + 4);
}
else {
int v = (int)strtol(&script[p], &endp, 10);
if (v < 0 || v > 16) throw content_error("Explosion script error: \"" + script + "\" : Buffer index is out of bounds.");
p += endp - &script[p];
code += opcode;
code.append((char*) &v, ((char*) &v) + 4);
}
}
switch (bt->id) {
case creg::crInt: code.push_back(OP_STOREI); break;
case creg::crBool: code.push_back(OP_STOREI); break;
case creg::crFloat: code.push_back(OP_STOREF); break;
case creg::crUChar: code.push_back(OP_STOREC); break;
default:
throw content_error("Explosion script variable is of unsupported type. "
"Contact the Spring team to fix this.");
break;
}
boost::uint16_t ofs = offset;
code.append((char*)&ofs, (char*)&ofs + 2);
}
else if (dynamic_cast<creg::ObjectInstanceType*>(type.get())) {
creg::ObjectInstanceType *oit = (creg::ObjectInstanceType *)type.get();
string::size_type start = 0;
for (creg::Class* c = oit->objectClass; c; c=c->base) {
for (int a=0;a<c->members.size();a++) {
string::size_type end = script.find(',', start+1);
ParseExplosionCode(psi, offset + c->members [a]->offset, c->members[a]->type, script.substr(start,end-start), code);
start = end+1;
if (start >= script.length()) break;
}
if (start >= script.length()) break;
}
}
else if (dynamic_cast<creg::StaticArrayBaseType*>(type.get())) {
creg::StaticArrayBaseType *sat = (creg::StaticArrayBaseType*)type.get();
string::size_type start = 0;
for (unsigned int i=0; i < sat->size; i++) {
string::size_type end = script.find(',', start+1);
ParseExplosionCode(psi, offset + sat->elemSize * i, sat->elemType, script.substr(start,end-start), code);
start = end+1;
if (start >= script.length()) break;
}
}
else {
if (type->GetName() == "AtlasedTexture*") {
string::size_type end = script.find(';', 0);
string texname = script.substr(0, end);
void* tex = ph->textureAtlas->GetTexturePtr(texname);
code += OP_LOADP;
code.append((char*)(&tex), ((char*)(&tex)) + sizeof(void*));
code += OP_STOREP;
boost::uint16_t ofs = offset;
code.append((char*)&ofs, (char*)&ofs + 2);
}
else if (type->GetName() == "GroundFXTexture*") {
string::size_type end = script.find(';', 0);
string texname = script.substr(0, end);
void* tex = ph->groundFXAtlas->GetTexturePtr(texname);
code += OP_LOADP;
code.append((char*)(&tex), ((char*)(&tex)) + sizeof(void*));
code += OP_STOREP;
boost::uint16_t ofs = offset;
code.append((char*)&ofs, (char*)&ofs + 2);
}
else if (type->GetName() == "CColorMap*") {
string::size_type end = script.find(';', 0);
string colorstring = script.substr(0, end);
void* colormap = CColorMap::LoadFromDefString(colorstring);
code += OP_LOADP;
code.append((char*)(&colormap), ((char*)(&colormap)) + sizeof(void*));
code += OP_STOREP;
boost::uint16_t ofs = offset;
code.append((char*)&ofs, (char*)&ofs + 2);
}
else if (type->GetName() == "CExplosionGenerator*") {
string::size_type end = script.find(';', 0);
string name = script.substr(0, end);
void* explgen = explGenHandler->LoadGenerator(name);
code += OP_LOADP;
code.append((char*)(&explgen), ((char*)(&explgen)) + sizeof(void*));
code += OP_STOREP;
boost::uint16_t ofs = offset;
code.append((char*)&ofs, (char*)&ofs + 2);
}
}
}
static unsigned int GetFlagsFromTable(const LuaTable& table)
{
unsigned int flags = 0;
if (table.GetBool("ground", false)) { flags |= SPW_GROUND; }
if (table.GetBool("water", false)) { flags |= SPW_WATER; }
if (table.GetBool("air", false)) { flags |= SPW_AIR; }
if (table.GetBool("underwater", false)) { flags |= SPW_UNDERWATER; }
if (table.GetBool("unit", false)) { flags |= SPW_UNIT; }
if (table.GetBool("nounit", false)) { flags |= SPW_NO_UNIT; }
return flags;
}
void CCustomExplosionGenerator::Load(CExplosionGeneratorHandler* h, const string& tag)
{
typedef std::map<string, CEGData> CEGMap;
typedef std::map<string, CEGData>::iterator CEGMapIt;
CEGMapIt it = cachedCEGs.find(tag);
if (it == cachedCEGs.end()) {
CEGData cegData;
const LuaTable& root = h->GetTable();
const LuaTable expTable = root.SubTable(tag);
if (!expTable.IsValid()) {
throw content_error("Explosion info for " + tag + " not found.");
}
vector<string> spawns;
expTable.GetKeys(spawns);
for (vector<string>::iterator si = spawns.begin(); si != spawns.end(); ++si) {
ProjectileSpawnInfo psi;
const string& spawnName = *si;
const LuaTable spawnTable = expTable.SubTable(spawnName);
if (!spawnTable.IsValid() || spawnName == "groundflash") {
continue;
}
const string className = spawnTable.GetString("class", spawnName);
psi.projectileClass = h->projectileClasses.GetClass(className);
psi.flags = GetFlagsFromTable(spawnTable);
psi.count = spawnTable.GetInt("count", 1);
if (psi.projectileClass->binder->flags & creg::CF_Synced) {
psi.flags |= SPW_SYNCED;
}
string code;
map<string, string> props;
map<string, string>::const_iterator propIt;
spawnTable.SubTable("properties").GetMap(props);
for (propIt = props.begin(); propIt != props.end(); ++propIt) {
creg::Class::Member* m = psi.projectileClass->FindMember(propIt->first.c_str());
if (m && (m->flags & creg::CM_Config)) {
ParseExplosionCode(&psi, m->offset, m->type, propIt->second, code);
}
}
code += (char)OP_END;
psi.code.resize(code.size());
copy(code.begin(), code.end(), psi.code.begin());
cegData.projectileSpawn.push_back(psi);
}
const LuaTable gndTable = expTable.SubTable("groundflash");
const int ttl = gndTable.GetInt("ttl", 0);
if (ttl > 0) {
cegData.groundFlash.circleAlpha = gndTable.GetFloat("circleAlpha", 0.0f);
cegData.groundFlash.flashSize = gndTable.GetFloat("flashSize", 0.0f);
cegData.groundFlash.flashAlpha = gndTable.GetFloat("flashAlpha", 0.0f);
cegData.groundFlash.circleGrowth = gndTable.GetFloat("circleGrowth", 0.0f);
cegData.groundFlash.color = gndTable.GetFloat3("color", float3(1.0f, 1.0f, 0.8f));
cegData.groundFlash.flags = SPW_GROUND | GetFlagsFromTable(gndTable);
cegData.groundFlash.ttl = ttl;
}
cegData.useDefaultExplosions = expTable.GetBool("useDefaultExplosions", false);
it = cachedCEGs.insert(std::make_pair(tag, cegData)).first;
}
currentCEG = it;
}
void CCustomExplosionGenerator::Explosion(const float3& pos, float damage, float radius, CUnit* owner, float gfxMod, CUnit* hit, const float3& dir)
{
if (currentCEG == cachedCEGs.end()) {
// Explosion() called before Load()'ing a CEG (after
// constructing a CCustomExplosionGenerator object)
return;
}
float h2 = ground->GetHeight2(pos.x, pos.z);
unsigned int flags = 0;
if (pos.y - max(0.0f, h2) > 20) flags = SPW_AIR;
else if (h2 < -3) flags = SPW_WATER;
else if (pos.y < -15) flags = SPW_UNDERWATER;
else flags = SPW_GROUND;
if (hit) flags |= SPW_UNIT;
else flags |= SPW_NO_UNIT;
for (int a = 0; a < (currentCEG->second).projectileSpawn.size(); a++) {
ProjectileSpawnInfo& psi = (currentCEG->second).projectileSpawn[a];
if (!(psi.flags & flags)) {
continue;
}
// If we're saturated, spawn only synced projectiles.
// Whether a class is synced is determined by the creg::CF_Synced flag.
if (ph->particleSaturation > 1 && !(psi.flags & SPW_SYNCED)) {
continue;
}
for (int c = 0; c < psi.count; c++) {
CExpGenSpawnable* projectile = (CExpGenSpawnable*) (psi.projectileClass)->CreateInstance();
ExecuteExplosionCode(&psi.code[0], damage, (char*) projectile, c, dir);
projectile->Init(pos, owner);
}
}
const GroundFlashInfo& groundFlash = (currentCEG->second).groundFlash;
if ((flags & SPW_GROUND) && groundFlash.ttl > 0) {
new CStandardGroundFlash(pos, groundFlash.circleAlpha, groundFlash.flashAlpha,
groundFlash.flashSize, groundFlash.circleGrowth, groundFlash.ttl, groundFlash.color);
}
if ((currentCEG->second).useDefaultExplosions) {
CStdExplosionGenerator::Explosion(pos, damage, radius, owner, gfxMod, hit, dir);
}
}
void CCustomExplosionGenerator::OutputProjectileClassInfo()
{
const vector<creg::Class*>& classes = creg::System::GetClasses();
std::ofstream fs("projectiles.txt");
CExplosionGeneratorHandler egh;
if (fs.bad() || !fs.is_open()) {
return;
}
for (vector<creg::Class*>::const_iterator ci = classes.begin(); ci != classes.end(); ++ci) {
if (!(*ci)->IsSubclassOf (CExpGenSpawnable::StaticClass()) || (*ci) == CExpGenSpawnable::StaticClass()) {
continue;
}
creg::Class *klass = *ci;
fs << "Class: " << klass->name << ". Scriptname: " << egh.projectileClasses.FindAlias(klass->name) << std::endl;
for (; klass; klass = klass->base) {
for (unsigned int a = 0; a < klass->members.size(); a++) {
if (klass->members[a]->flags & creg::CM_Config) {
fs << "\t" << klass->members[a]->name << ": " << klass->members[a]->type->GetName() << "\n";
}
}
}
fs << "\n\n";
}
}
|