Modified Nicks popup trouble window code

Posted by Dontarion on Wed 13 Jul 2011 06:45 PM — 2 posts, 14,139 views.

USA #0
Nick pointed me to a plugin of his that would setup a combat message mid screen and you could call it at any time with a timer removal. This code was great but for Achaea I thought I'd expand it since our lives are constantly filled with instakill danger.

The code is setup to be used in a .lua file you can require.

Code: combat_message("type", "messsage", "time")

Type is an internal tracker. Only one of the given type allowed at any time. Pushing a message with the same type only refreshes the popups timer.

The code manages all six and as windows are removed it reshuffles the remaining upwards until none remain.

http://sites.google.com/site/dontarion/popupStart.png
https://sites.google.com/site/dontarion/popup5s.png

-- WINDOW module, adapted from Nick Gammon
-- Created July 12, 2011
-- Modified July 13, 2011

ColourTell("orange", "black", "Loading module WINDOW.. ")

-- configuration
FONT_NAME = "Dina"
FONT_SIZE = 22
font_height = 0
TEXT_COLOUR = ColourNameToRGB ("yellow")
BACKGROUND_COLOUR = ColourNameToRGB ("darkred")
TRANSPARENCY = 100
WINDOW_POSITION = 12  -- centered
OFFSET = 5  -- gap inside box
TOP_OFFSET = 10 -- offset to draw next box.
BOX_OFFSET = 10 -- offset between messages
SOUND_FILE = "/Windows/Media/Windows XP Balloon.wav"

active_messages = {}
active_messages_locations = {}
active_messages_message = {}

-- alias goes here
function combat_message (a, b, c)

  message_type = a
  message_message = b
  message_time = tonumber(c)
  
  if not active_messages["combat_message_" .. message_type] then -- Lets draw the initial window
    --Lets set us up some tables to track the window variables
    active_messages["combat_message_" .. message_type] = message_time
    active_messages_message["combat_message_" .. message_type] = message_message

    win = "combat_message_" .. message_type
    WindowCreate (win, 0, 0, 0, 0, 
          0, 0, 
          BACKGROUND_COLOUR)
    
    WindowFont (win, "f", FONT_NAME, FONT_SIZE) -- define font
    width = WindowTextWidth (win, "f", message_message) 
    font_height = WindowFontInfo (win, "f", 1)   -- height of the font
    
    --active_messages_locations = {} -- this is how I track where a message is.  I have 6 spots total: 1-6.
    -- This also tells the drawing where to draw it in 1 of 6 spots, could be expanded theoretically
    a = 1
    while a < 7 do
      if active_messages_locations[a] == nil then
        window_location = (TOP_OFFSET + font_height + (OFFSET * 2) + BOX_OFFSET)*a
        active_messages_locations[a] = "combat_message_" .. message_type
        break
      end
      a = a + 1
    end
    
    WindowCreate (win, 50, window_location, 
          width + (OFFSET * 2), 
          font_height + (OFFSET * 2), 
          0, 2, 
          BACKGROUND_COLOUR) -- create window
    WindowText (win, "f", message_message, OFFSET, OFFSET, 0, 0, TEXT_COLOUR)
    WindowRectOp (win, 1, 0, 0, 0, 0, TEXT_COLOUR)
    WindowShow (win,  true)  -- show it 
    
    Sound (SOUND_FILE)
    
    -- start the box decay timer
    AddTimer("combat_message_" .. message_type, 0, 0, message_time, "", 5, "remove_message")
  else
    -- Refreshes the box decay timer if you try and call the same message again
    DeleteTimer("combat_message_" .. message_type)
    AddTimer("combat_message_" .. message_type, 0, 0, message_time, "", 5, "remove_message")
  end
end -- end combat_message

-- here to remove the message
function remove_message (name)
  WindowShow (name,  false)
  WindowDelete(name)
  active_messages[name] = nil

  for a,b in pairs(active_messages_locations) do
    if b == name then
      active_messages_locations[a] = nil
      break
    end
  end
  
  active_messages_shift()
  
end -- remove_message

function active_messages_shift()
  -- This function will shift all the messages upwards on screen after one of them is removed.
  -- that is only if its needed.

  a = 1
  b = 1
  while a < 7 do
    b = a + 1
    if active_messages_locations[a] == nil then
      while b < 7 do
        if active_messages_locations\[b\] ~= nil then
          active_messages_locations[a] = active_messages_locations\[b\]
          active_messages_locations\[b\] = nil
          break
        end
      b = b + 1
      end
    end
    a = a + 1
  end
  redraw_all()
end

function redraw_all()
  -- This function looks at each active_mesages_locations to redraw all windows, if possible
  -- This is sloppy code and I suspect I can make a loop to do this.
  a = 1
  
  while a < 7 do
    if active_messages_locations[a] ~= nil then
      
      WindowShow(active_messages_locations[a], false)
      WindowDelete(active_messages_locations[a])
      
      WindowCreate (active_messages_locations[a], 0, 0, 0, 0, 
            WINDOW_POSITION, 0, 
            BACKGROUND_COLOUR)
      
      WindowFont (active_messages_locations[a], "f", FONT_NAME, FONT_SIZE) -- define font
      width = WindowTextWidth (active_messages_locations[a], "f", active_messages_message[active_messages_locations[a]]) 
      font_height = WindowFontInfo (active_messages_locations[a], "f", 1)   -- height of the font
      window_location = (TOP_OFFSET + font_height + (OFFSET * 2) + BOX_OFFSET)*a
      
      WindowCreate (active_messages_locations[a], 50, window_location, 
        width + (OFFSET * 2), 
        font_height + (OFFSET * 2), 
        0, 2, 
        BACKGROUND_COLOUR) -- create window

      WindowText (active_messages_locations[a], "f", active_messages_message[active_messages_locations[a]], OFFSET, OFFSET, 0, 0, TEXT_COLOUR)
      WindowRectOp (active_messages_locations[a], 1, 0, 0, 0, 0, TEXT_COLOUR)
      WindowShow (active_messages_locations[a],  true)  -- show it 
    end
  a = a + 1
  end

end

ColourNote("yellow", "black", "Loaded!")
Amended on Wed 13 Jul 2011 09:02 PM by Nick Gammon
Australia Forum Administrator #1
I amended your post to convert tabs to spaces so it fitted better, and fix up the forum codes, as described here:

Template:codetag
To make your code more readable please use [code] tags as described here.


Nice improvement! It reminds me of the "floating combat text" used by some MMO games, which is in fact what inspired the original plugin.