Chit* LumosChitBag::NewMonsterChit(const Vector3F& pos, const char* name, int team) { const ChitContext* context = Context(); Chit* chit = NewChit(); AddItem( name, chit, context->engine, team, 0 ); chit->Add( new RenderComponent( chit->GetItem()->ResourceName() )); chit->Add( new PathMoveComponent()); chit->Add( new AIComponent()); chit->SetPosition( pos ); chit->Add( new HealthComponent()); IString mob = chit->GetItem()->keyValues.GetIString(ISC::mob); if (ReserveBank::Instance()) { ReserveBank::Instance()->WithdrawMonster(chit->GetWallet(), mob == ISC::greater); } if (mob == ISC::greater) { // Mark this item as important with a destroyMsg: chit->GetItem()->SetSignificant(GetNewsHistory(), ToWorld2F(pos), NewsEvent::GREATER_MOB_CREATED, NewsEvent::GREATER_MOB_KILLED, 0); } chit->GetItem()->GetTraitsMutable()->Roll(chit->ID()); chit->GetItem()->FullHeal(); if (XenoAudio::Instance()) { XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos); } return chit; }
int CoreScript::Citizens(CChitArray* arr) { int i=0; while (i < citizens.Size()) { int id = citizens[i]; Chit* chit = CitizenFilter(id); if (chit) { // check for team change, throw out of citizens. if (arr) arr->Push(chit); ++i; } else { // Dead and gone. citizens.SwapRemove(i); // Reset the timer so that there is a little time // between a dead citizen and re-spawn spawnTick.Reset(); } } #if 0 // This is annoying: seeing if cranking down the spawn rate and not // destroying the sleep tube achieves success. // Also, destroy a sleeptube, so it costs something to replace, and towns can fall. if ( sc ) { Vector2F pos2 = sc->GetPosition2D(); Vector2I sector = ToSector( ToWorld2I( pos2 )); Chit* bed = scriptContext->chitBag->FindBuilding( ISC::bed, sector, &pos2, LumosChitBag::RANDOM_NEAR, 0, 0 ); if ( bed && bed->GetItem() ) { bed->GetItem()->hp = 0; bed->SetTickNeeded(); } } #endif return citizens.Size(); }
void CoreScript::AddFlag(const Vector2I& _pos) { Vector2I pos = _pos; // A little UI fixup: set the flag to a porch // if we click on the switch. Chit* building = Context()->chitBag->QueryBuilding(IString(), pos, 0); if (building && (building->GetItem()->IName() == ISC::switchOn || building->GetItem()->IName() == ISC::switchOff)) { MapSpatialComponent* msc = GET_SUB_COMPONENT(building, SpatialComponent, MapSpatialComponent); if (msc) { pos = msc->PorchPos().min; } } Flag f = { pos, 0 }; if (flags.Find(f) < 0) { Chit* chit = Context()->chitBag->NewChit(); RenderComponent* rc = new RenderComponent("flag"); chit->Add(rc); ProcRenderInfo info; AssignProcedural(ISC::team, false, ParentChit()->GetItem()->ID(), ParentChit()->Team(), false, 0, 0, &info); rc->SetProcedural(0, info); chit->SetPosition(ToWorld3F(pos)); f.chitID = chit->ID(); flags.Push(f); } }
void CoreScript::OnChitMsg(Chit* chit, const ChitMsg& msg) { // Logic split between Sim::OnChitMsg and CoreScript::OnChitMsg if (msg.ID() == ChitMsg::CHIT_DESTROYED && (chit == parentChit)) { while (!citizens.Empty()) { int citizenID = citizens.Pop(); Chit* citizen = Context()->chitBag->GetChit(citizenID); if (citizen && citizen->GetItem()) { // Set to rogue team. citizen->GetItem()->SetRogue(); } } Vector2I pos2i = ToWorld2I(chit->Position()); Vector2I sector = ToSector(pos2i); if (Team::Instance()->IsController(chit->Team())) { NewsEvent news(NewsEvent::SUPERTEAM_DELETED, ToWorld2F(pos2i), chit->GetItemID(), 0); Context()->chitBag->GetNewsHistory()->Add(news); } int controllerTeam = 0; if (Team::Instance()->IsControlled(chit->Team(), &controllerTeam)) { CoreScript* controller = CoreScript::GetCoreFromTeam(controllerTeam); GLASSERT(controller); if (controller) { NewsEvent news(NewsEvent::SUBTEAM_DELETED, ToWorld2F(pos2i), chit->GetItemID(), controller->ParentChit()->GetItemID()); Context()->chitBag->GetNewsHistory()->Add(news); } } int deleterID = chit->GetItemComponent() ? chit->GetItemComponent()->LastDamageID() : 0; Chit* deleter = Context()->chitBag->GetChit(deleterID); int superTeam = 0; if (deleter && (deleter->Team() == Team::Instance()->SuperTeam(deleter->Team())) && Team::IsDenizen(deleter->Team()) && Team::IsDenizen(chit->Team())) { superTeam = deleter->Team(); } if (chit->Team() != TEAM_NEUTRAL) { if (superTeam) { LumosChitBag::CreateCoreData data = { sector, true, chit->Team(), deleter ? deleter->Team() : 0 }; Context()->chitBag->coreCreateList.Push(data); } else { LumosChitBag::CreateCoreData data = { sector, false, chit->Team(), deleter ? deleter->Team() : 0 }; Context()->chitBag->coreCreateList.Push(data); } } else { // Neutral cores are taken over by wandering over them // with enough friend units to have critical mass. LumosChitBag::CreateCoreData data = { sector, false, 0, 0 }; Context()->chitBag->coreCreateList.Push(data); } Team::Instance()->CoreDestroyed(parentChit->Team()); } }
bool CoreScript::RecruitNeutral() { Vector2I sector = ToSector(parentChit->Position()); Rectangle2I inner = InnerSectorBounds(sector); MOBKeyFilter filter; filter.value = ISC::denizen; CChitArray arr; Context()->chitBag->QuerySpatialHash(&arr, ToWorld2F(inner), 0, &filter); for (int i = 0; i < arr.Size(); ++i) { Chit* chit = arr[i]; if (Team::Instance()->GetRelationship(chit, parentChit) != ERelate::ENEMY) { if (this->IsCitizen(chit)) continue; if (!chit->GetItem()) continue; if (Team::IsRogue(chit->Team())) { // ronin! denizen without a core. this->AddCitizen( chit ); GLASSERT(chit->GetItem()->Significant()); NewsEvent news(NewsEvent::ROGUE_DENIZEN_JOINS_TEAM, ToWorld2F(chit->Position()), chit->GetItemID(), 0); Context()->chitBag->GetNewsHistory()->Add(news); return true; } } } return false; }
Chit* CoreScript::PrimeCitizen() { for (int i = 0; i < citizens.Size(); ++i) { int id = citizens[i]; Chit* chit = CitizenFilter(id); if (chit && chit->GetItem()) { if (chit->GetItem()->keyValues.Has(ISC::prime)) { return chit; } } } return 0; }
Chit* LumosChitBag::FindBuilding( const grinliz::IString& name, const grinliz::Vector2I& sector, const grinliz::Vector2F* pos, LumosChitBag::EFindMode flags, CDynArray<Chit*>* arr, IChitAccept* filter ) { CDynArray<Chit*>& match = arr ? *arr : findMatch; // sleazy reference trick to point to either passed in or local. match.Clear(); findWeight.Clear(); for( MapSpatialComponent* it = mapSpatialHash[sector.y*NUM_SECTORS+sector.x]; it; it = it->nextBuilding ) { Chit* chit = it->ParentChit(); GLASSERT( chit ); if ( filter && !filter->Accept( chit )) { // if a filter, check it. continue; } const GameItem* item = chit->GetItem(); if ( item && ( name.empty() || item->IName() == name )) { // name, if empty, matches everything match.Push( chit ); } } // If we found nothing, or we don't care about the position, return early. // Else deal with choice / sorting / etc. below. if ( match.Empty() ) return 0; if ( !pos ) return match[0]; // NEAREST scans and finds the closest one. // RANDOM_NEAR chooses one at random, but weighted by the (inverse) of the distance if ( flags == EFindMode::NEAREST ) { float closest = ( ToWorld2F(match[0]->Position()) - *pos ).LengthSquared(); int ci = 0; for( int i=1; i<match.Size(); ++i ) { float len2 = ( ToWorld2F(match[i]->Position()) - *pos ).LengthSquared(); if ( len2 < closest ) { closest = len2; ci = i; } } return match[ci]; } if ( flags == EFindMode::RANDOM_NEAR ) { for( int i=0; i<match.Size(); ++i ) { float len = ( ToWorld2F(match[i]->Position()) - *pos ).Length(); if (len < 1) len = 1; findWeight.Push( 1.0f/len ); } int index = random.Select( findWeight.Mem(), findWeight.Size() ); return match[index]; } // Bad flag? Something didn't return? GLASSERT( 0 ); return 0; }
Chit* LumosChitBag::NewGoldChit( const grinliz::Vector3F& pos, Wallet* src ) { GLASSERT(src); if (src->Gold() == 0) return 0; const ChitContext* context = Context(); Vector2F v2 = { pos.x, pos.z }; ItemNameFilter goldFilter( ISC::gold); this->QuerySpatialHash( &chitList, v2, 1.0f, 0, &goldFilter ); Chit* chit = 0; // Evil bug this: adding gold to a wallet just before // deletion. I'm a little concerned where else // this could be a problem. Would be nice to make // deletion immediate. for (int i = 0; i < chitList.Size(); ++i) { Chit* c = chitList[i]; if (c && !IsQueuedForDelete(c) && c->GetWallet() && !c->GetWallet()->Closed()) { chit = chitList[i]; break; } } if ( !chit ) { chit = this->NewChit(); AddItem( "gold", chit, context->engine, 0, 0 ); chit->Add( new RenderComponent( chit->GetItem()->ResourceName() )); chit->SetPosition( pos ); chit->Add(new GameMoveComponent()); } chit->GetWallet()->Deposit(src, src->Gold()); return chit; }
Chit* LumosChitBag::NewBadGuy(const grinliz::Vector2I& pos, const IString& name, const grinliz::IString& type, int team, int level ) { const ChitContext* context = Context(); Chit* chit = NewChit(); const GameItem& root = ItemDefDB::Instance()->Get(type.safe_str()); chit->Add( new RenderComponent(root.ResourceName())); chit->Add( new PathMoveComponent()); AddItem(root.Name(), chit, context->engine, team, level, 0, "human"); ReserveBank::Instance()->WithdrawMonster(chit->GetWallet(), true); chit->GetItem()->GetTraitsMutable()->Roll( random.Rand() ); chit->GetItem()->GetPersonalityMutable()->Roll( random.Rand(), &chit->GetItem()->Traits() ); chit->GetItem()->FullHeal(); chit->GetItem()->SetProperName(name); AIComponent* ai = new AIComponent(); chit->Add( ai ); chit->Add( new HealthComponent()); chit->SetPosition( (float)pos.x+0.5f, 0, (float)pos.y+0.5f ); for (int i = 0; i < ForgeScript::NUM_ITEM_TYPES; ++i) { ForgeScript::ForgeData forgeData; forgeData.type = i; forgeData.subType = 0; forgeData.tech = 3; forgeData.level = level; forgeData.team = team; int seed = random.Rand(); ForgeScript::BestSubItem(&forgeData, seed); TransactAmt cost; TransactAmt freeCreate; static const int CRYSTAL[NUM_CRYSTAL_TYPES] = { 3, 2, 1, 1 }; freeCreate.Set(0, CRYSTAL); GameItem* loot = ForgeScript::ForgeRandomItem(forgeData, freeCreate, &cost, seed, ReserveBank::GetWallet()); if (loot) { chit->GetItemComponent()->AddToInventory(loot); loot->SetSignificant(chit->Context()->chitBag->GetNewsHistory(), ToWorld2F(chit->Position()), NewsEvent::FORGED, NewsEvent::UN_FORGED, chit->GetItem()); } } chit->GetItem()->SetSignificant(GetNewsHistory(), ToWorld2F(pos), NewsEvent::DENIZEN_CREATED, NewsEvent::DENIZEN_KILLED, 0); if (XenoAudio::Instance()) { Vector3F pos3 = ToWorld3F(pos); XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos3); } return chit; }
Chit* LumosChitBag::QueryBuilding( const IString& name, const grinliz::Rectangle2I& bounds, CChitArray* arr ) { GLASSERT( MAX_BUILDING_SIZE == 2 ); // else adjust logic Vector2I sector = ToSector( bounds.min ); for( MapSpatialComponent* it = mapSpatialHash[SectorIndex(sector)]; it; it = it->nextBuilding ) { if ( it->Bounds().Intersect( bounds )) { Chit* chit = it->ParentChit(); if (name.empty() || (chit->GetItem() && chit->GetItem()->IName() == name)) { if (!arr) { return chit; } if (arr->HasCap()) { arr->Push(chit); } } } } if (arr && !arr->Empty()) { return (*arr)[0]; } return 0; }
Chit* LumosChitBag::NewDenizen( const grinliz::Vector2I& pos, int team ) { const ChitContext* context = Context(); IString itemName; switch (Team::Group(team)) { case TEAM_HOUSE: itemName = (random.Bit()) ? ISC::humanFemale : ISC::humanMale; break; case TEAM_GOB: itemName = ISC::gobman; break; case TEAM_KAMAKIRI: itemName = ISC::kamakiri; break; default: GLASSERT(0); break; } Chit* chit = NewChit(); const GameItem& root = ItemDefDB::Instance()->Get(itemName.safe_str()); chit->Add( new RenderComponent(root.ResourceName())); chit->Add( new PathMoveComponent()); const char* altName = 0; if (Team::Group(team) == TEAM_HOUSE) { altName = "human"; } AddItem(root.Name(), chit, context->engine, team, 0, 0, altName); ReserveBank::Instance()->WithdrawDenizen(chit->GetWallet()); chit->GetItem()->GetTraitsMutable()->Roll( random.Rand() ); chit->GetItem()->GetPersonalityMutable()->Roll( random.Rand(), &chit->GetItem()->Traits() ); chit->GetItem()->FullHeal(); IString nameGen = chit->GetItem()->keyValues.GetIString( "nameGen" ); if ( !nameGen.empty() ) { LumosChitBag* chitBag = chit->Context()->chitBag; if ( chitBag ) { chit->GetItem()->SetProperName(chitBag->NameGen(nameGen.c_str(), chit->ID())); } } AIComponent* ai = new AIComponent(); chit->Add( ai ); chit->Add( new HealthComponent()); chit->SetPosition( (float)pos.x+0.5f, 0, (float)pos.y+0.5f ); chit->GetItem()->SetSignificant(GetNewsHistory(), ToWorld2F(pos), NewsEvent::DENIZEN_CREATED, NewsEvent::DENIZEN_KILLED, 0); if (XenoAudio::Instance()) { Vector3F pos3 = ToWorld3F(pos); XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos3); } return chit; }
Chit* LumosChitBag::NewLawnOrnament(const Vector2I& pos, const char* name, int team) { const ChitContext* context = Context(); Chit* chit = NewChit(); GameItem* rootItem = ItemDefDB::Instance()->Get(name).Clone(); // Hack...how to do this better?? if (rootItem->IResourceName() == "ruins1.0") { CStr<32> str; str.Format("ruins1.%d", random.Rand(2)); rootItem->SetResource(str.c_str()); } int size = 1; rootItem->keyValues.Get(ISC::size, &size); MapSpatialComponent* msc = new MapSpatialComponent(); msc->SetBuilding(size, false, 0); msc->SetBlocks((rootItem->flags & GameItem::PATH_NON_BLOCKING) ? false : true); chit->Add(msc); MapSpatialComponent::SetMapPosition(chit, pos.x, pos.y); chit->Add(new RenderComponent(rootItem->ResourceName())); chit->Add(new HealthComponent()); AddItem(rootItem, chit, context->engine, team, 0); IString proc = rootItem->keyValues.GetIString("procedural"); if (!proc.empty()) { ProcRenderInfo info; AssignProcedural(chit->GetItem(), &info); chit->GetRenderComponent()->SetProcedural(0, info); } context->engine->particleSystem->EmitPD(ISC::constructiondone, ToWorld3F(pos), V3F_UP, 0); if (XenoAudio::Instance()) { Vector3F pos3 = ToWorld3F(pos); XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos3); } return chit; }
Chit* LumosChitBag::NewCrystalChit( const grinliz::Vector3F& _pos, Wallet* src, bool fuzz ) { Vector3F pos = _pos; if ( fuzz ) { pos.x = floorf(_pos.x) + random.Uniform(); pos.z = floorf(_pos.z) + random.Uniform(); } int crystal = -1; for (int i = 0; i < NUM_CRYSTAL_TYPES; ++i) { if (src->Crystal(i)) { crystal = i; break; } } if (crystal == -1) return 0; // done const char* name = 0; switch ( crystal ) { case CRYSTAL_GREEN: name="crystal_green"; break; case CRYSTAL_RED: name="crystal_red"; break; case CRYSTAL_BLUE: name="crystal_blue"; break; case CRYSTAL_VIOLET: name="crystal_violet"; break; } const ChitContext* context = Context(); Chit* chit = this->NewChit(); AddItem( name, chit, context->engine, 0, 0 ); chit->Add( new RenderComponent( chit->GetItem()->ResourceName() )); chit->Add(new GameMoveComponent()); chit->SetPosition( pos ); int c[NUM_CRYSTAL_TYPES] = { 0 }; c[crystal] = 1; chit->GetWallet()->Deposit(src, 0, c); return chit; }
void LumosChitBag::BuildingCounts(const Vector2I& sector, int* counts, int n) { BuildScript buildScript; for( MapSpatialComponent* it = mapSpatialHash[sector.y*NUM_SECTORS+sector.x]; it; it = it->nextBuilding ) { Chit* chit = it->ParentChit(); GLASSERT( chit ); const GameItem* item = chit->GetItem(); if (!item) continue; const IString& name = item->IName(); int id = 0; if (!name.empty()) { buildScript.GetDataFromStructure(name, &id); if (id < n) { counts[id] += 1; } } } }
Chit* LumosChitBag::NewBuilding(const Vector2I& pos, const char* name, int team) { const ChitContext* context = Context(); Chit* chit = NewChit(); const GameItem& rootItem = ItemDefDB::Instance()->Get(name); GameItem* item = rootItem.Clone(); // Hack...how to do this better?? if (item->IResourceName() == "pyramid0") { CStr<32> str; str.Format("pyramid%d", random.Rand(3)); item->SetResource(str.c_str()); } if (item->IResourceName() == ISC::kiosk) { switch (random.Rand(4)) { case 0: item->SetResource("kiosk.n"); break; case 1: item->SetResource("kiosk.m"); break; case 2: item->SetResource("kiosk.s"); break; default:item->SetResource("kiosk.c"); break; } } int size = 1; rootItem.keyValues.Get(ISC::size, &size); int porch = 0; rootItem.keyValues.Get(ISC::porch, &porch); const int circuit = 0; // Should be pre-cleared. But recover from weird situations by clearing. // Note that water is a real problem. Rectangle2I r; r.Set(pos.x, pos.y, pos.x + size - 1, pos.y + size - 1); for (Rectangle2IIterator it(r); !it.Done(); it.Next()) { const WorldGrid& wg = context->worldMap->GetWorldGrid(it.Pos()); GLASSERT(wg.IsLand()); (void)wg; context->worldMap->SetRock(it.Pos().x, it.Pos().y, 0, false, 0); context->worldMap->SetPlant(it.Pos().x, it.Pos().y, 0, 0); } MapSpatialComponent* msc = new MapSpatialComponent(); msc->SetBuilding(size, porch != 0, circuit); msc->SetBlocks((rootItem.flags & GameItem::PATH_NON_BLOCKING) ? false : true); chit->Add(msc); MapSpatialComponent::SetMapPosition(chit, pos.x, pos.y); chit->Add(new RenderComponent(item->ResourceName())); chit->Add(new HealthComponent()); AddItem(item, chit, context->engine, team, 0); IString script = rootItem.keyValues.GetIString("script"); if (!script.empty()) { Component* s = ComponentFactory::Factory(script.c_str(), &chitContext); GLASSERT(s); chit->Add(s); } IString proc = rootItem.keyValues.GetIString("procedural"); if (!proc.empty()) { ProcRenderInfo info; AssignProcedural(chit->GetItem(), &info); chit->GetRenderComponent()->SetProcedural(0, info); } IString consumes = rootItem.keyValues.GetIString(ISC::zone); if (!consumes.empty()) { Component* s = ComponentFactory::Factory("EvalBuildingScript", &chitContext); GLASSERT(s); chit->Add(s); } IString nameGen = rootItem.keyValues.GetIString( "nameGen"); if ( !nameGen.empty() ) { IString p = Context()->chitBag->NameGen(nameGen.c_str(), chit->random.Rand()); chit->GetItem()->SetProperName( p ); } #if 0 // debugging SectorPort sp; sp.sector.Set( pos.x/SECTOR_SIZE, pos.y/SECTOR_SIZE ); sp.port = 1; worldMap->SetRandomPort( sp ); #endif context->engine->particleSystem->EmitPD( ISC::constructiondone, ToWorld3F( pos ), V3F_UP, 0 ); if (XenoAudio::Instance()) { Vector3F pos3 = ToWorld3F(pos); XenoAudio::Instance()->PlayVariation(ISC::rezWAV, random.Rand(), &pos3); } return chit; }
void CoreScript::DoStrategicTick() { // Look around for someone to attack. They should be: // - someone we have a negative attitude about (accounts for lots of things) // - weaker overall // // We should: // - have a reasonably armed squad bool squadReady[MAX_SQUADS] = { false }; CChitArray squaddies; // Check for ready squads. for (int i = 0; i < MAX_SQUADS; ++i) { this->Squaddies(i, &squaddies); if (squaddies.Size() < SQUAD_SIZE) continue; if (!SquadAtRest(i)) continue; // Do we have enough guns? Seems to be // the best / easiest metric. int nGuns = 0; for (int k = 0; k < squaddies.Size(); ++k) { Chit* chit = squaddies[k]; ItemComponent* ic = chit->GetItemComponent(); if ( chit->GetItem() && chit->GetItem()->HPFraction() > 0.75f && ic && ic->QuerySelectRanged()) { nGuns++; } } squadReady[i] = nGuns >= SQUAD_SIZE - 1; } int nReady = 0; for (int i = 0; i < MAX_SQUADS; ++i) { if (squadReady[i]) ++nReady; } Sim* sim = Context()->chitBag->GetSim(); GLASSERT(sim); if (!sim) return; Vector2I sector = ToSector(ParentChit()->Position()); CCoreArray stateArr; sim->CalcStrategicRelationships(sector, 3, &stateArr); // The strategic relationships need to be calculated, but after that, // there's no point in computing further if we don't have a squad to // send into action. if (nReady == 0) return; int myPower = this->CorePower(); CoreScript* target = 0; for (int i = 0; i < stateArr.Size(); ++i) { CoreScript* cs = stateArr[i]; if (cs->NumTemples() == 0) // Ignore starting out domains so it isn't a complete wasteland out there. continue; if ( Team::Instance()->GetRelationship(cs->ParentChit(), this->ParentChit()) == ERelate::ENEMY // Are we enemies at the diplomatic level && Team::Instance()->Attitude(this, cs) < 0) // Does 'this' have a negative attitude to the other? { int power = cs->CorePower(); if (power < myPower * 0.75f) { // Assuming this is actually so rare that it doesn't matter to select the best. target = cs; break; } } } if (!target) return; // Attack!!! bool first = true; Vector2F targetCorePos2 = ToWorld2F(target->ParentChit()->Position()); Vector2I targetCorePos = ToWorld2I(target->ParentChit()->Position());; Vector2I targetSector = ToSector(targetCorePos); for (int i = 0; i < MAX_SQUADS; ++i) { if (!squadReady[i]) continue; Vector2I pos = { 0, 0 }; pos = targetCorePos; if (first) { first = false; } else { BuildingWithPorchFilter filter; Chit* building = Context()->chitBag->FindBuilding(IString(), targetSector, &targetCorePos2, LumosChitBag::EFindMode::RANDOM_NEAR, 0, &filter); if (building) { pos = ToWorld2I(building->Position()); } } GLASSERT(!pos.IsZero()); this->SetWaypoints(i, pos); } }
CoreScript* CoreScript::CreateCore( const Vector2I& sector, int team, const ChitContext* context) { // Destroy the existing core. // Create a new core, attached to the player. CoreScript* cs = CoreScript::GetCore(sector); if (cs) { Chit* core = cs->ParentChit(); GLASSERT(core); CDynArray< Chit* > queryArr; // Tell all the AIs the core is going away. ChitHasAIComponent filter; Rectangle2F b = ToWorld2F(InnerSectorBounds(sector)); context->chitBag->QuerySpatialHash(&queryArr, b, 0, &filter); for (int i = 0; i < queryArr.Size(); ++i) { queryArr[i]->GetAIComponent()->ClearTaskList(); } //context.chitBag->QueueDelete(core); // QueueDelete is safer, but all kinds of asserts fire (correctly) // if 2 cores are in the same place. This may cause an issue // if CreateCore is called during the DoTick() // Setting the hp to 0 and then calling DoTick() // is a sleazy trick to clean up. core->GetItem()->hp = 0; if (core->GetHealthComponent()) { core->GetHealthComponent()->DoTick(1); } context->chitBag->DeleteChit(core); } const SectorData& sd = context->worldMap->GetSectorData(sector); if (sd.HasCore()) { int group = 0, id = 0; Team::SplitID(team, &group, &id); // Lots of trouble with this code. Used to assert, // but always seemed to be another case. White list // groups that *can* take over a core. if (team == TEAM_NEUTRAL || team == TEAM_TROLL || Team::IsDeity(team) || Team::IsDenizen(team)) { // Okay! take over. GLASSERT(!Team::IsDenizen(team) || id); // make sure rogues got filtered out. } else { team = group = id = 0; } Chit* chit = context->chitBag->NewBuilding(sd.core, "core", team); // 'in use' instead of blocking. MapSpatialComponent* ms = GET_SUB_COMPONENT(chit, SpatialComponent, MapSpatialComponent); GLASSERT(ms); ms->SetBlocks(false); CoreScript* cs = new CoreScript(); chit->Add(cs); GLASSERT(CoreScript::GetCore(ToSector(sd.core)) == cs); if (Team::IsDeity(team)) chit->GetItem()->SetProperName(Team::Instance()->TeamName(team)); else chit->GetItem()->SetProperName(sd.name); if (team != TEAM_NEUTRAL) { chit->GetItem()->SetSignificant(context->chitBag->GetNewsHistory(), ToWorld2F(chit->Position()), NewsEvent::DOMAIN_CREATED, NewsEvent::DOMAIN_DESTROYED, 0); // Make the dwellers defend the core. chit->Add(new GuardScript()); // Make all buildings to be this team. CDynArray<Chit*> buildings; Vector2I buildingSector = ToSector(chit->Position()); context->chitBag->FindBuilding(IString(), buildingSector, 0, LumosChitBag::EFindMode::NEAREST, &buildings, 0); for (int i = 0; i < buildings.Size(); ++i) { Chit* c = buildings[i]; if (c->GetItem() && c->GetItem()->IName() != ISC::core) { c->GetItem()->SetTeam(team); } } } return cs; } return 0; }
void FluidTestScene::Tap3D(const grinliz::Vector2F& view, const grinliz::Ray& world) { Vector3F at = { 0, 0, 0 }; float t = 0; int result = IntersectRayAAPlane(world.origin, world.direction, 1, 0, &at, &t); if (result == INTERSECT) { Vector2I pos2i = ToWorld2I(at); Vector2I sector = { 0, 0 }; CircuitSim* circuitSim = context.physicsSims->GetCircuitSim(sector); if (context.worldMap->Bounds().Contains(pos2i)) { bool trigger = false; if (!buildButton[BUTTON_DELETE].Down() && !buildButton[BUTTON_ROTATE].Down()) { Chit* building = context.chitBag->QueryPorch(pos2i); if (!building) { building = context.chitBag->QueryBuilding(IString(), pos2i, 0); } if (building) { if (building->GetItem()->IName() == ISC::detector) { circuitSim->TriggerDetector(pos2i); trigger = true; } else if (building->GetItem()->IName() == ISC::switchOn || building->GetItem()->IName() == ISC::switchOff) { circuitSim->TriggerSwitch(pos2i); trigger = true; } } } int id = -1; if (!trigger) { for (int i = 0; i < NUM_BUTTONS; ++i) { if (buildButton[i].Down()) { id = i; break; } } } if (id >= 0) { Chit* chit = 0; switch (id) { case BUTTON_ROCK0: case BUTTON_ROCK1: case BUTTON_ROCK2: case BUTTON_ROCK3: context.worldMap->SetRock(pos2i.x, pos2i.y, id - BUTTON_ROCK0, false, WorldGrid::ROCK); break; case BUTTON_SWITCH_ON: chit = context.chitBag->NewBuilding(pos2i, "switchOn", TEAM_HOUSE); break; case BUTTON_SWITCH_OFF: chit = context.chitBag->NewBuilding(pos2i, "switchOff", TEAM_HOUSE); break; case BUTTON_TEMPLE: chit = context.chitBag->NewBuilding(pos2i, "temple", TEAM_HOUSE); break; case BUTTON_GATE: chit = context.chitBag->NewBuilding(pos2i, "gate", TEAM_HOUSE); break; case BUTTON_TIMED_GATE: chit = context.chitBag->NewBuilding(pos2i, "timedGate", TEAM_HOUSE); break; case BUTTON_DETECTOR: chit = context.chitBag->NewBuilding(pos2i, "detector", TEAM_HOUSE); break; case BUTTON_TURRET: chit = context.chitBag->NewBuilding(pos2i, "turret", TEAM_HOUSE); break; case BUTTON_DELETE: { Chit* building = context.chitBag->QueryBuilding(IString(), pos2i, 0); if (building) { building->QueueDelete(); } } break; case BUTTON_ROTATE: { Chit* building = context.chitBag->QueryBuilding(IString(), pos2i, 0); if (building) { Matrix4 m; building->Rotation().ToMatrix(&m); float r = m.CalcRotationAroundAxis(1); r = NormalizeAngleDegrees(r + 90.0f); building->SetRotation(Quaternion::MakeYRotation(r)); } } break; } if (chit) { MapSpatialComponent* msc = GET_SUB_COMPONENT(chit, SpatialComponent, MapSpatialComponent); if (msc) msc->SetNeedsCorePower(false); } } } } }
double EvalBuildingScript::EvalIndustrial( bool debugLog ) { Chit* building = ParentChit(); int hitB = 0, hitIBuilding = 0, hitNBuilding = 0, hitWater = 0, hitPlant = 0, hitRock = 0, hitWaterfall = 0; int hitShrub = 0; // doesn't terminate a ray. if (lastEval == 0 || (Context()->chitBag->AbsTime() - lastEval) > 2000) { lastEval = Context()->chitBag->AbsTime(); GameItem* item = building->GetItem(); GLASSERT(item); reachable = true; IString consume = item->keyValues.GetIString(ISC::zone); if (consume.empty()) { eval = 0; return eval; } MapSpatialComponent* msc = GET_SUB_COMPONENT(building, SpatialComponent, MapSpatialComponent); GLASSERT(msc); if (!msc) { eval = 0; return eval; } Rectangle2I porch = msc->PorchPos(); static const int RAD = 4; Rectangle2I bounds = porch; if (porch.min.x == 0) { // shouldn't happen, but be sure. GLASSERT(0); eval = 0; return eval; } bounds.Outset(RAD); Vector2I sector = ToSector(building->Position()); WorldMap* worldMap = Context()->worldMap; Rectangle2I mapBounds = worldMap->Bounds(); if (!mapBounds.Contains(bounds)) { eval = 0; return eval; // not worth dealing with edge of world } // Check if we can go from the core to the porch. // And make sure the core is inUse! CoreScript* cs = CoreScript::GetCore(ToSector(porch.min)); if (!cs || !cs->InUse()) reachable = false; if (reachable) { const SectorData& sd = worldMap->GetSectorData(ToSector(porch.min)); reachable = worldMap->CalcPath(ToWorld2F(sd.core), ToWorld2F(porch.min), 0, 0, false); } CChitArray arr; BuildingFilter buildingFilter; const FluidSim* fluidSim = Context()->physicsSims->GetFluidSim(sector); bool hasWaterfalls = fluidSim->NumWaterfalls() > 0; LumosChitBag* chitBag = Context()->chitBag; Rectangle2IEdgeIterator it(bounds); while (!it.Done()) { Vector2I pos = { it.Pos().x >= porch.max.x ? porch.max.x : porch.min.x, it.Pos().y >= porch.max.y ? porch.max.y : porch.min.y }; LineWalk walk(pos.x, pos.y, it.Pos().x, it.Pos().y); walk.Step(); // ignore where we are standing. while ( !walk.Done() ) { // non-intuitive iterator. See linewalk docs. // - building // - plant // - ice // - rock // - waterfall // - water // Buildings. Can be 2x2. Extend out beyond current check. bool hitBuilding = false; Vector2I p = walk.P(); // Don't count self as a hit, but stops the ray cast. // Also, use a larger radius because buildings can be 2x2 chitBag->QuerySpatialHash(&arr, ToWorld2F(p), 0.8f, 0, &buildingFilter); for (int i = 0; i < arr.Size(); ++i) { if (arr[i] != building) { MapSpatialComponent* buildingMSC = GET_SUB_COMPONENT(arr[i], SpatialComponent, MapSpatialComponent); GLASSERT(buildingMSC); if (buildingMSC->Bounds().Contains(p)) { hitBuilding = true; double thisSys = arr[i]->GetItem()->GetBuildingIndustrial(); hitB++; if (thisSys <= -0.5) hitNBuilding++; if (thisSys >= 0.5) hitIBuilding++; break; } } } if (hitBuilding) break; const WorldGrid& wg = worldMap->GetWorldGrid(p.x, p.y); if (wg.Plant()) { // int type = wg.Plant() - 1; int stage = wg.PlantStage(); if (stage >= 2) { ++hitPlant; break; } else { hitShrub++; } } if (wg.RockHeight()) { ++hitRock; break; } if (wg.IsWater()) { ++hitWater; break; } Rectangle2I wb; wb.min = wb.max = p; if (hasWaterfalls && fluidSim->ContainsWaterfalls(wb)) { ++hitWaterfall; break; } walk.Step(); } it.Next(); } // Note rock/ice isn't counted either way. int natural = hitNBuilding + hitWater + hitPlant + 10 * hitWaterfall + hitShrub / 4; // small plants don't add to rRays, so divide is okay. int industrial = hitIBuilding; int nRays = hitNBuilding + hitWater + hitPlant + hitWaterfall + hitIBuilding; eval = 0; if (nRays) { // With this system, that one ray (say from a distillery to plant) can be // hugely impactful. This may need tweaking: if (nRays < 2) nRays = 2; eval = double(industrial - natural) / double(nRays); } eval = Clamp(eval, -1.0, 1.0); if (debugLog) { Vector2I pos = ToWorld2I(building->Position()); GLOUTPUT(("Building %s at %d,%d eval=%.2f nRays=%d \n hit: Build=%d (I=%d N=%d) water=%d plant=%d rock=%d\n", building->GetItem()->Name(), pos.x, pos.y, eval, nRays, hitB, hitIBuilding, hitNBuilding, hitWater, hitPlant, hitRock)); (void)pos; } } return eval; }