Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to use coordinate arrays for one block #46

Open
Xisdar opened this issue Sep 2, 2023 · 4 comments
Open

Possibility to use coordinate arrays for one block #46

Xisdar opened this issue Sep 2, 2023 · 4 comments
Labels
Features New feature or request Low Priority question Further information is requested

Comments

@Xisdar
Copy link

Xisdar commented Sep 2, 2023

Good afternoon, the mod doesn't have an option to use coordinate arrays for one block?
I have a json multiblock structure with 8k lines of code, I wrote a passer that arrays the same blocks.
The problem is that I get a different multiblock in the result.
Maybe I didn't quite understand how it is realized, I relied on this documentation.
The original json of the mechanism
Received json

@KasumiNova
Copy link
Collaborator

@Xisdar
Copy link
Author

Xisdar commented Sep 2, 2023

This code, as I understand it, supports arrays of coordinates in JSON format. In the addCoordinates method of the DynamicMachine class, the coordinate arrays for the X, Y, and Z axes are added to lists avX, avY, and avZ. "These lists are then used to create all possible permutations of the coordinates, allowing the set of positions in space to be defined." As I understand it you can use these lists to combine the locations of identical blocks, for a compact json look.

So the question is not python, but how the mod should handle coordinate arrays, I don't fully understand the mechanics itself.

private static void addCoordinates(String key, JsonObject part, List<Integer> out) throws JsonParseException {
    if (!part.has(key)) {
        out.add(0);
        return;
    }
    JsonElement coordinateElement = part.get(key);
    if (coordinateElement.isJsonPrimitive() && coordinateElement.getAsJsonPrimitive().isNumber()) {
        out.add(coordinateElement.getAsInt());
    } else if (coordinateElement.isJsonArray() && coordinateElement.getAsJsonArray().size() > 0) {
        for (JsonElement element : coordinateElement.getAsJsonArray()) {
            if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
                out.add(element.getAsInt());
            } else {
                throw new JsonParseException("Expected only numbers in JsonArray " + coordinateElement + " but found " + element);
            }
        }
    }
}

This method allows you to add coordinates in the form of numbers or as an array of numbers to a JSON object, and then it builds all possible combinations of coordinates that are used, for example, to determine the positions of modifiers or parts of a structure.

{
	"registryname": "auto_hammer",
    "localizedname": "Auto Dissolver",
	"color": "1C77D8",
    "parts": [
        {
            "x": -1,
            "y": 0,
            "z": 0,
            "elements": [
                "modularmachinery:blockcasing@4"
            ]
        },
                {
            "x": 1,
            "y": 0,
            "z": 0,
            "elements": [
                "modularmachinery:blockcasing@4"
            ]
        },
        // Other parts of the element
    ]
}
{
   "registryname": "auto_hammer",
   "localizedname": "Auto Dissolver",
   "parts": [{
         "x": [-1,1,-1,1,1,-1,0,1,-1,1,0,-1,-1,0,1,-1,1,1,-1,-1,1,0,-1,0,1,1,-1,-1,1],
         "y": [0,0,0,2,0,3,3,-1,3,3,-1,1,3,3,3,-1,3,1,1,-1,1,-1,-1,-1,-1,-1,2,2,2],
         "z": [0,0,2,2,2,0,0,0,1,0,0,0,2,2,1,0,2,0,2,1,2,1,2,2,1,2,0,2,0],
         "elements": ["modularmachinery:blockcasing@4"]
      },{
         "x": [-1],
         "y": [0],
         "z": [1],
         "elements": ["modularmachinery:blockinputbus"]
      },
      // Other elements
   ]
}

I specify a single block element so that it would just arrange them according to the coordinates. But as I suspect it can add additional coordinates between blocks on its own.

I just don't understand how to specify one type of block by certain coordinates as an array. Or is it necessary to specify it all separately? This way we get huge json files...

Example:

The original machine
The original machine2

Reassembled json
Reassembled json2

As you can clearly see here that my mechanism has turned into a kind of cube, there are blocks that I did not specify the coordinates, the mod adds them itself.

@KasumiNova
Copy link
Collaborator

In reality, component coordinates in modular machinery are relative to the controller; coordinate resolution is just a series of arrays that are added in a loop, and if you define duplicate coordinates in the parts array, they will be overwritten.

{
   "registryname": "auto_hammer",
   "localizedname": "Auto Dissolver",
   "parts": [
          // Starting with an x of -1 and a y of 0, for example, the module iterates over all the coordinates under the z array to get the following values
          // 1. x: -1, y: 0, z: 0
                           //   ^ Index 0
          // 2. x: -1, y: 0, z: 0
                           //   ^ Index 1
          // 3. x: -1, y: 0, z: 2
                           //   ^ Index 2
          // 4. x: -1, y: 0, z: 2
                           //   ^ Index 3
          // 5...6...7...
          // As you can see, the first coordinate conflicts with the second, and the third with the forth.
          // And after the `z` traversal is complete, it will continue to traverse up through the `y` array, followed by the `x`.

      {
         "x": [-1, 1, -1, 1, 1, -1, 0, 1, -1, 1, 0, -1, -1, 0, 1, -1, 1, 1, -1, -1, 1, 0, -1, 0, 1, 1, -1, -1, 1],
         "y": [0, 0, 0, 2, 0, 3, 3, -1, 3, 3, -1, 1, 3, 3, 3, -1, 3, 1, 1, -1, 1, -1, -1, -1, -1, -1, 2, 2, 2],
         "z": [0,        0,        2,        2,       2, 0, 0, 0, 1, 0, 0, 0, 2, 2, 1, 0, 2, 0, 2, 1, 2, 1, 2, 2, 1, 2, 0, 2, 0],
         //    ^ Index 0 ^ Index 1 ^ Index 2 ^ Index 3
         "elements": ["modularmachinery:blockcasing@4"]
      },
      {
         "x": [-1],
         "y": [0],
         "z": [1],
         "elements": ["modularmachinery:blockinputbus"]
      }
      // Other elements
   ]
}

If you need to write the same components in a single part, I'm sorry to say that the current modular mechanism may not be able to do that.
Although I have plans to rewrite the JSON parsing methods for MM, this would break MMCE's compatibility with the original MM.

If you must use arrays, I would suggest defining only 1 of the three arrays x, y, z in each part (but the coordinates in the arrays can't be the same), and subsequently splitting it up into multiple parts, with the other 2 writing only 1 coordinate instead of a series of arrays, so that it traverses only one array and prevents conflicts.

Example:

{
   "x": [-1],
   "y": [0],
   "z": [0, 1, 2],
   "elements": ["modularmachinery:blockcasing@4"]
},
{
   "x": [0],
   "y": [0],
   "z": [0, 1, 2],
   "elements": ["modularmachinery:blockcasing@4"]
},
{
   "x": [1],
   "y": [0],
   "z": [0, 1, 2],
   "elements": ["modularmachinery:blockcasing@4"]
},

Sorry, my description may not be accurate, but I would still recommend writing the structure file as normal.

@Xisdar
Copy link
Author

Xisdar commented Sep 3, 2023

Specifically I understand your example, thanks for explaining the mechanics of how array parsing works in a script.

Although I have plans to rewrite the JSON parsing methods for MM, this would break MMCE's compatibility with the original MM.

I would suggest not removing the old mechanics, as you say, so as not to break compatibility. But adding a new one, switchable in config, would be a great option in my opinion.

@KasumiNova KasumiNova added Features New feature or request question Further information is requested Low Priority labels Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Features New feature or request Low Priority question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants