import Trigger from '../../../parsers/w3x/wtg/trigger' ; import ECA from '../../../parsers/w3x/wtg/eca' ; import Parameter from '../../../parsers/w3x/wtg/parameter' ; import SubParameters from '../../../parsers/w3x/wtg/subparameters' ; import CustomTextTrigger from '../../../parsers/w3x/wct/customtexttrigger' ; import { TriggerData } from '../../../parsers/w3x/wtg/triggerdata' ; import War3MapWts from '../../../parsers/w3x/wts/file' ; export interface WEUChange { type : string ; reason : string ; data : string ; stack : string ; } export default class WeuData { triggerData : TriggerData ; stringTable : War3MapWts ; preplacedObjects : Map < string , boolean > = new Map ( ) ; changes : WEUChange [ ] = [ ] ; stack : ( Trigger | ECA | Parameter | SubParameters ) [ ] = [ ] ; constructor ( triggerData , stringTable ) { this . triggerData = triggerData ; this . stringTable = stringTable ; } push ( object ) { this . stack . unshift ( object ) ; } pop ( ) { this . stack . shift ( ) ; } change ( type , reason , data ) { this . changes . push ( { type , reason , data , stack : this . stackToString ( ) } ) ; } stackToString ( ) { return this . stack . map ( ( object ) => { if ( object instanceof Parameter ) { return object . value ; } else { return object . name ; } } ) . reverse ( ) . join ( ' > ' ) ; } getTriggerName ( ) { for ( const node of this . stack ) { if ( node instanceof Trigger ) { return node . name ; } } return '' ; } /**
     * Every time a reference to a preplaced object is encountered while testing the GUI, this will be called with isGUI being true.
     * Every time a reference to a preplaced object is converted to custom script, this will be called with isGUI being false.
     * This is used to track references that existed in GUI before the conversion, but that will be only in custom scripts afterwards.
     * References that are lost due to the conversion are then added in a new trigger called PreplacedObjectReferences.
     */ updateGUIReference ( name , isGUI ) { if ( name . startsWith ( 'gg_unit' ) || name . startsWith ( 'gg_dest' ) ) { const preplacedObjects = this . preplacedObjects ; if ( ! preplacedObjects . get ( name ) ) { preplacedObjects . set ( name , isGUI ) ; } } } saveGUIReferences ( triggers , customTextTriggers ) { const references = [ ] ; for ( const [ name , isGUI ] of this . preplacedObjects ) { if ( ! isGUI ) { references . push ( name ) ; } } if ( references . length ) { const trigger = new Trigger ( ) ; trigger . name = 'PreplacedObjectReferences' ; trigger . isEnabled = 1 ; trigger . isInitiallyOff = 1 ; for ( const reference of references ) { const eca = new ECA ( ) ; eca . type = 2 ; eca . isEnabled = 1 ; if ( reference . startsWith ( 'gg_unit' ) ) { eca . name = 'RemoveUnit' ; } else if ( reference . startsWith ( 'gg_dest' ) ) { eca . name = 'RemoveDestructable' ; } const parameter = new Parameter ( ) ; parameter . type = 1 ; parameter . value = reference ; eca . parameters [ 0 ] = parameter ; trigger . ecas . push ( eca ) ; } triggers . push ( trigger ) ; customTextTriggers . push ( new CustomTextTrigger ( ) ) ; this . change ( 'references' , 'Saved references to preplaced objects lost due to conversions' , references . join ( '\n' ) ) ; } } }