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

Function Script_Structure_RotateTurret

src/script/structure.c:375–428  ·  view source on GitHub ↗

* Rotate the turret to look at a tile. * * Stack: 1 - Tile to look at. * * @param script The script engine to operate on. * @return 0 if looking at target, otherwise 1. */

Source from the content-addressed store, hash-verified

373 * @return 0 if looking at target, otherwise 1.
374 */
375uint16 Script_Structure_RotateTurret(ScriptEngine *script)
376{
377 Structure *s;
378 tile32 lookAt;
379 Tile *tile;
380 uint16 baseTileID;
381 uint16 encoded;
382 int16 rotation;
383 int16 rotationNeeded;
384 int16 rotateDiff;
385
386 encoded = STACK_PEEK(1);
387
388 if (encoded == 0) return 0;
389
390 s = g_scriptCurrentStructure;
391 lookAt = Tools_Index_GetTile(encoded);
392 tile = &g_map[Tile_PackTile(s->o.position)];
393
394 /* Find the base sprite of the structure */
395 if (s->o.type == STRUCTURE_ROCKET_TURRET) {
396 baseTileID = g_iconMap[g_iconMap[ICM_ICONGROUP_BASE_ROCKET_TURRET] + 2];
397 } else {
398 baseTileID = g_iconMap[g_iconMap[ICM_ICONGROUP_BASE_DEFENSE_TURRET] + 2];
399 }
400
401 rotation = tile->groundTileID - baseTileID;
402 if (rotation < 0 || rotation > 7) return 1;
403
404 /* Find what rotation we should have to look at the target */
405 rotationNeeded = Orientation_Orientation256ToOrientation8(Tile_GetDirection(s->o.position, lookAt));
406
407 /* Do we need to rotate */
408 if (rotationNeeded == rotation) return 0;
409
410 /* Find the fastest way to rotate to the correct rotation */
411 rotateDiff = rotationNeeded - rotation;
412 if (rotateDiff < 0) rotateDiff += 8;
413
414 if (rotateDiff < 4) {
415 rotation++;
416 } else {
417 rotation--;
418 }
419 rotation &= 0x7;
420
421 /* Set the new sprites */
422 tile->groundTileID = baseTileID + rotation;
423 s->rotationSpriteDiff = rotation;
424
425 Map_Update(Tile_PackTile(s->o.position), 0, false);
426
427 return 1;
428}
429
430/**
431 * Find the direction a tile is, seen from the structure. If the tile is

Callers

nothing calls this directly

Calls 4

Tools_Index_GetTileFunction · 0.85
Tile_GetDirectionFunction · 0.85
Map_UpdateFunction · 0.85

Tested by

no test coverage detected