Browse Source

Refactor input bounds checking by clamping the values with min and max functions

tjalkane 4 years ago
parent
commit
fd65e47730

+ 8 - 14
book/src/chapter_2.md

@@ -57,6 +57,7 @@ At the top of `main.rs` we add a few lines of code:
 ```rust
 use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
 use specs::prelude::*;
+use std::cmp::{max, min};
 #[macro_use]
 extern crate specs_derive;
 ```
@@ -237,6 +238,7 @@ If you've typed all of that in correctly, your `main.rs` now looks like this:
 ```rust
 use rltk::{Console, GameState, Rltk, RGB};
 use specs::prelude::*;
+use std::cmp::{max, min};
 #[macro_use]
 extern crate specs_derive;
 
@@ -402,6 +404,7 @@ So your code now looks like this:
 ```rust
 use rltk::{Console, GameState, Rltk, RGB};
 use specs::prelude::*;
+use std::cmp::{max, min};
 #[macro_use]
 extern crate specs_derive;
 
@@ -540,13 +543,8 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     let mut players = ecs.write_storage::<Player>();
 
     for (_player, pos) in (&mut players, &mut positions).join() {
-        pos.x += delta_x;
-        pos.y += delta_y;
-
-        if pos.x < 0 { pos.x = 0; }
-        if pos.x > 79 { pos.x = 79; }
-        if pos.y < 0 { pos.y = 0; }
-        if pos.y > 49 { pos.y = 49; }
+        pos.x = min(79 , max(0, pos.x + delta_x));
+        pos.y = min(49, max(0, pos.y + delta_y));
     }
 }
 ```
@@ -598,6 +596,7 @@ The source code for this completed example may be found ready-to-run in `chapter
 ```rust
 use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
 use specs::prelude::*;
+use std::cmp::{max, min};
 #[macro_use]
 extern crate specs_derive;
 
@@ -631,13 +630,8 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     let mut players = ecs.write_storage::<Player>();
 
     for (_player, pos) in (&mut players, &mut positions).join() {
-        pos.x += delta_x;
-        pos.y += delta_y;
-
-        if pos.x < 0 { pos.x = 0; }
-        if pos.x > 79 { pos.x = 79; }
-        if pos.y < 0 { pos.y = 0; }
-        if pos.y > 49 { pos.y = 49; }
+        pos.x = min(79 , max(0, pos.x + delta_x));
+        pos.y = min(49, max(0, pos.y + delta_y));
     }
 }
 

+ 5 - 14
book/src/chapter_3.md

@@ -161,13 +161,8 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos) in (&mut players, &mut positions).join() {
         let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
         }
     }
 }
@@ -184,6 +179,7 @@ The full program now looks like this:
 ```rust
 use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
 use specs::prelude::*;
+use std::cmp::{max, min};
 #[macro_use]
 extern crate specs_derive;
 
@@ -255,13 +251,8 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos) in (&mut players, &mut positions).join() {
         let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
         }
     }
 }

+ 2 - 7
book/src/chapter_5.md

@@ -449,13 +449,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() {
         let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map.tiles[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
         }

+ 3 - 7
chapter-02-helloecs/src/main.rs

@@ -1,5 +1,6 @@
 use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
 use specs::prelude::*;
+use std::cmp::{max, min};
 #[macro_use]
 extern crate specs_derive;
 
@@ -33,13 +34,8 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     let mut players = ecs.write_storage::<Player>();
 
     for (_player, pos) in (&mut players, &mut positions).join() {
-        pos.x += delta_x;
-        pos.y += delta_y;
-
-        if pos.x < 0 { pos.x = 0; }
-        if pos.x > 79 { pos.x = 79; }
-        if pos.y < 0 { pos.y = 0; }
-        if pos.y > 49 { pos.y = 49; }
+        pos.x = min(79 , max(0, pos.x + delta_x));
+        pos.y = min(49, max(0, pos.y + delta_y));
     }
 }
 

+ 2 - 7
chapter-03-walkmap/src/main.rs

@@ -71,13 +71,8 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos) in (&mut players, &mut positions).join() {
         let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
         }
     }
 }

+ 2 - 7
chapter-04-newmap/src/player.rs

@@ -10,13 +10,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos) in (&mut players, &mut positions).join() {
         let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
         }
     }
 }

+ 3 - 7
chapter-05-fov/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, TileType, State, Map};
 
 pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
@@ -13,13 +14,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() {
         let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map.tiles[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
         }

+ 3 - 7
chapter-06-monsters/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, TileType, State, Map, RunState};
 
 pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
@@ -13,13 +14,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
     for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() {
         let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
         if map.tiles[destination_idx] != TileType::Wall {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-07-damage/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee};
 
 pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
@@ -25,13 +26,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-08-ui/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee};
 
 pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
@@ -25,13 +26,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-09-items/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-10-ranged/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-11-loadsave/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-12-delvingdeeper/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-13-difficulty/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-14-gear/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-16-nicewalls/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-17-blood/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-18-particles/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-19-food/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-20-magicmapping/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-21-rexmenu/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState};
 
@@ -26,13 +27,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
-
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
 
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();

+ 3 - 7
chapter-22-simpletraps/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState, EntityMoved};
 
@@ -27,15 +28,10 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
             entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
 
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
-
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();
             ppos.x = pos.x;

+ 3 - 7
chapter-23-generic-map/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState, EntityMoved};
 
@@ -27,15 +28,10 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
             entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
 
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
-
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();
             ppos.x = pos.x;

+ 3 - 7
chapter-24-map-testing/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState, EntityMoved};
 
@@ -27,15 +28,10 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
             entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
 
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
-
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();
             ppos.x = pos.x;

+ 3 - 7
chapter-25-bsproom-dungeons/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState, EntityMoved};
 
@@ -27,15 +28,10 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
             entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
 
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
-
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();
             ppos.x = pos.x;

+ 3 - 7
chapter-26-bsp-interiors/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState, EntityMoved};
 
@@ -27,15 +28,10 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
             entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
 
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
-
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();
             ppos.x = pos.x;

+ 3 - 7
chapter-27-cellular-automota/src/player.rs

@@ -2,6 +2,7 @@ extern crate rltk;
 use rltk::{VirtualKeyCode, Rltk, Point};
 extern crate specs;
 use specs::prelude::*;
+use std::cmp::{max, min};
 use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
     gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState, EntityMoved};
 
@@ -27,15 +28,10 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
         }
 
         if !map.blocked[destination_idx] {
-            pos.x += delta_x;
-            pos.y += delta_y;
+            pos.x = min(79 , max(0, pos.x + delta_x));
+            pos.y = min(49, max(0, pos.y + delta_y));
             entity_moved.insert(entity, EntityMoved{}).expect("Unable to insert marker");
 
-            if pos.x < 0 { pos.x = 0; }
-            if pos.x > 79 { pos.x = 79; }
-            if pos.y < 0 { pos.y = 0; }
-            if pos.y > 49 { pos.y = 49; }
-
             viewshed.dirty = true;
             let mut ppos = ecs.write_resource::<Point>();
             ppos.x = pos.x;