Skip to content

Commit 5306602

Browse files
committedJun 19, 2013
Add drowning
1 parent e65ac4d commit 5306602

File tree

15 files changed

+67
-5
lines changed

15 files changed

+67
-5
lines changed
 

‎doc/lua_api.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,7 @@ Node definition (register_node)
17101710
liquid_alternative_source = "", -- Source version of flowing liquid
17111711
liquid_viscosity = 0, -- Higher viscosity = slower flow (max. 7)
17121712
liquid_renewable = true, -- Can new liquid source be created by placing
1713+
drowning = true, -- Player will drown in these
17131714
two or more sources nearly?
17141715
light_source = 0, -- Amount of light emitted by node
17151716
damage_per_second = 0, -- If player is inside node, this damage is caused
273 Bytes
Loading

‎src/client.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,13 @@ u16 Client::getHP()
26802680
return player->hp;
26812681
}
26822682

2683+
u16 Client::getBreath()
2684+
{
2685+
Player *player = m_env.getLocalPlayer();
2686+
assert(player != NULL);
2687+
return player->breath;
2688+
}
2689+
26832690
bool Client::getChatMessage(std::wstring &message)
26842691
{
26852692
if(m_chat_queue.size() == 0)

‎src/client.h

+1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
349349
void setCrack(int level, v3s16 pos);
350350

351351
u16 getHP();
352+
u16 getBreath();
352353

353354
bool checkPrivilege(const std::string &priv)
354355
{ return (m_privileges.count(priv) != 0); }

‎src/environment.cpp

+39-1
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,45 @@ void ClientEnvironment::step(float dtime)
22272227
damageLocalPlayer(damage_per_second, true);
22282228
}
22292229
}
2230-
2230+
2231+
/*
2232+
Drowning
2233+
*/
2234+
if(m_drowning_interval.step(dtime, 2.0))
2235+
{
2236+
v3f pf = lplayer->getPosition();
2237+
2238+
// head
2239+
v3s16 p = floatToInt(pf + v3f(0, BS*1.6, 0), BS);
2240+
MapNode n = m_map->getNodeNoEx(p);
2241+
ContentFeatures c = m_gamedef->ndef()->get(n);
2242+
2243+
if(c.isLiquid() && c.drowning){
2244+
if(lplayer->breath > 10)
2245+
lplayer->breath = 11;
2246+
if(lplayer->breath > 0)
2247+
lplayer->breath -= 1;
2248+
}
2249+
2250+
if(lplayer->breath == 0){
2251+
damageLocalPlayer(1, true);
2252+
}
2253+
}
2254+
if(m_breathing_interval.step(dtime, 0.5))
2255+
{
2256+
v3f pf = lplayer->getPosition();
2257+
2258+
// head
2259+
v3s16 p = floatToInt(pf + v3f(0, BS*1.6, 0), BS);
2260+
MapNode n = m_map->getNodeNoEx(p);
2261+
ContentFeatures c = m_gamedef->ndef()->get(n);
2262+
2263+
if(!c.isLiquid() || !c.drowning){
2264+
if(lplayer->breath <= 10)
2265+
lplayer->breath += 1;
2266+
}
2267+
}
2268+
22312269
/*
22322270
Stuff that can be done in an arbitarily large dtime
22332271
*/

‎src/environment.h

+2
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ class ClientEnvironment : public Environment
494494
Queue<ClientEnvEvent> m_client_event_queue;
495495
IntervalLimiter m_active_object_light_update_interval;
496496
IntervalLimiter m_lava_hurt_interval;
497+
IntervalLimiter m_drowning_interval;
498+
IntervalLimiter m_breathing_interval;
497499
std::list<std::string> m_player_names;
498500
};
499501

‎src/game.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3310,7 +3310,7 @@ void the_game(
33103310
if (show_hud)
33113311
{
33123312
hud.drawHotbar(v2s32(displaycenter.X, screensize.Y),
3313-
client.getHP(), client.getPlayerItem());
3313+
client.getHP(), client.getPlayerItem(), client.getBreath());
33143314
}
33153315

