Skip to content

Commit

Permalink
maybe sync dice
Browse files Browse the repository at this point in the history
  • Loading branch information
bgkillas committed Jan 23, 2025
1 parent 1207efd commit f98b00b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
6 changes: 6 additions & 0 deletions ewext/noita_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ impl EntityID {
raw::entity_add_component::<C>(self)?.ok_or_eyre("Couldn't create a component")
}

pub fn get_var(self, name: &str) -> Option<VariableStorageComponent> {
self.iter_all_components_of_type_including_disabled::<VariableStorageComponent>(None)
.map(|mut i| i.find(|var| var.name().unwrap_or("".into()) == name))
.unwrap_or(None)
}

pub fn add_lua_init_component<C: Component>(self, file: &str) -> eyre::Result<C> {
raw::entity_add_lua_init_component::<C>(self, file)?
.ok_or_eyre("Couldn't create a component")
Expand Down
2 changes: 1 addition & 1 deletion ewext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int {
let entity = lua.to_string(1)?.parse::<isize>()?;
let peer = PeerId::from_hex(&lua.to_string(2)?)?;
let mut rng: u64 =
u32::from_ne_bytes(lua.to_string(3)?.parse::<i32>()?.to_ne_bytes()) as u64;
u32::from_le_bytes(lua.to_string(3)?.parse::<i32>()?.to_le_bytes()) as u64;
if rng == 0 {
rng = 1;
}
Expand Down
58 changes: 52 additions & 6 deletions ewext/src/modules/entity_sync/diff_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,34 @@ impl LocalDiffModelTracker {
.map(|(e, _)| e.clone())
.collect::<Vec<GameEffectData>>();

info.current_stains = entity.get_current_stains()?;
let filename = entity.filename()?;
info.current_stains = if [
"data/entities/items/pickup/physics_die.xml",
"data/entities/items/pickup/physics_die_greed.xml",
]
.contains(&&*filename)
{
if entity
.get_var("rolling")
.map(|v| v.value_int().unwrap_or(0) < 8)
.unwrap_or(false)
&& entity
.try_get_first_component::<SpriteComponent>(Some("enable_in_world".into()))?
.map(|s| s.rect_animation().unwrap_or("".into()) == "roll")
.unwrap_or(false)
{
let rng = rand::random::<i32>();
let var = entity.add_component::<VariableStorageComponent>()?;
var.set_name("ew_rng".into())?;
var.set_value_int(rng)?;
let bytes = rng.to_le_bytes();
u64::from_le_bytes([0, 0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3]])
} else {
info.current_stains
}
} else {
entity.get_current_stains()?
};

let mut any = false;
for ai in
Expand Down Expand Up @@ -485,7 +512,7 @@ impl LocalDiffModel {
let var = entity.add_component::<VariableStorageComponent>()?;
var.set_name("ew_gid_lid".into())?;
var.set_value_string(gid.0.to_string().into())?;
var.set_value_int(i32::from_ne_bytes(lid.0.to_ne_bytes()))?;
var.set_value_int(i32::from_le_bytes(lid.0.to_le_bytes()))?;
var.set_value_bool(true)?;

if entity
Expand All @@ -507,7 +534,10 @@ impl LocalDiffModel {

let drops_gold = entity
.iter_all_components_of_type::<LuaComponent>(None)?
.any(|lua| lua.script_death().ok() == Some("data/scripts/items/drop_money.lua".into()));
.any(|lua| lua.script_death().ok() == Some("data/scripts/items/drop_money.lua".into()))
&& entity
.iter_all_components_of_type::<VariableStorageComponent>(None)?
.all(|var| !var.has_tag("no_gold_drop"));

self.entity_entries.insert(
lid,
Expand Down Expand Up @@ -1245,8 +1275,24 @@ impl RemoteDiffModel {

entity.set_game_effects(&entity_info.game_effects)?;

entity.set_current_stains(entity_info.current_stains)?;

let filename = entity.filename()?;
if [
"data/entities/items/pickup/physics_die.xml",
"data/entities/items/pickup/physics_die_greed.xml",
]
.contains(&&*filename)
{
if entity.iter_all_components_of_type_including_disabled::<VariableStorageComponent>(None)?.all(|var| var.name().unwrap_or("".into()) != "ew_rng") {
let var = entity.add_component::<VariableStorageComponent>()?;
var.set_name("ew_rng".into())?;
let bytes = entity_info.current_stains.to_le_bytes();
let bytes: [u8; 4] = [bytes[4], bytes[5], bytes[6], bytes[7]];
let rng = i32::from_le_bytes(bytes);
var.set_value_int(rng)?;
}
} else {
entity.set_current_stains(entity_info.current_stains)?;
}
if let Some(ai) = entity
.try_get_first_component_including_disabled::<AnimalAIComponent>(None)?
{
Expand Down Expand Up @@ -1587,7 +1633,7 @@ pub fn init_remote_entity(
if let Some(gid) = gid {
var.set_value_string(gid.0.to_string().into())?;
}
var.set_value_int(i32::from_ne_bytes(lid.0.to_ne_bytes()))?;
var.set_value_int(i32::from_le_bytes(lid.0.to_le_bytes()))?;
var.set_value_bool(false)?;
}
Ok(())
Expand Down
12 changes: 12 additions & 0 deletions quant.ew/files/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ function util.print_error(error)
print("---err end---")
end

function util.prepend(file, target, text)
local content = ModTextFileGetContent(file)
local first, last = content:find(target, 0, true)
if not first then
return
end
local before = content:sub(1, first - 1)
local after = content:sub(last + 1)
local new = before .. text .. after
ModTextFileSetContent(file, new)
end

function util.tpcall(fn, ...)
local res = { xpcall(fn, debug.traceback, ...) }
if not res[1] then
Expand Down
17 changes: 17 additions & 0 deletions quant.ew/files/system/dice/dice.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
util.prepend(
"data/scripts/items/greed_die_status.lua",
"SetRandomSeed( GameGetFrameNum(), pos_x + pos_y + entity_id )",
'local function get_num() if get_variable_storage_component(entity_id, "ew_rng") then return ComponentGetValue2(get_variable_storage_component(entity_id, "ew_rng"), "value_int") else return 0 end end SetRandomSeed(get_num(), 0)'
)
util.prepend(
"data/scripts/items/die_status.lua",
"SetRandomSeed( GameGetFrameNum(), pos_x + pos_y + entity_id )",
'local function get_num() if get_variable_storage_component(entity_id, "ew_rng") then return ComponentGetValue2(get_variable_storage_component(entity_id, "ew_rng"), "value_int") else return 0 end end SetRandomSeed(get_num(), 0)'
)
util.prepend(
"data/scripts/items/die_status.lua",
'bullet_circle( "fungus", 8, 300 )',
'if CrossCall("ew_do_i_own", entity_id) then bullet_circle("fungus", 8, 300) end'
)

return {}
8 changes: 7 additions & 1 deletion quant.ew/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ local function load_modules()
ctx.load_system("wang_hooks")
ctx.load_system("entity_sync_helper")
ctx.load_system("telekenisis")
ctx.load_system("dice")
end

local function load_extra_modules()
Expand Down Expand Up @@ -506,7 +507,12 @@ local function on_world_pre_update_inner()
end

local sha_check = GameGetFrameNum() % 5 == 0 and inventory_helper.has_inventory_changed(ctx.my_player)
if ctx.events.new_player_just_connected or ctx.events.inventory_maybe_just_changed or sha_check or cross_force_send_inventory then
if
ctx.events.new_player_just_connected
or ctx.events.inventory_maybe_just_changed
or sha_check
or cross_force_send_inventory
then
cross_force_send_inventory = false
local inventory_state, spells = player_fns.serialize_items(ctx.my_player)
if inventory_state ~= nil then
Expand Down

0 comments on commit f98b00b

Please sign in to comment.