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

Function Unit_IsValidMovementIntoStructure

src/unit.c:660–693  ·  view source on GitHub ↗

* Determines whether a move order into the given structure is OK for * a particular unit. * * It handles orders to invade enemy buildings as well as going into * a friendly structure (e.g. refinery, repair facility). * * @param unit The Unit to operate on. * @param s The Structure to operate on. * @return * 0 - invalid movement * 1 - valid movement, will try to get close to the structure

Source from the content-addressed store, hash-verified

658 * 2 - valid movement, will attempt to damage/conquer the structure
659 */
660uint16 Unit_IsValidMovementIntoStructure(Unit *unit, Structure *s)
661{
662 const StructureInfo *si;
663 const UnitInfo *ui;
664 uint16 unitEnc;
665 uint16 structEnc;
666
667 if (unit == NULL || s == NULL) return 0;
668
669 si = &g_table_structureInfo[s->o.type];
670 ui = &g_table_unitInfo[unit->o.type];
671
672 unitEnc = Tools_Index_Encode(unit->o.index, IT_UNIT);
673 structEnc = Tools_Index_Encode(s->o.index, IT_STRUCTURE);
674
675 /* Movement into structure of other owner. */
676 if (Unit_GetHouseID(unit) != s->o.houseID) {
677 /* Saboteur can always enter houses */
678 if (unit->o.type == UNIT_SABOTEUR && unit->targetMove == structEnc) return 2;
679 /* Entering houses is only possible for foot-units and if the structure is conquerable.
680 * Everyone else can only move close to the building. */
681 if (ui->movementType == MOVEMENT_FOOT && si->o.flags.conquerable) return unit->targetMove == structEnc ? 2 : 1;
682 return 0;
683 }
684
685 /* Prevent movement if target structure does not accept the unit type. */
686 if ((si->enterFilter & (1 << unit->o.type)) == 0) return 0;
687
688 /* TODO -- Not sure. */
689 if (s->o.script.variables[4] == unitEnc) return 2;
690
691 /* Enter only if structure not linked to any other unit already. */
692 return s->o.linkedID == 0xFF ? 1 : 0;
693}
694
695/**
696 * Sets the destination for the given unit.

Callers 3

Unit_SetDestinationFunction · 0.85
Unit_GetTileEnterScoreFunction · 0.85

Calls 2

Tools_Index_EncodeFunction · 0.85
Unit_GetHouseIDFunction · 0.85

Tested by

no test coverage detected