This file is indexed.

/usr/share/infon-server/api/state.lua is in infon-server 0~r198-8build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
--[[

    Copyright (c) 2006 Florian Wesch <fw@dividuum.de>. All Rights Reserved.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

]]--

global = {}

function setupCreature()
    -- Internal Stuff -----------------

    function _set_state(state, ...)
        _GG.assert(_G[state], "cannot change from state '" .. _state_name .. "' to undefined state '" .. state .. "'")
        if _debug_state_change then
            print("creature " .. id .. " is changing from '" .. _state_name .. "' to '" .. state .. "'")
        end
        _state_name = state
        _state_args = {...}
    end

    function _state_loop() 
        while true do
            local function handle_state_change(state, ...) 
                if not state then 
                    -- change to idle if not already there
                    if _state_name ~= "idle" then
                       _set_state("idle")
                    end
                elseif state == true then
                    -- keep state (restart state handler)
                else
                    -- change to new state
                    _set_state(state, ...)
                end
            end
            if not _G[_state_name] then
                print("state '" .. _state_name .. "' does not exist. changing to 'idle'")
                _set_state("idle")
            end
            handle_state_change(_G[_state_name](_GG.unpack(_state_args)))
            wait_for_next_round()
        end
    end

    function _restart_thread()
        thread = _GG.coroutine.create(_state_loop)
    end

    -- Utility functions ----------------

    -- Enforces a switch to state 'state' and a restart 
    -- of the corresponding state handler.
    and_start_state   = function(state, ...)
        _GG.assert(_G[state], "state '" .. state .. "' not defined")
        return state, ...
    end
    
    -- Enforces being in state 'state'. If the creature 
    -- already is in state 'state', nothing happens. If
    -- the creature is in any other state, the creature
    -- switches to 'state'.
    and_be_in_state   = function(state, ...)
        if _state_name == state then
            return true
        else
            _GG.assert(_G[state], "state '" .. state .. "' not defined")
            return state, ...
        end
    end
    
    -- The creature stays in its current state
    and_keep_state    = false

    -- The creature restarts its current state
    and_restart_state = true

    function reload()
        if _GG.bot then
            local ok, msg = _GG.epcall(_GG._TRACEBACK, _GG.setfenv(_GG.bot, _G))
            if not ok then
                _GG.print("cannot restart creature " .. id .. ": " .. msg)
            end
        end
    end

    function restart()
        message = nil
        reload()
        _set_state("restarting")
        _restart_thread()
    end

    function wait_for_next_round()
        if _GG.can_yield then
            _GG.coroutine.yield()
        else
            _GG.error("cannot wait")
        end
    end

    function execute_event(event, ...)
        if not _G[event] then return end
        local function handle_state_change(ok, state, ...) 
            if not ok then
                _GG.print("calling event '" .. event .. "' failed: " .. state)
            else
                if not state then 
                    -- no change
                elseif state == true then
                    -- keep state (restart state handler)
                    _restart_thread()
                else
                    -- change to new state
                    _set_state(state, ...)
                    _restart_thread()
                end
            end
        end
        return handle_state_change(_GG.epcall(_GG._TRACEBACK, _G[event], ...))
    end

    -- States ---------------------------

    function idle()
        _GG.set_state(id, _GG.CREATURE_IDLE)
        if onIdle then return onIdle() end
    end

    function restarting()
        if onRestart then return onRestart() end
    end

    -- Functions ------------------------
    
    time       = _GG.game_time
    koth_pos   = _GG.get_koth_pos
    world_size = _GG.world_size

    function suicide()
        _GG.suicide(id)
    end

    function nearest_enemy()
        return _GG.get_nearest_enemy(id)
    end

    function set_path(x, y)
        return _GG.set_path(id, x, y)
    end

    function set_target(c)
        return _GG.set_target(id, c)
    end

    function set_conversion(t)
        return _GG.set_convert(id, t)
    end

    function pos()
        return _GG.get_pos(id)
    end

    function speed()
        return _GG.get_speed(id)
    end

    function health()
        return _GG.get_health(id)
    end

    function food()
        return _GG.get_food(id)
    end

    function max_food()
        return _GG.get_max_food(id)
    end

    function tile_food()
        return _GG.get_tile_food(id)
    end

    function tile_type()
        return _GG.get_tile_type(id)
    end

    function type()
        return _GG.get_type(id)
    end

    function say(msg)
        return _GG.set_message(id, msg)
    end

    function begin_idling()
        return _GG.set_state(id, _GG.CREATURE_IDLE)
    end

    function begin_walk_path()
        return _GG.set_state(id, _GG.CREATURE_WALK)
    end

    function begin_healing()
        return _GG.set_state(id, _GG.CREATURE_HEAL)
    end

    function begin_eating()
        return _GG.set_state(id, _GG.CREATURE_EAT)
    end

    function begin_attacking()
        return _GG.set_state(id, _GG.CREATURE_ATTACK)
    end

    function begin_converting()
        return _GG.set_state(id, _GG.CREATURE_CONVERT)
    end

    function begin_spawning()
        return _GG.set_state(id, _GG.CREATURE_SPAWN)
    end

    function begin_feeding()
        return _GG.set_state(id, _GG.CREATURE_FEED)
    end

    function is_idle()
        return _GG.get_state(id) == _GG.CREATURE_IDLE
    end

    function is_healing()
        return _GG.get_state(id) == _GG.CREATURE_HEAL
    end

    function is_walking()
        return _GG.get_state(id) == _GG.CREATURE_WALK
    end

    function is_eating()
        return _GG.get_state(id) == _GG.CREATURE_EAT
    end

    function is_attacking()
        return _GG.get_state(id) == _GG.CREATURE_ATTACK
    end

    function is_converting()
        return _GG.get_state(id) == _GG.CREATURE_CONVERT
    end

    function is_spawning()
        return _GG.get_state(id) == _GG.CREATURE_SPAWN
    end

    function is_feeding()
        return _GG.get_state(id) == _GG.CREATURE_FEED
    end

    function random_path()
        local x1, y1, x2, y2 = world_size()
        local i = 0
        while true do 
            for i = 1, 300 do 
                local x, y = math.random(x1, x2), math.random(y1, y2)
                if set_path(x, y) then
                    return  x, y
                end
            end
            wait_for_next_round()
        end
    end

    function in_state(state)
        if _GG.type(state) == "function" then
            return _G[_state_name] == state
        elseif _GG.type(state) == "string" then
            return _state_name == state
        else
            return false
        end
    end

    -- Blocking Actions -----------------
    
    function sleep(msec)
        local now = time()
        while now + msec < time() do
            wait_for_next_round()
        end
    end
    
    function random_move()
        return move_to(random_path())
    end

    function move_to(tx, ty)
        local x, y = pos()
        if x == tx and y == ty then
            return true
        end
        
        if not _GG.set_path(id, tx, ty) then
            return false
        end

        return move_path(tx, ty)
    end

    function move_path(tx, ty)
        if not _GG.set_state(id, _GG.CREATURE_WALK) then
            return false
        end

        while _GG.get_state(id) == _GG.CREATURE_WALK do
            wait_for_next_round()
        end
        
        local x, y = pos()
        return x == tx and y == ty
    end

    function can_eat()
        return food() < max_food()
    end

    function heal() 
        if not begin_healing() then
            return false
        end
        while is_healing() do
            wait_for_next_round()
        end
        return true
    end

    function eat()
        if not begin_eating() then
            return false
        end
        while is_eating() do
            wait_for_next_round()
        end
        return true
    end

    function feed(target)
        if not set_target(target) then
            return false
        end
        if not begin_feeding() then
            return false
        end
        while is_feeding() do
            wait_for_next_round()
        end
        return true
    end

    function attack(target) 
        if not set_target(target) then
            return false
        end
        if not begin_attacking() then
            return false
        end
        while is_attacking() do
            wait_for_next_round()
        end
        return true
    end

    function convert(to_type)
        if not set_conversion(to_type) then
            return false
        end
        if not begin_converting() then 
            return false
        end
        while is_converting() do
            wait_for_next_round()
        end
        return type() == to_type
    end

    function spawn()
        if not begin_spawning() then
            return false
        end
        while is_spawning() do
            wait_for_next_round()
        end
        return true
    end
