* Create a new Unit. * * @param index The new index of the Unit, or UNIT_INDEX_INVALID to assign one. * @param typeID The type of the new Unit. * @param houseID The House of the new Unit. * @param position To where on the map this Unit should be transported, or TILE_INVALID for not on the map yet. * @param orientation Orientation of the Unit. * @return The new created Unit, or NULL if somet
| 389 | * @return The new created Unit, or NULL if something failed. |
| 390 | */ |
| 391 | Unit *Unit_Create(uint16 index, uint8 typeID, uint8 houseID, tile32 position, int8 orientation) |
| 392 | { |
| 393 | const UnitInfo *ui; |
| 394 | Unit *u; |
| 395 | |
| 396 | if (houseID >= HOUSE_MAX) return NULL; |
| 397 | if (typeID >= UNIT_MAX) return NULL; |
| 398 | |
| 399 | ui = &g_table_unitInfo[typeID]; |
| 400 | u = Unit_Allocate(index, typeID, houseID); |
| 401 | if (u == NULL) return NULL; |
| 402 | |
| 403 | u->o.houseID = houseID; |
| 404 | |
| 405 | Unit_SetOrientation(u, orientation, true, 0); |
| 406 | Unit_SetOrientation(u, orientation, true, 1); |
| 407 | |
| 408 | Unit_SetSpeed(u, 0); |
| 409 | |
| 410 | u->o.position = position; |
| 411 | u->o.hitpoints = ui->o.hitpoints; |
| 412 | u->currentDestination.x = 0; |
| 413 | u->currentDestination.y = 0; |
| 414 | u->originEncoded = 0x0000; |
| 415 | u->route[0] = 0xFF; |
| 416 | |
| 417 | if (position.x != 0xFFFF || position.y != 0xFFFF) { |
| 418 | u->originEncoded = Unit_FindClosestRefinery(u); |
| 419 | u->targetLast = position; |
| 420 | u->targetPreLast = position; |
| 421 | } |
| 422 | |
| 423 | u->o.linkedID = 0xFF; |
| 424 | u->o.script.delay = 0; |
| 425 | u->actionID = ACTION_GUARD; |
| 426 | u->nextActionID = ACTION_INVALID; |
| 427 | u->fireDelay = 0; |
| 428 | u->distanceToDestination = 0x7FFF; |
| 429 | u->targetMove = 0x0000; |
| 430 | u->amount = 0; |
| 431 | u->wobbleIndex = 0; |
| 432 | u->spriteOffset = 0; |
| 433 | u->blinkCounter = 0; |
| 434 | u->timer = 0; |
| 435 | |
| 436 | Script_Reset(&u->o.script, g_scriptUnit); |
| 437 | |
| 438 | u->o.flags.s.allocated = true; |
| 439 | |
| 440 | if (ui->movementType == MOVEMENT_TRACKED) { |
| 441 | if (Tools_Random_256() < g_table_houseInfo[houseID].degradingChance) { |
| 442 | u->o.flags.s.degrades = true; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (ui->movementType == MOVEMENT_WINGER) { |
| 447 | Unit_SetSpeed(u, 255); |
| 448 | } else { |
no test coverage detected