-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_data.lua
More file actions
176 lines (137 loc) · 4.55 KB
/
record_data.lua
File metadata and controls
176 lines (137 loc) · 4.55 KB
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
--------------------
-- Configuration
--------------------
local output_filepath = "mrds_data.csv"
local update_interval = 0 -- seconds
local only_update_on_change = true
local record_full_monster_struct = false
--------------------
-- Code
--------------------
function read_data()
data = {}
data["year"] = read_11110000(0x21B6560)
data["month"] = read_00001100(0x21B6560)
data["week"] = read_00000011(0x21B6560)
data["week_full"] = data["week"] + 4 * (data["month"] + 12 * data["year"])
data["gold"] = read_11111111(0x21B6690)
data["name"] = read_ascii_string(0x21B66F8, 12)
data["species_id"] = read_00001111(0x021B6708)
data["main_breed"] = memory.readbyteunsigned(0x021B670C)
data["sub_breed"] = memory.readbyteunsigned(0x021B670D)
data["fame"] = memory.readbyteunsigned(0x021B6710)
data["lifespan"] = read_11110000(0x021B6718)
data["age"] = read_00001111(0x021B6714)
data["action_points"] = read_11110000(0x021B671C)
data["discipline"] = memory.readbytesigned(0x021B6741)
data["trust"] = memory.readbyteunsigned(0x021B6743)
data["stress"] = read_11110000(0x021B6744)
data["fatigue"] = read_00001111(0x021B6744)
data["wits"] = memory.readbyteunsigned(0x021B6748)
data["shape"] = memory.readbytesigned(0x021B6749)
data["power"] = read_11110000(0x021B6720)
data["intelligence"] = read_00001111(0x021B6720)
data["skill"] = read_11110000(0x021B6724)
data["speed"] = read_00001111(0x021B6724)
data["defense"] = read_11110000(0x021B6728)
data["life"] = read_00001111(0x021B6728)
if record_full_monster_struct then
data["zzz_monster_struct"] = tostring(memory.readbyterange(0x21B66F8, 420))
end
return data
end
function read_11111111(offset)
return memory.readdwordunsigned(offset)
end
function read_11110000(offset)
return bit.band(memory.readdwordunsigned(offset), 0x0000FFFF)
end
function read_00001111(offset)
return bit.rshift(memory.readdwordunsigned(offset), 16)
end
function read_00001100(offset)
return bit.rshift(bit.band(memory.readdwordunsigned(offset), 0x00FF0000), 16)
end
function read_00000011(offset)
return bit.rshift(bit.band(memory.readdwordunsigned(offset), 0xFF000000), 24)
end
function read_ascii_string(offset, size)
return decode_ascii_string(memory.readbyterange(offset, size))
end
function decode_ascii_string(bytes)
s = ""
for _, value in ipairs(bytes) do
if value == 0 then
-- Stop if we see a string terminator, since the data after it is not guaranteed to be
-- zeroed out
break
end
s = s .. string.char(value)
end
return s
end
-- Keep file handle open, so that we do not have to keep opening and closing it frequently.
--
-- Unfortunately there doesn't seem to be a good place to close it now...
local data_file = nil
function write_data(data)
columns = {}
for key, _ in pairs(data) do
table.insert(columns, key)
end
table.sort(columns)
-- Create the file if it does not already exist
if data_file == nil then
f = io.open(output_filepath, "r")
if not f then
f = io.open(output_filepath, "w")
-- Add the header row
for i, key in ipairs(columns) do
if i ~= 1 then
f:write(",")
end
f:write(key)
end
f:write("\n")
end
f:close()
end
-- Append the new data
if data_file == nil then
data_file = io.open(output_filepath, "a+")
end
for i, key in ipairs(columns) do
if i ~= 1 then
data_file:write(",")
end
data_file:write(data[key])
end
data_file:write("\n")
data_file:flush()
--f:close()
end
function dictionaries_neq(dict_1, dict_2)
for key, value in pairs(dict_1) do
if value ~= dict_2[key] then
return true
end
end
return false
end
local last_update_time = 0
local previous_data = nil
function main()
current_time = os.time()
if current_time >= (last_update_time + update_interval) then
data = read_data()
last_update_time = current_time
if previous_data == nil or dictionaries_neq(previous_data, data) or not only_update_on_change then
previous_data = data
data["current_time"] = current_time
write_data(data)
-- Remove the key so that it can be used for comparison in the next run of the function
data["current_time"] = nil
end
end
end
gui.register(main)