]> git.lizzy.rs Git - dragonfireclient.git/blob - src/particles.cpp
Merge pull request #482 from proller/liquid
[dragonfireclient.git] / src / particles.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 #include "particles.h"
21 #include "constants.h"
22 #include "debug.h"
23 #include "main.h" // For g_profiler and g_settings
24 #include "settings.h"
25 #include "tile.h"
26 #include "gamedef.h"
27 #include "collision.h"
28 #include <stdlib.h>
29 #include "util/numeric.h"
30 #include "light.h"
31 #include "environment.h"
32 #include "clientmap.h"
33 #include "mapnode.h"
34
35 Particle::Particle(
36         IGameDef *gamedef,
37         scene::ISceneManager* smgr,
38         LocalPlayer *player,
39         s32 id,
40         v3f pos,
41         v3f velocity,
42         v3f acceleration,
43         float expirationtime,
44         float size,
45         AtlasPointer ap
46 ):
47         scene::ISceneNode(smgr->getRootSceneNode(), smgr, id)
48 {
49         // Misc
50         m_gamedef = gamedef;
51
52         // Texture
53         m_material.setFlag(video::EMF_LIGHTING, false);
54         m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
55         m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
56         m_material.setFlag(video::EMF_FOG_ENABLE, true);
57         m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
58         m_material.setTexture(0, ap.atlas);
59         m_ap = ap;
60         m_light = 0;
61
62
63         // Particle related
64         m_pos = pos;
65         m_velocity = velocity;
66         m_acceleration = acceleration;
67         m_expiration = expirationtime;
68         m_time = 0;
69         m_player = player;
70         m_size = size;
71
72         // Irrlicht stuff (TODO)
73         m_collisionbox = core::aabbox3d<f32>(-size/2,-size/2,-size/2,size/2,size/2,size/2);
74         this->setAutomaticCulling(scene::EAC_OFF);
75 }
76
77 Particle::~Particle()
78 {
79 }
80
81 void Particle::OnRegisterSceneNode()
82 {
83         if (IsVisible)
84         {
85                 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
86                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
87         }
88
89         ISceneNode::OnRegisterSceneNode();
90 }
91
92 void Particle::render()
93 {
94         // TODO: Render particles in front of water and the selectionbox
95
96         video::IVideoDriver* driver = SceneManager->getVideoDriver();
97         driver->setMaterial(m_material);
98         driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
99         video::SColor c(255, m_light, m_light, m_light);
100
101         video::S3DVertex vertices[4] =
102         {
103                 video::S3DVertex(-m_size/2,-m_size/2,0, 0,0,0, c, m_ap.x0(), m_ap.y1()),
104                 video::S3DVertex(m_size/2,-m_size/2,0, 0,0,0, c, m_ap.x1(), m_ap.y1()),
105                 video::S3DVertex(m_size/2,m_size/2,0, 0,0,0, c, m_ap.x1(), m_ap.y0()),
106                 video::S3DVertex(-m_size/2,m_size/2,0, 0,0,0, c ,m_ap.x0(), m_ap.y0()),
107         };
108
109         for(u16 i=0; i<4; i++)
110         {
111                 vertices[i].Pos.rotateYZBy(m_player->getPitch());
112                 vertices[i].Pos.rotateXZBy(m_player->getYaw());
113                 m_box.addInternalPoint(vertices[i].Pos);
114                 vertices[i].Pos += m_pos*BS;
115         }
116
117         u16 indices[] = {0,1,2, 2,3,0};
118         driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
119                         video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
120 }
121
122 void Particle::step(float dtime, ClientEnvironment &env)
123 {
124         core::aabbox3d<f32> box = m_collisionbox;
125         v3f p_pos = m_pos*BS;
126         v3f p_velocity = m_velocity*BS;
127         v3f p_acceleration = m_acceleration*BS;
128         collisionMoveSimple(&env.getClientMap(), m_gamedef,
129                 BS*0.5, box,
130                 0, dtime,
131                 p_pos, p_velocity, p_acceleration);
132         m_pos = p_pos/BS;
133         m_velocity = p_velocity/BS;
134         m_acceleration = p_acceleration/BS;
135         m_time += dtime;
136
137         // Update lighting
138         u8 light = 0;
139         try{
140                 v3s16 p = v3s16(
141                         floor(m_pos.X+0.5),
142                         floor(m_pos.Y+0.5),
143                         floor(m_pos.Z+0.5)
144                 );
145                 MapNode n = env.getClientMap().getNode(p);
146                 light = n.getLightBlend(env.getDayNightRatio(), m_gamedef->ndef());
147         }
148         catch(InvalidPositionException &e){
149                 light = blend_light(env.getDayNightRatio(), LIGHT_SUN, 0);
150         }
151         m_light = decode_light(light);
152 }
153
154 std::vector<Particle*> all_particles;
155
156 void allparticles_step (float dtime, ClientEnvironment &env)
157 {
158         for(std::vector<Particle*>::iterator i = all_particles.begin(); i != all_particles.end();)
159         {
160                 if ((*i)->get_expired())
161                 {
162                         (*i)->remove();
163                         delete *i;
164                         all_particles.erase(i);
165                 }
166                 else
167                 {
168                         (*i)->step(dtime, env);
169                         i++;
170                 }
171         }
172 }
173
174 void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
175 {
176         for (u16 j = 0; j < 32; j++) // set the amount of particles here
177         {
178                 addNodeParticle(gamedef, smgr, player, pos, tiles);
179         }
180 }
181
182 void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
183 {
184         addNodeParticle(gamedef, smgr, player, pos, tiles);
185 }
186
187 // add a particle of a node
188 // used by digging and punching particles
189 void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr, LocalPlayer *player, v3s16 pos, const TileSpec tiles[])
190 {
191         // Texture
192         u8 texid = myrand_range(0,5);
193         AtlasPointer ap = tiles[texid].texture;
194         float size = rand()%64/512.;
195         float visual_size = BS*size;
196         float texsize = size*2;
197
198         float x1 = ap.x1();
199         float y1 = ap.y1();
200
201         ap.size.X = (ap.x1() - ap.x0()) * texsize;
202         ap.size.Y = (ap.x1() - ap.x0()) * texsize;
203
204         ap.pos.X = ap.x0() + (x1 - ap.x0()) * ((rand()%64)/64.-texsize);
205         ap.pos.Y = ap.y0() + (y1 - ap.y0()) * ((rand()%64)/64.-texsize);
206
207         // Physics
208         v3f velocity((rand()%100/50.-1)/1.5, rand()%100/35., (rand()%100/50.-1)/1.5);
209         v3f acceleration(0,-9,0);
210         v3f particlepos = v3f(
211                 (f32)pos.X+rand()%100/200.-0.25,
212                 (f32)pos.Y+rand()%100/200.-0.25,
213                 (f32)pos.Z+rand()%100/200.-0.25
214         );
215
216         Particle *particle = new Particle(
217                 gamedef,
218                 smgr,
219                 player,
220                 0,
221                 particlepos,
222                 velocity,
223                 acceleration,
224                 rand()%100/100., // expiration time
225                 visual_size,
226                 ap);
227
228         all_particles.push_back(particle);
229 }