Then, to actual on_unit_reduce() and on_unit_kill() event handlers:
There was a ton of redundant code with various messages, so I created a few helper functions to have the code simpler in these functions:
on_unit_reduce() event handler
If here, the 2SP Snake has lost one SP, otherwise it would have been killed.
Code: Select all
function on_unit_reduce (hc, trackid, pid, name, side, nation, oid, orgname, points, strength, HQ, Leader, loss, combat) -- DO NOT REMOVE
...
-- Event handlers for either of the Snake flights losing a strength point
if member(trackid, _1_1ST_WEAPONS_139) then
handle_snake_loss(trackid, _1_1ST_WEAPONS_139, SNAKE_ONEONE_CREW_REINF_ID, "SNAKE_ONEONE_SHOT_DOWN", "1st Flight", hc, side)
end
if member(trackid, _2_1ST_WEAPONS_140) then
handle_snake_loss(trackid, _2_1ST_WEAPONS_140, SNAKE_TWOONE_CREW_REINF_ID, "SNAKE_TWOONE_SHOT_DOWN", "2nd Flight", hc, side)
end
on_unit_kill() event handler
Here, a Snake flight either lost a remaining stength point (helicopter), or both at once. Latter will be punished more severely.
Code: Select all
function on_unit_kill (hc, trackid, pid, name, side, nation, oid, orgname, points, strength, HQ, Leader) -- DO NOT REMOVE
...
if member(trackid, _1_1ST_WEAPONS_139) then
handle_snake_down(trackid, _1_1ST_WEAPONS_139, {SNAKE_ONEONE_CREW_REINF_ID, SNAKE_ONETWO_CREW_REINF_ID},
"SNAKE_ONEONE_SHOT_DOWN", "SNAKE_ONETWO_SHOT_DOWN", "1st Flight", hc, {-30, -40, -50, -20, -30})
end
if member(trackid, _2_1ST_WEAPONS_140) then
handle_snake_down(trackid, _2_1ST_WEAPONS_140, {SNAKE_TWOONE_CREW_REINF_ID, SNAKE_TWOTWO_CREW_REINF_ID},
"SNAKE_TWOONE_SHOT_DOWN", "SNAKE_TWOTWO_SHOT_DOWN", "2nd Flight", hc, {-30, -40, -50, -20, -30})
end
Next, let us have a look at those sub functions. To be continued.
First, for a single helicopter loss, upon first time, there is a warning, but no point deduction. Something like this, still quite simple innit.
Code: Select all
-- Snake SP loss but no event point penalty
function handle_snake_loss(trackid, weapons_id, crew_id, snake_shot_down_var, flight_name, hc, side)
if member(trackid, weapons_id) then
if die_roll(100) > 30 then
place_reinforcement(crew_id, hc)
handle_dialog(flight_name .. " Gunship losses!",
"One of the " .. flight_name .. " Snake gunships is down, but the crew survived! MEDEVAC response requested. \n\n" ..
"Initiate SAR operations for the downed crew immediately using all available assets. \n\n" ..
"\n" ..
"HINT: Losing the whole flight will cost you Event points. Even though they are offensive weapons, do your best to protect gunships from groundfire.",
0) -- No points deduction here, just a notification
else
handle_dialog(flight_name .. " Gunship losses!",
"One of the " .. flight_name .. " Snakes down with no surviving crew members! \n\n" ..
"\n" ..
"HINT: Losing the whole flight will cost you Event Points. Even though they are offensive weapons, do your best to protect gunships from groundfire.",
0) -- No points deduction here, just a notification
end
_G[snake_shot_down_var] = true -- Mark the event as having taken place
end
end
If crew survived, placed as Reinforcements in both cases (unit reduce or kill)
Code: Select all
-- Function to handle reinforcement placement
function place_reinforcement(crew_reinf_id, hc)
set_reinforcement_hex(crew_reinf_id, hc)
set_reinforcement_turn(crew_reinf_id, (current_turn() + 1))
end
However, if both Snakes for a flight were lost as one event, there is an extra penalty for that, while loss for a single remaining flight unit is a tad more hardgiving. Per case, check if either or both crews survived. Here this gets quite complicated.
Code: Select all
-- Function to handle crew survival scenarios
function handle_crew_survival(hardlanding, hc, crew_ids, flight_name, deduction_1, deduction_2, deduction_3)
if hardlanding > 70 then
for _, crew_id in ipairs(crew_ids) do
place_reinforcement(crew_id, hc)
end
handle_dialog(flight_name .. " Destroyed",
flight_name .. " gunships are down, but both crews survived! Urgent MEDEVAC response requested. \n\n" ..
"Initiate SAR operations immediately using all available assets. \n\n" ..
"You are deducted " .. deduction_1 .. " Event Points for losing a full flight of Snakes, Commander.\n\n" ..
"\n" ..
"HINT: Even though they are offensive weapons, do your best to protect gunships from groundfire.",
-deduction_1)
elseif hardlanding > 50 then
place_reinforcement(crew_ids[1], hc)
handle_dialog(flight_name .. " Destroyed",
flight_name .. " gunships are down, but one of the crews survived! MEDEVAC response requested. \n\n" ..
"Initiate SAR operations immediately using all available assets. \n\n" ..
"You are deducted " .. deduction_2 .. " Event Points for losing a full flight of Snakes while taking casualties.\n\n" ..
"\n" ..
"HINT: Even though they are offensive weapons, do your best to protect gunships from groundfire.",
-deduction_2)
else
handle_dialog(flight_name .. " Destroyed",
"FUBAR! You lost the full " .. flight_name .. " Snakes with no surviving crew members! \n\n" ..
"You are deducted " .. deduction_3 .. " Event Points for this SNAFU, Commander.\n\n" ..
"\n" ..
"HINT: Even though they are offensive weapons, do your best to protect gunships from groundfire.",
-deduction_3)
end
end
-- Function to handle remaining snake down scenarios
function handle_remaining_snake_down(hardlanding, hc, crew_id, flight_name, deduction_1, deduction_2)
if hardlanding > 30 then
place_reinforcement(crew_id, hc)
handle_dialog(flight_name .. " Destroyed",
"The remaining " .. flight_name .. " gunship is now down as well, but one of the crews survived! MEDEVAC response requested. \n\n" ..
"Initiate SAR operations immediately using all available assets. \n\n" ..
"You are deducted " .. deduction_1 .. " Event Points for losing a full flight of Snakes.\n\n" ..
"\n" ..
"HINT: Even though they are offensive weapons, do your best to protect gunships from groundfire.",
-deduction_1)
else
handle_dialog(flight_name .. " Destroyed",
"You lost the remaining " .. flight_name .. " with no surviving crew members! \n\n" ..
"You are deducted " .. deduction_2 .. " Event Points for this FUBAR, Commander.\n\n" ..
"\n" ..
"HINT: Even though they are offensive weapons, do your best to protect gunships from groundfire.",
-deduction_2)
end
end
-- Main logic for both track IDs
function handle_snake_down(trackid, weapons_id, crew_ids, snake_one_shot_down, snake_two_shot_down, flight_name, hc, deductions)
if member(trackid, weapons_id) then
hardlanding = die_roll(100)
if not _G[snake_one_shot_down] then
_G[snake_one_shot_down] = true
_G[snake_two_shot_down] = true
handle_crew_survival(hardlanding, hc, crew_ids, flight_name, deductions[1], deductions[2], deductions[3])
else
_G[snake_two_shot_down] = true
handle_remaining_snake_down(hardlanding, hc, crew_ids[2], flight_name, deductions[4], deductions[5])
end
end
end