Multiple conditions in a while loop

All about the macro plugin can be found in this forum. This forum is intended for the macro plugin only.

Moderator: Moderators

Lethor
Noob
Noob
Posts: 6
Joined: 26 Jan 2014, 08:52
Noob?: No

Multiple conditions in a while loop

#1 Post by Lethor »

I got a problem within a while loop for an holy water automacro. I want the loop to continue while i have empty bottles in my inventory AND my sp are over 10. So i thought to do it like this:
while (@invamount(Empty Bottle) > 0 && $.sp > 10) as loop
and so on.
As soon as i write more than one condition for the while loop the loop just skips.
Well the whole macro would be:

Code: Select all

macro holywater {
do ai manual
do move 55 122 pay_arche
do talknpc 55 123 r1
pause 2
$weig1 = @eval($.maxweight/2)
$weig2 = @eval($weig1-$.weight)
$weig3 = @eval(int($weig2/2))
do storage get Empty Bottle $weig3
do storage close
do move 120 91
$emptys = @invamount(Empty Bottle)
while ($emptys > 0 && $.sp > 10) as loop
do ss 31
pause 1
$emptys--
pause 0.4
end loop
if (@storamount(Empty Bottle) > 0) goto stor

do ai auto
stop

:stor
do move 55 122 pay_arche
do talknpc 55 123 r1
pause 2
do storage add Holy Water
do storage close
pause 2
call holywater


}
Could anyone help me on this?
Kaspy
Halfway to Eternity
Halfway to Eternity
Posts: 398
Joined: 08 Jun 2012, 15:42
Noob?: No
Location: Brazil

Re: Multiple conditions in a while loop

#2 Post by Kaspy »

Viewing the source of the Macro Plugin

Code: Select all

	##########################################
	# while statement: while (foo <= bar) as label
	} elsif ($line =~ /^while\s/) {
		my ($first, $cond, $last, $label) = $line =~ /^while\s+\(\s*"?(.*?)"?\s+([<>=!]+?)\s+"?(.*?)"?\s*\)\s+as\s+(.*)/;
		if (!defined $first || !defined $cond || !defined $last || !defined $label) {$self->{error} = "$errtpl: syntax error in while statement"}
		else {
			my $pfirst = parseCmd($first, $self); my $plast = parseCmd($last, $self);
			if (defined $self->{error}) {$self->{error} = "$errtpl: $self->{error}"; return}
			unless (defined $pfirst && defined $plast) {$self->{error} = "$errtpl: either '$first' or '$last' has failed"}
			elsif (!cmpr($pfirst, $cond, $plast)) {$self->{line} = $self->{label}->{"end ".$label}}
			$self->{line}++
		}
		$self->{timeout} = 0
The while command does not support multiple parameters.
You need to do something like

Code: Select all

while ($emptys > 0) as loop1
	while (&& $.sp > 10) as loop2
		do ss 31
		pause 1
		$emptys--
		pause 0.4
	end loop2
end loop1
By the way, why not use a command block in the if? The code will be more readable.

Code: Select all

if (@storamount(Empty Bottle) > 0) {
	do move 55 122 pay_arche
	do talknpc 55 123 r1
	pause 2
	do storage add Holy Water
	do storage close
	pause 2
	call holywater
} else {
	do ai auto
	stop
}
Result:

Code: Select all

macro holywater {
	do ai manual
	do move 55 122 pay_arche
	do talknpc 55 123 r1
	pause 2
	
	$weig1 = @eval($.maxweight/2)
	$weig2 = @eval($weig1-$.weight)
	$weig3 = @eval(int($weig2/2))
	
	do storage get Empty Bottle $weig3
	do storage close
	do move 120 91
	$emptys = @invamount(Empty Bottle)
	
	while ($emptys > 0) as loop1
		while (&& $.sp > 10) as loop2
			do ss 31
			pause 1
			$emptys--
			pause 0.4
		end loop2
	end loop1
	
	if (@storamount(Empty Bottle) > 0) {
		do move 55 122 pay_arche
		do talknpc 55 123 r1
		pause 2
		do storage add Holy Water
		do storage close
		pause 2
		call holywater
	} else {
		do ai auto
		stop
	}
}
Image
Lethor
Noob
Noob
Posts: 6
Joined: 26 Jan 2014, 08:52
Noob?: No

Re: Multiple conditions in a while loop

#3 Post by Lethor »

Thanks that helped