33163316
/*

‎src/hud.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
278278
}
279279

280280

281-
void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
281+
void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, s32 breath) {
282282
InventoryList *mainlist = inventory->getList("main");
283283
if (mainlist == NULL) {
284284
errorstream << "draw_hotbar(): mainlist == NULL" << std::endl;
@@ -295,6 +295,9 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
295295
if (player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)
296296
drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
297297
"heart.png", halfheartcount, v2s32(0, 0));
298+
if (player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE && breath <= 10)
299+
drawStatbar(pos - v2s32(-180, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
300+
"bubble.png", breath*2, v2s32(0, 0));
298301
}
299302

300303

‎src/hud.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3535
#define HUD_FLAG_HEALTHBAR_VISIBLE (1 << 1)
3636
#define HUD_FLAG_CROSSHAIR_VISIBLE (1 << 2)
3737
#define HUD_FLAG_WIELDITEM_VISIBLE (1 << 3)
38+
#define HUD_FLAG_BREATHBAR_VISIBLE (1 << 4)
3839

3940
#define HUD_PARAM_HOTBAR_ITEMCOUNT 1
4041

@@ -122,7 +123,7 @@ class Hud {
122123
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
123124
std::string texture, s32 count, v2s32 offset);
124125

125-
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem);
126+
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, s32 breath);
126127
void resizeHotbar();
127128

128129
void drawCrosshair();

‎src/nodedef.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ void ContentFeatures::reset()
211211
liquid_alternative_source = "";
212212
liquid_viscosity = 0;
213213
liquid_renewable = true;
214+
drowning = true;
214215
light_source = 0;
215216
damage_per_second = 0;
216217
node_box = NodeBox();
@@ -279,6 +280,7 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
279280
writeU8(os, rightclickable);
280281
// Stuff below should be moved to correct place in a version that otherwise changes
281282
// the protocol version
283+
writeU8(os, drowning);
282284
}
283285

284286
void ContentFeatures::deSerialize(std::istream &is)
@@ -343,6 +345,7 @@ void ContentFeatures::deSerialize(std::istream &is)
343345
try{
344346
// Stuff below should be moved to correct place in a version that
345347
// otherwise changes the protocol version
348+
drowning = readU8(is);
346349
}catch(SerializationError &e) {};
347350
}
348351

‎src/nodedef.h

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ struct ContentFeatures
219219
u8 liquid_viscosity;
220220
// Is liquid renewable (new liquid source will be created between 2 existing)
221221
bool liquid_renewable;
222+
bool drowning;
222223
// Amount of light the node emits
223224
u8 light_source;
224225
u32 damage_per_second;

‎src/player.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Player::Player(IGameDef *gamedef):
3636
camera_barely_in_ceiling(false),
3737
inventory(gamedef->idef()),
3838
hp(PLAYER_MAX_HP),
39+
breath(-1),
3940
peer_id(PEER_ID_INEXISTENT),
4041
// protected
4142
m_gamedef(gamedef),
@@ -80,7 +81,8 @@ Player::Player(IGameDef *gamedef):
8081
physics_override_gravity = 1;
8182

8283
hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
83-
HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE;
84+
HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
85+
HUD_FLAG_BREATHBAR_VISIBLE;
8486

8587
hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
8688
}

‎src/player.h

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class Player
232232
float physics_override_gravity;
233233

234234
u16 hp;
235+
u16 breath;
235236

236237
float hurt_tilt_timer;
237238
float hurt_tilt_strength;

‎src/script/common/c_content.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ ContentFeatures read_content_features(lua_State *L, int index)
389389
f.liquid_viscosity = getintfield_default(L, index,
390390
"liquid_viscosity", f.liquid_viscosity);
391391
getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
392+
getboolfield(L, index, "drowning", f.drowning);
392393
// Amount of light the node emits
393394
f.light_source = getintfield_default(L, index,
394395
"light_source", f.light_source);

‎src/script/lua_api/l_object.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct EnumString es_HudBuiltinElement[] =
6262
{HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
6363
{HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
6464
{HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
65+
{HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
6566
{0, NULL},
6667
};
6768

0 commit comments

Comments
 (0)
Please sign in to comment.