Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Creating a Utility mods, making full use of JSON files.  (Read 1536 times)

silentstormpt

  • Admiral
  • *****
  • Posts: 1060
    • View Profile
Creating a Utility mods, making full use of JSON files.
« on: December 10, 2013, 03:47:11 AM »

As the title mentioned, the sole purpose is to make a utility that can be used with any mod, its structure should be fairly simple and used as a extension for any mod, it also serves easily "teach" how to make full use of custom made JSON files to save/create new data, without resorting to create static arrays in your code.

Right now im not even home to start this, here's the general idea that ill be aiming when making this, lets start with a Blueprint mod, theres has been some development from our chinese modders but like most mods, it used static arrays to save its data.

BLUEPRINTS

Datafiles:
object.blueprint > Its a json file with a special extension just like the files already used by the game itself like, for example, *.weapon, *.ship, *.system ...
This allows any mod to add a individual blueprint that can be used by the game, just like other Json files it has multiple proprieties (might changed based on its needs) that need to be defined on the file, here a general example:

Code: json
{
  "blueprint_id": "thisid",
  "cargo_id": "op_weaponid",
  "imageURL": "graphics/blueprint/op_weapon_bp.png",
  "input" : [
    { "cargo_id" : "light_metal_id", "amount" : 35 },
    { "cargo_id" : "heavy_metal_id", "amount" : 5 }
  ],
  "output" : [
    { "object_id" : "op_weapon", "amount" : 1 }
  ],
  "buy_value": 10000,
  "sell_value": 5000,
  "must_be_found": false
}
All JSONObjects and Arrays are mostly self explanatory, to create clean Json files use these online tools:

Class:
[initBlueprints] > Should be initiated in OnEnabled(). Translate all the JSON files into data and converts that data into BlueprintAPI objects, once it completes it saves all into a ArrayList.
Spoiler
Code: java
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by eXist3nZ on 15-12-2013.
 */
public class initBlueprints {

    private static ArrayList<BlueprintAPI> listBlueprints = new ArrayList<BlueprintAPI>();
    private static final String FILE_DIR = "\\data\\blueprints\\";
    private static final String FILE_TEXT_EXT = ".blueprint";

    public void loadBlueprints()
    {
        getAllBlueprints(finder(FILE_DIR));
    }

    public File[] finder(String dirName){
        File dir = new File(dirName);

        return dir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String filename)
            {
                return filename.endsWith(FILE_TEXT_EXT);
            }
        } );

    }

    public ArrayList<BlueprintAPI> getAllBlueprints(File[] files)
    {
        for(File file: files)
        {
            this.listBlueprints.add(readJSON(file));
        }

        return this.listBlueprints;
    }

    public BlueprintAPI readJSON(File file)
    {
        /*
        {
              "blueprint_id": "thisid",
              "cargo_id": "op_weaponid",
              "imageURL": "graphics/blueprint/op_weapon_bp.png",
              "input" : [
                { "cargo_id" : "light_metal_id", "amount" : 35 },
                { "cargo_id" : "heavy_metal_id", "amount" : 5 }
              ],
              "output" : {
                "object_id" : "op_weapon",
                "amount" : 1
              },
              "buy_value": 10000,
              "sell_value": 5000,
              "must_be_found": false
        }
        */

        try {
            JSONObject mainfile = new JSONObject(file.getCanonicalPath());
            JSONArray inputarray = new JSONArray(mainfile.getJSONArray("input"));
            JSONArray outputarray = new JSONArray(mainfile.getJSONArray("output"));

            //You dont need to make 4 ArrayLists, instead you can make 2 ArrayLists with type Object or with a Output Class and Input Class to save those values in.
            ArrayList<String> inputidlist = new ArrayList<String>();
            ArrayList<Integer> inputamountlist = new ArrayList<Integer>();
            ArrayList<String> outputidlist = new ArrayList<String>();
            ArrayList<Integer> outputamountlist = new ArrayList<Integer>();

            for(int a1 = 0;a1 < inputarray.length();a1++)
            {
                JSONObject inputobj = inputarray.getJSONObject(a1);
                inputidlist.add(inputobj.getString("cargo_id"));
                inputamountlist.add(inputobj.getInt("amount"));
            }

            for(int a2 = 0;a2 < outputarray.length();a2++)
            {
                JSONObject outputobj = outputarray.getJSONObject(a2);
                outputidlist.add(outputobj.getString("cargo_id"));
                outputamountlist.add(outputobj.getInt("amount"));
            }

            return new BlueprintAPI(
                     mainfile.getString("blueprint_id"),
                     mainfile.getString("cargo_id"),
                     mainfile.getString("imageURL"),
                     inputidlist,
                     inputamountlist,
                     outputidlist,
                     outputamountlist,
                     mainfile.getInt("buy_value"),
                     mainfile.getInt("sell_value"),
                     mainfile.getBoolean("must_be_found")
            );
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //should never reach here.
        return null;
    }
}
[close]
[BlueprintAPI] > Created by the init class, each object is a blueprint json file that has all its saved data. Each object have its gets/sets in case you want to change any of the proprieties used.
Spoiler
Code: java
import java.util.ArrayList;

