src/ai/trolly/trolly.cpp
branchgamebalance
changeset 9911 0b8b245a2391
parent 9910 0b2aebc8283e
child 7290 647d6416cdfe
child 9912 1ac8aac92385
equal deleted inserted replaced
9910:0b2aebc8283e 9911:0b8b245a2391
   269 		// All check are okay, so we can build here!
   269 		// All check are okay, so we can build here!
   270 		return true;
   270 		return true;
   271 	}
   271 	}
   272 	if (type == AI_INDUSTRY) {
   272 	if (type == AI_INDUSTRY) {
   273 		const Industry* i = GetIndustry(ic);
   273 		const Industry* i = GetIndustry(ic);
       
   274 		const IndustrySpec *indsp = GetIndustrySpec(i->type);
   274 		const Station* st;
   275 		const Station* st;
   275 		int count = 0;
   276 		int count = 0;
   276 		int j = 0;
   277 		int j = 0;
   277 
   278 
   278 		if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1, 4)) return false;
   279 		if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1, 4)) return false;
   279 
   280 
   280 		// No limits on delevering stations!
   281 		// No limits on delevering stations!
   281 		//  Or for industry that does not give anything yet
   282 		//  Or for industry that does not give anything yet
   282 		if (i->produced_cargo[0] == CT_INVALID || i->total_production[0] == 0) return true;
   283 		if (indsp->produced_cargo[0] == CT_INVALID || i->total_production[0] == 0) return true;
   283 
   284 
   284 		if (i->total_production[0] - i->total_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false;
   285 		if (i->total_production[0] - i->total_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false;
   285 
   286 
   286 		// Check if we have build a station in this town the last 6 months
   287 		// Check if we have build a station in this town the last 6 months
   287 		//  else we don't do it. This is done, because stat updates can be slow
   288 		//  else we don't do it. This is done, because stat updates can be slow
   300 			} else {
   301 			} else {
   301 				// We do not own it, request some info about the station
   302 				// We do not own it, request some info about the station
   302 				//  we want to know if this station gets the same good. If so,
   303 				//  we want to know if this station gets the same good. If so,
   303 				//  we want to know its rating. If it is too high, we are not going
   304 				//  we want to know its rating. If it is too high, we are not going
   304 				//  to build there
   305 				//  to build there
   305 				if (i->produced_cargo[0] == CT_INVALID) continue;
   306 				if (indsp->produced_cargo[0] == CT_INVALID) continue;
   306 				// It does not take this cargo
   307 				// It does not take this cargo
   307 				if (!st->goods[i->produced_cargo[0]].last_speed) continue;
   308 				if (!st->goods[indsp->produced_cargo[0]].last_speed) continue;
   308 				// Is it around our industry
   309 				// Is it around our industry
   309 				if (DistanceManhattan(st->xy, i->xy) > 5) continue;
   310 				if (DistanceManhattan(st->xy, i->xy) > 5) continue;
   310 				// It does take this cargo.. what is his rating?
   311 				// It does take this cargo.. what is his rating?
   311 				if (st->goods[i->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue;
   312 				if (st->goods[indsp->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue;
   312 				j++;
   313 				j++;
   313 				// The rating is high.. a little chance that we still continue
   314 				// The rating is high.. a little chance that we still continue
   314 				//  But if there are 2 stations of this size, we never go on...
   315 				//  But if there are 2 stations of this size, we never go on...
   315 				if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
   316 				if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
   316 				// We don't like this station :(
   317 				// We don't like this station :(
   456 
   457 
   457 				return;
   458 				return;
   458 			}
   459 			}
   459 		} else if (p->ainew.tbt == AI_TRUCK) {
   460 		} else if (p->ainew.tbt == AI_TRUCK) {
   460 			const Industry* ind_from = GetIndustry(p->ainew.from_ic);
   461 			const Industry* ind_from = GetIndustry(p->ainew.from_ic);
       
   462 			const IndustrySpec *indsp_from = GetIndustrySpec(ind_from->type);
   461 			const Industry* ind_temp = GetIndustry(p->ainew.temp);
   463 			const Industry* ind_temp = GetIndustry(p->ainew.temp);
       
   464 			const IndustrySpec *indsp_temp = GetIndustrySpec(ind_temp->type);
   462 			bool found = false;
   465 			bool found = false;
   463 			int max_cargo = 0;
   466 			int max_cargo = 0;
   464 			uint i;
   467 			uint i;
   465 
   468 
   466 			// TODO: in max_cargo, also check other cargo (beside [0])
   469 			// TODO: in max_cargo, also check other cargo (beside [0])
   467 			// First we check if the from_ic produces cargo that this ic accepts
   470 			// First we check if the from_ic produces cargo that this ic accepts
   468 			if (ind_from->produced_cargo[0] != CT_INVALID && ind_from->total_production[0] != 0) {
   471 			if (indsp_from->produced_cargo[0] != CT_INVALID && ind_from->total_production[0] != 0) {
   469 				for (i = 0; i < lengthof(ind_temp->accepts_cargo); i++) {
   472 				for (i = 0; i < lengthof(indsp_temp->accepts_cargo); i++) {
   470 					if (ind_temp->accepts_cargo[i] == CT_INVALID) break;
   473 					if (indsp_temp->accepts_cargo[i] == CT_INVALID) break;
   471 					if (ind_from->produced_cargo[0] == ind_temp->accepts_cargo[i]) {
   474 					if (indsp_from->produced_cargo[0] == indsp_temp->accepts_cargo[i]) {
   472 						// Found a compatible industry
   475 						// Found a compatible industry
   473 						max_cargo = ind_from->total_production[0] - ind_from->total_transported[0];
   476 						max_cargo = ind_from->total_production[0] - ind_from->total_transported[0];
   474 						found = true;
   477 						found = true;
   475 						p->ainew.from_deliver = true;
   478 						p->ainew.from_deliver = true;
   476 						p->ainew.to_deliver = false;
   479 						p->ainew.to_deliver = false;
   477 						break;
   480 						break;
   478 					}
   481 					}
   479 				}
   482 				}
   480 			}
   483 			}
   481 			if (!found && ind_temp->produced_cargo[0] != CT_INVALID && ind_temp->total_production[0] != 0) {
   484 			if (!found && indsp_temp->produced_cargo[0] != CT_INVALID && ind_temp->total_production[0] != 0) {
   482 				// If not check if the current ic produces cargo that the from_ic accepts
   485 				// If not check if the current ic produces cargo that the from_ic accepts
   483 				for (i = 0; i < lengthof(ind_from->accepts_cargo); i++) {
   486 				for (i = 0; i < lengthof(indsp_from->accepts_cargo); i++) {
   484 					if (ind_from->accepts_cargo[i] == CT_INVALID) break;
   487 					if (indsp_from->accepts_cargo[i] == CT_INVALID) break;
   485 					if (ind_temp->produced_cargo[0] == ind_from->accepts_cargo[i]) {
   488 					if (indsp_from->produced_cargo[0] == indsp_from->accepts_cargo[i]) {
   486 						// Found a compatbiel industry
   489 						// Found a compatbiel industry
   487 						found = true;
   490 						found = true;
   488 						max_cargo = ind_temp->total_production[0] - ind_temp->total_transported[0];
   491 						max_cargo = ind_temp->total_production[0] - ind_temp->total_transported[0];
   489 						p->ainew.from_deliver = false;
   492 						p->ainew.from_deliver = false;
   490 						p->ainew.to_deliver = true;
   493 						p->ainew.to_deliver = true;
   499 
   502 
   500 				if (distance > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE &&
   503 				if (distance > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE &&
   501 						distance <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) {
   504 						distance <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) {
   502 					p->ainew.to_ic = p->ainew.temp;
   505 					p->ainew.to_ic = p->ainew.temp;
   503 					if (p->ainew.from_deliver) {
   506 					if (p->ainew.from_deliver) {
   504 						p->ainew.cargo = ind_from->produced_cargo[0];
   507 						p->ainew.cargo = indsp_from->produced_cargo[0];
   505 					} else {
   508 					} else {
   506 						p->ainew.cargo = ind_temp->produced_cargo[0];
   509 						p->ainew.cargo = indsp_temp->produced_cargo[0];
   507 					}
   510 					}
   508 					p->ainew.state = AI_STATE_FIND_STATION;
   511 					p->ainew.state = AI_STATE_FIND_STATION;
   509 
   512 
   510 					DEBUG(ai, 1, "[LocateRoute] found truck-route of %d tiles long (from %d to %d)",
   513 					DEBUG(ai, 1, "[LocateRoute] found truck-route of %d tiles long (from %d to %d)",
   511 						distance,
   514 						distance,