diff --git a/assets/materials/commons.json b/assets/materials/commons.json new file mode 100644 index 0000000..00103cf --- /dev/null +++ b/assets/materials/commons.json @@ -0,0 +1,26 @@ +{ + "material_flags": [ + { + "metal": { + "conducts_elictricity": true, + "blocks_liquid": true, + "acid_resistant": true, + "blocks_gas": true, + "flammable": false, + "conducts_heat": true, + "radiates": true + } + }, + { + "wood": { + "conducts_elictricity": false, + "blocks_liquid": true, + "acid_resistant": false, + "blocks_gas": true, + "flammable": true, + "conducts_heat": false, + "radiates": false + } + } + ] +} \ No newline at end of file diff --git a/assets/materials/metals/metals.json b/assets/materials/metals/metals.json new file mode 100644 index 0000000..32e17de --- /dev/null +++ b/assets/materials/metals/metals.json @@ -0,0 +1,28 @@ +{ + "materials": [ + { + "steel": { + "name": "steel", + "material_flags": { + "$ref": "#/material_flags/metal" + }, + "density": "7800", + "fracture_toughness": "30", + "melting_point": "1400", + "boiling_point": "3200" + } + }, + { + "iron": { + "name": "iron", + "material_flags": { + "$ref": "#/material_flags/metal" + }, + "density": "7800", + "fracture_toughness": "12", + "melting_point": "1400", + "boiling_point": "3200" + } + } + ] +} \ No newline at end of file diff --git a/assets/materials/misc.json b/assets/materials/misc.json new file mode 100644 index 0000000..a2ac6dd --- /dev/null +++ b/assets/materials/misc.json @@ -0,0 +1,15 @@ +{ + "material_flags": [ + { + "acme-fiber": { + "conducts_elictricity": false, + "blocks_liquid": true, + "acid_resistant": true, + "blocks_gas": true, + "flammable": false, + "conducts_heat": false, + "radiates": false + } + } + ] +} \ No newline at end of file diff --git a/assets/materials/wood/wood.json b/assets/materials/wood/wood.json new file mode 100644 index 0000000..9ba017d --- /dev/null +++ b/assets/materials/wood/wood.json @@ -0,0 +1,16 @@ +{ + "materials": [ + { + "oakwood": { + "name": "testoakwood", + "material_flags": { + "$ref": "#/material_flags/wood" + }, + "density": "700", + "fracture_toughness": "4.5", + "melting_point": "600", + "boiling_point": null + } + } + ] +} \ No newline at end of file diff --git a/engine/items/itemprops/body.go b/engine/items/itemprops/body.go index 96a10d3..a723133 100644 --- a/engine/items/itemprops/body.go +++ b/engine/items/itemprops/body.go @@ -84,6 +84,7 @@ type BodyPart struct { LayerMiddle MedicalMaterial LayerInner MedicalMaterial + Size DimensionItemSize Joints []Joint Contains []InnerOrgan Exposes []OuterOrgan diff --git a/engine/items/itemprops/ip_test.go b/engine/items/itemprops/ip_test.go index 35e720a..a0a9655 100644 --- a/engine/items/itemprops/ip_test.go +++ b/engine/items/itemprops/ip_test.go @@ -2,9 +2,13 @@ package itemprops import ( "encoding/json" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/shopspring/decimal" "github.com/stretchr/testify/suite" + "os" "testing" + "time" ) type IpTestSuite struct { @@ -173,3 +177,32 @@ func (suite *IpTestSuite) TestMaterialSerialization() { string(bytes), ) } + +func (suite *IpTestSuite) TestMaterialDeserialization() { + + logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}) + mm, err := NewMaterialMap("../../..", logger) + suite.NoError(err) + + metalMaterialFlags := MaterialFlags{ + ConductsElictricity: true, + BlocksLiquid: true, + AcidResistant: true, + BlocksGas: true, + Flammable: false, + ConductsHeat: true, + Radiates: true, + } + + teststeel := Material{ + Name: "teststeel", + Flags: metalMaterialFlags, + Density: DimensionItemDensity{decimal.NewFromInt(7800)}, + FractureToughness: DimensionFractureToughness{decimal.NewFromInt(30)}, + MeltingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(1400), true}}, + BoilingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(3200), true}}, + } + + _ = teststeel + _ = mm +} diff --git a/engine/items/itemprops/material.go b/engine/items/itemprops/material.go index b7c86a0..2e9565b 100644 --- a/engine/items/itemprops/material.go +++ b/engine/items/itemprops/material.go @@ -1,6 +1,17 @@ package itemprops +import ( + "encoding/json" + "fmt" + "github.com/rs/zerolog" + "io/ioutil" + "os" + "path/filepath" + "strings" +) + type Material struct { + Id string `json:"id"` Name string `json:"name"` Flags MaterialFlags `json:"material_flags"` Density DimensionItemDensity `json:"density"` @@ -9,6 +20,8 @@ type Material struct { BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"` } +func (m Material) Unmarshal() {} + type MaterialFlags struct { ConductsElictricity bool `json:"conducts_elictricity"` BlocksLiquid bool `json:"blocks_liquid"` @@ -18,3 +31,79 @@ type MaterialFlags struct { ConductsHeat bool `json:"conducts_heat"` Radiates bool `json:"radiates"` } + +type MaterialMap map[string]Material + +type tt map[string]MaterialMap + +func NewMaterialMap(path string, logger zerolog.Logger) (*MaterialMap, error) { + + mm := &MaterialMap{} + tmp := make(map[string][]interface{}) + flags := make(map[string]*MaterialFlags) + err := filepath.Walk(path+"/assets/materials", + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if strings.HasSuffix(path, ".json") { + splt := strings.Split(path, "/") + logger.Info().Msgf("loading %s %d", splt[len(splt)-1], info.Size()) + bytes, err := ioutil.ReadFile(path) + if err != nil { + return err + } + ttmp := make(map[string][]interface{}) + err = json.Unmarshal(bytes, &ttmp) + + if err != nil { + return err + } + for idx, _ := range ttmp { + tmp[idx] = append(tmp[idx], ttmp[idx]...) + } + } + return nil + }) + + _ = flags + if lst, ok := tmp["material_flags"]; ok { + for _, item := range lst { + ttt := item.(map[string]interface{}) + _ = ttt + for clause, item2 := range ttt { + bts, err := json.Marshal(item2) + if err != nil { + return nil, fmt.Errorf("Could not marshal back:%w", err) + } + flags[clause] = &MaterialFlags{} + err = json.Unmarshal(bts, flags[clause]) + if err != nil { + return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err) + } + } + } + } + logger.Info().Msgf("loaded %d material flag sets", len(flags)) + + if lst, ok := tmp["materials"]; ok { + for _, item := range lst { + ttt := item.(map[string]interface{}) + _ = ttt + for clause, item2 := range ttt { + + bts, err := json.Marshal(item2) + if err != nil { + return nil, fmt.Errorf("Could not marshal back:%w", err) + } + flags[clause] = &MaterialFlags{} + err = json.Unmarshal(bts, flags[clause]) + if err != nil { + return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err) + } + } + } + } + return mm, err +}