Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions config/craftax_clean.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
env_name = craftax_clean

[vec]
total_agents = 16384
total_agents = 2048
num_buffers = 16
num_threads = 16

Expand All @@ -11,23 +11,23 @@ reset_pool_size = 32
action_mask = 0

[train]
total_timesteps = 2_427_869_180
learning_rate = 0.00577104278
ent_coef = 0.0000466404454
gamma = 0.998501122
gae_lambda = 0.900757194
horizon = 128
total_timesteps = 1_872_097_150
learning_rate = 0.00179971103
ent_coef = 0.0000203385534
gamma = 0.997127712
gae_lambda = 0.960012317
horizon = 64
minibatch_size = 65536
replay_ratio = 1.06381893
clip_coef = 0.2003396
vf_coef = 1.89120328
replay_ratio = 3.26567292
clip_coef = 0.207467377
vf_coef = 2.66733265
vf_clip_coef = 0.01
max_grad_norm = 0.1
momentum = 0.865953863
max_grad_norm = 0.496576279
momentum = 0.961853921

[policy]
hidden_size = 256
num_layers = 2
hidden_size = 1024
num_layers = 4

[sweep]
metric = score
Expand Down
21 changes: 20 additions & 1 deletion ocean/craftax/pack_textures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
[88..90] passive types: cow, bat, snail
[91..98] ranged types: skeleton, gnome_archer, orc_mage, kobold, knight_archer, deep_thing, fire_elemental, ice_elemental
[99..102] projectiles: dagger, fireball, iceball, slimeball
[103..104] sword enchant overlays: fire, ice
[105..106] arrow enchant overlays: fire, ice
[107..110] armour fire overlays: helmet, chest, pants, boots
[111..114] armour ice overlays: helmet, chest, pants, boots
"""

import os
Expand Down Expand Up @@ -213,6 +217,21 @@ def find_assets() -> Path:
"iceball.png",
"slimeball.png",
]
# HUD enchant overlays. Level 0 is empty (no tile); fire then ice.
ENCHANT_FILES = [
"sword_fire_enchantment.png",
"sword_ice_enchantment.png",
"arrow_fire_enchantment.png",
"arrow_ice_enchantment.png",
"helmet_fire_enchantment.png",
"chestplate_fire_enchantment.png",
"pants_fire_enchantment.png",
"boots_fire_enchantment.png",
"helmet_ice_enchantment.png",
"chestplate_ice_enchantment.png",
"pants_ice_enchantment.png",
"boots_ice_enchantment.png",
]


def load_tile(name: str | None) -> np.ndarray:
Expand Down Expand Up @@ -250,7 +269,7 @@ def main() -> None:
MOB_FILES + ARROW_FILES + ARMOUR_FILES + WEAPON_FILES
+ POTION_FILES + HUD_ITEM_FILES
+ MELEE_TYPE_FILES + PASSIVE_TYPE_FILES + RANGED_TYPE_FILES
+ PROJECTILE_TYPE_FILES
+ PROJECTILE_TYPE_FILES + ENCHANT_FILES
):
tiles.append(load_tile(f))

Expand Down
8 changes: 7 additions & 1 deletion ocean/craftax_clean/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ static const DungeonConfig DUNGEON_LEVEL_CONFIGS[3] = {
#define TEX_TILE_PX 16
#define TEX_SCALE 3
#define TEX_DRAW_PX (TEX_TILE_PX * TEX_SCALE)
#define TEX_NUM (37 + 5 + 5 + 3 + 4 + 8 + 9 + 6 + 3 + 8 + 3 + 8 + 4)
#define TEX_NUM (37 + 5 + 5 + 3 + 4 + 8 + 9 + 6 + 3 + 8 + 3 + 8 + 4 + 12)
#define RENDER_ROWS 14
#define RENDER_COLS 16
#define ACTION_PANEL_W 280
Expand Down Expand Up @@ -488,3 +488,9 @@ static const DungeonConfig DUNGEON_LEVEL_CONFIGS[3] = {
#define TEX_PROJ_FIREBALL 100
#define TEX_PROJ_ICEBALL 101
#define TEX_PROJ_SLIMEBALL 102
#define TEX_SWORD_ENCHANT_FIRE 103
#define TEX_SWORD_ENCHANT_ICE 104
#define TEX_ARROW_ENCHANT_FIRE 105
#define TEX_ARROW_ENCHANT_ICE 106
#define TEX_ARMOUR_ENCHANT_FIRE 107
#define TEX_ARMOUR_ENCHANT_ICE 111
126 changes: 77 additions & 49 deletions ocean/craftax_clean/craftax_clean.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,83 @@
#include "craftax_clean.h"
#ifdef CRAFTAX_CLEAN_IN_HEADER

static int key_to_action(void) {
// Makes it human playable
if (IsKeyPressed(KEY_Q)) return ACTION_NOOP;
if (IsKeyPressed(KEY_W) || IsKeyPressed(KEY_UP)) return ACTION_UP;
if (IsKeyPressed(KEY_D) || IsKeyPressed(KEY_RIGHT)) return ACTION_RIGHT;
if (IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)) return ACTION_DOWN;
if (IsKeyPressed(KEY_A) || IsKeyPressed(KEY_LEFT)) return ACTION_LEFT;
if (IsKeyPressed(KEY_SPACE)) return ACTION_DO;
if (IsKeyPressed(KEY_ONE)) return ACTION_MAKE_WOOD_PICKAXE;
if (IsKeyPressed(KEY_TWO)) return ACTION_MAKE_STONE_PICKAXE;
if (IsKeyPressed(KEY_THREE)) return ACTION_MAKE_IRON_PICKAXE;
if (IsKeyPressed(KEY_FOUR)) return ACTION_MAKE_DIAMOND_PICKAXE;
if (IsKeyPressed(KEY_FIVE)) return ACTION_MAKE_WOOD_SWORD;
if (IsKeyPressed(KEY_SIX)) return ACTION_MAKE_STONE_SWORD;
if (IsKeyPressed(KEY_SEVEN)) return ACTION_MAKE_IRON_SWORD;
if (IsKeyPressed(KEY_EIGHT)) return ACTION_MAKE_DIAMOND_SWORD;
if (IsKeyPressed(KEY_T)) return ACTION_PLACE_TABLE;
if (IsKeyPressed(KEY_TAB)) return ACTION_SLEEP;
if (IsKeyPressed(KEY_R)) return ACTION_PLACE_STONE;
if (IsKeyPressed(KEY_F)) return ACTION_PLACE_FURNACE;
if (IsKeyPressed(KEY_P)) return ACTION_PLACE_PLANT;
if (IsKeyPressed(KEY_E)) return ACTION_REST;
if (IsKeyPressed(KEY_COMMA)) return ACTION_ASCEND;
if (IsKeyPressed(KEY_PERIOD)) return ACTION_DESCEND;
if (IsKeyPressed(KEY_Y)) return ACTION_MAKE_IRON_ARMOUR;
if (IsKeyPressed(KEY_U)) return ACTION_MAKE_DIAMOND_ARMOUR;
if (IsKeyPressed(KEY_I)) return ACTION_SHOOT_ARROW;
if (IsKeyPressed(KEY_O)) return ACTION_MAKE_ARROW;
if (IsKeyPressed(KEY_G)) return ACTION_CAST_FIREBALL;
if (IsKeyPressed(KEY_H)) return ACTION_CAST_ICEBALL;
if (IsKeyPressed(KEY_J)) return ACTION_PLACE_TORCH;
if (IsKeyPressed(KEY_Z)) return ACTION_DRINK_POTION_RED;
if (IsKeyPressed(KEY_X)) return ACTION_DRINK_POTION_GREEN;
if (IsKeyPressed(KEY_C)) return ACTION_DRINK_POTION_BLUE;
if (IsKeyPressed(KEY_V)) return ACTION_DRINK_POTION_PINK;
if (IsKeyPressed(KEY_B)) return ACTION_DRINK_POTION_CYAN;
if (IsKeyPressed(KEY_N)) return ACTION_DRINK_POTION_YELLOW;
if (IsKeyPressed(KEY_M)) return ACTION_READ_BOOK;
if (IsKeyPressed(KEY_K)) return ACTION_ENCHANT_SWORD;
if (IsKeyPressed(KEY_L)) return ACTION_ENCHANT_ARMOUR;
if (IsKeyPressed(KEY_LEFT_BRACKET)) return ACTION_MAKE_TORCH;
if (IsKeyPressed(KEY_RIGHT_BRACKET)) return ACTION_LEVEL_UP_DEXTERITY;
if (IsKeyPressed(KEY_MINUS)) return ACTION_LEVEL_UP_STRENGTH;
if (IsKeyPressed(KEY_EQUAL)) return ACTION_LEVEL_UP_INTELLIGENCE;
if (IsKeyPressed(KEY_SEMICOLON)) return ACTION_ENCHANT_BOW;
static const int map[][2] = {
{KEY_Q, ACTION_NOOP},
{KEY_W, ACTION_UP},
{KEY_UP, ACTION_UP},
{KEY_D, ACTION_RIGHT},
{KEY_RIGHT, ACTION_RIGHT},
{KEY_S, ACTION_DOWN},
{KEY_DOWN, ACTION_DOWN},
{KEY_A, ACTION_LEFT},
{KEY_LEFT, ACTION_LEFT},
{KEY_SPACE, ACTION_DO},
{KEY_ONE, ACTION_MAKE_WOOD_PICKAXE},
{KEY_TWO, ACTION_MAKE_STONE_PICKAXE},
{KEY_THREE, ACTION_MAKE_IRON_PICKAXE},
{KEY_FOUR, ACTION_MAKE_DIAMOND_PICKAXE},
{KEY_FIVE, ACTION_MAKE_WOOD_SWORD},
{KEY_SIX, ACTION_MAKE_STONE_SWORD},
{KEY_SEVEN, ACTION_MAKE_IRON_SWORD},
{KEY_EIGHT, ACTION_MAKE_DIAMOND_SWORD},
{KEY_T, ACTION_PLACE_TABLE},
{KEY_TAB, ACTION_SLEEP},
{KEY_R, ACTION_PLACE_STONE},
{KEY_F, ACTION_PLACE_FURNACE},
{KEY_P, ACTION_PLACE_PLANT},
{KEY_E, ACTION_REST},
{KEY_COMMA, ACTION_ASCEND},
{KEY_PERIOD, ACTION_DESCEND},
{KEY_Y, ACTION_MAKE_IRON_ARMOUR},
{KEY_U, ACTION_MAKE_DIAMOND_ARMOUR},
{KEY_I, ACTION_SHOOT_ARROW},
{KEY_O, ACTION_MAKE_ARROW},
{KEY_G, ACTION_CAST_FIREBALL},
{KEY_H, ACTION_CAST_ICEBALL},
{KEY_J, ACTION_PLACE_TORCH},
{KEY_Z, ACTION_DRINK_POTION_RED},
{KEY_X, ACTION_DRINK_POTION_GREEN},
{KEY_C, ACTION_DRINK_POTION_BLUE},
{KEY_V, ACTION_DRINK_POTION_PINK},
{KEY_B, ACTION_DRINK_POTION_CYAN},
{KEY_N, ACTION_DRINK_POTION_YELLOW},
{KEY_M, ACTION_READ_BOOK},
{KEY_K, ACTION_ENCHANT_SWORD},
{KEY_L, ACTION_ENCHANT_ARMOUR},
{KEY_LEFT_BRACKET, ACTION_MAKE_TORCH},
{KEY_RIGHT_BRACKET, ACTION_LEVEL_UP_DEXTERITY},
{KEY_MINUS, ACTION_LEVEL_UP_STRENGTH},
{KEY_EQUAL, ACTION_LEVEL_UP_INTELLIGENCE},
{KEY_SEMICOLON, ACTION_ENCHANT_BOW},
};
for (int i = 0; i < (int)(sizeof(map) / sizeof(map[0])); i++) {
if (IsKeyPressed(map[i][0])) {
return map[i][1];
}
}
return -1;
}

int main(void) {
// Shift + action-panel key. 1 = applied, 0 = policy, -1 = skip tick.
int craftax_clean_human_controls(Craftax* env) {
int shift = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
if (!IsWindowReady() || !shift
|| env->state.is_sleeping || env->state.is_resting) {
return 0;
}
int action = key_to_action();
if (action < 0) {
return -1;
}
env->agents[0].actions[0] = (float)action;
return 1;
}

#else

#include "craftax_clean.h"

int main(void) {
Craftax env;
memset(&env, 0, sizeof(env));
env.num_agents = 1;
Expand All @@ -61,12 +89,11 @@ int main(void) {
env.agents[0].actions = (float*)calloc(1, sizeof(float));
env.agents[0].rewards = (float*)calloc(1, sizeof(float));
env.agents[0].terminals = (float*)calloc(1, sizeof(float));
env.agents[0].action_mask = (unsigned char*)calloc(ATN_DIM, sizeof(unsigned char));
env.agents[0].action_mask = (unsigned char*)calloc(ATN_DIM, 1);
puf_reset(&env);
env.agents[0].actions[0] = -1.0f;

puf_render(&env);

while (!WindowShouldClose()) {
int action = key_to_action();
if (action < 0 && (env.state.is_sleeping || env.state.is_resting)) {
Expand All @@ -76,7 +103,6 @@ int main(void) {
puf_render(&env);
continue;
}

env.agents[0].actions[0] = (float)action;
puf_step(&env);
puf_render(&env);
Expand All @@ -90,3 +116,5 @@ int main(void) {
free(env.agents[0].action_mask);
return 0;
}

#endif
Loading
Loading