--[[
    bezier_curvemath( int x1, int y1, int hx1, int hy1,
        int x2, int y2, int hx2, int hy2, int samples, bool tableGroup )

    Generates a table containing the (x, y) of points
    of a bezier line between (x1, y1) and (x2, y2).
   
    tableGroup controls table style
    false:  Ungrouped values: { x1, y1, x2, y2, x3, y3 }
    true:   Grouped by point: { { xy, y1 }, { x2, y2 }, { x3, y3 } }
    ]]

function bezier_curvemath( x1, y1, hx1, hy1, x2, y2, hx2, hy2, samples, tableGroup )
    local samples = samples or 50
    local s = 1/samples
    local Ax = x1 + hx1
    local Ay = y1 + hy1
    local Bx = x2 + hx2
    local By = y2 + hy2
    local n,xn,yn
    local myLocations = {  }
   
    for t=0,1,s do
        n = 1-t
        xn = ( x1 * n^3 ) + t * ( ( 3 * n^2 * Ax ) + t * ( ( 3 * n * Bx ) + ( x2 * t ) ) )
        yn = ( y1 * n^3 ) + t * ( ( 3 * n^2 * Ay ) + t * ( ( 3 * n * By ) + ( y2 * t ) ) )
        if tableGroup then
            table.insert( myLocations, { xn, yn } )
        else
            table.insert( myLocations, xn )
            table.insert( myLocations, yn )
        end
    end
   
    return myLocations
end
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Slashdot
  • Twitter

Here’s a function I wrote out of necessity. Hope you find it useful.

UPDATE – I realized I could simplify the function, so here’s the rewrite. Also added examples to the header comment.

--[[
    fileAppend( file theFile [, string string1. string string2, string string3 ...] )
    Examples:
    -- Move to end of file:
    fileAppend(myFile)
    -- Move to end of file, write a string:
    fileAppend(myFile, "My String!")
    -- Move to end of file, write multiple strings:
    fileAppend(myFile, "It's a", " wonderful", " world", " after all!")
    ]]

function fileAppend( theFile, ... )
    -- Set Position to End-Of-File
    fileSetPos( theFile, fileGetSize( theFile ) )
    -- Check if any strings have been passed
    if arg.n ~= 0 then
        theStrings = ""
        for i, v in ipairs( arg ) do
            theStrings = theStrings .. tostring( v )
        end
        fileWrite( theFile, theStrings )
    end
end

I don’t have a big post ready, as I’m not sure what I should cover. Please comment with a topic suggestion.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Slashdot
  • Twitter
, ,

Today I’d like to continue a look into Multi Theft Auto’s event system. In my previous post, Event Handlers (Multi Theft Auto’s CallBacks), I began talking about the Event system and how it is used to trigger functions. I also mentioned that those functions can, in return, trigger and create additional events.

Event Inheritance

In the chart above, I’ve detailed an example chain of Events and Event Handlers. Again, I’m using the onPlayerJoin event as the basis for the chain. This time, however, I’m showing how a function can call other Events, including custom ones. The example function getSQLAccount() is called when a player joins. This function attempts to retrieve data from a MySQL database about the player, and has three events depending on what occurs.

  • onSQLExist
    • Called if SQL row exists for user
  • onSQLEmpty
    • Called if SQL row does not exist for user
  • onSQLError
    • Called if SQL information doesn’t match, or error occurs

Just like the standard Events, you use the function addEventHandler(). However, you need to declare the new Event with the function addEvent(). In the short example below, I show how the custom event “onMyCustomEvent” is created, and an Event Handler is connected with it.

addEvent( "onMyCustomEvent", true )
addEventHandler( "onMyCustomEvent", getRootElement( ), onMyCustomEvent )

While this creates the event, and assigns an Event Handler, we don’t have anything triggering it. You need to trigger the event with triggerEvent(). This function can be called loose within the code, within another Event, or when the Resource is started or stopped. By having the triggerEvent() function loose in the code, the function is called when the code is first executed. (For example, on resource start)

Read the rest of this entry

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Slashdot
  • Twitter
, , ,

People who are just starting to develop for Multi Theft Auto (MTA) often have trouble grasping Event Handlers. As a developer in C and PHP, I’ve often worked with Callbacks, which are equivalent to Event Handlers. You can think of an Event Handler as “What to do when the situation arises.” So, let’s say you’re waiting for the Bus. The Bus arriving is considered an “Event” and your “Event Handler” is you getting on the Bus when the “Event” occurs.
In this case, the Event chain is: Bus Arrives -> Get on Bus (Event Handler) -> Continue through

Structure of Event Handlers

In the Chart above, I’ve detailed an example set of three Event Handlers combined with the onPlayerJoin Event. First the Event is triggered, in this case by a Player joining the server. Then the server grabs any Event Handlers, in this case welcomePlayer( ), checkBanStatus( ), and displayRules which all return something relating to the source player. Once all the Event Handlers are called, the Event ceases. Event Handlers can trigger further Events and their Event Handlers.

In the Chart, the Event Handler welcomePlayer( ) is called when the event onPlayerJoin is triggered. Below is an example of what that could be.

-- Modified from Development Wiki onPlayerJoin Example
function welcomePlayer( )
        -- Get Player's Name and Server's Name
        local joinedPlayerName = getClientName ( source )
        local serverName = getServerName( )
        -- Output to Chatbox
        outputChatBox (
                "Welcome " .. joinedPlayerName ..
                " to " .. serverName .. ", enjoy your stay!",
                source, 255, 255, 255
        )
end
-- Event Handler, hooking function welcomePlayer( ) to Event onPlayerJoin
addEventHandler ( "onPlayerJoin", getRootElement( ), welcomePlayer )
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Slashdot
  • Twitter
, , ,