summaryrefslogtreecommitdiff
path: root/npc/functions/event-listeners.txt
blob: 111b27374f15272851569c63227f5d41c7c21b95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Event listeners work similar to the JavaScript event dispatching system,
// except that the event object is a hash table.
//
// Javascript reference:
// https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
//
// Example usage:
/*
// Create a handler function:
public function myHandler {
    // ... do something with getarg(0)
}

// Create a new event:
$@event = htnew();

// Listen for the event:
addEventListener($@event, "myHandler");

// Dispatch the event:
dispatchEvent($@event, "misc data");
*/


/**
 * getEventHt(<hash table expression>) => ht id
 *
 * Parses a hash table "event" expression and creates one when necessary.
 * The following are all valid:
 *
 * getEventHt(.@ht); // variable
 * getEventHt(72); // arbitrary integer (global namespace)
 * getEventHt("some event"); // arbitrary string (global namespace)
 *
 * NOTE: This is only called internally; you don't have to use this function
 *
 * @param 0 - a variable / string / integer
 * @return the hash table id
 */
function	script	getEventHt	{
    .@ht = ((getdatatype(getarg(0)) & (DATATYPE_VAR | DATATYPE_INT)) != 0) ? getarg(0) : 0;

    if (htexists(.@ht)) {
        // this is already a valid hash table
        return getarg(0);
    } else if ((getdatatype(getarg(0)) & DATATYPE_VAR) != 0) {
        // the hash table doesn't exist yet: create one and store it in the
        // provided variable

        if ((getdatatype(getarg(0)) & DATATYPE_STR) != 0) {
            consolemes(CONSOLEMES_ERROR, "getEventHt: variable must be an integer variable (cannot use %s)", data_to_string(getarg(0)));
            end;
        }

        if (getarg(0) > 0) {
            consolemes(CONSOLEMES_WARNING, "getEventHt: variable is already assigned (overwriting %s)", data_to_string(getarg(0)));
        }

        return set(getarg(0), htnew());
    } else {
        // global event listener namespace
        if ($@GLOBAL_EVTL_HT < 1) {
            $@GLOBAL_EVTL_HT = htnew();

            // the global scope is new: create a new one
            .@ht = htnew();
            htput($@GLOBAL_EVTL_HT, data_to_string(getarg(0)), "" + .@ht);
            return .@ht;
        } else if ((.@ht$ = htget($@GLOBAL_EVTL_HT, data_to_string(getarg(0)), "")) != "") {
            // found in the global scope
            return atoi(.@ht$);
        } else {
            // not found in the global scope: create new
            .@ht = htnew();
            htput($@GLOBAL_EVTL_HT, data_to_string(getarg(0)), "" + .@ht);
            return .@ht;
        }
    }
}

/**
 * addEventListener(<ht: event>, "<handler: public local function>") => void
 *
 * Register a public local function in the current NPC as an event handler for
 * the given hash table. Can also register for another NPC using the syntax
 * `npcName::function`
 *
 * NOTE: only one handler may be assigned per NPC per Event
 *
 * @param 0 - the event to listen to (string, variable, or hash table)
 * @param 1 - the event handler (must be a public local function)
 */
function	script	addEventListener	{
    .@ht = getEventHt(getarg(0));
    .@pos = strpos(getarg(1), "::");

    if (.@pos > -1) {
        .@id = getnpcid(substr(getarg(1), 0, .@pos - 1));
        .@function$ = substr(getarg(1), .@pos + 2, getstrlen(getarg(1)) - 1);
    } else {
        .@id = getnpcid();
        .@function$ = getarg(1);
    }

    if (.@id == 0) {
        consolemes(CONSOLEMES_WARNING, "addEventListener: the target NPC doesn't exist");
        return;
    }

    // TODO: check that the target function exists
    return htput(.@ht, "" + .@id, .@function$);
}

/**
 * dispatchEvent(<ht: event>{, ...<arg>}) => bool
 *
 * Calls every event handler for the given hash table and passes the given
 * arguments (if any)
 *
 * @param 0 - the event to dispatch (string, variable, or hash table)
 * @return true when handlers were called, else false
 */
function	script	dispatchEvent	{
    .@ht = getEventHt(getarg(0));

    if (htsize(.@ht) < 1) {
        // no handler to call
        return false;
    }

    .@it = htiterator(.@ht);

    freeloop(true);
    for (.@npc$ = htinextkey(.@it); hticheck(.@it); .@npc$ = htinextkey(.@it)) {
        if (.@npc$ == "") {
            continue;
        }

        .@func$ = htget(.@ht, .@npc$, "");

        // this is ugly but we don't have rest parameters (...args) to apply args to function calls
        switch (getargcount()) {
        case 1:
            callfunctionofnpc(atoi(.@npc$), .@func$);
            break;
        case 2:
            callfunctionofnpc(atoi(.@npc$), .@func$, getarg(1));
            break;
        case 3:
            callfunctionofnpc(atoi(.@npc$), .@func$, getarg(1), getarg(2));
            break;
        case 4:
            callfunctionofnpc(atoi(.@npc$), .@func$, getarg(1), getarg(2), getarg(3));
            break;
        case 5:
            callfunctionofnpc(atoi(.@npc$), .@func$, getarg(1), getarg(2), getarg(3), getarg(4));
            break;
        default:
            consolemes(CONSOLEMES_WARNING, "dispatchEvent: truncating to 5 arguments (%d given)", getargcount() - 1);
        case 6:
            callfunctionofnpc(atoi(.@npc$), .@func$, getarg(1), getarg(2), getarg(3), getarg(4), getarg(5));
            break;
        }
    }
    freeloop(false);

    htidelete(.@it);
    return true;
}