Commits

Leo Wandersleb committed 39f1125

reactivated the segfaulting fireing of unneeded workers

  • Participants
  • Parent commits 15d6de3
  • Branches unitAssignmentByLeo

Comments (0)

Files changed (3)

File src/Building.cpp

 	}
 	else
 	{
-		//updateUnitsWorkingFreeAllThatBringUnwantedRessources();
-		std::list<boost::tuple<int, int> > sortableUnitList;
+		updateUnitsWorkingFreeAllThatBringUnwantedRessources();
+		std::list<boost::tuple<int, Unit *> > sortableUnitList;
 		for (std::list<Unit *>::iterator it=unitsWorking.begin();
 			it!=unitsWorking.end();
 			++it)
 		{
 			int r=(*it)->carriedRessource;
-			int value=Score(*it,r);
-			sortableUnitList.push_back(boost::make_tuple(value, (*it)->gid));
+			int value=Score(*it, r);
+			if (verbose)
+				printf("%d carrying %d\n", *it, r);
+
+			sortableUnitList.push_back(boost::make_tuple(value, *it));
 		}
 		if (sortableUnitList.size() > 0)
 		{
 			if (verbose)
-			{
-				printf(" %d units found\n", sortableUnitList.size());
-				for (std::list<boost::tuple<int, int> >::iterator it2 = sortableUnitList.begin();
-					it2!=sortableUnitList.end();
-					it2++) printf("boost::tuple<%d, %d>\n", it2->get<0>(), it2->get<1>());
-			}
+				printScoreUnitList(sortableUnitList);
 			sortableUnitList.sort();
 			if (verbose)
-			{
-				printf(" %d units found\n", sortableUnitList.size());
-				for (std::list<boost::tuple<int, int> >::iterator it2 = sortableUnitList.begin();
-					it2!=sortableUnitList.end();
-					it2++) printf("boost::tuple<%d, %d>\n", it2->get<0>(), it2->get<1>());
-				//printf(" unit %d choosen. (Unit %d in upper list)\n", chosen->gid, it->get<1>());
-			}
-			for (std::list<boost::tuple<int, int> >::iterator it = sortableUnitList.begin();
+				printScoreUnitList(sortableUnitList);
+			for (std::list<boost::tuple<int, Unit *> >::iterator it = sortableUnitList.begin();
 				unitsWorking.size() > (unsigned)desiredMaxUnitWorking;
 				it++)
 			{
-				Unit * chosen=owner->myUnits[it->get<1>() % 1024];
+				Unit * chosen= it->get<1>();
 				chosen->standardRandomActivity();
-				unitsWorking.erase(chosen);
-				if (verbose) printf("fired unit %D.\n", it->get<1>() % 1024);
+				unitsWorking.remove(chosen);
+				if (verbose) printf("fired unit %d.\n", it->get<0>());
 			}
 		}
 	}
 }
-
+void Building::printScoreUnitList(std::list<boost::tuple<int, Unit *> > ul)
+{
+	printf(" %d units in list\nScore\tUnit *\n", ul.size());
+	for (std::list<boost::tuple<int, Unit *> >::iterator it = ul.begin();
+		it!=ul.end();
+		it++)
+		printf("%d\t%d\n", it->get<0>(), it->get<1>());
+	
+}
 void Building::update(void)
 {
 	if (buildingState==DEAD)
 	+100/harvest
 */
 	if(u==NULL
-	|| u->activity != Unit::ACT_RANDOM
 	|| u->medical != Unit::MED_FREE
 	|| !u->performance[HARVEST])
 	{
 	std::list<boost::tuple<int, int> > sortableUnitList;
 	for(int n=0; n<1024; ++n)
 	{
-		value=Score(owner->myUnits[n],thisTurnsResource);
+		if(owner->myUnits[n]==NULL
+		|| owner->myUnits[n]->activity != Unit::ACT_RANDOM)
+			value=INT_MIN;
+		else
+			value=Score(owner->myUnits[n],thisTurnsResource);
 		if (value > INT_MIN)
 			sortableUnitList.push_back(boost::make_tuple(value, n));
 	}

File src/Building.h

 	/// get flag from units attached to flag.
 	void computeFlagStatLocal(int *goingTo, int *onSpot);
 
+	/// prints the list of tuples for debugging
+	void printScoreUnitList(std::list<boost::tuple<int, Unit *> > ul);
+
 	/// Eats one wheat and one of each of the available fruit from the building.
 	/// Return the number of differents fruits in this building. If mask is non-null,
 	/// set masks value to the mask as well
 	std::list<Order *> orderQueue;
 	
 	static std::string getBuildingName(int type);
-	
+
 public:
 	// type
 	Sint32 typeNum; // number in BuildingTypes

File src/Unit.cpp

 						printf("guid=(%d) Giving ressource (%d) to building gbid=(%d) old-amount=(%d)\n", gid, destinationPurpose, targetBuilding->gid, targetBuilding->ressources[carriedRessource]);
 					targetBuilding->addRessourceIntoBuilding(carriedRessource);
 					carriedRessource=-1;
-
 				}
-				
 				if (!loopMove && !exchangeReady)
 				{
 					//NOTE: if attachedBuilding has become NULL; it's beacause the building doesn't need me anymore.