|
| 1 | +-- Unstick noble symbols that cannot be re-designated. |
| 2 | + |
| 3 | +local utils = require('utils') |
| 4 | + |
| 5 | +local function GetArtifact(item) |
| 6 | + local artifact |
| 7 | + for _, generalRef in ipairs(item.general_refs) do |
| 8 | + if df.general_ref_is_artifactst:is_instance(generalRef) then |
| 9 | + artifact = df.artifact_record.find(generalRef.artifact_id) |
| 10 | + break |
| 11 | + end |
| 12 | + end |
| 13 | + local civ_id = df.global.plotinfo.civ_id |
| 14 | + -- Check if selected item is claimed by current site's civ. |
| 15 | + local entClaim_idx = artifact and utils.linear_index(artifact.entity_claims, civ_id) |
| 16 | + if artifact and entClaim_idx then |
| 17 | + return artifact, entClaim_idx |
| 18 | + end |
| 19 | + return nil |
| 20 | +end |
| 21 | + |
| 22 | +local function UnstickSymbol(artifact, entClaim_idx) |
| 23 | + local civEntity = df.historical_entity.find(df.global.plotinfo.civ_id) |
| 24 | + local idx = utils.linear_index(civEntity.artifact_claims, artifact.id, 'artifact_id') |
| 25 | + -- Check if selected item is a symbol. |
| 26 | + if idx and civEntity.artifact_claims[idx].claim_type == df.artifact_claim_type.Symbol then |
| 27 | + local position_idx = civEntity.artifact_claims[idx].symbol_claim_id |
| 28 | + -- Check if symbol's position has been vacated. |
| 29 | + if civEntity.positions.assignments[position_idx].histfig == -1 then |
| 30 | + -- Erase claim from the entity and its reference in the artifact's record. |
| 31 | + -- Note: it's also possible to re-assign symbol by changing symbol_claim_id |
| 32 | + -- to point to the appropriate idx in civEntity.positions.assignments |
| 33 | + civEntity.artifact_claims:erase(idx) |
| 34 | + artifact.entity_claims:erase(entClaim_idx) |
| 35 | + return true |
| 36 | + end |
| 37 | + end |
| 38 | + return false |
| 39 | +end |
| 40 | + |
| 41 | +local function Main(args) |
| 42 | + if args[1] == 'help' then |
| 43 | + print(dfhack.script_help()) |
| 44 | + return |
| 45 | + end |
| 46 | + local item = dfhack.gui.getSelectedItem(true) |
| 47 | + if not item then |
| 48 | + qerror('No item selected.') |
| 49 | + end |
| 50 | + local artifact, entClaim_idx = GetArtifact(item) |
| 51 | + if artifact then |
| 52 | + if UnstickSymbol(artifact, entClaim_idx) then |
| 53 | + print('Symbol designation removed from selected item.') |
| 54 | + local strItemName = item and dfhack.items.getReadableDescription(item) |
| 55 | + print(('%s can now be re-designated as a symbol of nobility.'):format(strItemName)) |
| 56 | + else |
| 57 | + qerror('Selected item is not a defunct symbol of the current site\'s civilization.') |
| 58 | + end |
| 59 | + else |
| 60 | + qerror('Selected item is not an artifact claimed by the current site\'s civilization.') |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +if not dfhack.isSiteLoaded() and not dfhack.world.isFortressMode() then |
| 65 | + qerror('This script requires the game to be in fortress mode.') |
| 66 | +end |
| 67 | + |
| 68 | +Main({...}) |
0 commit comments