Login

russian armor

Worldbuilder Tip of the Week #18 & #19

highlight
WorldBuilder Tip of the Week Returns!
Welcome back, ladies and gents, to the return of the Tip of the Week for Company of Heroes 2's WorldBuilder Tool, here at COH2.ORG.

This week, MonolithicBacon trawls over some previous tutorials created by Janne252, to breathe them back into life. Thanks to several updates, several changes have occurred when adding SCAR coding to a map, breaking many older tutorials; this two-part video tutorial will remedy all of the issues that were previously found when creating blizzards, and enabling pre-built tech buildings.

So sit back, relax, and enjoy this double-barreled WorldBuilder Tip of the Week!





Further Information for Creating Blizzards
If you're following the video, use the code below to paste into your .SCAR file and get blizzards in your map! Or, if you need a little more detail, feel free to check out Janne252's original tutorial.


Code

-------------------------------------------------------------------------
-------------------------------------------------------------------------

import("ScarUtil.scar")
import("Systems/BlizzardMulitplayer.scar")

-------------------------------------------------------------------------
-- [[ ONINIT ]]
-------------------------------------------------------------------------

function OnInit()


MP_BlizzardInit("data:art/scenarios/presets/atmosphere/DLC/mp_don_river_01_blizzard.aps" ,"data:art/scenarios/presets/atmosphere/DLC/mp_don_river_01.aps", nil, nil, true, nil)

end
Further Information for Enabling Pre-Built Tech
When attempting to enable Pre-Built Tech Buildings/Units, you can always use Janne252's original tutorial, or if you're following the video above, this code box is the exact same as the one featured in the video!


Code
function InstantTech_Init()
InstantTech_SystemInit()
end

Scar_AddInit(InstantTech_Init)

function InstantTech_SystemInit()
local instantTechConfig = {
factions = {
[0] = { -- Wehrmacht
tiers = {
[1] = {ebp = EBP.GERMAN.BEREICH_FESTUNG_MP},
[2] = {ebp = EBP.GERMAN.DOLCH_AKTIONEN_MP, ubp = UPG.GERMAN.BATTLE_PHASE_2_MP},
[3] = {ebp = EBP.GERMAN.HINTERE_PANZERWERK_MP, ubp = UPG.GERMAN.BATTLE_PHASE_3_MP},
[4] = {ebp = EBP.GERMAN.SCHWERES_KRIEGSWERK_MP, ubp = UPG.GERMAN.BATTLE_PHASE_4_MP},
},
},
[1] = { -- Soviet
tiers = {
[1] = {ebp = EBP.SOVIET.BARRACKS_MP},
[2] = {ebp = EBP.SOVIET.WEAPON_SUPPORT_CENTER_MP},
[3] = {ebp = EBP.SOVIET.MOTORPOOL_MP},
[4] = {ebp = EBP.SOVIET.TANK_DEPOT_MP},
},
},
[2] = { -- Oberkommando West
tiers = {
-- T1 for OKW is a bit different than for others because of T1 and T2 are pretty much the same.
-- If you set globalTechLevel to 1, OKW will get T1 OR T2 truck setup for free. If T1 is setup, T2 setup cost modifier will be removed.
[1] = {ebp = EBP.WEST_GERMAN.HEAVY_ARMOR_SUPPORT_MP, additional_ebp = EBP.WEST_GERMAN.LIGHT_ARMOR_SUPPORT_MP},
[2] = {ebp = EBP.WEST_GERMAN.LIGHT_ARMOR_SUPPORT_MP},
[3] = {ebp = EBP.WEST_GERMAN.INFANTRY_SUPPORT_MP},
},
},
[3] = { -- US Forces
tiers = {
[1] = {ubp = UPG.AEF.LIEUTENANT_DISPATCHED_UPGRADE_MP, sbp = SBP.AEF.LIEUTENANT_SQUAD_MP},
[2] = {ubp = UPG.AEF.CAPTAIN_DISPATCHED_UPGRADE_MP, sbp = SBP.AEF.CAPTAIN_SQUAD_MP},
[3] = {ubp = UPG.AEF.MAJOR_DISPATCHED_UPGRADE_MP, sbp = SBP.AEF.MAJOR_SQUAD_MP},
},
},
},
}


--[[
- For US Forces and Oberkommando West, 1 will be substracted from this value.
- For example globalTechLevel of 4 is 3 for US Forces and Oberkommando West.
- You can adjust this in the config as well.
- If setupTrucks is set to false, OKW will get the number of trucks matching the globalTechLevel, with the possible WFASubtract.
-
--]]

local instantTechConfigMap = {
globalTechLevel = 3,
WFASubtract = 1,
setupTrucks = false,
spawnOfficers = false,
applyToHumans = true,
applyToAis = true,
}

g_okw_truck_setup_modifier = {}

g_resource_type_okw_truck_setup = {
RT_Manpower, RT_Fuel,
}

eg_all_instant_tech = EGroup_CreateIfNotFound("eg_all_instant_tech")
sg_all_instant_tech = SGroup_CreateIfNotFound("sg_all_instant_tech")

