Results 1 to 7 of 7

Thread: Creating a World - Building and maintaining unique Buildings

  1. #1
    Gigantus's Avatar I am not special - I am a limited edition.
    Patrician Moderator Emeritus Administrator Emeritus

    Join Date
    Aug 2006
    Location
    Goa - India
    Posts
    53,095
    Blog Entries
    35

    Default Creating a World - Building and maintaining unique Buildings

    IntroductionI suppose everyone has used unique buildings before, the most wide spread application is a preplaced (descr_strat) building that has been disabled in the EDB via event_counters or the removal of entries in the faction condition. Recently I had the challenge to actually build it and not the script 'puff, building exists' way but rather the 'normal' way of adding it to a building queue.

    It did throw up some peculiar issues, like: how to progress the building over several turns, seeing that the building conditions have to be disabled. Or how to handle a cancellation of the building or it's rebuilding after destruction. After finding more then a dozen ways how not to do it I came up with the workable solution underneath. So far the script works for a single player faction, making the building unique for a range of factions may require some nifty footwork in the EDB. Making it available for AI factions is an altogether different issue due to the script code involved.
    PreparationI used my Bare Geomod set up as base and edited the export_descr_buildings (EDB), descr_strat and campaign_script files - these files are available as download here if someone would like to test them or use them as templates.
    Coding
    export_descr_buildingsThe building used for testing is the corn_exchange which received an event_counter condition at the end of it's building condition.

    Spoiler Alert, click show to read: 
    Code:
    corn_exchange city requires factions { northern_european, middle_eastern, eastern_european, greek, southern_european, } and event_counter build_corn_exchange 1
    descr_stratNottingham and London got relegated to towns with 800 population and only a palisade. I also removed (optionally) the Scottish diplomat and the French princess as they really bug me during testing when playing England.

    Spoiler Alert, click show to read: 
    Code:
    settlement
    {
        level town
        region Nottingham_Province
    
        year_founded 0
        population 800
        plan_set default_set
        faction_creator england
        building
        {
            type core_building wooden_pallisade
        }
    }
    
    settlement
    {
        level town
        region London_Province
    
        year_founded 0
        population 800
        plan_set default_set
        faction_creator england
        building
        {
            type core_building wooden_pallisade
        }
    }
    campaign_scriptThis is suitably convoluted as it wasn't a simple build&forget matter but needed to cater for the cancellation of the building (removal from queue cannot be tested for directly) as well as a destruction which would necessitate a re-building. I commented the entries as best as I could and I think most of it is self explanatory for those that have a basic understanding in coding.

    There are very little variables in the script which can easily be replaced:
    england - the faction that can build
    corn_exchange - the name of the building
    numerical value in 'the building should have been finished' line - building turns plus 2

    Spoiler Alert, click show to read: 
    Code:
    script
        restrict_strat_radar false
    
        ; The building exists
        monitor_event PreFactionTurnStart FactionType england
            and FactionBuildingExists = corn_exchange            ; building exists in faction
            set_event_counter build_corn_exchange 0                ; disable EDB event counter
            set_event_counter corn_exchange_queued 0            ; reset queue counter
        end_monitor
    
        ; The triggering event
        monitor_event FactionTurnStart FactionType england
            and not FactionBuildingExists = corn_exchange        ; building does not exist in faction
            if I_IsFactionAIControlled england                    ; terminate if faction is AI controlled
                terminate_monitor
            end_if
            ;        if I_EventCounter what_ever_you_want        ; possible counter from defining (and terminating) monitor
            ;        if I_CompareCounter what_ever_you_want        ; possible counter from defining (and terminating) monitor
            if I_TurnNumber > 0                                    ; testing counter only, will allow trigger only as of second turn, replace with above counter
                and I_EventCounter corn_exchange_queued < 1        ; the building is not queued already
                set_event_counter build_corn_exchange 1            ; enabling EDB event counter
            end_if
        end_monitor
    
        ; Adding the building to the queue
        monitor_event AddedToBuildingQueue FactionType england
            and BuildingName = corn_exchange
            if I_IsFactionAIControlled england                    ; terminate if faction is AI controlled
                terminate_monitor
            end_if
            set_event_counter build_corn_exchange 0                ; disable EDB event counter
            set_event_counter corn_exchange_queued 1            ; the building has been queued
        end_monitor
    
        ; Re-enable the event counter, if this isn't done the building will never complete.
        monitor_event ButtonPressed ButtonPressed end_turn
            if not I_IsFactionAIControlled england
                set_event_counter build_corn_exchange 1            ; enabling EDB event counter
            end_if
        end_monitor
    
        ; Disable the event counter again if building is queued to prevent more buildings being added
        monitor_event FactionTurnEnd FactionType england
            and not FactionBuildingExists = corn_exchange        ; building does not exist in faction
            if I_IsFactionAIControlled england                    ; terminate if faction is AI controlled
                terminate_monitor
            end_if
            if I_EventCounter corn_exchange_queued > 0            ; the building has been queued
                set_event_counter build_corn_exchange 0            ; disable EDB event counter
                inc_event_counter corn_exchange_queued 1        ; increasing the queue counter
            end_if
            if I_EventCounter corn_exchange_queued > 3            ; the building should have been finished (building turns plus one)
                set_event_counter build_corn_exchange 1            ; enable EDB event counter
                set_event_counter corn_exchange_queued 0        ; reset queue counter
            end_if
        end_monitor
    
        wait_monitors
    end_script
    Test ResultsWhen testing this with England I had the following results (blue = action):
    --- Building destroyed ---
    Round one: building is unavailable
    Round two: building is available
    queued in London
    unavailable in Nottingham
    Round three: building progresses
    unavailable in Nottingham
    Round four: building completed
    unavailable in Nottingham
    Round five: unavailable in Nottingham
    destroy building in London
    building is unavailable
    Round six: building is available, repeat from round two

    --- Building cancelled ---
    Round one: building is unavailable
    Round two: building is available
    queued in London
    unavailable in Nottingham
    Round three: building cancelled
    building is unavailable
    Round four: building is unavailable
    Round five: building is available, repeat from round two
    Last edited by Gigantus; November 19, 2015 at 09:27 AM.










  2. #2
    Withwnar's Avatar Script To The Waist
    Join Date
    Oct 2008
    Location
    Earth
    Posts
    6,329

    Default Re: Creating a World - Building and maintaining unique Buildings

    Nice one.

    needed to cater for the cancellation of the building (removal from queue cannot be tested for directly)
    Which you have done via a cooldown period of three turns, yes? This bit...

    Code:
            if I_EventCounter corn_exchange_queued > 3            ; the building should have been finished (building turns plus one)
                set_event_counter build_corn_exchange 1            ; enable EDB event counter
                inc_event_counter corn_exchange_queued 0        ; reset queue counter
            end_if
    I think you meant "set_event_counter corn_exchange_queued 0" there.

    If the settlement is under siege while the building is under construction then it would take longer for it to be built. The counter might 'time out' and allow construction elsewhere, meanwhile the original could still be eventually finished once the siege ends. Also, it might be added to an already-busy queue, so there's no telling how long it will take to finish construction and meanwhile another one might be allowed to start. Or would it drop from the queue when the event counter becomes 0?

  3. #3
    Gigantus's Avatar I am not special - I am a limited edition.
    Patrician Moderator Emeritus Administrator Emeritus

    Join Date
    Aug 2006
    Location
    Goa - India
    Posts
    53,095
    Blog Entries
    35

    Default Re: Creating a World - Building and maintaining unique Buildings

    I think you meant "set_event_counter corn_exchange_queued 0" there.
    That was a silly copy\paste error. Fixed it now.

    A building will not drop from a queue unless done so physically by the player. It might never finish if the building counter is disabled, which would be the case in your scenarios as the existence of the building sets to the counter to false.
    Meaning if you are able to add another building to a queue somewhere else you pretty much shot yourself in the foot as you will not be able to finish the second one unless you destroy the first. Which I think will scrambled the script somewhat and is rather pointless, removing the second building from the queue will refund building costs and allow the queue in that settlement to progress again.
    So, in the end you will still only have one building, regardless how many you may be able to queue at one time.










  4. #4
    Withwnar's Avatar Script To The Waist
    Join Date
    Oct 2008
    Location
    Earth
    Posts
    6,329

    Default Re: Creating a World - Building and maintaining unique Buildings

    Ah, so a disabled event counter condition does not remove it, just prevents it from beginning/continuing construction? Didn't know that.

  5. #5
    Gigantus's Avatar I am not special - I am a limited edition.
    Patrician Moderator Emeritus Administrator Emeritus

    Join Date
    Aug 2006
    Location
    Goa - India
    Posts
    53,095
    Blog Entries
    35

    Default Re: Creating a World - Building and maintaining unique Buildings

    It's the reason for this on\off script part (remember the event processing sequence discussion?):
    Code:
        ; Re-enable the event counter, if this isn't done the building will never complete.
        monitor_event ButtonPressed ButtonPressed end_turn
            if not I_IsFactionAIControlled england
                set_event_counter build_corn_exchange 1            ; enabling EDB event counter
            end_if
        end_monitor
    
        ; Disable the event counter again if building is queued to prevent more buildings being added
        monitor_event FactionTurnEnd FactionType england
            and not FactionBuildingExists = corn_exchange        ; building does not exist in faction
            if I_IsFactionAIControlled england                    ; terminate if faction is AI controlled
                terminate_monitor
            end_if
            if I_EventCounter corn_exchange_queued > 0            ; the building has been queued
                set_event_counter build_corn_exchange 0            ; disable EDB event counter
                inc_event_counter corn_exchange_queued 1        ; increasing the queue counter
            end_if
            if I_EventCounter corn_exchange_queued > 3            ; the building should have been finished (building turns plus one)
                set_event_counter build_corn_exchange 1            ; enable EDB event counter
                set_event_counter corn_exchange_queued 0        ; reset queue counter
            end_if
        end_monitor
    Last edited by Gigantus; November 20, 2015 at 05:49 AM.










  6. #6

    Default Re: Creating a World - Building and maintaining unique Buildings

    Gig, from all users explaining something, I find you are the best one which I understand easily.

    Please: What would to do if I want a message displaying something to the user? Can you add this to your code?
    Would be great! Thanks.

  7. #7
    Gigantus's Avatar I am not special - I am a limited edition.
    Patrician Moderator Emeritus Administrator Emeritus

    Join Date
    Aug 2006
    Location
    Goa - India
    Posts
    53,095
    Blog Entries
    35

    Default Re: Creating a World - Building and maintaining unique Buildings

    The code for adding a message would be like this, entries in the box brackets are variables. The faction section for the message will restrict the message to the faction(s) listed - you can omit it if the message is for all factions.
    Code:
    monitor_event [WhatEver]
        [more conditions here]
        [some commands added]
        
        historic_event [MessageName] factions { [faction name], }
        
    end_monitor
    In data\text\historic_events.txt
    Code:
    {MESSAGENAME_TITLE}Title of the message
    {MESSAGENAME_BODY}Message text










Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •