* Create a new Structure. * * @param index The new index of the Structure, or STRUCTURE_INDEX_INVALID to assign one. * @param typeID The type of the new Structure. * @param houseID The House of the new Structure. * @param position The packed position where to place the Structure. If 0xFFFF, the Structure is not placed. * @return The new created Structure, or NULL if something failed. */
| 371 | * @return The new created Structure, or NULL if something failed. |
| 372 | */ |
| 373 | Structure *Structure_Create(uint16 index, uint8 typeID, uint8 houseID, uint16 position) |
| 374 | { |
| 375 | const StructureInfo *si; |
| 376 | Structure *s; |
| 377 | |
| 378 | if (houseID >= HOUSE_MAX) return NULL; |
| 379 | if (typeID >= STRUCTURE_MAX) return NULL; |
| 380 | |
| 381 | si = &g_table_structureInfo[typeID]; |
| 382 | s = Structure_Allocate(index, typeID); |
| 383 | if (s == NULL) return NULL; |
| 384 | |
| 385 | s->o.houseID = houseID; |
| 386 | s->creatorHouseID = houseID; |
| 387 | s->o.flags.s.isNotOnMap = true; |
| 388 | s->o.position.x = 0; |
| 389 | s->o.position.y = 0; |
| 390 | s->o.linkedID = 0xFF; |
| 391 | s->state = (g_debugScenario) ? STRUCTURE_STATE_IDLE : STRUCTURE_STATE_JUSTBUILT; |
| 392 | |
| 393 | if (typeID == STRUCTURE_TURRET) { |
| 394 | s->rotationSpriteDiff = g_iconMap[g_iconMap[ICM_ICONGROUP_BASE_DEFENSE_TURRET] + 1]; |
| 395 | } |
| 396 | if (typeID == STRUCTURE_ROCKET_TURRET) { |
| 397 | s->rotationSpriteDiff = g_iconMap[g_iconMap[ICM_ICONGROUP_BASE_ROCKET_TURRET] + 1]; |
| 398 | } |
| 399 | |
| 400 | s->o.hitpoints = si->o.hitpoints; |
| 401 | s->hitpointsMax = si->o.hitpoints; |
| 402 | |
| 403 | if (houseID == HOUSE_HARKONNEN && typeID == STRUCTURE_LIGHT_VEHICLE) { |
| 404 | s->upgradeLevel = 1; |
| 405 | } |
| 406 | |
| 407 | /* Check if there is an upgrade available */ |
| 408 | if (si->o.flags.factory) { |
| 409 | s->upgradeTimeLeft = Structure_IsUpgradable(s) ? 100 : 0; |
| 410 | } |
| 411 | |
| 412 | s->objectType = 0xFFFF; |
| 413 | |
| 414 | Structure_BuildObject(s, 0xFFFE); |
| 415 | |
| 416 | s->countDown = 0; |
| 417 | |
| 418 | /* AIs get the full upgrade immediately */ |
| 419 | if (houseID != g_playerHouseID) { |
| 420 | while (true) { |
| 421 | if (!Structure_IsUpgradable(s)) break; |
| 422 | s->upgradeLevel++; |
| 423 | } |
| 424 | s->upgradeTimeLeft = 0; |
| 425 | } |
| 426 | |
| 427 | if (position != 0xFFFF && !Structure_Place(s, position)) { |
| 428 | Structure_Free(s); |
| 429 | return NULL; |
| 430 | } |
no test coverage detected