Skip to content

Commit

Permalink
Merge pull request #13 from UtkarshMe/new_key_w
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva authored Nov 22, 2017
2 parents 78c2982 + 22fa961 commit 304fb12
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ void map_set_cursor(point_t);
*/
void map_set_real_cursor();


/**
* Search the map for the given tile in the given direction from the cursor
* @param The tile to search for
* @param The step for increment
* @return The pointer to the tile
*/
point_t * map_search_tile(map_tile_t, int);

#endif /* end of include guard: MAP_H_K8P4JCPW */
17 changes: 16 additions & 1 deletion src/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <string.h> /* for strcmp(), strcat() */
#include <stdlib.h> /* for free() */

Expand Down Expand Up @@ -51,6 +52,8 @@ command_t action_make_move(const map_t *map)
/* 1 if the real x of cursor is touched and thus has to be changed.
* a successful change of x by a key can do that */
int touched = 0;
map_tile_t tile;
point_t *temp_point;

if (!key_unlocked(key)) {
key = interface_input_key();
Expand Down Expand Up @@ -80,6 +83,7 @@ command_t action_make_move(const map_t *map)
touched = 1;
command.type = COMMAND_MOTION;
break;

case '0':
case '1':
case '2':
Expand All @@ -91,12 +95,23 @@ command_t action_make_move(const map_t *map)
case '8':
case '9':
multiplier = (multiplier) * 10 + key - '0';

/* loop it multiplier number of times */
loop = multiplier;
key = interface_input_key();
break;

case 'w':
tile = map_get_tile(map -> cursor);
if (tile.type != TILE_TEXT) break;
tile.type = TILE_TEXT;
tile.value = ' ';
if ((temp_point = map_search_tile(tile, 1)) != NULL) {
point.x = temp_point -> x + 1;
point.y = temp_point -> y;
free(temp_point);
}
break;

case ':':
return input_command();
}
Expand Down
22 changes: 22 additions & 0 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,25 @@ void map_set_real_cursor()
{
map -> real_x = map -> cursor.x;
}

point_t * map_search_tile(map_tile_t searchtile, int step)
{
map_tile_t tile;
point_t *point = (point_t *) malloc(sizeof(point_t));

point -> x = map -> cursor.x;
point -> y = map -> cursor.y;

while (point -> x < map -> size.x || point -> y < map -> size.y) {
tile = map_get_tile(*point);
if (tile.type == searchtile.type && tile.value == searchtile.value) {
return point;
}
point -> x += step;
if (point -> x > map -> size.x) {
point -> x = 0;
point -> y += (step > 0) ? 1 : -1;
}
}
return NULL;
}

0 comments on commit 304fb12

Please sign in to comment.