//Scope is used to have global variables only usable by the functions in the scope
//Initalizer is used to state which function is run on map init
scope BossDrop initializer InitBossDrop
globals
//Go to object manager and under the menu "view" set "display raw data" to true it will then show all units unitId, change this variable to the boss id
constant integer BOSSID = 'hpea'
//This constant changes how many random drops the unit is going to create
constant integer NDROPS = 5
//The id of the shop, sadly this code only works if you have just one of these shops. Otherwise it will add it's boss drops to all of those shops
constant integer SHOPID = 'nmrk'
endglobals
//Our boolexpr leak fix
function ReturnNull takes nothing returns boolexpr
return null
endfunction
//Get shop
function GroupFilter takes nothing returns boolean
return GetUnitTypeId(GetFilterUnit()) == SHOPID
endfunction
function GetShop takes nothing returns unit
//Create a group which will contain the shop (units from neatural passive)
local group neutralPassiveGroup = CreateGroup()
//Get entire map
local rect worldBounds = GetWorldBounds()
//Get all units on the map that has the unit type SHOPID
call GroupEnumUnitsInRect(neutralPassiveGroup, worldBounds, function GroupFilter)
//Clear memory leaks
call RemoveRect(worldBounds)
//Return the first unit
return FirstOfGroup(neutralPassiveGroup)
endfunction
function RandomBossDrops takes nothing returns nothing
local unit boss = GetTriggerUnit()
local unit shop = GetShop()
local integer i = 0
//Create an "itempool", it holds items and can specify rarity of the items
local itempool ip = CreateItemPool()
//This will be our temporary random item
local item itemDrop
//We add item to the itempool with "weight" 1. The more weight it has the higher chance of drop
//Formula for drop chance: divide the individual items wieght with the sum of all items weight.
//'wild's chance to drop is 1/7 since 1 / (1 + 1 + 1 + 1 + 1 + 1 + 1) = 1/7 which is 14.28%
call ItemPoolAddItemType(ip, 'wild', 1)
call ItemPoolAddItemType(ip, 'ankh', 1)
call ItemPoolAddItemType(ip, 'fgsk', 1)
call ItemPoolAddItemType(ip, 'whwd', 1)
call ItemPoolAddItemType(ip, 'hlst', 1)
call ItemPoolAddItemType(ip, 'infs', 1)
call ItemPoolAddItemType(ip, 'pdiv', 1)
for i = 1 to NDROPS
//Get item from itempool
set itemDrop = PlaceRandomItem(ip, 0, 0)
//Add item to shops stock. First one is current stock, second one is max stock
call AddItemToStock(shop, GetItemTypeId(itemDrop), 1, 1)
//This will prevent the item from being randomed again, but this will increase the chance of the other items
call ItemPoolRemoveItemType(ip, GetItemTypeId(itemDrop))
endfor
call DestroyItemPool(ip)
set ip = null
set boss = null
set shop = null
set itemDrop = null
endfunction
//Checks if the dying unit is the boss
function IsUnitBoss takes nothing returns boolean
if GetUnitTypeId(GetTriggerUnit()) == BOSSID then
return true
endif
return false
endfunction
function InitBossDrop takes nothing returns nothing
//Creates a local trigger which we are going to add action functions and condition functions
local trigger dropTrg = CreateTrigger()
//This connects the trigger to an event
//Player(11) is Player 12 Brown, since computers start counting with 0
//returnNull() is because null'ed bool expressions leak, so we use a function that can be reused to return null
call TriggerRegisterPlayerUnitEvent(dropTrg, Player(11), EVENT_PLAYER_UNIT_DEATH, ReturnNull())
//Every time the event happens it checks the condition function if it's true
//Our condition is if the dying unit (or TriggerUnit ) is the boss
call TriggerAddCondition(dropTrg, Condition(function IsUnitBoss))
call TriggerAddAction(dropTrg, function RandomBossDrops)
endfunction
endscope