Skip to content

Commit f98b00b

Browse files
committed
maybe sync dice
1 parent 1207efd commit f98b00b

File tree

6 files changed

+95
-8
lines changed

6 files changed

+95
-8
lines changed

ewext/noita_api/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ impl EntityID {
219219
raw::entity_add_component::<C>(self)?.ok_or_eyre("Couldn't create a component")
220220
}
221221

222+
pub fn get_var(self, name: &str) -> Option<VariableStorageComponent> {
223+
self.iter_all_components_of_type_including_disabled::<VariableStorageComponent>(None)
224+
.map(|mut i| i.find(|var| var.name().unwrap_or("".into()) == name))
225+
.unwrap_or(None)
226+
}
227+
222228
pub fn add_lua_init_component<C: Component>(self, file: &str) -> eyre::Result<C> {
223229
raw::entity_add_lua_init_component::<C>(self, file)?
224230
.ok_or_eyre("Couldn't create a component")

ewext/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int {
426426
let entity = lua.to_string(1)?.parse::<isize>()?;
427427
let peer = PeerId::from_hex(&lua.to_string(2)?)?;
428428
let mut rng: u64 =
429-
u32::from_ne_bytes(lua.to_string(3)?.parse::<i32>()?.to_ne_bytes()) as u64;
429+
u32::from_le_bytes(lua.to_string(3)?.parse::<i32>()?.to_le_bytes()) as u64;
430430
if rng == 0 {
431431
rng = 1;
432432
}

ewext/src/modules/entity_sync/diff_model.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,34 @@ impl LocalDiffModelTracker {
298298
.map(|(e, _)| e.clone())
299299
.collect::<Vec<GameEffectData>>();
300300

301-
info.current_stains = entity.get_current_stains()?;
301+
let filename = entity.filename()?;
302+
info.current_stains = if [
303+
"data/entities/items/pickup/physics_die.xml",
304+
"data/entities/items/pickup/physics_die_greed.xml",
305+
]
306+
.contains(&&*filename)
307+
{
308+
if entity
309+
.get_var("rolling")
310+
.map(|v| v.value_int().unwrap_or(0) < 8)
311+
.unwrap_or(false)
312+
&& entity
313+
.try_get_first_component::<SpriteComponent>(Some("enable_in_world".into()))?
314+
.map(|s| s.rect_animation().unwrap_or("".into()) == "roll")
315+
.unwrap_or(false)
316+
{
317+
let rng = rand::random::<i32>();
318+
let var = entity.add_component::<VariableStorageComponent>()?;
319+
var.set_name("ew_rng".into())?;
320+
var.set_value_int(rng)?;
321+
let bytes = rng.to_le_bytes();
322+
u64::from_le_bytes([0, 0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3]])
323+
} else {
324+
info.current_stains
325+
}
326+
} else {
327+
entity.get_current_stains()?
328+
};
302329

303330
let mut any = false;
304331
for ai in
@@ -485,7 +512,7 @@ impl LocalDiffModel {
485512
let var = entity.add_component::<VariableStorageComponent>()?;
486513
var.set_name("ew_gid_lid".into())?;
487514
var.set_value_string(gid.0.to_string().into())?;
488-
var.set_value_int(i32::from_ne_bytes(lid.0.to_ne_bytes()))?;
515+
var.set_value_int(i32::from_le_bytes(lid.0.to_le_bytes()))?;
489516
var.set_value_bool(true)?;
490517

491518
if entity
@@ -507,7 +534,10 @@ impl LocalDiffModel {
507534

508535
let drops_gold = entity
509536
.iter_all_components_of_type::<LuaComponent>(None)?
510-
.any(|lua| lua.script_death().ok() == Some("data/scripts/items/drop_money.lua".into()));
537+
.any(|lua| lua.script_death().ok() == Some("data/scripts/items/drop_money.lua".into()))
538+
&& entity
539+
.iter_all_components_of_type::<VariableStorageComponent>(None)?
540+
.all(|var| !var.has_tag("no_gold_drop"));
511541

512542
self.entity_entries.insert(
513543
lid,
@@ -1245,8 +1275,24 @@ impl RemoteDiffModel {
12451275

12461276
entity.set_game_effects(&entity_info.game_effects)?;
12471277

1248-
entity.set_current_stains(entity_info.current_stains)?;
1249-
1278+
let filename = entity.filename()?;
1279+
if [
1280+
"data/entities/items/pickup/physics_die.xml",
1281+
"data/entities/items/pickup/physics_die_greed.xml",
1282+
]
1283+
.contains(&&*filename)
1284+
{
1285+
if entity.iter_all_components_of_type_including_disabled::<VariableStorageComponent>(None)?.all(|var| var.name().unwrap_or("".into()) != "ew_rng") {
1286+
let var = entity.add_component::<VariableStorageComponent>()?;
1287+
var.set_name("ew_rng".into())?;
1288+
let bytes = entity_info.current_stains.to_le_bytes();
1289+
let bytes: [u8; 4] = [bytes[4], bytes[5], bytes[6], bytes[7]];
1290+
let rng = i32::from_le_bytes(bytes);
1291+
var.set_value_int(rng)?;
1292+
}
1293+
} else {
1294+
entity.set_current_stains(entity_info.current_stains)?;
1295+
}
12501296
if let Some(ai) = entity
12511297
.try_get_first_component_including_disabled::<AnimalAIComponent>(None)?
12521298
{
@@ -1587,7 +1633,7 @@ pub fn init_remote_entity(
15871633
if let Some(gid) = gid {
15881634
var.set_value_string(gid.0.to_string().into())?;
15891635
}
1590-
var.set_value_int(i32::from_ne_bytes(lid.0.to_ne_bytes()))?;
1636+
var.set_value_int(i32::from_le_bytes(lid.0.to_le_bytes()))?;
15911637
var.set_value_bool(false)?;
15921638
}
15931639
Ok(())

quant.ew/files/core/util.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ function util.print_error(error)
3535
print("---err end---")
3636
end
3737

38+
function util.prepend(file, target, text)
39+
local content = ModTextFileGetContent(file)
40+
local first, last = content:find(target, 0, true)
41+
if not first then
42+
return
43+
end
44+
local before = content:sub(1, first - 1)
45+
local after = content:sub(last + 1)
46+
local new = before .. text .. after
47+
ModTextFileSetContent(file, new)
48+
end
49+
3850
function util.tpcall(fn, ...)
3951
local res = { xpcall(fn, debug.traceback, ...) }
4052
if not res[1] then

quant.ew/files/system/dice/dice.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
util.prepend(
2+
"data/scripts/items/greed_die_status.lua",
3+
"SetRandomSeed( GameGetFrameNum(), pos_x + pos_y + entity_id )",
4+
'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)'
5+
)
6+
util.prepend(
7+
"data/scripts/items/die_status.lua",
8+
"SetRandomSeed( GameGetFrameNum(), pos_x + pos_y + entity_id )",
9+
'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)'
10+
)
11+
util.prepend(
12+
"data/scripts/items/die_status.lua",
13+
'bullet_circle( "fungus", 8, 300 )',
14+
'if CrossCall("ew_do_i_own", entity_id) then bullet_circle("fungus", 8, 300) end'
15+
)
16+
17+
return {}

quant.ew/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ local function load_modules()
142142
ctx.load_system("wang_hooks")
143143
ctx.load_system("entity_sync_helper")
144144
ctx.load_system("telekenisis")
145+
ctx.load_system("dice")
145146
end
146147

147148
local function load_extra_modules()
@@ -506,7 +507,12 @@ local function on_world_pre_update_inner()
506507
end
507508

508509
local sha_check = GameGetFrameNum() % 5 == 0 and inventory_helper.has_inventory_changed(ctx.my_player)
509-
if ctx.events.new_player_just_connected or ctx.events.inventory_maybe_just_changed or sha_check or cross_force_send_inventory then
510+
if
511+
ctx.events.new_player_just_connected
512+
or ctx.events.inventory_maybe_just_changed
513+
or sha_check
514+
or cross_force_send_inventory
515+
then
510516
cross_force_send_inventory = false
511517
local inventory_state, spells = player_fns.serialize_items(ctx.my_player)
512518
if inventory_state ~= nil then

0 commit comments

Comments
 (0)