From 38353751c9f4e03fb6a0b855e6d8b5691af71dc0 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Tue, 30 Nov 2010 15:35:03 +0200 Subject: better water --- src/constants.h | 3 +++ src/environment.cpp | 19 +++++++++++++++- src/main.cpp | 22 ++++++++++++++++-- src/map.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- src/mapblockobject.cpp | 4 ++-- src/mapnode.h | 28 +++++++++++++++++++++++ src/mapsector.h | 2 -- src/player.cpp | 34 +++++++++++++++++++++++---- src/player.h | 1 + src/server.cpp | 8 +++---- 10 files changed, 167 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/constants.h b/src/constants.h index 84adfa79c..d59597774 100644 --- a/src/constants.h +++ b/src/constants.h @@ -85,5 +85,8 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #define MAX_OBJECTDATA_SIZE 450 +//#define WATER_LEVEL (-5) +#define WATER_LEVEL (0) + #endif diff --git a/src/environment.cpp b/src/environment.cpp index c76721a77..2d7590f1a 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -101,11 +101,28 @@ void Environment::step(float dtime) { Player *player = *i; - // Apply gravity to local player + // Apply physics to local player if(player->isLocal()) { + // Apply gravity to local player v3f speed = player->getSpeed(); speed.Y -= 9.81 * BS * dtime_part * 2; + + /* + Apply water resistance + */ + if(player->in_water) + { + f32 max_down = 1.0*BS; + if(speed.Y < -max_down) speed.Y = -max_down; + + f32 max = 2.0*BS; + if(speed.getLength() > max) + { + speed = speed / speed.getLength() * max; + } + } + player->setSpeed(speed); } diff --git a/src/main.cpp b/src/main.cpp index a2185450f..677f03843 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -167,6 +167,12 @@ TODO: Check what goes wrong with caching map to disk (Kray) TODO: Remove LazyMeshUpdater. It is not used as supposed. +FIXME: Rats somehow go underground sometimes (you can see it in water) + - Does their position get saved to a border value or something? + +TODO: MovingObject::move and Player::move are basically the same. + combine them. + Doing now: ====================================================================== @@ -1013,6 +1019,19 @@ int main(int argc, char *argv[]) */ std::cout<__| \\___ >____ > |__| "< neighbor_rankings; + + for(u32 i=0; i::Iterator + i = neighbor_rankings.getIterator(); + i.atEnd() == false; i++) + { + u8 m = i.getNode()->getKey(); + u8 c = i.getNode()->getValue(); + if( + c > highest_ranking || + // Prefer something else than air + (c >= highest_ranking && m != MATERIAL_AIR) + + ) + { + replace_material = m; + highest_ranking = c; + } + } + } + /* If there is a node at top and it doesn't have sunlight, there will be no sunlight going down. @@ -1073,7 +1133,7 @@ void Map::removeNodeAndUpdate(v3s16 p, Remove the node */ MapNode n; - n.d = MATERIAL_AIR; + n.d = replace_material; n.setLight(0); setNode(p, n); diff --git a/src/mapblockobject.cpp b/src/mapblockobject.cpp index 1fda9baed..3a28e2250 100644 --- a/src/mapblockobject.cpp +++ b/src/mapblockobject.cpp @@ -145,9 +145,9 @@ void MovingObject::move(float dtime, v3f acceleration) for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++) { try{ - if(m_block->getNodeParent(v3s16(x,y,z)).d == MATERIAL_AIR){ + if(material_walkable(m_block->getNodeParent(v3s16(x,y,z)).d) + == false) continue; - } } catch(InvalidPositionException &e) { diff --git a/src/mapnode.h b/src/mapnode.h index 910239136..789cedb27 100644 --- a/src/mapnode.h +++ b/src/mapnode.h @@ -133,6 +133,34 @@ inline u8 material_solidness(u8 m) return 2; } +// Objects collide with walkable materials +inline bool material_walkable(u8 m) +{ + return (m != MATERIAL_AIR && m != MATERIAL_WATER); +} + +// A liquid resists fast movement +inline bool material_liquid(u8 m) +{ + return (m == MATERIAL_WATER); +} + +// Pointable materials can be pointed to in the map +inline bool material_pointable(u8 m) +{ + return (m != MATERIAL_AIR && m != MATERIAL_WATER); +} + +inline bool material_diggable(u8 m) +{ + return (m != MATERIAL_AIR && m != MATERIAL_WATER); +} + +inline bool material_buildable_to(u8 m) +{ + return (m == MATERIAL_AIR || m == MATERIAL_WATER); +} + /* Nodes make a face if materials differ and solidness differs. Return value: diff --git a/src/mapsector.h b/src/mapsector.h index ad6161bdf..d5ffa7f1d 100644 --- a/src/mapsector.h +++ b/src/mapsector.h @@ -34,8 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc., This is an Y-wise stack of MapBlocks. */ -#define WATER_LEVEL (-5) - #define SECTOR_OBJECT_TEST 0 #define SECTOR_OBJECT_TREE_1 1 #define SECTOR_OBJECT_BUSH_1 2 diff --git a/src/player.cpp b/src/player.cpp index c3e14fc90..5e838bf7a 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., Player::Player(): touching_ground(false), + in_water(false), inventory(PLAYER_INVENTORY_SIZE), peer_id(PEER_ID_NEW), m_speed(0,0,0), @@ -64,6 +65,26 @@ void Player::move(f32 dtime, Map &map) v3s16 pos_i = floatToInt(position); + /* + Check if player is in water + */ + try{ + if(in_water) + { + v3s16 pp = floatToInt(position + v3f(0,0,0)); + in_water = material_liquid(map.getNode(pp).d); + } + else + { + v3s16 pp = floatToInt(position + v3f(0,BS/2,0)); + in_water = material_liquid(map.getNode(pp).d); + } + } + catch(InvalidPositionException &e) + { + in_water = false; + } + // The frame length is limited to the player going 0.1*BS per call f32 d = (float)BS * 0.15; @@ -100,10 +121,8 @@ void Player::move(f32 dtime, Map &map) for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){ for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){ for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){ - //std::cout<<"with ("<getMaterial(); try{ - // Don't add a node if there isn't air + // Don't add a node if this is not a free space MapNode n2 = m_env.getMap().getNode(p_over); - if(n2.d != MATERIAL_AIR) + if(material_buildable_to(n2.d) == false) return; } catch(InvalidPositionException &e) -- cgit v1.2.3