Author Topic: [Code] [WB] Dynamic Villages and Scene Props Iterator  (Read 2624 times)

0 Members and 1 Guest are viewing this topic.

Lav

  • Knight
  • *
    • View Profile
  • Faction: Vaegir
  • MP nick: Lav
  • M&BWB
[Code] [WB] Dynamic Villages and Scene Props Iterator
« on: July 26, 2011, 05:33:44 PM »
This code is intended to do one thing: provide module authors with the ability to quickly process scene props at the beginning of a mission, removing some of them from view, or doing something else but equally wicked to them poor propsies.

My original motivator was to make villages more dynamic. So you could build a Messenger Post in the village, then go to village center and voila - there's a Messenger Post standing right there! The main idea was to use variation ID numbers, which are not used by the game anywhere except in the main tutorial. However even when I implemented the very first version of the code, it was already much greater than that, and allowed for pretty generic manipulation of objects on any mission.

Below are two version of the code. Both are tested and confirmed to work. The first code sample explains the basic idea of how to make dynamic scene editing available to everyone, however it's recommended to use the second code sample as it's much more versatile and powerful.

(click to show/hide)

(click to show/hide)

My sincere thanks to:

MadocComadrin for the idea to use ti_on_scene_prop_init triggers, which eradicated the performance problem.
Caba`drin and Vorrne for advice and suggestions.

(click to show/hide)

Dawg of War

  • Sergeant Knight at Arms
  • *
  • Silverstag Dev Team
    • View Profile
  • Faction: Nord
  • MP nick: Dawg_of_War
  • M&BWBWF&S
Re: Dynamic Villages
« Reply #1 on: July 26, 2011, 05:41:18 PM »
saaaaaweeet   :)

Sayd Ûthman

  • Language Moderator
  • *
  • Battle of Europe Developer
    • View Profile
  • Faction: Neutral
  • MP nick: Sayd_uthman
  • WB
Re: Dynamic Villages
« Reply #2 on: July 26, 2011, 05:44:02 PM »
That's awesome ! works also for sieges and other scenes ??

Lav

  • Knight
  • *
    • View Profile
  • Faction: Vaegir
  • MP nick: Lav
  • M&BWB
Re: Dynamic Villages
« Reply #3 on: July 26, 2011, 05:46:41 PM »
Of course. Insert trigger into appropriate mission template and you are good to go.

Though honestly, I would buy my Warband a second time if only Taleworlds implemented try_for_scene_props.

That would be SO. MUCH. FASTER.

Sayd Ûthman

  • Language Moderator
  • *
  • Battle of Europe Developer
    • View Profile
  • Faction: Neutral
  • MP nick: Sayd_uthman
  • WB
Re: Dynamic Villages
« Reply #4 on: July 26, 2011, 05:49:03 PM »
You use a try_for_range scene_props_begin,scene_props_end instead :D

GetAssista

  • Grandmaster Knight
  • *
  • TLD corovan robber
    • View Profile
  • Faction: Neutral
  • M&BWF&S
Re: Dynamic Villages
« Reply #5 on: July 26, 2011, 05:55:47 PM »
I supose using a troop array  (with slots =0/1 as the mark for removal and slot number equal to scene prop id ) and iterating through this array with acting only on slot=1 will spare you the necessity of getting instances for the props you dont want to remove.

Setting 1 and 0 should be done elsewhere ofc

Lav

  • Knight
  • *
    • View Profile
  • Faction: Vaegir
  • MP nick: Lav
  • M&BWB
Re: Dynamic Villages
« Reply #6 on: July 26, 2011, 05:59:20 PM »
You use a try_for_range scene_props_begin,scene_props_end instead :D
Makes sense, thanks. Fixed the script.

I supose using a troop array  (with slots =0/1 as the mark for removal and slot number equal to scene prop id ) and iterating through this array with acting only on slot=1 will spare you the necessity of getting instances for the props you dont want to remove.

Setting 1 and 0 should be done elsewhere ofc
That's another way to do things, but this way you cannot delete some instances of a scene prop, you will be deleting all of them. And I don't expect it to be much faster, as you still have to iterate through entire scene_props array.

GetAssista

  • Grandmaster Knight
  • *
  • TLD corovan robber
    • View Profile
  • Faction: Neutral
  • M&BWF&S
Re: Dynamic Villages
« Reply #7 on: July 26, 2011, 06:12:21 PM »
That's another way to do things, but this way you cannot delete some instances of a scene prop, you will be deleting all of them. And I don't expect it to be much faster, as you still have to iterate through entire scene_props array.
Oh, nothing an additional array (that holds e.g. bitmask of instances you want to delete) can't fix.

And key to speed is not number of iterations, but how fast unnecessary iterations are skipped. In your script, you need script_scene_prop_needs_removal called as fast as possible. But since you need those instances for the script, you are left with the necessity of checking each and every prop

Lav

  • Knight
  • *
    • View Profile
  • Faction: Vaegir
  • MP nick: Lav
  • M&BWB
Re: Dynamic Villages
« Reply #8 on: July 26, 2011, 06:32:47 PM »
Oh, nothing an additional array (that holds e.g. bitmask of instances you want to delete) can't fix.

And key to speed is not number of iterations, but how fast unnecessary iterations are skipped. In your script, you need script_scene_prop_needs_removal called as fast as possible. But since you need those instances for the script, you are left with the necessity of checking each and every prop
Can you write your own version of the code? Because if I understand you correctly, there is a nasty side-effect with your approach - scene and related script (bitmask) become a single entity, and a person editing the scene no longer can make any modifications without being forced to make corresponding modifications to the code as well.

GetAssista

  • Grandmaster Knight
  • *
  • TLD corovan robber
    • View Profile
  • Faction: Neutral
  • M&BWF&S
Re: Dynamic Villages
« Reply #9 on: July 26, 2011, 06:42:18 PM »
Can you write your own version of the code?
Nope, sorry, busy with other things
Because if I understand you correctly, there is a nasty side-effect with your approach - scene and related script (bitmask) become a single entity, and a person editing the scene no longer can make any modifications without being forced to make corresponding modifications to the code as well.
Not if he uses variation IDs as powers of 2, and bitmask directly reflects them (I assume that one has no restrictions on var id he can assign to a prop

Lav

  • Knight
  • *
    • View Profile
  • Faction: Vaegir
  • MP nick: Lav
  • M&BWB
Re: Dynamic Villages
« Reply #10 on: July 26, 2011, 06:45:05 PM »
I assume that one has no restrictions on var id he can assign to a prop
Don't. You are limited to 0..127 range. Just seven bits.

And yeah, now that I understand your idea - no, it wouldn't be faster at all even if it was possible. Bitmask or not, but you must iterate through all scene props to remove marked ones, and there's no way to speed this up but to reduce the range of scene prop types checked for removal (that is, playing with scene_props_start and scene_props_end constants).

GetAssista

  • Grandmaster Knight
  • *
  • TLD corovan robber
    • View Profile
  • Faction: Neutral
  • M&BWF&S
Re: Dynamic Villages
« Reply #11 on: July 26, 2011, 07:24:56 PM »
And yeah, now that I understand your idea - no, it wouldn't be faster at all even if it was possible. Bitmask or not, but you must iterate through all scene props to remove marked ones
Yes, it would be faster. I would iterate through slots in an array, not through actual props. It would spend time only on actually replacing props.

7 variations seem fine for the vast majority of practical purposes, or you can use 6 variations and remaining bit to indicate mass removal (all props of that kind)

MadocComadrin

  • Water-Borne Annelid
  • Grandmaster Knight
  • *
  • ...et cum iustis non scribantur.
    • View Profile
  • Faction: Neutral
  • MP nick: MadocComadrin
  • M&BWBWF&S
Re: Dynamic Villages
« Reply #12 on: July 26, 2011, 08:34:00 PM »
Ideally, the fastest way (run-time) would be to have each prop store it's instance id at initialization time into a dummy troop array; then manipulate them by iterating over the array. (however, I'm not 100% sure if the init trigger is called early enough that you can use them in conjunction with the proper mt triggers to remove the props, can I get a solid yes/no on this?).

The only setback is that each scene prop would need to have that trigger added, but you can mange that easily with a little python at the end of the file.
Resident Wereparakeet - Tasty and Dangerous Pastry
Madmin - Sophistacted tools for responsible admins


cmpxchg8b

  • Modder++
  • Grandmaster Knight
  • *
  • Master of rglPool
    • View Profile
  • Faction: Vaegir
  • MP nick: cmp
  • M&BWBWF&SNW
Re: Dynamic Villages
« Reply #13 on: July 26, 2011, 08:56:26 PM »
It's slow because of fail implementation of scene_prop_get_num_instances and scene_prop_get_instance (both have O(n) complexity).
Add that to two nested loops... :lol:
Warband Script Enhancer v3.1.5 - additional operations, game scripts and triggers for Warband modders

Lav

  • Knight
  • *
    • View Profile
  • Faction: Vaegir
  • MP nick: Lav
  • M&BWB
Re: Dynamic Villages
« Reply #14 on: July 26, 2011, 10:20:22 PM »
Ideally, the fastest way (run-time) would be to have each prop store it's instance id at initialization time into a dummy troop array; then manipulate them by iterating over the array. (however, I'm not 100% sure if the init trigger is called early enough that you can use them in conjunction with the proper mt triggers to remove the props, can I get a solid yes/no on this?).

The only setback is that each scene prop would need to have that trigger added, but you can mange that easily with a little python at the end of the file.
Now that's an idea!

There are before_mission_start and after_mission_start triggers for mission templates, and scene_prop_init for scene props. I have edited all three to send messages and confirmed that after_mission_start fires AFTER all of scene_prop_inits. I assume before_mission_start fires first, though cannot confirm at the moment.

So I changed the code completely. Now, there's a before_mission_start trigger which clears the dummy troop array, scene_prop_init trigger on every scene_prop which checks it's variation ID and adds it to the dummy troop array if necessary, and finally after_mission_start trigger which iterates through dummy troop array, checking all scene prop instances and deleting them where appropriate.

Except it didn't work. :-(

Installing the check and removal code directly into scene_prop_init trigger didn't work as well.

Oh well, seems some exhaustive testing and debugging is planned for tomorrow.