Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - Kamigousu

Pages: [1]
Your scripts / Enemy drop rate and multiple item drops
« on: July 13, 2019, 04:26:00 am »
Hello! During the development of a project of mine, I found myself in need of an drop rate script that would be able to decide the type and number of item(s) you will get when killing an enemy. I didn't find anything on here ( although I could have overlooked something) so I made one of my own that works well enough for me to share. Hopefully someone will find a use for it. It isn't really pretty or perfect, but it works well.

Code: [Select]
function enemy:on_dying()
  local x, y, layer = self:get_position()
  local chance = math.random(100)
  if chance >= 95 then  --you have a 5% chance for this to execute.
    bonus_drop = math.random(1,3)

  elseif 75 <= chance and chance < 95 then  --you have a 20% chance for this to execute.
    bonus_drop = math.random(1,2)

  elseif 25 <= chance and chance < 75 then  --you have a 50% chance for this to execute.
    bonus_drop = 1
  else  --you have a 25% chance for this to execute.
return bonus_drop

function enemy:on_dead()
  local map = enemy:get_map()
  local bonus_drop = bonus_drop   --# of extra items dropped.
  local c_treasure, c_variant = self:get_treasure() --common treasure (currency)
  local x, y, layer = self:get_position()

  if bonus_drop ~= nil then
    --the following for loop is the heart of this function. It lays out multiple currency units (or items) depending on a random number generator.
    for i = 1, bonus_drop do
      local irons_prop = {    --properties table for irons.
        ["layer"] = layer,
        ["x"] = x + (math.random(-16, 16)),   --using a random range to mix up the placement of each iron spawned.
        ["y"] = y + (math.random(-16, 16)),   --change the +/-16 to the min and max values for the radius of pixels
        ["treasure_name"] = c_treasure,         --that you would like to allow the item to spawn in. The larger the range
        ["treasure_variant"] = c_variant,         --(and the larger the numbers) the larger the spawn area for the items.

The on_dying section sets up the item chance and determines the drop and the on_death function spawns the dropped items on the map in a pseudo random fashion to make them easy to pick up and see. I think the script is pretty self explanatory but any feedback or criticism is welcome.

edited with corrections as described below.

Was working on stats for a new game and noticed that some of the resources like health and stamina were not coming out to their expected value, but rather they were rounded up or down depending on the following decimal. 100.1 becomes 100, 89.97 becomes 90.

I found that other values I printed were coming out with all decimal places intact though these values weren't called through any 'game' function. It mostly hasn't been a problem until I needed to use a multiplier that scales between 1.01 and 1.9999999. Calling the multiplier through the 'game' function I made for it truncates and rounds the number to either 1 or 2. It's causing a little bit of a problem with a few things but nothing game breaking.

Any one know of a work around or a way to get 'game' functions to print out full number values? I have tried a few things but nothing has worked out yet.

Development / Is it possible to make the hero face the cursor?
« on: August 16, 2018, 11:22:25 pm »
Hello, Solarus World.

    I have been learning Solarus through the online tutorial series by Christopho. It has been incredibly helpful. However, there are many things in my current project that are not covered in any of the tutorial videos and I've not been able to find any info on them online. The API has also been incredibly helpful but I admit I lack a complete understanding of Lua and so I may not fully understand everything as written in the API (for instance, I had trouble with the drawable:draw_region()...the two sets of coordinates confused me, but I figured out through practice what they meant). I am very new to using it but I have been quickly picking things up and reading through previously posted links on this forum for Lua learning resources.

    The problem I am facing right now is my face_cursor script. I wanted to allow the player to change their character's facing direction by moving the mouse around on the screen (and add support for joysticks as well). Essentially, I'm looking to make it behave similar to a two stick shooter game. Is this possible in the engine as it stands? I will attach my script, which is just supposed to be a proof of concept, for your scrutiny..

Code: [Select]
local function init_cursor_info(game)        --Local Function encapsulating lesser functions and pulling 'game'.

  local map = game:get_map()
  local hero = game:get_hero()
  local h_x, h_y, h_L = hero:get_position()
  local h_dir = hero:get_direction()
  local c_x, c_y = sol.input.get_mouse_position()
  local c_dir

  --The following function should update cursor coordinates continuously.
  function game:cursor_update()
    local nc_x, nc_y = sol.input.get_mouse_position()

    if nc_x == c_x and nc_y == c_y then
      c_x = nc_x
      c_y = nc_y
    return true


  --The following function should update hero coordinates and direction continuously.
  function game:hero_update()
    local nh_x, nh_y, nh_L = hero:get_position()
    local nh_dir = hero:get_direction()

    if nh_x == h_x and nh_y == h_y then
      h_x = nh_x
      h_y = nh_y
      h_L = nh_L

    if nh_dir == h_dir then
      h_dir = nh_dir

    return true


  function game:get_cursor_direction()

    local x_chg
    local y_chg

    x_chg = cursor_x - hero_x
    y_chg = cursor_y - hero_y

    if x_chg > 0 and y_chg > 0 then     --Face Hero South-East.
      cur_dir = 7
    elseif x_chg == 0 and y_chg > 0 then --Face Hero South.
      cur_dir = 6
    elseif x_chg < 0 and y_chg > 0 then --Face Hero South-West.
      cur_dir = 5
    elseif x_chg < 0 and y_chg == 0 then --Face Hero West.
      cur_dir = 4
    elseif x_chg < 0 and y_chg < 0 then
      cur_dir = 3
    elseif x_chg == 0 and y_chg < 0 then
      cur_dir = 2
    elseif x_chg > 0 and y_chg < 0 then
      cur_dir = 1
    elseif x_chg > 0 and y_chg == 0 then
      cur_dir = 0
    else if x_chg == 0 and y_chg == 0 then
      cur_dir = 2


    return true

  end                                                  --End of get_cursor_direction()

  function game:on_started()

    local time = 100

    sol.timer.start(game, time, cursor_update())
    sol.timer.start(game, time, hero_update())
    sol.timer.start(game, time, get_cursor_direction())

    print(time)                                        --This print was to test if this segment far it hasn't printed.
end                                                    --Not sure why I needed two ends here...I was getting an error with only one.
end                                                    --End Local Function init_cursor_info(game)

--The following code was taken from OniLinkBegins. My understanding is that this works as is but I am unsure.
-- Set up face_cursor features on any game that starts.
local game_meta = sol.main.get_metatable("game")
game_meta:register_event("on_started", init_cursor_info)

return true

..I'm sure I am making some rookie mistake, but as I said I'm learning still so any advice is appreciated.

Pages: [1]