]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mesh_generator_thread.h
Fix #5617 - respect message and reconnect parameters when shutting down immediately...
[dragonfireclient.git] / src / mesh_generator_thread.h
1 /*
2 Minetest
3 Copyright (C) 2013, 2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MESH_GENERATOR_THREAD_HEADER
21 #define MESH_GENERATOR_THREAD_HEADER
22
23 #include "mapblock_mesh.h"
24 #include "threading/mutex_auto_lock.h"
25 #include "util/thread.h"
26
27 struct CachedMapBlockData
28 {
29         v3s16 p;
30         MapNode *data; // A copy of the MapBlock's data member
31         int refcount_from_queue;
32         int last_used_timestamp;
33
34         CachedMapBlockData();
35         ~CachedMapBlockData();
36 };
37
38 struct QueuedMeshUpdate
39 {
40         v3s16 p;
41         bool ack_block_to_server;
42         bool urgent;
43         int crack_level;
44         v3s16 crack_pos;
45         MeshMakeData *data; // This is generated in MeshUpdateQueue::pop()
46
47         QueuedMeshUpdate();
48         ~QueuedMeshUpdate();
49 };
50
51 /*
52         A thread-safe queue of mesh update tasks and a cache of MapBlock data
53 */
54 class MeshUpdateQueue
55 {
56         enum UpdateMode
57         {
58                 FORCE_UPDATE,
59                 SKIP_UPDATE_IF_ALREADY_CACHED,
60         };
61
62 public:
63         MeshUpdateQueue(Client *client);
64
65         ~MeshUpdateQueue();
66
67         // Caches the block at p and its neighbors (if needed) and queues a mesh
68         // update for the block at p
69         void addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
70
71         // Returned pointer must be deleted
72         // Returns NULL if queue is empty
73         QueuedMeshUpdate *pop();
74
75         u32 size()
76         {
77                 MutexAutoLock lock(m_mutex);
78                 return m_queue.size();
79         }
80
81 private:
82         Client *m_client;
83         std::vector<QueuedMeshUpdate *> m_queue;
84         std::set<v3s16> m_urgents;
85         std::map<v3s16, CachedMapBlockData *> m_cache;
86         Mutex m_mutex;
87
88         // TODO: Add callback to update these when g_settings changes
89         bool m_cache_enable_shaders;
90         bool m_cache_use_tangent_vertices;
91         bool m_cache_smooth_lighting;
92         int m_meshgen_block_cache_size;
93
94         CachedMapBlockData *cacheBlock(Map *map, v3s16 p, UpdateMode mode,
95                         size_t *cache_hit_counter = NULL);
96         CachedMapBlockData *getCachedBlock(const v3s16 &p);
97         void fillDataFromMapBlockCache(QueuedMeshUpdate *q);
98         void cleanupCache();
99 };
100
101 struct MeshUpdateResult
102 {
103         v3s16 p;
104         MapBlockMesh *mesh;
105         bool ack_block_to_server;
106
107         MeshUpdateResult()
108             : p(-1338, -1338, -1338), mesh(NULL), ack_block_to_server(false)
109         {
110         }
111 };
112
113 class MeshUpdateThread : public UpdateThread
114 {
115 public:
116         MeshUpdateThread(Client *client);
117
118         // Caches the block at p and its neighbors (if needed) and queues a mesh
119         // update for the block at p
120         void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
121
122         v3s16 m_camera_offset;
123         MutexedQueue<MeshUpdateResult> m_queue_out;
124
125 private:
126         MeshUpdateQueue m_queue_in;
127
128         // TODO: Add callback to update these when g_settings changes
129         int m_generation_interval;
130
131 protected:
132         virtual void doUpdate();
133 };
134
135 #endif