File: counters.lua

package info (click to toggle)
instead 3.5.2%2Bdfsg-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,316 kB
  • sloc: ansic: 28,336; sh: 452; makefile: 236
file content (97 lines) | stat: -rw-r--r-- 1,675 bytes parent folder | download | duplicates (4)
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
if not stead.api_atleast(1, 6, 3) then
	error ("Counters module can not run with api version < 1.6.3", 3)
end

local function inc_nr(v, n)
	local name = '__'..n..'_nr'
	if not v[name] then
		v[name] = 0
	end
	v[name] = v[name] + 1
end

local function read_nr(v, n, set)
	local name = '__'..n..'_nr'
	v = stead.ref(v)
	if stead.type(v) ~= 'table' then
		return v
	end
	if not v[name] then
		if stead.type(set) == 'number' then v[name] = set end
		return 0
	end
	n = v[name]
	if stead.type(set) == 'number' then v[name] = set end
	return n
end

game.onact = stead.hook(game.onact, function(f, v, w, ...)
	inc_nr(v, 'act');
	inc_nr(w, 'act');
	return f(v, w, ...)
end)

game.onuse = stead.hook(game.onuse, 
function(f, v, w, ...)
	inc_nr(v, 'use');
	inc_nr(w, 'use');
	return f(v, w, ...)
end)

game.oninv = stead.hook(game.oninv, 
function(f, v, w, ...)
	inc_nr(v, 'inv');
	inc_nr(w, 'inv');
	return f(v, w, ...)
end)

game.onwalk = stead.hook(game.onwalk, 
function(f, v, w, ...)
	inc_nr(v, 'walk');
	inc_nr(w, 'walk');
	return f(v, w, ...)
end)

function act_count(s, v)
	if not s and not v then
		s = game
		v = nil
	elseif stead.tonum(s) then
		v = s
		s = game
	end
	return read_nr(s, 'act', v)
end

function inv_count(s, v)
	if not s and not v then
		s = game
		v = nil
	elseif stead.tonum(s) then
		v = s
		s = game
	end
	return read_nr(s, 'inv', v)
end

function use_count(s, v)
	if not s and not v then
		s = game
		v = nil
	elseif stead.tonum(s) then
		v = s
		s = game
	end
	return read_nr(s, 'use', v)
end

function walk_count(s, v)
	if not s and not v then
		s = game
		v = nil
	elseif stead.tonum(s) then
		v = s
		s = game
	end
	return read_nr(s, 'walk', v)
end