* Get the score of entering this tile from a direction. * * @param unit The Unit to operate on. * @param packed The packed tile. * @param direction The direction entering this tile from. * @return 256 if tile is not accessable, -1 when it is an accessable structure, * or a score to enter the tile otherwise. */
| 2333 | * or a score to enter the tile otherwise. |
| 2334 | */ |
| 2335 | int16 Unit_GetTileEnterScore(Unit *unit, uint16 packed, uint16 orient8) |
| 2336 | { |
| 2337 | const UnitInfo *ui; |
| 2338 | Unit *u; |
| 2339 | Structure *s; |
| 2340 | uint16 type; |
| 2341 | uint16 res; |
| 2342 | |
| 2343 | if (unit == NULL) return 0; |
| 2344 | |
| 2345 | ui = &g_table_unitInfo[unit->o.type]; |
| 2346 | |
| 2347 | if (!Map_IsValidPosition(packed) && ui->movementType != MOVEMENT_WINGER) return 256; |
| 2348 | |
| 2349 | u = Unit_Get_ByPackedTile(packed); |
| 2350 | if (u != NULL && u != unit && unit->o.type != UNIT_SANDWORM) { |
| 2351 | if (unit->o.type == UNIT_SABOTEUR && unit->targetMove == Tools_Index_Encode(u->o.index, IT_UNIT)) return 0; |
| 2352 | |
| 2353 | if (House_AreAllied(Unit_GetHouseID(u), Unit_GetHouseID(unit))) return 256; |
| 2354 | if (g_table_unitInfo[u->o.type].movementType != MOVEMENT_FOOT || (ui->movementType != MOVEMENT_TRACKED && ui->movementType != MOVEMENT_HARVESTER)) return 256; |
| 2355 | } |
| 2356 | |
| 2357 | s = Structure_Get_ByPackedTile(packed); |
| 2358 | if (s != NULL) { |
| 2359 | res = Unit_IsValidMovementIntoStructure(unit, s); |
| 2360 | if (res == 0) return 256; |
| 2361 | return -res; |
| 2362 | } |
| 2363 | |
| 2364 | type = Map_GetLandscapeType(packed); |
| 2365 | |
| 2366 | if (g_dune2_enhanced) { |
| 2367 | res = g_table_landscapeInfo[type].movementSpeed[ui->movementType] * ui->movingSpeedFactor / 256; |
| 2368 | } else { |
| 2369 | res = g_table_landscapeInfo[type].movementSpeed[ui->movementType]; |
| 2370 | } |
| 2371 | |
| 2372 | if (unit->o.type == UNIT_SABOTEUR && type == LST_WALL) { |
| 2373 | if (!House_AreAllied(g_map[packed].houseID, Unit_GetHouseID(unit))) res = 255; |
| 2374 | } |
| 2375 | |
| 2376 | if (res == 0) return 256; |
| 2377 | |
| 2378 | /* Check if the unit is travelling diagonally. */ |
| 2379 | if ((orient8 & 1) != 0) { |
| 2380 | res -= res / 4 + res / 8; |
| 2381 | } |
| 2382 | |
| 2383 | /* 'Invert' the speed to get a rough estimate of the time taken. */ |
| 2384 | res ^= 0xFF; |
| 2385 | |
| 2386 | return (int16)res; |
| 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * Gets the best target for the given unit. |
no test coverage detected