Small Code Question. (Or/And)

finaltidus

New member
I have a small question about some code that isn't working the way I want it too. I just can't see the issue so hopefully someone else can.

I have a script that checks for historical pricing and will mall search only if either of them are over 3 days but for some reason when I use "or", "(historical_age(i) > 3.0 || historical_age(j) > 3.0)" it will always mall search on a new session but if I replace or(||) with and(&&) it works how I would expect. I don't want && though because if something is a month old and another is a day old it still won't search. What am I missing here?

offending block:
Code:
foreach i in complexCandy{
	foreach j in complexCandy{
		if((i.to_int() + j.to_int()) % 5 == 0){
			if(item_amount(i) < 1 && !is_tradeable(i)){
				continue;
			}
			if(item_amount(j) < 1 && !is_tradeable(j)){
				continue;
			}
			if(historical_age(i) > 3.0 || historical_age(j) > 3.0){
				mallPrice1 = mall_price(i);
				mallPrice2 = mall_price(j);
			}else{
				mallPrice1 = historical_price(i);
				mallPrice2 = historical_price(j);
			}
			totalPrice = mallPrice1 + mallPrice2;
			if(totalPrice < currentPrice){
				currentPrice = totalPrice;
				candy1 = i;
				candy2 = j;
			}
		}
	}
}

Here is the entire code if it would help but it isn't much more than what's here: https://pastebin.com/27ziXPKQ
Also I just wrote this because I wanted untradeable candies used in synth first and had no idea how to default use them in mafia, and it's good practice I guess :p
 

Veracity

Developer
Staff member
Code:
			if(historical_age(i) > 3.0 || historical_age(j) > 3.0){
				mallPrice1 = mall_price(i);
				mallPrice2 = mall_price(j);
			}else{
				mallPrice1 = historical_price(i);
				mallPrice2 = historical_price(j);
			}
Using OR, if either historical age is older than 3.0, you will mall search both. If both are <= 3.0, you will use the historical price for both.

Using AND, if both historical ages are older than 3.0, you will mall search both. If either is <= 3.0 - even if the other is 50.0 - it will use historical price.

You probably want to separate them into two age checks. Something like this:

Code:
mallprice1 = historical_age(i) > 3.0 ? mall_price(i) : historical_price(i);
mallprice2 = historical_age(j) > 3.0 ? mall_price(j) : historical_price(j);
 

finaltidus

New member
It works! Thank you for the help Veracity. As a side note I always avoided using ternary operations in school because it was confusing at first but it is so much more elegant isnt it. ;)
 

ereinion

Member
To avoid using ternary operations all over my scripts, I define the function:

Code:
int get_price(item it, float max_age) {
    return ((historical_age(it) > max_age)? mall_price(it) : historical_price(it));
}

It saves a little bit of typing and I feel that it improves the ease of reading my code, but that may just be my personal preference.
 
Top