Welcome, Guest. Please login or register.

Author Topic: Comparing Multiple math_counter's  (Read 10378 times)

Offline svbg869

  • SolidSurf Elite
  • Sr. Member
  • ******
  • Posts: 974
  • [insert witty comment hear]
    • View Profile
Comparing Multiple math_counter's
« on: March 31, 2010, 08:42:05 PM »
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
READ THOUGH THE WHOLE TUTORIAL BEFORE YOU START
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
THIS IS ALL TEXT OK? I'M LAZY, DEAL WITH IT

TL;DR? to bad

This seems like an easy thing to do, you have 3 or more math counters and want to figure out which of them is bigger then the rest. Well, with 3 it is easy, and you can probably figure it out, but once you get into 6 or more the amount of logic_compare's to compare each of them to each other one is just insane, so not only will I show you how to compare multiple math_counter's, but also what i think is the best way to do it.

First you need to know some basic logic, and that is this: if we compare math_counter #1 to math_counter #2 do we need to compare math_counter #2 to math_counter #1?
the answer is obviously no. But this is a vary important concept if your trying to save yourself some entity space.

For this tutorial, I will show you how to compare 12 math_counters, AND fire an output based on which once is the biggest

this is because, when I made this system, I made it for a map, the map had a voting system built into it, so players could choose one of the 12 "levels" within the map. the map would tally the votes (after a time) then teleport all players to the selected level. (if your curious, that map was surf_XII, i wont link to it in the tutorial, because thats not what I'm hear for.)

first off, you need your math_counters, as I said, this system will work for any amount of math counters
you are going to need two math_counters for each possible outcome. In my example, there are 12 levels the players vote for (you could use this system for something other then voting, use your imagination), so I need 24 math_counters
now, half of the math counters are the actual tally, how many votes there are for area #
the keyvalues of these will depend on what your tallying, in my example each player can only vote once, so everything is default except for the name, and the Maximum Legal Value
I'm going to use a descriptive name, "area_1_tally" this way i know exactly what the entity is for, without looking at it (or thinking about it)
for my example, the maximum legal value is "64" because servers with more then this are rare (and even 64 is rare) this will depend on what your tallying

now, you need a math_counter with the same settings as that one for each possibility, so copy and paste it, changing the name to "area_2_tally" then "area_3_tally" and so on, till you have however many you need, in my case the last one is "area_12_tally"

time for a crash course in how to use the handy entity known as logic_compare:
this entity compares two values, only two. and then fires a output, that output can be OnGreaterThen, OnEquelTo, and OnLessThen. Were only going to use the first and last possibility.
IMPORTANT!!!!!
the logic_compare compares the "Value" to the "Compare Value" this is important because if we mixed it up the outputs would be opposite what we want, we want to compare area 1 to area 2, and not the other way around
if we mixed this up, all sorts of shenanigans would ensue

the logic compares for area 1 are easy, there are 11 (there will always be 1 less logic compare for area 1 then the total # of possibility's) of them, and for now their all the same (except for name)
keyvalue : amount
name : area_1_to_area_2_compare
again, a descriptive name, i know exactly what this entity does without looking at it
thats all for now with the logic compare, make the rest of the 11, changing the second number up by 1 each time
name : area_1_to_area_3_compare
name : area_1_to_area_4_compare
...
name : area_1_to_area_12_compare
now, make a second math_counter. your going to need a duplicate math_counter for each area, might as well make them now.
keyvalue : amount
name : area_1_compare_tally (once more a descriptive name, i know what this entity does, it tally's how many of the compares output an OnGreaterThen output)
Maximum Legal Value : 11

name : area_2_compare_tally
Maximum Legal Value : 11

name : area_3_compare_tally
Maximum Legal Value : 11
...
name : area_11_compare_tally
Maximum Legal Value : 11

now, we need to add an output to each of the logic_compare's we made for area 1
OnGreaterThen : area_1_compare_tally : add : 1
once you have that in one of the entity's, hit the copy button in middle/bottom of the window
then select each other logic_compare's (either separately or at once) and hit paste

one last thing then we can work on area 2
the math_counter (area_1_tally) needs to set the value of each of the compares that start with area_1
output:
OutValue : area_1_to_area_2_compare : SetValue
OutValue : area_1_to_area_3_compare : SetValue
OutValue : area_1_to_area_4_compare : SetValue
...(their the same, you can figure it out)
OutValue : area_1_to_area_12_compare : SetValue

there will be a total of 11 outputs, thats important because each math_counter (area_#_tally) will have 11 outputs (each different of course)

so, onto area 2
you already should have its first math_counter, so we can jump to making the logic_case's for this area
now, for this area you only need 10 logic cases, and the reason is simple, we already compare area 1 to area 2 so why compare area 2 to area 1? theres no point, so one less logic compare, and we don't compare area 2 to its self, thats another one, 2 total, so area 2 will always have 2 less logic_compares then the total # of areas
the logic compares are similar to the ones for area one, just they start at
name : area_2_to_area_3_compare
name : area_2_to_area_4_compare
name : area_2_to_area_5_compare
...
name : area_2_to_area_12_compare

you already should have the math counter "area_2_compare_tally" made, so now they each just need the output:
OnGreaterThen : area_2_compare_tally : add : 1

and the math_counter "area_2_tally" needs 10 outputs:
OutValue : area_2_to_area_3_compare : SetValue
OutValue : area_2_to_area_4_compare : SetValue
OutValue : area_2_to_area_5_compare : SetValue
...
OutValue : area_2_to_area_12_compare : SetValue
but wait, thats only 10! we need 11.
well, now we start setting compare values, so one more output:
OutValue : area_1_to_area_2_compare : SetCompareValue

but theres one other problem, we only have 10 logic_compares adding to our "area_2_compare_tally" math_counter we need another one, and this is where the first areas entity's come in.
the logic_compare "area_1_to_area_2_compare" needs another output, to bring "area_2_compare_tally" to 11
so we add the output:
OnLessThen : area_2_compare_tally : add : 1
that output does the same as an OnGreaterThen output would have done were it coming from an entity comparing area 2 to area 1

now, area 3's entity's
you already should have both of the math_counters for area 3
now you just need the logic_compares
for area 3 there are 3 less logic_compare's then then total # of areas
so there are 9, make the first one, named
"area_3_to_area_4_compare"
and the output:
OnGreaterThen : area_3_compare_tally : add : 1
then copy and paste it, changing the second # in the name each time
"area_3_to_area_5_compare"
"area_3_to_area_6_compare"
...
"area_3_to_area_12_compare"

and the "area_3_tally" math_counter need to set the value of each of these logic_compares, so add the outputs:
OutValue : area_3_to_area_4_compare : SetValue
OutValue : area_3_to_area_5_compare : SetValue
...
OutValue : area_3_to_area_12_compare : SetValue

the two logic_compares that have 3 as their second # need to have their comparevalue set
so two more outputs:
OutValue : area_1_to_area_3_compare : SetCompareValue
OutValue : area_2_to_area_3_compare : SetCompareValue

and we need to add an output to both of them (the logic_compares):
OnLessThen : area_3_compare_tally : add : 1

now, the rest is just doing this over and over again, the # of logic_compares for each area can be found by subtracting that area's # from the total # of area's
and the outputs for the area_#_tally are simple, any logic_compare with that area's # first in the name, you use the SetValue, and if that areas # is second in the name you use the SetCompareValue
also note that EACH math_counter (area_#_tally) will have (the total # of areas -1) outputs (in this case, 11)

now, if you don't get what this does, it adds to a math_counter every time "area X" is greater then "area Y"
and that math_counter has a max of one less then the total # of areas, so, if area X is greater then all other areas it will hit its max, and you can fire it as an output. please note that, you still need to add to each area_#_tally (can be done many ways) and you need to set up each OnHitMax output (area_#_compare_tally)

if you guys need me to write out how to do the rest of it i will, but really you should be able to

there are only 2 other things you need:
a logic_relay with a whole lot of outputs:
OnTrigger : area_1_to_area_2_compare : Compare
OnTrigger : area_1_to_area_3_compare : Compare
OnTrigger : area_1_to_area_4_compare : Compare
...
OnTrigger : area_1_to_area_12_compare : Compare
OnTrigger : area_2_to_area_3_compare : Compare
OnTrigger : area_2_to_area_4_compare : Compare
... and so on for EVERY logic_compare

and what ever you want to happen based on which tally is the biggest

and thats all there is to it!

Offline S.T.A.L.K.E.R.

  • The Sexiest Man "Alive"
  • SolidSurf Elite
  • Elder Statesman
    of the Surf
  • ******
  • Posts: 2,830
  • daddy is home :^)
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #1 on: March 31, 2010, 09:07:56 PM »
+1 dude, awesome, AWESOME job.

Offline Lightning

  • Sr. Member
  • ****
  • Posts: 545
  • There's no need to elaborate.
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #2 on: March 31, 2010, 11:08:38 PM »
+1 dude, awesome, AWESOME job.

Sorry Ryan that i quote your text
but yeah very awesome work!

Offline silverlol

  • Sr. Member
  • ****
  • Posts: 580
  • super mapper!!
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #3 on: April 01, 2010, 10:24:12 AM »
hooooly shit, very nice job svbg!

Offline RazerSurf

  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,776
  • mapper of surfs
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #4 on: April 01, 2010, 11:21:30 AM »
Sheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeit.... This seems incredibly confusing and hard(at least for me).

Pictures would've helped tho :/

But still, it is a good tutorial, and props for knowing this shit haha \

Offline FAJ

  • Flying Badger Man
  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,925
  • GAJ
    • View Profile
    • NY-Surf
Re: Comparing Multiple math_counter's
« Reply #5 on: April 01, 2010, 11:44:22 AM »
Holy shit, thats a long, well-thought out tutorial. I am tired while reading this, so I couldn't really read into how it handles multiple ties.

But have you heard of occam's razor?

The FAJ way:
- Have one counter for each possibility
- When someone chooses their area, increment all other counters by +1
- When you want to detect which choice wins, you quickly, by timer, subtract increments of 1 from all counters
- Each counter has onHitMin, to disable all other area's counters, the timer, and to do its own thang.

1 counter per area and one timer overall. Wouldn't be too hard to add in a random-winner choice between the ties.

Offline RazerSurf

  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,776
  • mapper of surfs
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #6 on: April 01, 2010, 12:09:01 PM »
But have you heard of occam's razEEEEEr?

No? What is that?

Offline svbg869

  • SolidSurf Elite
  • Sr. Member
  • ******
  • Posts: 974
  • [insert witty comment hear]
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #7 on: April 01, 2010, 03:04:08 PM »
my way is to have a tally for each compare, and when a area is found to be greater then another area, that area gets +1 (compare tally) the compare tally has a max, so onhitmax you know that the area is grater then all others

and to save some entity's each and every logic_compare has both an onlessthen and ongreater then output
instead of having several logic_compares for each area (this also mean each area has 1 less logic_compare then each previous area)

EDIT:
dosent handle ties, just finds the greatest
if i really wanted to i could add in that ability, but i dont want to

Offline FAJ

  • Flying Badger Man
  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,925
  • GAJ
    • View Profile
    • NY-Surf
Re: Comparing Multiple math_counter's
« Reply #8 on: April 01, 2010, 04:13:04 PM »
Does it not fire anything when the greatest is a tie? It will not put a tally to the max in the case of a tie, I assume.

Offline S.T.A.L.K.E.R.

  • The Sexiest Man "Alive"
  • SolidSurf Elite
  • Elder Statesman
    of the Surf
  • ******
  • Posts: 2,830
  • daddy is home :^)
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #9 on: April 01, 2010, 04:37:28 PM »
Does it not fire anything when the greatest is a tie? It will not put a tally to the max in the case of a tie, I assume.
I think it picks the one that had the most first? I don't know though I've only tested the system with 1 person as of now.

Offline FAJ

  • Flying Badger Man
  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,925
  • GAJ
    • View Profile
    • NY-Surf
Re: Comparing Multiple math_counter's
« Reply #10 on: April 01, 2010, 04:41:43 PM »
I still think it is over-complicated; there must be a more elegant solution. (and maybe more sophisticated than mine haha)

Offline svbg869

  • SolidSurf Elite
  • Sr. Member
  • ******
  • Posts: 974
  • [insert witty comment hear]
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #11 on: April 01, 2010, 06:04:52 PM »
personally i dont like how yours works FAJ, find mine more simple

but thats just preference, but anyways

no nothing would be fired in the event of a tie, as none of the math_counters would hit max

Offline FAJ

  • Flying Badger Man
  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,925
  • GAJ
    • View Profile
    • NY-Surf
Re: Comparing Multiple math_counter's
« Reply #12 on: April 01, 2010, 06:38:31 PM »
I can understand if you don't like the way mine works, but you are out of your mind if you say yours is more simple. If not for the 1,500 word essay, then for the sheer number of entities and actual mapping-time.

I would be curious if there were a solution as simple/small as mine, that worked instantly, instead of waiting a few moments.

Offline S.T.A.L.K.E.R.

  • The Sexiest Man "Alive"
  • SolidSurf Elite
  • Elder Statesman
    of the Surf
  • ******
  • Posts: 2,830
  • daddy is home :^)
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #13 on: April 01, 2010, 07:06:14 PM »
I can understand if you don't like the way mine works, but you are out of your mind if you say yours is more simple. If not for the 1,500 word essay, then for the sheer number of entities and actual mapping-time.

I would be curious if there were a solution as simple/small as mine, that worked instantly, instead of waiting a few moments.
To be honest I like the few second wait, it gives you a second to catch yourself before you're thrown into something else. The system I used sits you in a black box for 2 seconds then picks whatever was highest.

Offline FAJ

  • Flying Badger Man
  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,925
  • GAJ
    • View Profile
    • NY-Surf
Re: Comparing Multiple math_counter's
« Reply #14 on: April 01, 2010, 08:35:57 PM »
The best I could come up with, procrastinating an hour at work, was this (just 2 entities over the counters, and instant result without waiting for my ghetto-method):

one counter per area
one compare overall
one extra counter, to hold a "max_value"

You increment each counter, to add to its value
Every time you add, outvalue to setvaluecompare to the counter
If greater than, setvalue to the max_value counter, and setcomparevalue to the compare

basically keeps track of the max as you go along. everytime a counter increases, it checks if it is bigger than the current max. If it is, it changes the current max. An easy way to use this 'max' number is to (when you want to check) setmaxvalue of all the counters to the max value, plus one. Then add one to all the counters. The maximum counter(s) will fire its onhitmax.

so, however many math_counters you want have this:
outValue > (compare_entity_name) > setvaluecompare

the logic_compare
onGreaterThan > (max_value_counter_name) > setValueNoFire
onGreaterThan > (compare_entity_name) > setCompareValue

this will leave you with a max_value counter that is always equal to the maximum number held by those counters. Whenever you want to use it to trigger an event, you can do that how you like.

Offline unt0uch4bl3

  • Full Member
  • ***
  • Posts: 357
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #15 on: April 01, 2010, 09:14:17 PM »
this will leave you with a max_value counter that is always equal to the maximum number held by those counters.
This method will work for ryan (and i suggested he should use it because this method handles ties - sort of), however it won't be particle for counting players (referring to svbg's map system) because this method does not compensate for subtraction of counters.

But counting on the go is a cool idea and I like it because it uses as little entities as possible.
« Last Edit: April 01, 2010, 09:44:55 PM by unt0uch4bl3 »


Offline FAJ

  • Flying Badger Man
  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,925
  • GAJ
    • View Profile
    • NY-Surf
Re: Comparing Multiple math_counter's
« Reply #16 on: April 01, 2010, 09:50:50 PM »
true, if you need to factor in subtraction, you would have to forgo keeping track the whole time, and just force a sequential test of the counters at the end. Same number of entities, just different way of counting with them.

P.S. to handle ties, just set use the same onHitMax method, but have that output just enable a starts-disabled relay. Then you can use a logic_case to randomly trigger relays until one works. Luckily the case can choose randomly with no repeats, so the process should take less than a second.
« Last Edit: April 01, 2010, 09:53:22 PM by FAJ »

Offline SintaxError

  • Admin
  • Elder Statesman
    of the Surf
  • *****
  • Posts: 1,731
  • There's an error in your sintax.
    • View Profile
    • SolidSurf
Re: Comparing Multiple math_counter's
« Reply #17 on: April 01, 2010, 10:37:06 PM »
oh my jesus you guys... give me some time and I'll show you what's up.

Edit: What's up is Valve sucks apparently, and I can't do what I wanted with CSS, but I (probably) could with an Orange Box game.  Anyway, I'm not going to be redesigning it tonight...
« Last Edit: April 02, 2010, 03:42:16 AM by SintaxError »

Offline unt0uch4bl3

  • Full Member
  • ***
  • Posts: 357
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #18 on: April 04, 2010, 02:09:20 AM »
I can't do what I wanted with CSS, but I (probably) could with an Orange Box game. 

Do explain

Offline svbg869

  • SolidSurf Elite
  • Sr. Member
  • ******
  • Posts: 974
  • [insert witty comment hear]
    • View Profile
Re: Comparing Multiple math_counter's
« Reply #19 on: April 04, 2010, 02:30:02 AM »
pretty sure they didn't add any new logic entities in for the orange box

except for that one, looking thing, has an output when you look somewhere