end

function createCreature(id, parent)
    local creature = {
        id          = id;
        parent      = parent;
        global      = global;

        print       = print;
        error       = error;
        math        = math;
        table       = table;
        string      = string;

        _GG         = _G; -- Reference to the global environment
        _state_name = 'none'
    }

    creature._G = creature

    setmetatable(creature, {
        __tostring = function(self)
            local x, y  = get_pos(self.id)
            local states = { [0]="idle",   [1]="walk",    [2]="heal",  [3]="eat",
                             [4]="attack", [5]="convert", [6]="spawn", [7]="feed"}
            return "<creature " .. self.id .." [" .. x .. "," .. y .."] " .. 
                    "type " .. get_type(self.id) ..", health " .. get_health(self.id) .. ", " ..
                    "food " .. get_food(self.id) .. ", state " .. states[get_state(self.id)]  .. ", handler " .. self._state_name .. ">"
        end, 
        __concat = function (op1, op2)
            return tostring(op1) .. tostring(op2)
        end
    })

    setfenv(setupCreature, creature)()
    return creature
end

------------------------------------------------------------------------
-- Callbacks von C
------------------------------------------------------------------------

function this_function_call_fails_if_cpu_limit_exceeded() end

-- global table containing all your creatures
creatures = creatures or {}

