| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | |
| 4 | #include "gameworld.h" |
| 5 | |
| 6 | #include "entities/character.h" |
| 7 | #include "entity.h" |
| 8 | #include "gamecontext.h" |
| 9 | #include "gamecontroller.h" |
| 10 | |
| 11 | #include <engine/shared/config.h> |
| 12 | |
| 13 | #include <game/collision.h> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <utility> |
| 17 | |
| 18 | ////////////////////////////////////////////////// |
| 19 | // game world |
| 20 | ////////////////////////////////////////////////// |
| 21 | |
| 22 | CGameWorld::CEntityIterator::CEntityIterator(CGameWorld *pWorld, CEntity *pFirstEntity) : |
| 23 | m_pWorld(pWorld), |
| 24 | m_pNext(pFirstEntity) |
| 25 | { |
| 26 | m_pWorld->m_vpEntityIterators.push_back(x: this); |
| 27 | } |
| 28 | |
| 29 | CGameWorld::CEntityIterator::CEntityIterator(CGameWorld *pWorld, int EntityType) : |
| 30 | m_pWorld(pWorld) |
| 31 | { |
| 32 | dbg_assert(EntityType >= 0 && EntityType < NUM_ENTTYPES, "Invalid EntityType: %d" , EntityType); |
| 33 | m_pNext = m_pWorld->m_apFirstEntityTypes[EntityType]; |
| 34 | m_pWorld->m_vpEntityIterators.push_back(x: this); |
| 35 | } |
| 36 | |
| 37 | CGameWorld::CEntityIterator::~CEntityIterator() |
| 38 | { |
| 39 | dbg_assert(m_pWorld->m_vpEntityIterators.back() == this, "Inconsistent entity iterator removal" ); |
| 40 | m_pWorld->m_vpEntityIterators.pop_back(); |
| 41 | } |
| 42 | |
| 43 | void CGameWorld::CEntityIterator::AllowEntityRemoval() |
| 44 | { |
| 45 | m_AllowEntityRemoval = true; |
| 46 | } |
| 47 | |
| 48 | CEntity *CGameWorld::CEntityIterator::Next() |
| 49 | { |
| 50 | CEntity *pResult = m_pNext; |
| 51 | if(m_pNext != nullptr) |
| 52 | { |
| 53 | m_pNext = m_pNext->m_pNextTypeEntity; |
| 54 | } |
| 55 | return pResult; |
| 56 | } |
| 57 | |
| 58 | CGameWorld::CGameWorld() |
| 59 | { |
| 60 | m_pGameServer = nullptr; |
| 61 | m_pConfig = nullptr; |
| 62 | m_pServer = nullptr; |
| 63 | |
| 64 | m_Paused = false; |
| 65 | m_ResetRequested = false; |
| 66 | std::fill(first: std::begin(arr&: m_apFirstEntityTypes), last: std::end(arr&: m_apFirstEntityTypes), value: nullptr); |
| 67 | } |
| 68 | |
| 69 | CGameWorld::~CGameWorld() |
| 70 | { |
| 71 | // delete all entities |
| 72 | for(auto &pFirstEntityType : m_apFirstEntityTypes) |
| 73 | while(pFirstEntityType) |
| 74 | delete pFirstEntityType; // NOLINT(clang-analyzer-cplusplus.NewDelete) |
| 75 | } |
| 76 | |
| 77 | void CGameWorld::SetGameServer(CGameContext *pGameServer) |
| 78 | { |
| 79 | m_pGameServer = pGameServer; |
| 80 | m_pConfig = m_pGameServer->Config(); |
| 81 | m_pServer = m_pGameServer->Server(); |
| 82 | } |
| 83 | |
| 84 | void CGameWorld::Init(CCollision *pCollision, CTuningParams *pTuningList) |
| 85 | { |
| 86 | m_Core.InitSwitchers(HighestSwitchNumber: pCollision->m_HighestSwitchNumber); |
| 87 | m_pTuningList = pTuningList; |
| 88 | } |
| 89 | |
| 90 | int CGameWorld::FindEntities(vec2 Pos, float Radius, CEntity **ppEnts, int Max, int Type) |
| 91 | { |
| 92 | int Num = 0; |
| 93 | CEntityIterator Iterator(this, Type); |
| 94 | while(CEntity *pEnt = Iterator.Next()) |
| 95 | { |
| 96 | if(distance(a: pEnt->m_Pos, b: Pos) < Radius + pEnt->m_ProximityRadius) |
| 97 | { |
| 98 | if(ppEnts) |
| 99 | ppEnts[Num] = pEnt; |
| 100 | Num++; |
| 101 | if(Num == Max) |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return Num; |
| 107 | } |
| 108 | |
| 109 | void CGameWorld::InsertEntity(CEntity *pEnt) |
| 110 | { |
| 111 | #ifdef CONF_DEBUG |
| 112 | for(CEntity *pCur = m_apFirstEntityTypes[pEnt->m_ObjType]; pCur; pCur = pCur->m_pNextTypeEntity) |
| 113 | dbg_assert(pCur != pEnt, "err" ); |
| 114 | #endif |
| 115 | |
| 116 | // insert it |
| 117 | if(m_apFirstEntityTypes[pEnt->m_ObjType]) |
| 118 | m_apFirstEntityTypes[pEnt->m_ObjType]->m_pPrevTypeEntity = pEnt; |
| 119 | pEnt->m_pNextTypeEntity = m_apFirstEntityTypes[pEnt->m_ObjType]; |
| 120 | pEnt->m_pPrevTypeEntity = nullptr; |
| 121 | m_apFirstEntityTypes[pEnt->m_ObjType] = pEnt; |
| 122 | } |
| 123 | |
| 124 | void CGameWorld::RemoveEntity(CEntity *pEnt) |
| 125 | { |
| 126 | // not in the list |
| 127 | if(!pEnt->m_pNextTypeEntity && !pEnt->m_pPrevTypeEntity && m_apFirstEntityTypes[pEnt->m_ObjType] != pEnt) |
| 128 | return; |
| 129 | |
| 130 | for(CEntityIterator *pEntityIterator : m_vpEntityIterators) |
| 131 | { |
| 132 | dbg_assert(pEntityIterator->m_AllowEntityRemoval, "Removing entities not allowed with current iterator" ); |
| 133 | } |
| 134 | |
| 135 | // remove |
| 136 | if(pEnt->m_pPrevTypeEntity) |
| 137 | pEnt->m_pPrevTypeEntity->m_pNextTypeEntity = pEnt->m_pNextTypeEntity; |
| 138 | else |
| 139 | m_apFirstEntityTypes[pEnt->m_ObjType] = pEnt->m_pNextTypeEntity; |
| 140 | if(pEnt->m_pNextTypeEntity) |
| 141 | pEnt->m_pNextTypeEntity->m_pPrevTypeEntity = pEnt->m_pPrevTypeEntity; |
| 142 | |
| 143 | // keep list traversing valid |
| 144 | for(CEntityIterator *pEntityIterator : m_vpEntityIterators) |
| 145 | { |
| 146 | if(pEntityIterator->m_pNext == pEnt) |
| 147 | { |
| 148 | pEntityIterator->m_pNext = pEnt->m_pNextTypeEntity; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | pEnt->m_pNextTypeEntity = nullptr; |
| 153 | pEnt->m_pPrevTypeEntity = nullptr; |
| 154 | } |
| 155 | |
| 156 | // |
| 157 | void CGameWorld::Snap(int SnappingClient) |
| 158 | { |
| 159 | { |
| 160 | CEntityIterator Iterator(this, m_apFirstEntityTypes[ENTTYPE_CHARACTER]); |
| 161 | while(CEntity *pEnt = Iterator.Next()) |
| 162 | { |
| 163 | pEnt->Snap(SnappingClient); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for(int i = 0; i < NUM_ENTTYPES; i++) |
| 168 | { |
| 169 | if(i == ENTTYPE_CHARACTER) |
| 170 | continue; |
| 171 | |
| 172 | CEntityIterator Iterator(this, m_apFirstEntityTypes[i]); |
| 173 | while(CEntity *pEnt = Iterator.Next()) |
| 174 | { |
| 175 | pEnt->Snap(SnappingClient); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void CGameWorld::Reset() |
| 181 | { |
| 182 | // reset all entities |
| 183 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 184 | { |
| 185 | CEntityIterator Iterator(this, pFirstEntity); |
| 186 | Iterator.AllowEntityRemoval(); |
| 187 | while(CEntity *pEnt = Iterator.Next()) |
| 188 | { |
| 189 | pEnt->Reset(); |
| 190 | } |
| 191 | } |
| 192 | RemoveEntities(); |
| 193 | |
| 194 | GameServer()->m_pController->OnReset(); |
| 195 | RemoveEntities(); |
| 196 | |
| 197 | m_ResetRequested = false; |
| 198 | |
| 199 | GameServer()->CreateAllEntities(Initial: false); |
| 200 | } |
| 201 | |
| 202 | void CGameWorld::RemoveEntitiesFromPlayer(int PlayerId) |
| 203 | { |
| 204 | RemoveEntitiesFromPlayers(PlayerIds: &PlayerId, NumPlayers: 1); |
| 205 | } |
| 206 | |
| 207 | void CGameWorld::RemoveEntitiesFromPlayers(int PlayerIds[], int NumPlayers) |
| 208 | { |
| 209 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 210 | { |
| 211 | CEntityIterator Iterator(this, pFirstEntity); |
| 212 | Iterator.AllowEntityRemoval(); |
| 213 | while(CEntity *pEnt = Iterator.Next()) |
| 214 | { |
| 215 | for(int i = 0; i < NumPlayers; i++) |
| 216 | { |
| 217 | if(pEnt->GetOwnerId() == PlayerIds[i]) |
| 218 | { |
| 219 | RemoveEntity(pEnt); |
| 220 | pEnt->Destroy(); |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void CGameWorld::RemoveEntities() |
| 229 | { |
| 230 | // destroy objects marked for destruction |
| 231 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 232 | { |
| 233 | CEntityIterator Iterator(this, pFirstEntity); |
| 234 | Iterator.AllowEntityRemoval(); |
| 235 | while(CEntity *pEnt = Iterator.Next()) |
| 236 | { |
| 237 | if(pEnt->m_MarkedForDestroy) |
| 238 | { |
| 239 | RemoveEntity(pEnt); |
| 240 | pEnt->Destroy(); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void CGameWorld::Tick() |
| 247 | { |
| 248 | if(m_ResetRequested) |
| 249 | Reset(); |
| 250 | |
| 251 | if(!m_Paused) |
| 252 | { |
| 253 | // update all objects |
| 254 | for(int i = 0; i < NUM_ENTTYPES; i++) |
| 255 | { |
| 256 | // It's important to call PreTick() and Tick() after each other. |
| 257 | // If we call PreTick() before, and Tick() after other entities have been processed, it causes physics changes such as a stronger shotgun or grenade. |
| 258 | if(g_Config.m_SvNoWeakHook && i == ENTTYPE_CHARACTER) |
| 259 | { |
| 260 | CEntityIterator Iterator(this, m_apFirstEntityTypes[ENTTYPE_CHARACTER]); |
| 261 | Iterator.AllowEntityRemoval(); |
| 262 | while(CCharacter *pCharacter = static_cast<CCharacter *>(Iterator.Next())) |
| 263 | { |
| 264 | pCharacter->PreTick(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | CEntityIterator Iterator(this, m_apFirstEntityTypes[i]); |
| 269 | Iterator.AllowEntityRemoval(); |
| 270 | while(CEntity *pEnt = Iterator.Next()) |
| 271 | { |
| 272 | pEnt->Tick(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 277 | { |
| 278 | CEntityIterator Iterator(this, pFirstEntity); |
| 279 | Iterator.AllowEntityRemoval(); |
| 280 | while(CEntity *pEnt = Iterator.Next()) |
| 281 | { |
| 282 | pEnt->TickDeferred(); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | // update all objects |
| 289 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 290 | { |
| 291 | CEntityIterator Iterator(this, pFirstEntity); |
| 292 | Iterator.AllowEntityRemoval(); |
| 293 | while(CEntity *pEnt = Iterator.Next()) |
| 294 | { |
| 295 | pEnt->TickPaused(); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | RemoveEntities(); |
| 301 | |
| 302 | // find the characters' strong/weak id |
| 303 | int StrongWeakId = 0; |
| 304 | CEntityIterator Iterator(this, m_apFirstEntityTypes[ENTTYPE_CHARACTER]); |
| 305 | while(CCharacter *pChar = static_cast<CCharacter *>(Iterator.Next())) |
| 306 | { |
| 307 | pChar->m_StrongWeakId = StrongWeakId; |
| 308 | StrongWeakId++; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | ESaveResult CGameWorld::BlocksSave(int ClientId) |
| 313 | { |
| 314 | // check all objects |
| 315 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 316 | { |
| 317 | CEntityIterator Iterator(this, pFirstEntity); |
| 318 | while(CEntity *pEnt = Iterator.Next()) |
| 319 | { |
| 320 | ESaveResult Result = pEnt->BlocksSave(ClientId); |
| 321 | if(Result != ESaveResult::SUCCESS) |
| 322 | return Result; |
| 323 | } |
| 324 | } |
| 325 | return ESaveResult::SUCCESS; |
| 326 | } |
| 327 | |
| 328 | void CGameWorld::SwapClients(int Client1, int Client2) |
| 329 | { |
| 330 | // update all objects |
| 331 | for(CEntity *pFirstEntity : m_apFirstEntityTypes) |
| 332 | { |
| 333 | CEntityIterator Iterator(this, pFirstEntity); |
| 334 | while(CEntity *pEnt = Iterator.Next()) |
| 335 | { |
| 336 | pEnt->SwapClients(Client1, Client2); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, const CCharacter *pNotThis, int CollideWith, const CCharacter *pThisOnly) |
| 342 | { |
| 343 | return (CCharacter *)IntersectEntity(Pos0, Pos1, Radius, Type: ENTTYPE_CHARACTER, NewPos, pNotThis, CollideWith, pThisOnly); |
| 344 | } |
| 345 | |
| 346 | CEntity *CGameWorld::IntersectEntity(vec2 Pos0, vec2 Pos1, float Radius, int Type, vec2 &NewPos, const CEntity *pNotThis, int CollideWith, const CEntity *pThisOnly) |
| 347 | { |
| 348 | float ClosestLen = distance(a: Pos0, b: Pos1) * 100.0f; |
| 349 | CEntity *pClosest = nullptr; |
| 350 | |
| 351 | CEntityIterator Iterator(this, Type); |
| 352 | while(CEntity *pEntity = Iterator.Next()) |
| 353 | { |
| 354 | if(pEntity == pNotThis) |
| 355 | continue; |
| 356 | |
| 357 | if(pThisOnly && pEntity != pThisOnly) |
| 358 | continue; |
| 359 | |
| 360 | if(CollideWith != -1 && !pEntity->CanCollide(ClientId: CollideWith)) |
| 361 | continue; |
| 362 | |
| 363 | vec2 IntersectPos; |
| 364 | if(closest_point_on_line(line_pointA: Pos0, line_pointB: Pos1, target_point: pEntity->m_Pos, out_pos&: IntersectPos)) |
| 365 | { |
| 366 | float Len = distance(a: pEntity->m_Pos, b: IntersectPos); |
| 367 | if(Len < pEntity->m_ProximityRadius + Radius) |
| 368 | { |
| 369 | Len = distance(a: Pos0, b: IntersectPos); |
| 370 | if(Len < ClosestLen) |
| 371 | { |
| 372 | NewPos = IntersectPos; |
| 373 | ClosestLen = Len; |
| 374 | pClosest = pEntity; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return pClosest; |
| 381 | } |
| 382 | |
| 383 | CCharacter *CGameWorld::ClosestCharacter(vec2 Pos, float Radius, const CEntity *pNotThis) |
| 384 | { |
| 385 | // Find other players |
| 386 | float ClosestRange = Radius * 2; |
| 387 | CCharacter *pClosest = nullptr; |
| 388 | |
| 389 | CEntityIterator Iterator(this, m_apFirstEntityTypes[ENTTYPE_CHARACTER]); |
| 390 | while(CCharacter *p = static_cast<CCharacter *>(Iterator.Next())) |
| 391 | { |
| 392 | if(p == pNotThis) |
| 393 | continue; |
| 394 | |
| 395 | float Len = distance(a: Pos, b: p->m_Pos); |
| 396 | if(Len < p->m_ProximityRadius + Radius) |
| 397 | { |
| 398 | if(Len < ClosestRange) |
| 399 | { |
| 400 | ClosestRange = Len; |
| 401 | pClosest = p; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return pClosest; |
| 407 | } |
| 408 | |
| 409 | std::vector<CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis) |
| 410 | { |
| 411 | std::vector<CCharacter *> vpCharacters; |
| 412 | CEntityIterator Iterator(this, m_apFirstEntityTypes[ENTTYPE_CHARACTER]); |
| 413 | while(CCharacter *pChr = static_cast<CCharacter *>(Iterator.Next())) |
| 414 | { |
| 415 | if(pChr == pNotThis) |
| 416 | continue; |
| 417 | |
| 418 | vec2 IntersectPos; |
| 419 | if(closest_point_on_line(line_pointA: Pos0, line_pointB: Pos1, target_point: pChr->m_Pos, out_pos&: IntersectPos)) |
| 420 | { |
| 421 | float Len = distance(a: pChr->m_Pos, b: IntersectPos); |
| 422 | if(Len < pChr->m_ProximityRadius + Radius) |
| 423 | { |
| 424 | vpCharacters.push_back(x: pChr); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | return vpCharacters; |
| 429 | } |
| 430 | |
| 431 | void CGameWorld::ReleaseHooked(int ClientId) |
| 432 | { |
| 433 | CEntityIterator Iterator(this, m_apFirstEntityTypes[ENTTYPE_CHARACTER]); |
| 434 | while(CCharacter *pChr = static_cast<CCharacter *>(Iterator.Next())) |
| 435 | { |
| 436 | if(pChr->Core()->HookedPlayer() == ClientId && !pChr->IsSuper()) |
| 437 | { |
| 438 | pChr->ReleaseHook(); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |