Skip to content

Commit 2442b9d

Browse files
committed
Make time of day advance in steps relative to frameskip in open-world env
1 parent 253c6f7 commit 2442b9d

File tree

1 file changed

+116
-118
lines changed
  • craftium-envs/voxel-libre2/mods/craftium_mod

1 file changed

+116
-118
lines changed
Lines changed: 116 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,151 @@
11
-- names of the items included in the initial inventory
22
init_tools = { "mcl_tools:axe_stone", "mcl_torches:torch 256" }
33

4-
timeofday_step = 1 / 5000 -- day/night cycle lasts 5000 steps
4+
timeofday_step = 1 / (10000 * minetest.settings:get("frameskip")) -- day/night cycle lasts 10K steps
55
timeofday = 0.5 -- start episode at midday
66

77
-- executed when the player joins the game
88
minetest.register_on_joinplayer(function(player, _last_login)
9-
minetest.set_timeofday(timeofday)
10-
11-
-- disable HUD elements
12-
player:hud_set_flags({
13-
crosshair = false,
14-
basic_debug = false,
15-
chat = false,
16-
})
17-
18-
-- setup initial inventory
19-
local inv = player:get_inventory()
20-
for i=1, #init_tools do
21-
inv:add_item("main", init_tools[i])
22-
end
23-
24-
-- set player's initial position
25-
player:set_pos({x = 120, z = 92, y = 16.5 })
9+
minetest.set_timeofday(timeofday)
10+
11+
-- disable HUD elements
12+
player:hud_set_flags({
13+
crosshair = false,
14+
basic_debug = false,
15+
chat = false,
16+
})
17+
18+
-- setup initial inventory
19+
local inv = player:get_inventory()
20+
for i = 1, #init_tools do
21+
inv:add_item("main", init_tools[i])
22+
end
23+
24+
-- set player's initial position
25+
player:set_pos({ x = 120, z = 92, y = 16.5 })
2626
end)
2727

2828
-- turn on the termination flag if the agent dies
2929
minetest.register_on_dieplayer(function(ObjectRef, reason)
30-
set_termination()
30+
set_termination()
3131
end)
3232

3333
-- make game's time match with learning timesteps
3434
minetest.register_globalstep(function(dtime)
35-
if timeofday > 1.0 then
36-
timeofday = 0.0
37-
end
38-
minetest.set_timeofday(timeofday)
39-
timeofday = timeofday + timeofday_step
35+
if timeofday > 1.0 then
36+
timeofday = 0.0
37+
end
38+
minetest.set_timeofday(timeofday)
39+
timeofday = timeofday + timeofday_step
4040
end)
4141

4242
--
4343
-- Tools
4444
-- ~~~~~
4545

4646
stages = {
47-
-- Inital stage
48-
{ name = "init" },
49-
-- Wood stage
50-
{
51-
name = "wood", -- name of the stage
52-
req_name = "tree", -- substring included in the target node's name
53-
req_num = 2, -- number of resources (req_name) required to jump to the next stage
54-
current = 0, -- number of (target) resources currently obtained
55-
provides = { "mcl_tools:pick_wood", "mcl_tools:sword_wood" }, -- the tools provided upon completion
56-
reward = 128.0, -- the reward of completing the stage
57-
},
58-
-- Stone stage
59-
{
60-
name = "stone",
61-
req_name = "stone",
62-
req_num = 3,
63-
current = 0,
64-
provides = { "mcl_tools:pick_stone", "mcl_tools:sword_stone" },
65-
reward = 256.0,
66-
},
67-
-- Stone stage
68-
{
69-
name = "iron",
70-
req_name = "iron",
71-
req_num = 3,
72-
current = 0,
73-
provides = { "mcl_tools:pick_iron", "mcl_tools:sword_iron", "mcl_tools:axe_iron" },
74-
reward = 1024.0,
75-
},
76-
{
77-
name = "diamond",
78-
req_name = "diamond",
79-
req_num = 1,
80-
current = 0,
81-
provides = { "mcl_tools:pick_diamond", "mcl_tools:sword_diamond", "mcl_tools:axe_diamond" },
82-
reward = 2048.0,
83-
},
84-
{ name = "end" }
47+
-- Inital stage
48+
{ name = "init" },
49+
-- Wood stage
50+
{
51+
name = "wood", -- name of the stage
52+
req_name = "tree", -- substring included in the target node's name
53+
req_num = 2, -- number of resources (req_name) required to jump to the next stage
54+
current = 0, -- number of (target) resources currently obtained
55+
provides = { "mcl_tools:pick_wood", "mcl_tools:sword_wood" }, -- the tools provided upon completion
56+
reward = 128.0, -- the reward of completing the stage
57+
},
58+
-- Stone stage
59+
{
60+
name = "stone",
61+
req_name = "stone",
62+
req_num = 3,
63+
current = 0,
64+
provides = { "mcl_tools:pick_stone", "mcl_tools:sword_stone" },
65+
reward = 256.0,
66+
},
67+
-- Stone stage
68+
{
69+
name = "iron",
70+
req_name = "iron",
71+
req_num = 3,
72+
current = 0,
73+
provides = { "mcl_tools:pick_iron", "mcl_tools:sword_iron", "mcl_tools:axe_iron" },
74+
reward = 1024.0,
75+
},
76+
{
77+
name = "diamond",
78+
req_name = "diamond",
79+
req_num = 1,
80+
current = 0,
81+
provides = { "mcl_tools:pick_diamond", "mcl_tools:sword_diamond", "mcl_tools:axe_diamond" },
82+
reward = 2048.0,
83+
},
84+
{ name = "end" },
8585
}
8686

8787
curr_stage = 1 -- index of the current stage
8888

8989
minetest.register_on_dignode(function(pos, node)
90-
-- table of the next stage
91-
local snext = stages[curr_stage+1]
92-
93-
-- there's nothing to do if we reached the last stage
94-
if snext.name == "end" then
95-
return
96-
end
97-
98-
-- check if the dug node contains the `req_name` substring
99-
if string.find(node["name"], snext.req_name) then
100-
snext.current = snext.current + 1
101-
end
102-
103-
-- check if we've enough resources to jump to the next stage
104-
if snext.current >= snext.req_num then
105-
print(string.format("[STAGE] Stage '%s' starts", snext.name), os.date())
106-
107-
-- provide one timestep reward
108-
set_reward_once(snext.reward, 0.0)
109-
110-
-- add new tools to the inventory (removing the current ones first)
111-
for _, player in pairs(minetest.get_connected_players()) do
112-
local inv = player:get_inventory()
113-
inv:set_list("main", {}) -- empty inventory
114-
for i=1, #init_tools do -- reset initial inventory (in the same slots)
115-
inv:add_item("main", init_tools[i])
116-
end
117-
for i = 1, #snext.provides do -- add the unlocked tools
118-
inv:add_item("main", snext.provides[i])
119-
end
120-
end
121-
122-
-- Move to the next stage
123-
curr_stage = curr_stage + 1
124-
end
90+
-- table of the next stage
91+
local snext = stages[curr_stage + 1]
92+
93+
-- there's nothing to do if we reached the last stage
94+
if snext.name == "end" then
95+
return
96+
end
97+
98+
-- check if the dug node contains the `req_name` substring
99+
if string.find(node["name"], snext.req_name) then
100+
snext.current = snext.current + 1
101+
end
102+
103+
-- check if we've enough resources to jump to the next stage
104+
if snext.current >= snext.req_num then
105+
print(string.format("[STAGE] Stage '%s' starts", snext.name), os.date())
106+
107+
-- provide one timestep reward
108+
set_reward_once(snext.reward, 0.0)
109+
110+
-- add new tools to the inventory (removing the current ones first)
111+
for _, player in pairs(minetest.get_connected_players()) do
112+
local inv = player:get_inventory()
113+
inv:set_list("main", {}) -- empty inventory
114+
for i = 1, #init_tools do -- reset initial inventory (in the same slots)
115+
inv:add_item("main", init_tools[i])
116+
end
117+
for i = 1, #snext.provides do -- add the unlocked tools
118+
inv:add_item("main", snext.provides[i])
119+
end
120+
end
121+
122+
-- Move to the next stage
123+
curr_stage = curr_stage + 1
124+
end
125125
end)
126126

127-
128127
--
129128
-- Hunt and Defend
130129
-- ~~~~~~~~~~~~~~~
131130

132131
for name, _ in pairs(mcl_mobs.spawning_mobs) do -- for all mobs that spawn
133-
-- access its definition
134-
local mob_def = minetest.registered_entities[name]
135-
local mob_type = mob_def.type
136-
local old_on_punch = mob_def.on_punch
137-
mob_def.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
138-
local damage = tool_capabilities.damage_groups
139-
and tool_capabilities.damage_groups.fleshy or 1
140-
-- provide a different reward for each type of punched mob
141-
if mob_type == "monster" then
142-
set_reward_once(damage, 0.0)
143-
elseif mob_type == "animal" then
144-
set_reward_once(damage*0.5, 0.0)
145-
elseif mob_type == "npc" then
146-
set_reward_once(-10.0, 0.0)
147-
end
148-
-- Call the original on_punch function
149-
if old_on_punch ~= nil then
150-
old_on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
151-
end
152-
end
132+
-- access its definition
133+
local mob_def = minetest.registered_entities[name]
134+
local mob_type = mob_def.type
135+
local old_on_punch = mob_def.on_punch
136+
mob_def.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
137+
local damage = tool_capabilities.damage_groups and tool_capabilities.damage_groups.fleshy or 1
138+
-- provide a different reward for each type of punched mob
139+
if mob_type == "monster" then
140+
set_reward_once(damage, 0.0)
141+
elseif mob_type == "animal" then
142+
set_reward_once(damage * 0.5, 0.0)
143+
elseif mob_type == "npc" then
144+
set_reward_once(-10.0, 0.0)
145+
end
146+
-- Call the original on_punch function
147+
if old_on_punch ~= nil then
148+
old_on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
149+
end
150+
end
153151
end

0 commit comments

Comments
 (0)