[MOD/FIX] Why good marines always die and a fix.

The place to discuss scripting and game modifications for X Rebirth.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

Phezzan
Posts: 126
Joined: Mon, 12. Nov 07, 05:48
x4

[MOD/FIX] Why good marines always die and a fix.

Post by Phezzan »

First the fix:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<diff>
	<replace sel="//library[@name='LoseMarines']/actions/do_if[@value='$nummarines']">
		<do_if value="$nummarines" min="3">
			<do_if value="$losschancemax" min="1">
				<set_value name="$InjuredAmount" min="($nummarines * $losschancemin) / 100" max="($nummarines * $losschancemax + 50) / 100"/>
				<debug_text text="'Player will lose ' + $InjuredAmount + ' marines'"/>

				<!-- KMOD - Vets are 1/2 as likely to die as rookies, and elites 1/2 as vets -->
				<set_value name="$randomTotal"                exact="@$ArrivedMarines.{1}.count * 4"/>
				<set_value name="$randomTotal" operation="add" exact="@$ArrivedMarines.{2}.count * 2"/>
				<set_value name="$randomTotal" operation="add" exact="@$ArrivedMarines.{3}.count"/>

				<do_all exact="$InjuredAmount">
					<set_value name="$random" min="1" max="$randomTotal"/>

					<do_if value="$random gt (@$ArrivedMarines.{1}.count * 4)">	<!-- we rolled above a rookie -->
						<set_value name="$random" operation="sub" exact="@$ArrivedMarines.{1}.count * 4" />
						<do_if value="$random gt (@$ArrivedMarines.{2}.count * 2)">	<!-- We rolled above a Vet! -->
							<set_value name="$rank" exact="3"/>
							<set_value name="$randomTotal" operation="sub" exact="1" />
						</do_if>
						<do_else>
							<set_value name="$rank" exact="2"/>
							<set_value name="$randomTotal" operation="sub" exact="2" />
						</do_else>
					</do_if>
					<do_else>	<!-- Rookie bites it -->
						<set_value name="$rank" exact="1"/>
						<set_value name="$randomTotal" operation="sub" exact="4" />
					</do_else>
					<do_if value="@$ArrivedMarines.{$rank}.count" min="1">
						<remove_units object="$boardership" macro="$ArrivedMarines.{$rank}.{1}" exact="1" unavailable="true"/>
						<remove_value name="$ArrivedMarines.{$rank}.{1}"/>
					</do_if>
					<do_else>
						<debug_text text="'Error: Failed to kill rank: %1 [%2 %3 %4]'.[$rank, $ArrivedMarines.{1}.count, $ArrivedMarines.{2}.count, $ArrivedMarines.{3}.count]"/>
					</do_else>
				</do_all>
			</do_if>
		</do_if>
	</replace>
</diff>
That replaces most of the "LoseMarines" library that picks off your grunts.

Both my code and the original code calculate the number of dead marines based on the input percentages.
Then we both loop for that number picking and killing one marine each iteration.

My code basically makes every Rookie draw four straws, every Vet draws two, and Elites only draw one straw.
The original code makes the entire group of Rookies draw just four straws, the group of Vets just two, and the Elites draw one straw.

Then we both kill a guy from the class that drew the short straw.

Lets throw some numbers:

45 marines: 32 Rookies, 12 Vets, 1 Elite
10 die this 'round'...

Old method:

Code: Select all

For 1..10
   Rookies (4 straws) + Vets (2 straws) + Elites (1) = 7 straws.
   Randomly pick one unlucky straw.
      4/7 == 57% chance a Rookie dies
      2/7 == 29% chance a Vet dies
      1/7 == 14% chance the only Elite dies
chance the elite lives == (6/7)^10 == 21%


New Method:

Code: Select all

For 1..10
   Rookies (4 * 32 straws) + Vets (2 * 12) + Elites (1 * 1) = 153 straws.
   Randomly pick one unlucky straw
       128 / 153 == 84% chance a Rookie dies
       24  / 153 == 16% chance a Vet dies
       1   / 153 == 0.6% chance an Elite dies 
Worst case scenario where the elite lives is all the deaths are rookies (each with 4 straws): 152/153 * 148/149 * 144/145 * ... 116/117
each term is better than 99% so I'll underestimate with 0.99^10 == 90% chance the elite lives

If the number of Rookies, Vets, and Elites were equal (and fairly large), then the two methods would be similar... but that's not how the game is played :)
Spoiler
Show

If you're particular about clean code, you might like the fact that I don't build an array every time a marine dies.
wwdragon
Posts: 3769
Joined: Mon, 1. Oct 07, 02:18
x4

Post by wwdragon »

I like those odds! 8)
Editing posts since long before I remember.

Return to “X Rebirth - Scripts and Modding”