/**
 * Created by eXist3nZ on 15-12-2013.
 */
public class BlueprintAPI {

    private String blueprint_id = "";
    private String cargo_id = "";
    private String imageURL = "";
    private ArrayList<String> inputidlist = null;
    private ArrayList<Integer> inputamountlist = null;
    private ArrayList<String> outputidlist = null;
    private ArrayList<Integer> outputamountlist = null;
    private Integer buy_value = 0;
    private Integer sell_value = 0;
    private Boolean must_be_found = false;

    public BlueprintAPI(String blueprint_id, String cargo_id,
                        String imageURL, ArrayList<String> inputidlist,
                        ArrayList<Integer> inputamountlist, ArrayList<String> outputidlist,
                        ArrayList<Integer> outputamountlist, Integer buy_value,
                        Integer sell_value, Boolean must_be_found)
    {
        this.setBlueprint_id(blueprint_id);
        this.setBuy_value(buy_value);
        this.setSell_value(sell_value);
        this.setCargo_id(cargo_id);
        this.setImageURL(imageURL);
        this.setInputidlist(inputidlist);
        this.setInputamountlist(inputamountlist);
        this.setOutputidlist(outputidlist);
        this.setOutputamountlist(outputamountlist);
        this.setMust_be_found(must_be_found);
    }

    public Integer getSell_value() {
        return sell_value;
    }

    public void setSell_value(Integer sell_value) {
        this.sell_value = sell_value;
    }

    public ArrayList<String> getOutputidlist() {
        return outputidlist;
    }

    public void setOutputidlist(ArrayList<String> outputidlist) {
        this.outputidlist = outputidlist;
    }

    public ArrayList<Integer> getInputamountlist() {
        return inputamountlist;
    }

    public void setInputamountlist(ArrayList<Integer> inputamountlist) {
        this.inputamountlist = inputamountlist;
    }

    public ArrayList<String> getInputidlist() {
        return inputidlist;
    }

    public void setInputidlist(ArrayList<String> inputidlist) {
        this.inputidlist = inputidlist;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }

    public Boolean getMust_be_found() {
        return must_be_found;
    }

    public void setMust_be_found(Boolean must_be_found) {
        this.must_be_found = must_be_found;
    }

    public ArrayList<Integer> getOutputamountlist() {
        return outputamountlist;
    }

    public void setOutputamountlist(ArrayList<Integer> outputamountlist) {
        this.outputamountlist = outputamountlist;
    }

    public Integer getBuy_value() {
        return buy_value;
    }

    public void setBuy_value(Integer buy_value) {
        this.buy_value = buy_value;
    }

    public String getCargo_id() {
        return cargo_id;
    }

    public void setCargo_id(String cargo_id) {
        this.cargo_id = cargo_id;
    }

    public String getBlueprint_id() {
        return blueprint_id;
    }

    public void setBlueprint_id(String blueprint_id) {
        this.blueprint_id = blueprint_id;
    }

}
[close]
MINING

Datafiles:
object.mining > Its a json file with a special extension just like the files already used by the game itself like, for example, *.weapon, *.ship, *.system ...
This allows any mod to identify specific weapons as mining "tools", just like other Json files it has multiple proprieties (might changed based on its needs) that need to be defined on the file, here a general example:
Code: json
{
  "mining_id": "thisid",
  "weapon_id": "mining_weapon_id",
  "effective_level": 0.5, #from 0 to 1f
  "affected_by_crew_XP": false
}
All JSONObjects and Arrays are mostly self explanatory,  each ship have x amount of weapons that allow mining, while some ships are mining ships they also possess weapons set to them removing any need to use ship_hull_id, to create clean Json files use these online tools:

Class:
[initMiningData] > Should be initiated in OnEnabled(). Translate all the JSON files into MiningAPI objects.
[MiningAPI] > Makes all the calculations using various methods, returning a specific amount of Cargo as a result. Requires a ShipAPI, FleetMemberAPI or a CampaignFleetAPI object to verify the mining effectiveness.

While i intent to use crew XP to be a real factor of how much mining resources are generated, some ships might not have crew, for these a default value of 0.5f is used. This happens when ships like "Mining Drones" come into play.

Will try to get these mods as "other mod friendly" as possible, it also lacks the interface to be used with (its not absolutely needed tho). Suggestions or help are welcome.

All code made by me is available with the following licence attached.
This work is licensed under a Creative Commons License
« Last Edit: December 17, 2013, 07:55:25 AM by silentstormpt »
Logged