• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • ✅ Time to vote for the top 3 models! The POLL for Hive's 6th HD Modeling Contest: Mechanical is now open! 📅 Poll close on July 16, 2024! 🔗 Cast your vote now!

How to create an efficeint, continuous "Event" system

Status
Not open for further replies.
Level 4
Joined
Jan 27, 2016
Messages
89
Ive been thinking of this for a while and have tried a few times, but I can never do it in a satisfyingly efficient way. The event system I describe would function like this:

>It would trigger after every certain amount of time, lets say every 60 seconds between 1 and 3 events fire.

>The actions of the events would have very varied requirements, such as say the player having 100 or less gold, or him having at least 10 buildings near the base, or another event having fired before it.

>The events would have a varied chance rate, so one would have a 10 in 200 (or any amount that totals the total "chances" of all events) chance of fireing (if its conditions are met) while another would have a 2 in 200

>The actions for the events would all be in a single trigger, as the amount of triggers to fulfill what I need would otherwise be very large

I can manage to make the skeleton for this easily, the problems are that it effectively becomes a massive range of If/Then/Else scripts that would keep firing while the trigger tries to find the Event that it was told to fire, then if its conditions are met it will fire it, otherwise the loop starts anew. Is there a more efficient way to accomplish all of this fulfilling all the listed conditions?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,233
Separate triggers would probably be the most efficient. They could be stored in trigger arrays which a single periodic trigger can then use to select and run from. A separate real/integer array could be used for probability. Other arrays could be used for other common conditions if required.
 
Level 4
Joined
Jan 27, 2016
Messages
89
So it would check all the conditions through an array and then check which events can run, seems fine, but how would the probability thing work? Set a random number between 1 and X events, if its one of the checked numbers then do another check for probability? Could you elaborate a bit on that?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,233
Depends how you want it to work. If you want to select a random event to try and do nothing if that fails then you pick a random integer between 0 and total weight where total weight is the sum of all probability weights of all events. You then iterate through all the events, decrementing the random integer value by the weight and testing if that integer becomes less than or equal to 0 in which case you execute that event.

If you want it to randomly select from the set of valid events (conditions are met) then the same algorithm applies except you filter an initial list into another list which you then perform the random lookup on. If you want it to select multiple such random event lookups but not allow for duplicate results then after every lookup you remove the resulting event from the list.

One could use a binary search tree instead of linear search by storing each event with its sequential weight as well as the event weight which defines a range of random numbers for which the event should be selected. This scales a lot better due to the nature of binary search however it should only really be considered if you are looking at hundreds or thousands of such events.

It is important to adjust total weight when adding/removing elements from the list. If using the linear lookup approach the list order does not matter.
 
Status
Not open for further replies.
Top