Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
nodetimer.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irr_v3d.h"
8#include <iostream>
9#include <map>
10#include <vector>
11
12/*
13 NodeTimer provides per-node timed callback functionality.
14 Can be used for:
15 - Furnaces, to keep the fire burnin'
16 - "activated" nodes that snap back to their original state
17 after a fixed amount of time (mesecons buttons, for example)
18*/
19
21{
22public:
23 NodeTimer() = default;
24 NodeTimer(const v3s16 &position_):
25 position(position_) {}
26 NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_):
27 timeout(timeout_), elapsed(elapsed_), position(position_) {}
28 ~NodeTimer() = default;
29
30 void serialize(std::ostream &os) const;
31 void deSerialize(std::istream &is);
32
33 f32 timeout = 0.0f;
34 f32 elapsed = 0.0f;
36};
37
38/*
39 List of timers of all the nodes of a block
40*/
41
43{
44public:
45 NodeTimerList() = default;
46 ~NodeTimerList() = default;
47
48 void serialize(std::ostream &os, u8 map_format_version) const;
49 void deSerialize(std::istream &is, u8 map_format_version);
50
51 // Get timer
52 NodeTimer get(const v3s16 &p) {
53 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
54 m_iterators.find(p);
55 if (n == m_iterators.end())
56 return NodeTimer();
57 NodeTimer t = n->second->second;
58 t.elapsed = t.timeout - (n->second->first - m_time);
59 return t;
60 }
61 // Deletes timer
62 void remove(v3s16 p) {
63 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
64 m_iterators.find(p);
65 if(n != m_iterators.end()) {
66 double removed_time = n->second->first;
67 m_timers.erase(n->second);
68 m_iterators.erase(n);
69 // Yes, this is float equality, but it is not a problem
70 // since we only test equality of floats as an ordered type
71 // and thus we never lose precision
72 if (removed_time == m_next_trigger_time) {
73 if (m_timers.empty())
75 else
76 m_next_trigger_time = m_timers.begin()->first;
77 }
78 }
79 }
80 // Undefined behavior if there already is a timer
81 void insert(const NodeTimer &timer) {
82 v3s16 p = timer.position;
83 double trigger_time = m_time + (double)(timer.timeout - timer.elapsed);
84 std::multimap<double, NodeTimer>::iterator it = m_timers.emplace(trigger_time, timer);
85 m_iterators.emplace(p, it);
86 if (m_next_trigger_time == -1. || trigger_time < m_next_trigger_time)
87 m_next_trigger_time = trigger_time;
88 }
89 // Deletes old timer and sets a new one
90 inline void set(const NodeTimer &timer) {
91 remove(timer.position);
92 insert(timer);
93 }
94 // Deletes all timers
95 void clear() {
96 m_timers.clear();
97 m_iterators.clear();
99 }
100
101 // Move forward in time, returns elapsed timers
102 std::vector<NodeTimer> step(float dtime);
103
104private:
105 std::multimap<double, NodeTimer> m_timers;
106 std::map<v3s16, std::multimap<double, NodeTimer>::iterator> m_iterators;
107 double m_next_trigger_time = -1.0;
108 double m_time = 0.0;
109};
Definition nodetimer.h:43
~NodeTimerList()=default
std::multimap< double, NodeTimer > m_timers
Definition nodetimer.h:105
void insert(const NodeTimer &timer)
Definition nodetimer.h:81
std::map< v3s16, std::multimap< double, NodeTimer >::iterator > m_iterators
Definition nodetimer.h:106
void remove(v3s16 p)
Definition nodetimer.h:62
double m_time
Definition nodetimer.h:108
void set(const NodeTimer &timer)
Definition nodetimer.h:90
NodeTimer get(const v3s16 &p)
Definition nodetimer.h:52
double m_next_trigger_time
Definition nodetimer.h:107
std::vector< NodeTimer > step(float dtime)
Definition nodetimer.cpp:113
void clear()
Definition nodetimer.h:95
void serialize(std::ostream &os, u8 map_format_version) const
Definition nodetimer.cpp:31
NodeTimerList()=default
void deSerialize(std::istream &is, u8 map_format_version)
Definition nodetimer.cpp:60
Definition nodetimer.h:21
NodeTimer()=default
f32 timeout
Definition nodetimer.h:33
f32 elapsed
Definition nodetimer.h:34
~NodeTimer()=default
v3s16 position
Definition nodetimer.h:35
void serialize(std::ostream &os) const
Definition nodetimer.cpp:15
NodeTimer(const v3s16 &position_)
Definition nodetimer.h:24
NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_)
Definition nodetimer.h:26
void deSerialize(std::istream &is)
Definition nodetimer.cpp:21
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
static std::string p(std::string path)
Definition test_filesys.cpp:53