Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
pointabilities.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2023 cx384
4
5#pragma once
6#include <string>
7#include <unordered_map>
8#include "itemgroup.h"
9#include <optional>
10#include "irrlichttypes.h"
11
12enum class PointabilityType : u8
13{
14 // Can be pointed through.
15 // Note: This MUST be the 0-th item in the enum for backwards compat.
16 // Older Minetest versions send "pointable=false" as "0".
18 // Is pointable.
19 // Note: This MUST be the 1-th item in the enum for backwards compat:
20 // Older Minetest versions send "pointable=true" as "1".
22 // Note: Since (u8) 2 is truthy,
23 // older clients will understand this as "pointable=true",
24 // which is a reasonable fallback.
26};
27
28// An object to store overridden pointable properties
30{
31 // Nodes
32 std::unordered_map<std::string, PointabilityType> nodes;
33 std::unordered_map<std::string, PointabilityType> node_groups;
34
35 // Objects
36 std::unordered_map<std::string, PointabilityType> objects;
37 std::unordered_map<std::string, PointabilityType> object_groups; // armor_groups
38
39 // Match functions return fitting pointability,
40 // otherwise the default pointability should be used.
41
42 std::optional<PointabilityType> matchNode(const std::string &name,
43 const ItemGroupList &groups) const;
44 std::optional<PointabilityType> matchObject(const std::string &name,
45 const ItemGroupList &groups) const;
46 // For players only armor groups will work
47 std::optional<PointabilityType> matchPlayer(const ItemGroupList &groups) const;
48
49 void serialize(std::ostream &os) const;
50 void deSerialize(std::istream &is);
51
52 // For a save enum conversion.
53 static PointabilityType deSerializePointabilityType(std::istream &is);
54 static void serializePointabilityType(std::ostream &os, PointabilityType pointable_type);
55 static std::string toStringPointabilityType(PointabilityType pointable_type);
56
57private:
58 static std::optional<PointabilityType> matchGroups(const ItemGroupList &groups,
59 const std::unordered_map<std::string, PointabilityType> &pointable_groups);
60 static void serializeTypeMap(std::ostream &os,
61 const std::unordered_map<std::string, PointabilityType> &map);
62 static void deSerializeTypeMap(std::istream &is,
63 std::unordered_map<std::string, PointabilityType> &map);
64};
std::unordered_map< std::string, int > ItemGroupList
Definition itemgroup.h:10
PointabilityType
Definition pointabilities.h:13
Definition pointabilities.h:30
std::unordered_map< std::string, PointabilityType > objects
Definition pointabilities.h:36
std::optional< PointabilityType > matchObject(const std::string &name, const ItemGroupList &groups) const
Definition pointabilities.cpp:52
static void serializePointabilityType(std::ostream &os, PointabilityType pointable_type)
Definition pointabilities.cpp:27
std::unordered_map< std::string, PointabilityType > nodes
Definition pointabilities.h:32
static void serializeTypeMap(std::ostream &os, const std::unordered_map< std::string, PointabilityType > &map)
Definition pointabilities.cpp:91
static std::string toStringPointabilityType(PointabilityType pointable_type)
Definition pointabilities.cpp:32
static void deSerializeTypeMap(std::istream &is, std::unordered_map< std::string, PointabilityType > &map)
Definition pointabilities.cpp:101
void serialize(std::ostream &os) const
Definition pointabilities.cpp:113
std::optional< PointabilityType > matchNode(const std::string &name, const ItemGroupList &groups) const
Definition pointabilities.cpp:45
void deSerialize(std::istream &is)
Definition pointabilities.cpp:122
std::optional< PointabilityType > matchPlayer(const ItemGroupList &groups) const
Definition pointabilities.cpp:59
static PointabilityType deSerializePointabilityType(std::istream &is)
Definition pointabilities.cpp:11
std::unordered_map< std::string, PointabilityType > object_groups
Definition pointabilities.h:37
std::unordered_map< std::string, PointabilityType > node_groups
Definition pointabilities.h:33
static std::optional< PointabilityType > matchGroups(const ItemGroupList &groups, const std::unordered_map< std::string, PointabilityType > &pointable_groups)
Definition pointabilities.cpp:64