function player_think(events)
    can_yield = false

    -- Handle the events since the last round.
    for n, event in ipairs(events) do 
        if event.type == CREATURE_SPAWNED then
            local id     = event.id
            local parent = event.parent ~= -1 and event.parent or nil
            local creature = createCreature(id, parent)
            creatures[id]  = creature
            creature.restart()
            creatures[id].execute_event("onSpawned", parent)
        elseif event.type == CREATURE_KILLED then
            local id     = event.id
            local killer = event.killer ~= -1 and event.killer or nil
            assert(creatures[id])
            creatures[id].execute_event("onKilled", killer)
            creatures[id] = nil
        elseif event.type == CREATURE_ATTACKED then
            local id       = event.id
            local attacker = event.attacker
            creatures[id].execute_event("onAttacked", attacker)
        elseif event.type == PLAYER_CREATED then
            -- TODO
        else
            error("invalid event " .. event.type)
        end
    end

    can_yield = true

    -- Iterate all creatures and execute their code by resuming the creatures coroutine
    for id, creature in pairs(creatures) do
        if type(creature.thread) ~= 'thread' then
            creature.message = 'uuh. self.thread is not a coroutine.'
        elseif coroutine.status(creature.thread) == 'dead' then
            if not creature.message then 
                creature.message = 'main() terminated (maybe it was killed for using too much cpu/memory?)'
            end
        else
            creature.execute_event("onTick")
            if get_tile_food(creature.id) > 0 then 
                creature.execute_event("onTileFood")
            end
            if get_health(creature.id) < 5 then
                creature.execute_event("onLowHealth")
            end
            local ok, msg = coroutine.resume(creature.thread)
            if not ok then
                creature.message = msg
                -- If the coroutine was interrupted for using too much
                -- CPU, the following function call will abort the
                -- player_think function (since no more function calls
                -- are possible at this point). The code that caused 
                -- too much CPU can be seen by inspecting the traceback 
                -- that was saved in creature.message. Use 'i' in the client.
                this_function_call_fails_if_cpu_limit_exceeded()
            end
        end
    end

    can_yield = false
end