I'm trying to mod my game so that instead of needing to dig for treasure you are instead automatically rewarded with whatever would have been dug up. I'm really noob at programming so I'm having some problems since lots of stuff can't be straight copied from the dig function to battle rewards function. The Original dug-chest spawning function looks like this:
	Code:
	function custom_genbox( racemask ) -- äëÿ ñóíäóêîâ äðàêîí÷èêà
  local K = Boxgen.rnd(100)
  K = K - 5
  if K < 0 then
    local r = Boxgen.rnd(3)
    if r == 0 then
      Boxgen.parcount( "rune_might", "1" )
    elseif r == 1 then
      Boxgen.parcount( "rune_magic", "1" )
    else
      Boxgen.parcount( "rune_mind", "1" )
    end
    return true
  end
  K = K - 5
  if K < 0 then
    local r = Boxgen.rnd(100)
    if r < 50 then
      Boxgen.parcount( "crystals", "1" )
    elseif r < 80 then
      Boxgen.parcount( "crystals", "2" )
    elseif r < 95 then
      Boxgen.parcount( "crystals", "3" )
    else
      Boxgen.parcount( "crystals", "5" )
    end
    return true
  end
  K = K - 15
  if K < 0 then -- ïðîñòîé ñâèòîê
    local r = Boxgen.rnd(100)
    local count = 3
    if r < 80 then
      count = 1
    elseif r < 95 then
      count = 2
    end
    if not Boxgen.scroll( Game.MapLocDifficulty(), count ) then
      -- cant add scroll, add money
      Boxgen.parcount( "money", "*7" ) -- ãåíåðèì äåíåã Mini,Small,Average
    end
    return true
  end
  K = K - 3
  if K < 0 then -- ìàãèÿ ñòðàíñòâèé
    if not Boxgen.scroll( ) then
      -- cant add scroll, add money
      Boxgen.parcount( "money", "*7" ) -- ãåíåðèì äåíåã Mini,Small,Average
    end
    return true
  end
  -- Boxgen.parcount( "money", "*7" ) -- ãåíåðèì äåíåã Mini,Small,Average
  local r = Boxgen.rnd(100)
	if r < 40 then
  	Boxgen.parcount( "money", "*1" ) -- ãåíåðèì äåíåã Mini
	elseif r < 75 then
  	Boxgen.parcount( "money", "*2" ) -- ãåíåðèì äåíåã Small
	else
		Boxgen.parcount( "money", "*4" ) -- ãåíåðèì äåíåã Average
	end
  return true
end
 While my added-in section (to the bottom of function calc_bonus( fake_win )) is 
	Code:
	for i=1, 4 do
  local K = Game.Random(1,100)
  if K < 5 then
    local r = Game.Random(1,3)
    if r == 0 then
      Bonus.add("rune_might",1)
    elseif r == 1 then
      Bonus.add("rune_magic",1)
    else
      Bonus.add("rune_mind",1)
    end
   elseif K < 10 then
		local r = Game.Random(1,100)
		if r < 50 then
			Bonus.add( "crystals", "1" )
		elseif r < 80 then
			Bonus.add( "crystals", "2" )
		elseif r < 95 then
			Bonus.add( "crystals", "3" )
		else
			Bonus.add( "crystals", "5" )
		end
	elseif K < 25 then
		local r = Game.Random(1,100)
		local count = 3
		if r < 80 then
			count = 1
		elseif r < 95 then
			count = 2
		end
		--if not Boxgen.scroll( Game.MapLocDifficulty(), count ) then
			-- cant add scroll, add money
		Bonus.add( "money", common_update_money( gold )*7 ) -- ãåíåðèì äåíåã Mini,Small,Average
		--end
	elseif K < 28 then -- ìàãèÿ ñòðàíñòâèé
		--if not Boxgen.scroll( ) then
			-- cant add scroll, add money
		Bonus.add( "money", common_update_money( gold )*7 ) -- ãåíåðèì äåíåã Mini,Small,Average
    else
		local r = Game.Random(1,100)
		if r < 40 then
			Bonus.add( "money", common_update_money( gold )*1 ) -- ãåíåðèì äåíåã Mini
		elseif r < 75 then
			Bonus.add( "money", common_update_money( gold )*2 ) -- ãåíåðèì äåíåã Small
		else
			Bonus.add( "money", common_update_money( gold )*4 ) -- ãåíåðèì äåíåã Average
		end
    end
  end
 I need to know how to create scrolls for the character, the money and run creation work fine already. 
Also, I need to know how to get the number of chests that are in the area (dependent on the pet digging level + the item that gives dig spots), I have it forced to 4 rewards at the moment but the original pet function uses 
	Code:
		local limits = {tonum(Attack.get_custom_param("green")), tonum(Attack.get_custom_param("red"))}
	if hero_item_count("sp_pet_digger")>0 then
		limits[1] = limits[1] + 1
		limits[2] = limits[2] + 1
	end
 Which also doesn't work for me.
Anyone here who can help me with this?