Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
pointabilities.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2023 cx384
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21#include <string>
22#include <unordered_map>
23#include "itemgroup.h"
24#include <optional>
25#include "irrlichttypes.h"
26
27enum class PointabilityType : u8
28{
29 // Can be pointed through.
30 // Note: This MUST be the 0-th item in the enum for backwards compat.
31 // Older Minetest versions send "pointable=false" as "0".
33 // Is pointable.
34 // Note: This MUST be the 1-th item in the enum for backwards compat:
35 // Older Minetest versions send "pointable=true" as "1".
37 // Note: Since (u8) 2 is truthy,
38 // older clients will understand this as "pointable=true",
39 // which is a reasonable fallback.
41};
42
43// An object to store overridden pointable properties
45{
46 // Nodes
47 std::unordered_map<std::string, PointabilityType> nodes;
48 std::unordered_map<std::string, PointabilityType> node_groups;
49
50 // Objects
51 std::unordered_map<std::string, PointabilityType> objects;
52 std::unordered_map<std::string, PointabilityType> object_groups; // armor_groups
53
54 // Match functions return fitting pointability,
55 // otherwise the default pointability should be used.
56
57 std::optional<PointabilityType> matchNode(const std::string &name,
58 const ItemGroupList &groups) const;
59 std::optional<PointabilityType> matchObject(const std::string &name,
60 const ItemGroupList &groups) const;
61 // For players only armor groups will work
62 std::optional<PointabilityType> matchPlayer(const ItemGroupList &groups) const;
63
64 void serialize(std::ostream &os) const;
65 void deSerialize(std::istream &is);
66
67 // For a save enum conversion.
68 static PointabilityType deSerializePointabilityType(std::istream &is);
69 static void serializePointabilityType(std::ostream &os, PointabilityType pointable_type);
70 static std::string toStringPointabilityType(PointabilityType pointable_type);
71
72private:
73 static std::optional<PointabilityType> matchGroups(const ItemGroupList &groups,
74 const std::unordered_map<std::string, PointabilityType> &pointable_groups);
75 static void serializeTypeMap(std::ostream &os,
76 const std::unordered_map<std::string, PointabilityType> &map);
77 static void deSerializeTypeMap(std::istream &is,
78 std::unordered_map<std::string, PointabilityType> &map);
79};
std::unordered_map< std::string, int > ItemGroupList
Definition: itemgroup.h:25
PointabilityType
Definition: pointabilities.h:28
Definition: pointabilities.h:45
std::unordered_map< std::string, PointabilityType > objects
Definition: pointabilities.h:51
std::optional< PointabilityType > matchObject(const std::string &name, const ItemGroupList &groups) const
Definition: pointabilities.cpp:67
static void serializePointabilityType(std::ostream &os, PointabilityType pointable_type)
Definition: pointabilities.cpp:42
std::unordered_map< std::string, PointabilityType > nodes
Definition: pointabilities.h:47
static void serializeTypeMap(std::ostream &os, const std::unordered_map< std::string, PointabilityType > &map)
Definition: pointabilities.cpp:106
static std::string toStringPointabilityType(PointabilityType pointable_type)
Definition: pointabilities.cpp:47
static void deSerializeTypeMap(std::istream &is, std::unordered_map< std::string, PointabilityType > &map)
Definition: pointabilities.cpp:116
void serialize(std::ostream &os) const
Definition: pointabilities.cpp:128
std::optional< PointabilityType > matchNode(const std::string &name, const ItemGroupList &groups) const
Definition: pointabilities.cpp:60
void deSerialize(std::istream &is)
Definition: pointabilities.cpp:137
std::optional< PointabilityType > matchPlayer(const ItemGroupList &groups) const
Definition: pointabilities.cpp:74
static PointabilityType deSerializePointabilityType(std::istream &is)
Definition: pointabilities.cpp:26
std::unordered_map< std::string, PointabilityType > object_groups
Definition: pointabilities.h:52
std::unordered_map< std::string, PointabilityType > node_groups
Definition: pointabilities.h:48
static std::optional< PointabilityType > matchGroups(const ItemGroupList &groups, const std::unordered_map< std::string, PointabilityType > &pointable_groups)
Definition: pointabilities.cpp:79