1
0
forked from MTSR/mapserver
mapserver/mapobject/smartshop.go

75 lines
1.6 KiB
Go
Raw Normal View History

2019-02-08 13:23:04 +03:00
package mapobject
import (
"mapserver/mapobjectdb"
2023-12-29 18:00:11 +03:00
"mapserver/types"
2019-02-08 13:45:56 +03:00
"math"
2019-02-08 18:02:24 +03:00
"strconv"
2021-10-12 08:44:07 +03:00
"github.com/minetest-go/mapparser"
2019-02-08 13:23:04 +03:00
)
type SmartShopBlock struct{}
2023-12-29 18:00:11 +03:00
func (this *SmartShopBlock) onMapObject(mbpos *types.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) []*mapobjectdb.MapObject {
2019-02-12 15:34:15 +03:00
list := make([]*mapobjectdb.MapObject, 0)
2019-02-08 13:45:56 +03:00
2019-02-08 13:23:04 +03:00
md := block.Metadata.GetMetadata(x, y, z)
2019-02-08 13:45:56 +03:00
invMap := block.Metadata.GetInventoryMapAtPos(x, y, z)
mainInv := invMap["main"]
isCreative := md["type"] == "0"
2019-02-08 13:45:56 +03:00
if mainInv.IsEmpty() {
return list
}
for i := 1; i <= 4; i++ {
payInvName := "pay" + strconv.Itoa(i)
giveInvName := "give" + strconv.Itoa(i)
pay := invMap[payInvName]
give := invMap[giveInvName]
if len(pay.Items) == 0 || len(give.Items) == 0 {
2019-02-08 13:45:56 +03:00
continue
}
2021-10-12 08:44:07 +03:00
o := mapobjectdb.NewMapObject(mbpos, x, y, z, "shop")
2019-02-08 13:45:56 +03:00
o.Attributes["type"] = "smartshop"
o.Attributes["owner"] = md["owner"]
in_item := pay.Items[0].Name
in_count := math.Max(1, float64(pay.Items[0].Count))
out_item := give.Items[0].Name
out_count := math.Max(1, float64(give.Items[0].Count))
stock := 0
2019-02-08 13:23:04 +03:00
if isCreative {
stock = 999
} else {
for _, item := range mainInv.Items {
if item.Name == out_item {
stock += item.Count
}
2019-02-08 18:02:24 +03:00
}
2019-02-08 13:45:56 +03:00
}
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
//multiples of out_count
2019-02-08 18:02:24 +03:00
stock_factor := math.Floor(float64(stock) / float64(out_count))
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
o.Attributes["in_item"] = in_item
o.Attributes["in_count"] = strconv.Itoa(int(in_count))
o.Attributes["out_item"] = out_item
o.Attributes["out_count"] = strconv.Itoa(int(out_count))
o.Attributes["stock"] = strconv.Itoa(int(stock_factor))
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
list = append(list, o)
}
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
return list
2019-02-08 13:23:04 +03:00
}