js function synchronizer

For AI and campaign script related discussions and questions
wanjia1
Trained
Trained
Posts: 63
Joined: 01 Nov 2023, 06:05

js function synchronizer

Post by wanjia1 »

some js function,like addDroid,is not synchronized. if it not called at all clients at the same tick,it will cause desync.
although the function syncRequest(req_id, x, y, object, object2) products a synchronized eventSyncRequest(from,req_id, x, y, obj_id, obj_id2)
it can only carry arguments of fixed length and type,

so i make this library, serialize the arguments of non-synchronized function(which must have fixed name and json serializable arguments) and wrapped in syncRequest send it to all clients, make it synchronized.
for example, make addDroid a synchronized function:

Code: Select all

include("multiplay/script/mods/sync.js");
var syncAddDroid=synchronize(addDroid)//must be executed in all clients
below is a demo mod that use this library.
when a player says "/tank", a "Gauss Cannon Hydra Dragon Hover" will be spawned at its startPositions. and it works in multiplay.
You do not have the required permissions to view the files attached to this post.
Last edited by wanjia1 on 06 Jan 2024, 07:42, edited 2 times in total.
wanjia1
Trained
Trained
Posts: 63
Joined: 01 Nov 2023, 06:05

Re: js function synchronizer

Post by wanjia1 »

screenshot:
You do not have the required permissions to view the files attached to this post.
wanjia1
Trained
Trained
Posts: 63
Joined: 01 Nov 2023, 06:05

Re: js function synchronizer

Post by wanjia1 »

its implement is inefficient. theoretically, a syncRequest can carry 24 bytes of data (req_id, x, y of type number in float64. object and object2 have constraints and unable to carry data),
but the implement only use the req_id to carry data, the charcode of every character of the json serialized argument, which take 1 byte at most cases.
the x is also used. it carry the position of charcode. because it's not sure if the eventSyncRequest called in order.
it also have no validation. it assumes there is no packet loss or errors, cheating. validation should be implemented in the function that to be synchronized.
Last edited by wanjia1 on 06 Jan 2024, 21:16, edited 1 time in total.
wanjia1
Trained
Trained
Posts: 63
Joined: 01 Nov 2023, 06:05

Re: js function synchronizer

Post by wanjia1 »

when nested synchronized function is called, only the outer-most function call will product synchronized events, and will no return value.
the inner function call is short-circuited and will execute at local client, returns as normal.
this avoided duplicate event.
User avatar
aubergine
Professional
Professional
Posts: 3462
Joined: 10 Oct 2010, 00:58

Re: js function synchronizer

Post by aubergine »

An update to `eventSyncRequest()` could potentially break this mod - would love your feedback (in github issue if possible as I rarely check forums): https://github.com/Warzone2100/warzone2100/pull/4814 - would adding an optional string param make porting easier? it would remove the need to serialize via the `x` argument, making data transfer much easier?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO