Foreach group

Grabuge

Member
Hi. I have a list of item that are grouped in group.

Code:
record item_info {
	item it;
	effect ef;
	string exclusive; // each item is given a group; groupA, groupB etc
	float boostA;
	float boostB;
        float boostC
	int turns; // number of turns the effect last
};

I made a new record containing the best of each group and then calculate the value for each group and store the best one. At the moment I duplicat the code for each group but it is starting to be unmanageable as I add more group. Is there a way to do this? I tried adding foreach exclusive before doing foreach item but it saves everything in one record and I end up with one item that is the best of all group.
Code:
	record MutualyExcl{
        item it;
	int bestvalue;
        };

	MutualyExcl bestgroupA;
	bestGroupA.bestvalue=0;
	MutualyExcl bestgroupB;
	bestgroupB.bestvalue=0;
	MutualyExcl bestgroupC;
	bestGroupC.bestvalue=0;

	foreach it,value in Item_map {
	int boost = to_float(( ValueA * value.boostA + ValueB * value.boostB + ValueC * value.boostC ) * value.turns);
	    if (value.exclusive=="GroupA"){
		if(bestgroupA.bestvalue  < boost | bestgroupA.bestvalue == 0)
                    bestgroupA.bestvalue = profit; bestgroupA.it=value.it;
             }
	    if (value.exclusive=="GroupB"){
		if(bestgroupB.bestvalue  < boost | bestgroupB.bestvalue == 0)
                    bestgroupB.bestvalue = profit; bestgroupB.it=value.it;
             }
	    if (value.exclusive=="GroupC"){
		if(bestgroupC.bestvalue  < boost | bestgroupC.bestvalue == 0)
                    bestgroupC.bestvalue = profit; bestgroupC.it=value.it;
             }
	}

	print("The best is "+bestgroupA.it+" for a boost of "+bestgroupA.bestvalue);
	print("The best is "+bestgroupB.it+" for a boost of "+bestgroupB.bestvalue);	
	print("The best is "+bestgroupC.it+" for a boost of "+bestgroupC.bestvalue);

I tried to figured out how ZapWizard does it, but it's a bit overwhelming.

Thanks

--Grab
 

digitrev

Member
Use maps. This is not in any way tested, nor is it guaranteed to work. Run at your own peril.

Code:
record item_info {
   item it;
   effect ef;
   string exclusive;
   float boost;
   int turns; // number of turns the effect last
};

item_info[int] Item_map; //at least, I'm assuming that's the nature of Item_map

string[int] groups; 
groups[0] = "groupA";
groups[1] = "groupB";
groups[2] = "groupC"; // add as necessary

int[string] Values;
Values["groupA"] = ValueA;
Values["groupB"] = ValueB;
Values["groupC"] = ValueC; // and so on

record MutualyExcl{
   item it;
   int bestvalue;
}

foreach i, s in groups {
   MutualyExcl best;
   foreach it, value in Item_map {
      int boost = 0;
      if (value.exclusive == s) boost = Values[s]*value.boost*value.turns;
      if (best < boost) {
         best.bestvalue = profit; // I'll assume that profit is defined elsewhere
         best.it = value.it;
      }
   }
   print("The best is "+best.it+" for a boost of "+best.bestvalue);
}
 
Last edited:

Grabuge

Member
Yes that should be boost not profit. Totally not old code from where ever I'm steali... taking my inspiration for this script.
 

xKiv

Active member
Why not

Code:
...

MutualyExc1[string] bestByGroup;

foreach it,value in Item_map {
 int boost = ...;
 MutualyExc1 bestgroup = bestByGroup[value.exclusive];
 if (bestgroup.bestvalue < boost || bestgroup.bestvalue == 0) { bestgroup.bestvalue = boost; bestgroup.it=value.it; }
 bestByGroup[value.exclusive] = bestgroup; # this is necessary for when bestByGroup didn't contain anything yet for this value.exclusive, if you don't assign it back to the map, it will have been created, but would still not be in the map
}

foreach groupexclusive,group in bestByGroup {
 print("The best for " + groupexclusive + " is " + group.it + " for a boost of " + group.bestvalue);
}
...
 

Grabuge

Member
Thanks. Using digirev code I ended up with something working. I do have to name every group in the script instead of generating it from the mapped file but at least it works. I'll try to figured out xKiv code when I have time.

Code:
record item_info {
	item it;
	effect ef;
	string exclusive; // each item is given a group; groupA, groupB etc
	float boostA;
	float boostB;
        float boostC
	int turns; // number of turns the effect last
};

	## Load the map
	MutualyExcl_info [int] MutualyExcl_map;
	file_to_map("MutualExcl_map.txt", MutualyExcl_map);

	## New record for the best buff
		record MutualyExclBuff{
		item it;
		int bestprofit;
		};

	## All the groups, don't know if there is a better way to do this.
	string[int] groups; 
	groups[0] = "groupA";
	groups[1] = "groupB";
	groups[2] = "groupC";

	## Look at each items and pick the best one for that group
	foreach i,s in groups {
		MutualyExclBuff besttonic;
		besttonic.bestprofit=0;

		foreach it,value in MutualyExcl_map {
			if (value.exclusive== s){
	                    int boost = to_float(( ValueA * value.boostA + ValueB * value.boostB + ValueC * value.boostC ) * value.turns);
	                    if(bestgroup.bestvalue  < boost | bestgroup.bestvalue == 0)
                          bestgroup.bestvalue = boost; bestgroup.it=value.it;
			 }
		}
	        print("The best is "+bestgroup.it+" for a boost of "+bestgroup.bestvalue);
	}
 
Top