Skip to content

Commit

Permalink
Merge pull request #4 from Dragosha/master
Browse files Browse the repository at this point in the history
I would like to merge this...
  • Loading branch information
selimanac committed Mar 21, 2024
2 parents 8129214 + f470804 commit 710f2a9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,60 @@ end
If your state space is huge, occasionally call astar.reset_cache() to free unused memory.



## astar.get_at(`x`, `y`)

Returns the value from the map array by coordinates.

**PARAMETERS**

X, Y of the tile possition on array not the screen position.

* ```x``` (int) - Tile X
* ```y``` (int) - Tile Y


**RETURN**

* ```value``` (int)


**EXAMPLE**

```lua

local value = astar.get_at(1, 1)

if value > 0 then
print("boo")
end

```


## astar.set_at(`x`, `y`, `value`)

Set your value to the map array by coordinates.
*Setting new data reset the current cache.

**PARAMETERS**

X, Y of the tile possition on array not the screen position.

* ```x``` (int) - Tile X
* ```y``` (int) - Tile Y
* ```value``` (int)


**EXAMPLE**

```lua

astar.set_at(1, 1, 0)


```

---

## Games using A-Star
Expand Down
1 change: 1 addition & 0 deletions astar/include/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Map : public Graph
void ResetPath();
void ClearPath();
int WorldAt(int x, int y);
void SetToWorldAt(int x, int y, int value);
void NodeToXY( void* node, int* x, int* y );
void* XYToNode( size_t x, size_t y );

Expand Down
9 changes: 9 additions & 0 deletions astar/src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ int Map::WorldAt(int x, int y)
}
}

void Map::SetToWorldAt(int x, int y, int value)
{
if (x >= 0 && x < worldWidth && y >= 0 && y < worldHeight)
{
ResetPath();
world[y * worldWidth + x] = value;
}
}

void Map::ResetPath()
{
if (pather != NULL)
Expand Down
20 changes: 20 additions & 0 deletions astar/src/astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ static int astar_reset(lua_State *L)
return 0;
}

static int astar_get_at(lua_State *L)
{
int x = luaL_checkint(L, 1);
int y = luaL_checkint(L, 2);
int ret = map.WorldAt(x, y);

lua_pushinteger(L, ret);
return 1;
}

static int astar_set_at(lua_State *L)
{
int x = luaL_checkint(L, 1);
int y = luaL_checkint(L, 2);
int value = luaL_checkint(L, 3);
map.SetToWorldAt(x, y, value);
return 0;
}

static int astar_solve(lua_State *L)
{
Expand Down Expand Up @@ -222,6 +240,8 @@ static const luaL_reg Module_methods[] =
{"set_costs", astar_setcosts},
{"set_map", astar_setmap},
{"setup", astar_setup},
{"get_at", astar_get_at},
{"set_at", astar_set_at},
{0, 0}

};
Expand Down

0 comments on commit 710f2a9

Please sign in to comment.