Players_ForEeach(function(idx, pid, player)
if (Player_IsHuman(player) and instantTechConfigMap.applyToHumans) or (not Player_IsHuman(player) and instantTechConfigMap.applyToAis) then
local pos = Player_GetStartingPosition(player)
local building_positions = InstantTech_GenerateBuildingPositions(pos)
local race = Player_GetRaceIndex(player)
if instantTechConfig.factions[race] then
local config = instantTechConfig.factions[race]
local _globalTechLevel = instantTechConfigMap.globalTechLevel
if race == 2 then
_globalTechLevel = _globalTechLevel - instantTechConfigMap.WFASubtract
if _globalTechLevel < 1 then _globalTechLevel = 1 end
end

for key, tier in pairs(config.tiers) do
if _globalTechLevel >= key then
if tier.ebp then
if race ~= 2 or instantTechConfigMap.setupTrucks then
local spawn_pos = InstantTech_GetAvailableBuildingSpawnPosition(building_positions)
local heading = Util_GetRandomPosition(pos, 30)
local entity = Entity_CreateAndSpawnToward(tier.ebp, player, spawn_pos.pos, heading)
elseif race == 2 and not instantTechConfigMap.setupTrucks then
local spawn_pos = InstantTech_GetAvailableBuildingSpawnPosition(building_positions)
local heading = Util_GetRandomPosition(pos, 30)
local squad = Squad_CreateAndSpawnToward(SBP.WEST_GERMAN.SWS_HALFTRACK_SQUAD_MP, player, 1, spawn_pos.pos, heading)

for key2, resource_type in ipairs(g_resource_type_okw_truck_setup) do
local modifier = Modify_EntityCostExpanded(player, tier.ebp, resource_type, 0)
table.insert(g_okw_truck_setup_modifier, {modifier = modifier, ebp = tier.ebp, player = player })

if tier.additional_ebp then
local modifier = Modify_EntityCostExpanded(player, tier.additional_ebp, resource_type, 0)
table.insert(g_okw_truck_setup_modifier, {modifier = modifier, ebp = tier.ebp, player = player })
end
end

local modifier = Modify_EntityBuildTime(player, tier.ebp, 0.15)
table.insert(g_okw_truck_setup_modifier, {modifier = modifier, ebp = tier.ebp, player = player })
end
end
if tier.ubp then
Player_CompleteUpgrade(player, tier.ubp)
end
if tier.sbp and not instantTechConfigMap.spawnOfficers then
SGroup_Clear(sg_all_instant_tech)
Player_GetAll(player, sg_all_instant_tech)
SGroup_Filter(sg_all_instant_tech, tier.sbp, FILTER_KEEP)
SGroup_DestroyAllSquads(sg_all_instant_tech)
end
end
end
end
end
end)

Rule_Add(InstantTech_MonitorOKWTrucks)
end

function InstantTech_MonitorOKWTrucks()
for key, truck_modifier in ipairs(g_okw_truck_setup_modifier) do
local player = truck_modifier.player
EGroup_Clear(eg_all_instant_tech)
Player_GetAll(player, eg_all_instant_tech)
EGroup_Filter(eg_all_instant_tech, truck_modifier.ebp, FILTER_KEEP)

EGroup_ForEach(eg_all_instant_tech, function(egid, idx, entity)
Modifier_Remove(truck_modifier.modifier)
table.remove(truck_modifier, key)
end)
end

if table.getn(g_okw_truck_setup_modifier) == 0 then
Rule_RemoveMe()
end
end

function InstantTech_GenerateBuildingPositions(pos)
local t_offsets = {OFFSET_FRONT, OFFSET_RIGHT, OFFSET_BACK, OFFSET_LEFT}
local result = {}
for key, offset in ipairs(t_offsets) do
table.insert(result, {pos = Util_GetOffsetPosition(pos, offset, 15), used = false})
end

return result
end

function InstantTech_GetAvailableBuildingSpawnPosition(pos_list)
for key, pos in ipairs(pos_list) do
if not pos.used then
pos.used = true
return pos
end
end
return World_Pos(0, 0, 0)
end

function Entity_CreateAndSpawnToward(ebp, player, pos, toward)
local entity

if player then
entity = Entity_Create(ebp, player, pos, toward)
else
entity = Entity_CreateENV(ebp, pos, toward)
end
Entity_Spawn(entity)
Entity_ForceConstruct(entity)
return entity
end

function Players_ForEeach(f)
for i = 1, World_GetPlayerCount() do
local player = World_GetPlayerAt(i)
f(Player_GetID(player), i, player)
end
end

function Modify_EntityCostExpanded(playerid, blueprint, resource, addition, mathtype)
local modifiertype = ""
mathtype = mathtype or MUT_Multiplication

if (resource == RT_Manpower) then
modifiertype = "modifiers\\cost_manpower_modifier.lua"
elseif (resource == RT_Munition) then
modifiertype = "modifiers\\cost_munition_modifier.lua"
elseif (resource == RT_Fuel) then
modifiertype = "modifiers\\cost_fuel_modifier.lua"
elseif (resource == RT_Action) then
modifiertype = "modifiers\\cost_action_modifier.lua"
end

local modifier = Modifier_Create(MAT_EntityType, modifiertype, mathtype, false, addition, blueprint)

return {Modifier_ApplyToPlayer(modifier, playerid)}
end

function Player_GetRaceIndex(player)
local racename = Player_GetRaceName(player)

if racename == "german" then
return 0
elseif racename == "soviet" then
return 1
elseif racename == "west_german" then
return 2
elseif racename == "aef" then
return 3
else
return -1
end
end

1 user is browsing this thread: 1 guest