Foldables

Banana Lord

Member
What is the best way to test for the availability of a foldable (ie: how many in inventory, hagnk's, closet or DC)? Currently I'm using if(item_amount($[BLAH]) > 0) repeatedly to check for every form my foldable could be in. Basically I want to be able to treat a foldable as a single item rather than having to determine what form it's in before I can execute any commands that involve it. Please tell me that's not wishful thinking (LLK :O).
 
Code:
int amounts;
foreach it in $items[list of foldables to check for]
{
     if(item_amount(it) > 0
          amounts++;
}
print("You have " + amounts + " of this foldable in various forms.");

I think that should work, which is roughly the same as what you described above I guess... :)
 
I'm having connection problems, so I can't test it, but you could use this:
PHP:
boolean get_foldable( item goal )
{
	if ( available_amount( goal ) > 0 ) return retrieve_item( 1, goal );
	foreach form in get_related( goal, "fold" )
	{
		if( available_amount( form ) > 0 )
		{
			cli_execute("fold " + goal );
			return item_amount( goal ) > 0 ;
		}
	}
	return false;
}
 
slyz, that's practically word-for-word a function in one of my scripts, so I'm pretty darn sure it will work.
 
Seems strange that there's nothing built into mafia to handle this sort of thing more elegantly. Oh well, what Slyz posted (and what Bale would have posted :P) is a great improvement on my own attempt! Thanks guys!

A question about what you posted Winterbay: What's the ++ after amounts for? Also, I had no idea you could define a function like that (or whatever it's meant to be called... I was one lecture into my computer science paper before we got earthquaked >_<).
 
++ adds one to the variable. It's not really what he wanted, since you COULD have more than one of a particular form of the item. It wuld be better to simply add up the actual amounts. Thus:

amounts += item_amount(it);
 
++ adds one to the variable. It's not really what he wanted, since you COULD have more than one of a particular form of the item. It wuld be better to simply add up the actual amounts. Thus:

amounts += item_amount(it);

True. I've just never had more than one of a foldable so forgot about that possibility :)
Also, I notice that I forgot the closing parenthesis on the if-statement, as per usual when I code. I don't know how many times I've had a script crash on me for that reason...

++ and += are two of the rather few things I remember from my short foray into programming at high school where we had to use C and C++ a bit.

Also, Banana Lord, which part of the posted code is the definition you wasn't sure you could do? I didn't think I were using anything exotic :)
 
I meant a function, in that it only works when the code parses it and treats it as "original number = original number + 1" as opposed to failing. Though I don't remember currently what I was doing that had that fail... it may have been updated to work since, as there was the patch to update the various number possibilities.
 
Seems strange that there's nothing built into mafia to handle this sort of thing more elegantly.
get_related() is pretty much all you need. Also, what I posted actually folds, but by rereading your OP, I realized you only wanted to test for the availability of a foldable:
PHP:
boolean have_foldable( item f ) 
{ 
    if ( available_amount( f ) > 0 ) return true; 
    foreach form in get_related( f, "fold" ) 
        if( available_amount( form ) > 0 ) return true;
    return false; 
}
 
I meant a function, in that it only works when the code parses it and treats it as "original number = original number + 1" as opposed to failing. Though I don't remember currently what I was doing that had that fail... it may have been updated to work since, as there was the patch to update the various number possibilities.
I still have no idea what you mean - since ASH doesn't actually HAVE "++", although it does have "+=".
If it did, ++ would be an operator, not a "function".
 
> ash int test; test = 0; test += 1;

Returned: 1

> ash int test; test = 0; test++;

Expected ;, found + ()
Returned: void

I was trying to come up with a proper word for "the code that the source has to make this work" and the best one I could come up with for describing that code itself was that it was a function, that parses my request of ++ and turns that into the += 1 operation. I'm aware that the end result is an operation... I believe I'm just talking about a different portion of the process (the internal code, as opposed to the end experience). At least, that's what I hope I'm talking about. Otherwise, I have no clue where my brain is at. :)
 
You misunderstand what I said: there is no code "that parses my request of ++ and turns that into the += 1 operation". ++ and --, prefix and postfix, are on my eventual "to do" list, but they do not exist in ASH currently - as your code sample demonstrates.
 
A question about what you posted Winterbay: What's the ++ after amounts for? Also, I had no idea you could define a function like that (or whatever it's meant to be called... I was one lecture into my computer science paper before we got earthquaked >_<).

I think he's referring to ++ as defining, as opposed to it simply being a number function.

What I was trying to say was that ++, when it's coded as a function, affects numbers as a += 1 operator. I think my mental processing was that he was envisioning something like:
Code:
int ++(int number)
{
  number += 1;
  return number;
}

Which doesn't actually work with the + sign, but does sort of work like this...
Code:
[COLOR=#808000]> ash int plusplus(int number) { number += 1; return number; } 5.plusplus()[/COLOR]
 
Returned: 6
That sort of thing is just where my brain went in his original "define a function" statement... something like the plusplus function I just defined there. :) That is a function, right? A bit of code that can be called to run through a specific set of instructions? It ran 5 through its system, did += 1 to it, and returned the result... A user-made hack for ++ or -- behaviour would be a function, since it's not built into the math system, unless the math system is user-extendable in a way I haven't found yet.

Anyways, looking forwards to the eventual ++ and -- implementation.
 
You CANNOT write ++ as a function that behaves like a ++ operator.

The operator takes the operand - which must be a variable or aggregate reference, not a constant - adds one to it, stores the incremented value in the variable or aggregate slot, and then returns either the original value or the incremented value, depending on whether it was a prefix or postfix operator.

In ASH, function parameters are all call by value; you have no access to the variable or what have you that the caller uses to store that value - if any; the caller may pass an expression to the function and there is no variable. Once you are within the function, you have a variable holding the parameter value, and you can change that variable all you want, but doing so can have no effect whatsoever on the caller.

The function that you defined named "plusplus" is nothing of the sort. It's really "plusone".
 
Well, I called it plusplus because I changed the name from ++ to plusplus. I suppose I could have called it plussignplussign, but that would have taken longer to type. :D And been harder to read.

And yes, that's very true... ++ and -- behave differently based on whether they're in the prefix or postfix (suffix?) position. I was just trying to replicate Winterbay's suggested behaviour (which, as you pointed out, doesn't work yet) which used it as a postfix/suffix. :)
 
Back
Top