Skip to content

Commit

Permalink
refactor, generate random map every play
Browse files Browse the repository at this point in the history
  • Loading branch information
aerth committed Jul 9, 2022
1 parent c5cbc3a commit 8203296
Show file tree
Hide file tree
Showing 40 changed files with 893 additions and 794 deletions.
6 changes: 0 additions & 6 deletions cheats.go

This file was deleted.

20 changes: 11 additions & 9 deletions cmd/aerpg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (

// _ "image/png"

"github.com/aerth/rpg"
rpg "github.com/aerth/rpc/librpg"
"github.com/aerth/rpc/librpg/common"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
Expand All @@ -26,7 +27,8 @@ func init() {

var (
flagenemies = flag.Int("e", 2, "number of enemies to begin with")
flaglevel = flag.String("test", "1", "custom world test (filename)")
flaglevel = flag.String("test", "", "custom world test (filename)")
flagseed = flag.String("seed", fmt.Sprintf("%d", rand.Int63()), "new random world")

debug = flag.Bool("v", false, "extra logs")
)
Expand Down Expand Up @@ -125,7 +127,7 @@ func run() {
cursorsprite := rpg.GetCursor(1)

// world generate
world := rpg.NewWorld(*flaglevel, *flagenemies)
world := rpg.NewWorld(*flaglevel, *flagenemies, *flagseed)
if world == nil {
return
}
Expand Down Expand Up @@ -154,7 +156,7 @@ func run() {

// draw menu bar
menubatch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
rpg.DrawPattern(menubatch, spritemap[67], pixel.R(0, 0, win.Bounds().W()+20, 60), 100)
common.DrawPattern(menubatch, spritemap[67], pixel.R(0, 0, win.Bounds().W()+20, 60), 100)
for _, btn := range buttons {
spritemap[200].Draw(menubatch, IM.Moved(btn.Frame.Center()))
}
Expand Down Expand Up @@ -246,7 +248,7 @@ MainLoop:
menubatch.Clear()
win.Update()
newbounds := win.Bounds()
rpg.DrawPattern(menubatch, spritemap[67], pixel.R(0, 0, newbounds.Max.X+20, 60), 100)
common.DrawPattern(menubatch, spritemap[67], pixel.R(0, 0, newbounds.Max.X+20, 60), 100)
for _, btn := range buttons {
spritemap[200].Draw(menubatch, IM.Moved(btn.Frame.Center()))
}
Expand All @@ -256,7 +258,7 @@ MainLoop:
// teleport random

if win.JustPressed(pixelgl.Key8) {
world.Char.Rect = rpg.DefaultSpriteRectangle.Moved(rpg.TileNear(world.Tiles, world.Char.Rect.Center()).Loc)
world.Char.Rect = common.DefaultSpriteRectangle.Moved(common.TileNear(world.Tiles, world.Char.Rect.Center()).Loc)
}
if win.JustPressed(pixelgl.KeyM) {
world.NewMobs(1)
Expand All @@ -271,7 +273,7 @@ MainLoop:
// move all enemies (debug)
if win.JustPressed(pixelgl.Key9) {
for _, v := range world.Entities {
v.Rect = rpg.DefaultEntityRectangle.Moved(rpg.TileNear(world.Tiles, v.Rect.Center()).Loc)
v.Rect = rpg.DefaultEntityRectangle.Moved(common.TileNear(world.Tiles, v.Rect.Center()).Loc)
}
}
if win.JustReleased(pixelgl.KeyI) {
Expand Down Expand Up @@ -320,9 +322,9 @@ MainLoop:
// highlight player tiles (left right up down and center)
if *debug {
for _, o := range world.Tile(world.Char.Rect.Center()).PathNeighbors() {
ob := o.(rpg.Object)
ob := o.(common.Object)
ob.W = world
ob.Highlight(win, rpg.TransparentPurple)
ob.Highlight(win, common.TransparentPurple)
}
}

Expand Down
150 changes: 3 additions & 147 deletions cmd/mapgen/main.go
Original file line number Diff line number Diff line change
@@ -1,154 +1,10 @@
package main

import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
"strconv"
"strings"
"time"

"github.com/aerth/rpg"
astar "github.com/beefsack/go-astar"
"github.com/faiface/pixel"
"github.com/aerth/rpc/librpg/maps"
)

var BOUNDS float64 = 700
var numbers = "0123456789"

func init() {
rand.Seed(time.Now().UnixNano())
// seed or random
if len(os.Args) == 2 {
if strings.HasPrefix(os.Args[1], "-h") {
fmt.Println("Usage:")
fmt.Println("\tmapgen [seed]")
fmt.Println("Example:")
fmt.Println("\tmapgen mycoolseed")

os.Exit(111)
}
hashb := md5.Sum([]byte(os.Args[1]))
hash := []byte(fmt.Sprintf("%x", hashb))
var seed []byte
for _, b := range hash {
if bytes.IndexAny([]byte{b}, numbers) != -1 {
log.Println(string(b), "is a number")
seed = append(seed, b)
} else {
log.Println(string(b), "is a letter")
}

}
worldseed, err := strconv.ParseInt(string(seed), 10, 64)
if err != nil {
log.Println(err)
}
rand.Seed(worldseed)
log.Printf("Using world seed: %q -> %v", os.Args[1], worldseed)
log.Printf("Hash: %q", string(hash))
}
// create maps dir if not exist
os.Mkdir("maps", 0755)
}

func main() {
olist := GenerateMap()
SaveMap(olist)
}

func GenerateMap() []rpg.Object {
var olist []rpg.Object
t := rpg.O_TILE
for i := 0; i < 100; i++ {

currentThing := 20 // grass
t = rpg.O_TILE
if i%3 == 0 {
currentThing = 53 // water
t = rpg.O_BLOCK
}
xmin := randfloat()
ymin := randfloat()
xmax := randfloat()
ymax := randfloat()
box := pixel.R(xmin, ymin, xmax, ymax).Norm()
log.Println(t, box)
pattern := rpg.DrawPatternObject(currentThing, t, box, 100)
for _, obj := range pattern {
if rpg.GetObjects(olist, obj.Loc) == nil {
olist = append(olist, obj)
}
}

}

// make dummy world for path finding
world := new(rpg.World)
world.Tiles = rpg.GetTiles(olist)

// detect islands, make bridges
oldlist := olist
olist = nil
spot := world.Tile(rpg.FindRandomTile(oldlist))
for _, o := range oldlist {
o.W = world
_, _, found := astar.Path(o, spot)
if o.Type == rpg.O_TILE && !found {
log.Println("found island tile", o)
} else {
olist = append(olist, o)
}
}

// fill in with water blocks
waterworld := rpg.DrawPatternObject(53, rpg.O_BLOCK, pixel.R(-BOUNDS, -BOUNDS, BOUNDS, BOUNDS), 0)
for _, water := range waterworld {
if rpg.GetObjects(olist, water.Loc) == nil {
olist = append(olist, water)
}
}

return olist
}

func SaveMap(olist []rpg.Object) {

b, err := json.Marshal(olist)
if err != nil {
log.Println(err)
return
}
f, err := ioutil.TempFile("maps", "map")
if err != nil {
log.Println(err)
return
}
_, err = f.Write(b)
if err != nil {
log.Println(err)
return
}
log.Println("saved file:", f.Name())
}

func randfloat() float64 {
step := 32.00
f := float64(rand.Intn(int(BOUNDS)))
f = math.Floor(f)
f = float64(int(f/step)) * step
switch rand.Intn(2) {
case 0:
f = -f
default:
}

return f

olist := maps.GenerateMap("")
maps.SaveMap(olist)
}
41 changes: 21 additions & 20 deletions cmd/mapmaker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (

"golang.org/x/image/colornames"

"github.com/aerth/rpg"
rpg "github.com/aerth/rpc/librpg"
"github.com/aerth/rpc/librpg/common"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
Expand Down Expand Up @@ -83,24 +84,24 @@ func run() {
if err != nil {
panic(err)
}
var oldthings = []rpg.Object{}
var oldthings = []common.Object{}
if b, err := ioutil.ReadFile(LEVEL); err == nil {
err = json.Unmarshal(b, &oldthings)
if err != nil {
panic(err)
}
}
var things []rpg.Object
var things []common.Object
for _, v := range oldthings {
if *convert {
log.Println("Converting")
v.Type = rpg.O_TILE
if v.SpriteNum == 53 && v.Type == rpg.O_TILE {
v.Type = rpg.O_BLOCK
v.Type = common.O_TILE
if v.SpriteNum == 53 && v.Type == common.O_TILE {
v.Type = common.O_BLOCK
}
}

v.Rect = rpg.DefaultSpriteRectangle.Moved(v.Loc)
v.Rect = common.DefaultSpriteRectangle.Moved(v.Loc)

things = append(things, v)

Expand All @@ -124,7 +125,7 @@ func run() {
text := rpg.NewTextSmooth(14)
fmt.Fprint(text, helpText)
cursor := rpg.GetCursor(2)
undobuffer := []rpg.Object{}
undobuffer := []common.Object{}
var turbo = false
var highlight = true
var box pixel.Rect
Expand Down Expand Up @@ -177,8 +178,8 @@ func run() {
}
}

deleteThing := func(loc pixel.Vec) []rpg.Object {
var newthings []rpg.Object
deleteThing := func(loc pixel.Vec) []common.Object {
var newthings []common.Object
for _, thing := range things {
if thing.Rect.Contains(mouse) {
log.Println("deleting:", thing)
Expand All @@ -203,7 +204,7 @@ func run() {
} else {
if win.Pressed(pixelgl.KeyLeftShift) && win.Pressed(pixelgl.MouseButtonRight) ||
win.JustPressed(pixelgl.MouseButtonRight) {
thing := rpg.NewBlock(mouse)
thing := common.NewBlock(mouse)
thing.SpriteNum = currentThing
log.Println("Stamping Block", mouse, thing.SpriteNum)
if replace {
Expand All @@ -216,7 +217,7 @@ func run() {
}
if win.Pressed(pixelgl.KeyLeftShift) && win.Pressed(pixelgl.MouseButtonLeft) ||
win.JustPressed(pixelgl.MouseButtonLeft) {
thing := rpg.NewTile(mouse)
thing := common.NewTile(mouse)
thing.SpriteNum = currentThing
log.Println("Stamping Tile", mouse, thing.SpriteNum)
if replace {
Expand Down Expand Up @@ -275,13 +276,13 @@ func run() {
box.Max = mouse
box = box.Norm()
log.Println("drawing rectangle:", box, currentThing)
things = append(DeleteThings(things, box), rpg.DrawPatternObject(currentThing, rpg.O_TILE, box, 100)...)
things = append(DeleteThings(things, box), common.DrawPatternObject(currentThing, common.O_TILE, box, 100)...)
}
if win.JustReleased(pixelgl.MouseButtonRight) {
box.Max = mouse
box = box.Norm()
log.Println("drawing rectangle:", box, currentThing)
things = append(DeleteThings(things, box), rpg.DrawPatternObject(currentThing, rpg.O_BLOCK, box, 100)...)
things = append(DeleteThings(things, box), common.DrawPatternObject(currentThing, common.O_BLOCK, box, 100)...)

}
}
Expand All @@ -290,14 +291,14 @@ func run() {
for i := range things {
things[i].Draw(batch, spritesheet, spritemap, 0)
if highlight {
color := rpg.TransparentRed
if things[i].Type == rpg.O_TILE {
color = rpg.TransparentBlue
color := common.TransparentRed
if things[i].Type == common.O_TILE {
color = common.TransparentBlue
}
things[i].Highlight(batch, color)
}
if things[i].Rect.Contains(mouse) {
things[i].Highlight(batch, rpg.TransparentPurple)
things[i].Highlight(batch, common.TransparentPurple)

}

Expand Down Expand Up @@ -334,8 +335,8 @@ func main() {
pixelgl.Run(run)
}

func DeleteThings(from []rpg.Object, at pixel.Rect) []rpg.Object {
var cleaned []rpg.Object
func DeleteThings(from []common.Object, at pixel.Rect) []common.Object {
var cleaned []common.Object
for _, o := range from {
if !at.Contains(o.Loc) {
cleaned = append(cleaned, o)
Expand Down
3 changes: 2 additions & 1 deletion character.go → librpg/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"time"

"github.com/aerth/rpc/librpg/common"
"github.com/faiface/pixel"
"github.com/faiface/pixel/text"
)
Expand Down Expand Up @@ -178,7 +179,7 @@ func (char *Character) Update(dt float64, dir Direction, world *World) {
f2 := func(nexttile pixel.Rect) bool {

for _, c := range world.Tiles {
if c.Type == O_TILE && c.Rect.Intersect(nexttile).Norm().Area() != 0 {
if c.Type == common.O_TILE && c.Rect.Intersect(nexttile).Norm().Area() != 0 {
return true
}
}
Expand Down
8 changes: 8 additions & 0 deletions librpg/cheats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package rpg

import "github.com/aerth/rpc/librpg/common"

func (w *World) RandomLootSomewhere() {
loot := createLoot()
w.NewLoot(common.FindRandomTile(w.Tiles), []Item{loot})
}
File renamed without changes.
Loading

0 comments on commit 8203296

Please sign in to comment.