function sellcotton($milllocation, $portlocation, $continuing, $thecard) { global $GAME, $SystemActing; $errorlistA = ''; $errorlistB = ''; $ExclamationMark = 0; if ($GAME['SpaceTile'][$milllocation] or $GAME['SpaceStatus'][$milllocation] == 9) { $errorlistA .= transtext('memIsNotCM') . '<br>'; // "The industry space that you specified as the location // of the Cotton Mill is not occupied by a Cotton Mill." } else { if ($GAME['SpaceStatus'][$milllocation] != $GAME['PlayerToMove']) { $errorlistA .= transtext('memCMNotYours') . '<br>'; } else { if (!$GAME['SpaceCubes'][$milllocation]) { $errorlistA .= transtext('memCMFlipped') . '<br>'; // "The Cotton Mill that you specified is already flipped." } } } if ($errorlistA == '') { if ($portlocation < $GAME['NumIndustrySpaces']) { if ($GAME['SpaceTile'][$portlocation] != 3) { $errorlistB .= transtext('memIsNotPort') . '<br>'; // "The industry space that you specified as the location // of the Port is not occupied by a Port." } else { if ($GAME['CoalNet'][$GAME['spacetowns'][$milllocation]] != $GAME['CoalNet'][$GAME['spacetowns'][$portlocation]]) { $errorlistB .= transtext('memPortNotConn') . '<br>'; // "The Port location that you specified is not connected to the Cotton Mill that you specified." } else { if (!$GAME['SpaceCubes'][$portlocation]) { $errorlistB .= transtext('memPortFlipped') . '<br>'; // "The Port in the location that you specified is already flipped." } } } } else { if (!$GAME['HasPort'][$GAME['spacetowns'][$milllocation]]) { $errorlistB .= transtext('memCMNoPorts') . '<br>'; // "The specified Cotton Mill cannot sell to the Distant Market, as it is not connected to any Ports." } else { if ($GAME['CottonDemand'] == 8) { $errorlistB .= transtext('memNoMoreDemand') . '<br>'; } } } } $errorlist = $errorlistA . $errorlistB; if ($errorlist == '') { if ($SystemActing) { $AdminTakingMove = 0; } else { if ($GAME['PlayerUserID'][$GAME['PlayerToMove']] == $_SESSION['MyUserID']) { $AdminTakingMove = 0; } else { $AdminTakingMove = 1; } } $append_action_end_note = true; if ($portlocation < $GAME['NumIndustrySpaces']) { fliptile($portlocation); fliptile($milllocation); if ($continuing) { if ($AdminTakingMove) { $altgoodoutput = '2D' . callmovetimediff() . letter_end_number($_SESSION['MyUserID']) . letter_end_number($_SESSION['MyGenderCode']) . letter_end_number($milllocation) . letter_end_number($portlocation); $altgoodoutputName = '|' . $_SESSION['MyUserName']; } else { $altgoodoutput = 'I' . callmovetimediff() . letter_end_number($milllocation) . letter_end_number($portlocation); $altgoodoutputName = ''; } } else { if ($AdminTakingMove) { $altgoodoutput = '2B' . callmovetimediff() . letter_end_number($_SESSION['MyUserID']) . letter_end_number($_SESSION['MyGenderCode']) . letter_end_number($thecard) . letter_end_number($milllocation) . letter_end_number($portlocation); $altgoodoutputName = '|' . $_SESSION['MyUserName']; } else { $altgoodoutput = 'G' . callmovetimediff() . letter_end_number($thecard) . letter_end_number($milllocation) . letter_end_number($portlocation); $altgoodoutputName = ''; } } $CheckCtu = true; } else { drawDMtile(); if ($continuing) { if ($AdminTakingMove) { $altgoodoutput = '2D' . callmovetimediff() . letter_end_number($_SESSION['MyUserID']) . letter_end_number($_SESSION['MyGenderCode']) . letter_end_number($milllocation) . '9J'; $altgoodoutputName = '|' . $_SESSION['MyUserName']; } else { $altgoodoutput = 'I' . callmovetimediff() . letter_end_number($milllocation) . '9J'; $altgoodoutputName = ''; } } else { if ($AdminTakingMove) { $altgoodoutput = '2B' . callmovetimediff() . letter_end_number($_SESSION['MyUserID']) . letter_end_number($_SESSION['MyGenderCode']) . letter_end_number($thecard) . letter_end_number($milllocation) . '9J'; $altgoodoutputName = '|' . $_SESSION['MyUserName']; } else { $altgoodoutput = 'G' . callmovetimediff() . letter_end_number($thecard) . letter_end_number($milllocation) . '9J'; $altgoodoutputName = ''; } } if ($GAME['CottonDemand'] < 8) { $CheckCtu = true; $Boost = array(3, 3, 2, 2, 1, 1, 0, 0); $Boost = $Boost[$GAME['CottonDemand']]; $GAME['IncomeSpace'][$GAME['PlayerToMove']] += $Boost; // If IncomeSpace[...] ends up above 99, // it's taken care of by the call to fliptile() fliptile($milllocation); if (!DMSaleSuccessProbability()) { $GAME['CottonDemand'] = 8; $altgoodoutput .= '9J'; } } else { $CheckCtu = false; $append_action_end_note = false; } } $GAME['ContinueSellingMode'] = 0; if ($CheckCtu) { $MillsArray = array(); $PortsArray = array(); for ($i = 0; $i < $GAME['NumIndustrySpaces']; $i++) { if ($GAME['SpaceStatus'][$i] == $GAME['PlayerToMove'] and !$GAME['SpaceTile'][$i] and $GAME['SpaceCubes'][$i]) { $MillsArray[] = $i; } if ($GAME['SpaceStatus'][$i] != 9 and $GAME['SpaceTile'][$i] == 3 and $GAME['SpaceCubes'][$i]) { $PortsArray[] = $i; } } for ($i = 0; $i < count($MillsArray); $i++) { if ($GAME['HasPort'][$GAME['spacetowns'][$MillsArray[$i]]] and $GAME['CottonDemand'] < 8) { $GAME['ContinueSellingMode'] = 1; break; } for ($j = 0; $j < count($PortsArray); $j++) { if ($GAME['CoalNet'][$GAME['spacetowns'][$MillsArray[$i]]] == $GAME['CoalNet'][$GAME['spacetowns'][$PortsArray[$j]]]) { $GAME['ContinueSellingMode'] = 1; break 2; } } } } if (!$GAME['ContinueSellingMode'] and $append_action_end_note) { $altgoodoutput .= '9I'; } } else { $altgoodoutput = ''; $altgoodoutputName = ''; } return array($errorlist, '', $altgoodoutput, $altgoodoutputName); }
function flip_UOCMs() { // This function runs at the end of each phase if the game has been downsized. It simulates // attempting to flip orphan Cotton Mills, in order to avoid unfairness to players who have // attempted a Port strategy. It first makes a list of unflipped orphan Cotton Mills, // then it makes lists of "good" Ports (those that generate points for the "orphan player") // and of "bad" Ports (those that generate points only for the "real" players). Then // it goes through four steps to try and flip these Cotton Mills and good Ports. // "Desirability" of flipping a tile is evaluated first on how many points it would earn for // the "orphan player" (more points is good), then on how many points it would earn for the // the "real players" collectively (more points is bad). If two tiles of the same type are // tied for desirability, then their relative order is randomised. global $GAME; $UOCMs_numbers = array(); $UOCMs_desirability = array(); $UOCMs_networkcomponent = array(); $UOCMs_random = array(); $good_UPs_numbers = array(); $good_UPs_desirability = array(); $good_UPs_networkcomponent = array(); $good_UPs_random = array(); $bad_UPs_numbers = array(); $bad_UPs_desirability = array(); $bad_UPs_networkcomponent = array(); $bad_UPs_random = array(); for ($i = 0; $i < $GAME['NumIndustrySpaces']; $i++) { if ($GAME['SpaceStatus'][$i] == 8 and $GAME['SpaceCubes'][$i] and !$GAME['SpaceTile'][$i]) { $desirability = flip_UOCMs_evaltile($i); if ($desirability > 0) { $UOCMs_numbers[] = $i; $UOCMs_desirability[] = $desirability; $UOCMs_networkcomponent[] = $GAME['CoalNet'][$GAME['spacetowns'][$i]]; $UOCMs_random[] = $i; } } if ($GAME['SpaceStatus'][$i] != 9 and $GAME['SpaceCubes'][$i] and $GAME['SpaceTile'][$i] == 3) { $desirability = flip_UOCMs_evaltile($i); if ($desirability > 0) { $good_UPs_numbers[] = $i; $good_UPs_desirability[] = flip_UOCMs_evaltile($i); $good_UPs_networkcomponent[] = $GAME['CoalNet'][$GAME['spacetowns'][$i]]; $good_UPs_random[] = $i; } else { $bad_UPs_numbers[] = $i; $bad_UPs_desirability[] = flip_UOCMs_evaltile($i); $bad_UPs_networkcomponent[] = $GAME['CoalNet'][$GAME['spacetowns'][$i]]; $bad_UPs_random[] = $i; } } } if (!count($UOCMs_numbers)) { return; } shuffle($UOCMs_random); shuffle($good_UPs_random); shuffle($bad_UPs_random); array_multisort($UOCMs_desirability, SORT_DESC, $UOCMs_random, $UOCMs_numbers, $UOCMs_networkcomponent); array_multisort($good_UPs_desirability, SORT_DESC, $good_UPs_random, $good_UPs_numbers, $good_UPs_networkcomponent); array_multisort($bad_UPs_desirability, SORT_DESC, $bad_UPs_random, $bad_UPs_numbers, $bad_UPs_networkcomponent); for ($i = 0; $i < count($good_UPs_numbers); $i++) { // First, for each "good" Port, I try to find the "best" // Cotton Mill that's connected to it, and flip them both. for ($j = 0; $j < count($UOCMs_numbers); $j++) { if ($UOCMs_networkcomponent[$j] == $good_UPs_networkcomponent[$i]) { $GAME['AltGameTicker'] .= '7F' . letter_end_number($UOCMs_numbers[$j]) . letter_end_number($good_UPs_numbers[$i]); fliptile($UOCMs_numbers[$j]); fliptile($good_UPs_numbers[$i]); array_splice($UOCMs_numbers, $j, 1); array_splice($UOCMs_networkcomponent, $j, 1); array_splice($good_UPs_numbers, $i, 1); array_splice($good_UPs_networkcomponent, $i, 1); $i--; break; } } } if (!count($UOCMs_numbers)) { return; } $NetworkComponents = array_unique($GAME['CoalNet']); $NetworkComponentsRenumbered = array(); foreach ($NetworkComponents as $key => $value) { $NetworkComponentsRenumbered[] = $value; // array_unique() preserves keys, which I don't want. } for ($i = 0; $i < count($NetworkComponentsRenumbered); $i++) { $PortCMBalance[$NetworkComponentsRenumbered[$i]] = 0; } for ($i = 0; $i < count($UOCMs_numbers); $i++) { $PortCMBalance[$UOCMs_networkcomponent[$i]]++; } for ($i = 0; $i < count($bad_UPs_numbers); $i++) { $PortCMBalance[$bad_UPs_networkcomponent[$i]]--; } while (DMSaleSuccessProbability() == 1) { // Second, for as long as success in selling to the Distant Market is assured, // I look for Cotton Mills that are in a network component with too many Cotton // Mills compared to Ports, and I flip them via the Distant Market. I only need // to look for "bad" Ports from this point on, since following the previous step, // all unflipped "good" Ports are disconnected from the remaining Cotton Mills. for ($i = 0; $i < count($UOCMs_numbers); $i++) { if ($GAME['HasPort'][$UOCMs_networkcomponent[$i]] and $PortCMBalance[$UOCMs_networkcomponent[$i]] > 0) { $GAME['AltGameTicker'] .= '7F' . letter_end_number($UOCMs_numbers[$i]) . '9J'; drawDMtile(); fliptile($UOCMs_numbers[$i]); array_splice($UOCMs_numbers, $i, 1); array_splice($UOCMs_networkcomponent, $i, 1); continue 2; } } break; } while (DMSaleSuccessProbability() == 1) { // Third, for as long as success in selling to the Distant Market is assured, // I try to find the "worst" Port and flip, via the Distant Market, any Cotton Mill // that's connected to it. The Port concerned is deleted from the array of // "bad" Ports when I do this - it can no longer be required, so I eliminate it // the better to identify the "worst" Port that I can still be compelled to use. for ($i = count($bad_UPs_numbers) - 1; $i >= 0; $i--) { for ($j = 0; $j < count($UOCMs_numbers); $j++) { if ($UOCMs_networkcomponent[$j] == $bad_UPs_networkcomponent[$i]) { $GAME['AltGameTicker'] .= '7F' . letter_end_number($UOCMs_numbers[$j]) . '9J'; drawDMtile(); fliptile($UOCMs_numbers[$j]); array_splice($UOCMs_numbers, $j, 1); array_splice($UOCMs_networkcomponent, $j, 1); array_splice($bad_UPs_numbers, $i, 1); array_splice($bad_UPs_networkcomponent, $i, 1); continue 3; } } } break; } for ($i = 0; $i < count($UOCMs_numbers); $i++) { // Fourth, for each Cotton Mill, I try to find the "best" Port // that's connected to it, and flip them both. for ($j = 0; $j < count($bad_UPs_numbers); $j++) { if ($bad_UPs_networkcomponent[$j] == $UOCMs_networkcomponent[$i]) { $GAME['AltGameTicker'] .= '7F' . letter_end_number($UOCMs_numbers[$i]) . letter_end_number($bad_UPs_numbers[$j]); fliptile($UOCMs_numbers[$i]); fliptile($bad_UPs_numbers[$j]); array_splice($UOCMs_numbers, $i, 1); array_splice($UOCMs_networkcomponent, $i, 1); array_splice($bad_UPs_numbers, $j, 1); array_splice($bad_UPs_networkcomponent, $j, 1); $i--; break; } } } while ($GAME['CottonDemand'] < 8) { // Finally, I try to flip the remaining Cotton Mills using the Distant Market, // until the demand for cotton runs out. for ($i = 0; $i < count($UOCMs_numbers); $i++) { if ($GAME['HasPort'][$UOCMs_networkcomponent[$i]]) { $GAME['AltGameTicker'] .= '7F' . letter_end_number($UOCMs_numbers[$i]) . '9J'; if (drawDMtile()) { fliptile($UOCMs_numbers[$i]); array_splice($UOCMs_numbers, $i, 1); array_splice($UOCMs_networkcomponent, $i, 1); if (!DMSaleSuccessProbability()) { $GAME['CottonDemand'] = 8; $GAME['AltGameTicker'] .= '9J'; } continue 2; } else { break 2; } } } break; } }