MCPcopy Create free account
hub / github.com/OpenDUNE/OpenDUNE / Script_Unit_MoveToTarget

Function Script_Unit_MoveToTarget

src/script/unit.c:427–480  ·  view source on GitHub ↗

* Move the Unit to the target, and keep repeating this function till we * arrived there. When closing in on the target it will slow down the Unit. * It is wise to only use this function on Carry-Alls. * * Stack: *none*. * * @param script The script engine to operate on. * @return 1 if arrived, 0 if still busy. */

Source from the content-addressed store, hash-verified

425 * @return 1 if arrived, 0 if still busy.
426 */
427uint16 Script_Unit_MoveToTarget(ScriptEngine *script)
428{
429 Unit *u;
430 uint16 delay;
431 tile32 tile;
432 uint16 distance;
433 int8 orientation;
434 int16 diff;
435
436 u = g_scriptCurrentUnit;
437
438 if (u->targetMove == 0) return 0;
439
440 tile = Tools_Index_GetTile(u->targetMove);
441
442 distance = Tile_GetDistance(u->o.position, tile);
443
444 if ((int16)distance < 128) {
445 Unit_SetSpeed(u, 0);
446
447 u->o.position.x += clamp((int16)(tile.x - u->o.position.x), -16, 16);
448 u->o.position.y += clamp((int16)(tile.y - u->o.position.y), -16, 16);
449
450 Unit_UpdateMap(2, u);
451
452 if ((int16)distance < 32) return 1;
453
454 script->delay = 2;
455
456 script->script--;
457 return 0;
458 }
459
460 orientation = Tile_GetDirection(u->o.position, tile);
461
462 Unit_SetOrientation(u, orientation, false, 0);
463
464 diff = abs(orientation - u->orientation[0].current);
465 if (diff > 128) diff = 256 - diff;
466
467 Unit_SetSpeed(u, (max(min(distance / 8, 255), 25) * (255 - diff) + 128) / 256);
468
469 delay = max((int16)distance / 1024, 1);
470
471 Unit_UpdateMap(2, u);
472
473 if (delay != 0) {
474 script->delay = delay;
475
476 script->script--;
477 }
478
479 return 0;
480}
481
482/**
483 * Kill a unit. When it was a saboteur, expect a big explosion.

Callers

nothing calls this directly

Calls 6

Tools_Index_GetTileFunction · 0.85
Tile_GetDistanceFunction · 0.85
Unit_SetSpeedFunction · 0.85
Unit_UpdateMapFunction · 0.85
Tile_GetDirectionFunction · 0.85
Unit_SetOrientationFunction · 0.85

Tested by

no test coverage detected