Untitled Roguelike

Changes On Branch btl
Login

Changes On Branch btl

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch btl Excluding Merge-Ins

This is equivalent to a diff from 941b13dad1 to b5bf464440

2018-08-28
03:42
Not much Leaf check-in: b5bf464440 user: zax tags: btl
2017-09-12
01:37
Convert to bearTermLib check-in: 77674d118b user: zax tags: btl
2017-09-11
23:16
Correct directory structure Leaf check-in: 941b13dad1 user: zax tags: trunk
22:47
Update after a bit check-in: 84350377c3 user: zax tags: trunk

Changes to nogue.nimble.

1
2
3
4
5
6
7
8
9
10
11

12
13
1
2
3
4
5
6
7
8
9
10

11
12
13










-
+


# Package

version       = "0.1.0"
author        = "ZDS"
description   = "rogue"
license       = "MIT"

# Dependencies

requires "nim >= 0.16.0"
requires "ncurses"
requires "https://github.com/zacharycarter/blt-nim.git"

bin = @["nogue"]

Changes to noguepkg/base.nim.

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15

1
2
3
4
5
6
7

8
9
10
11
12
13
14
-
+






-







import ncurses, random, options, deques, strutils
import random, options, deques, strutils
import map, characters

type
  Game* = ref GameObj
  GameObj = object
    y*, x*, ticks*: int
    gameWindow*: ptr window
    map*: Map
    charMap*: Matrix[Character]
    characters*: seq[Character]
    messages*: Deque[Message]
    mode*: GameMode

  GameMode* = enum

Changes to noguepkg/characters.nim.

15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36

37
38
39
40
41
42
43
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35

36
37
38
39
40
41
42
43







-
+













-
+







  Class* = ref object
    case kind*: CharacterKind
      of ckBeast:
        beastKind*: bcKind
        ch*: char
      of ckPlayer:
        playerKind*: pcKind
    color*: int64
    colorName*: string
    attrs*: seq[int64]
    maxHealth*: int
  bcKind* = enum
    wolf, bear, stag
  pcKind* = enum
    priest, warrior, thief

  HealthLevel = enum
    hlHealthy = "healthy"
    hlWounded = "wounded"
    hlCritical = "critically injured"

proc kind*(c: Character): CharacterKind = c.class.kind
proc color*(c: Character): int64 = c.class.color
proc colorName*(c: Character): string = c.class.colorName
proc ch*(c: Character): char = c.class.ch

proc isPC*[T](c: T): bool {. noSideEffect, inline .} = c.kind == ckPlayer

proc verb*(c: Character, v: string): string =
  if c.isPC: v
  else: v & "s"

Changes to noguepkg/classes.nim.

1

2
3
4
5
6
7
8
9

10
11
12
13
14
15

16
17
18
19
20
21

22
23
24
25
26

27
28
29
30
31

32
33
34
35
36

37
38
39
40
41
42
43

1
2
3
4
5
6
7
8

9
10
11
12
13
14

15
16
17
18
19
20

21
22
23
24
25

26
27
28
29
30

31
32
33
34
35

36
37
38
39
40
41
42
43
-
+







-
+





-
+





-
+




-
+




-
+




-
+







import ncurses, options, random
import blt, options, random
import map, characters

let
  WOLF = Class(
    kind: ckBeast,
    beastKind: wolf,
    ch: 'W',
    color: COLOR_PAIR 0
    colorName: "gray"
  )
  STAG = Class(
    kind: ckBeast,
    beastKind: stag,
    ch: 'S',
    color: COLOR_PAIR 1
    colorName: "brown"
  )
  BEAR = Class(
    kind: ckBeast,
    beastKind: bear,
    ch: 'B',
    color: COLOR_PAIR 5
    colorName: "brown"
  )
  PRIEST = Class(
    kind: ckPlayer,
    playerKind: priest,
    color: COLOR_PAIR 0
    colorName: "white"
  )
  WARRIOR = Class(
    kind: ckPlayer,
    playerKind: warrior,
    color: COLOR_PAIR 2
    colorName: "brown"
  )
  THIEF = Class(
    kind: ckPlayer,
    playerKind: thief,
    color: COLOR_PAIR 3
    colorName: "dark gray"
  )
  BEAST_CLASSES = [
    WOLF, STAG, BEAR
  ]
  PLAYER_CLASSES = [
    PRIEST, WARRIOR, THIEF
  ]
52
53
54
55
56
57
58
59

60
61

62
63
64
65


66
67

68
69
70
71
72




73
74
75
76

77
78
79
80
81
82
83
84
85
86
87
88
89
52
53
54
55
56
57
58

59
60

61
62
63


64
65
66

67
68




69
70
71
72




73
74
75
76
77
78
79
80
81
82
83
84
85
86







-
+

-
+


-
-
+
+

-
+

-
-
-
-
+
+
+
+
-
-
-
-
+














proc displayChar*(character: Character): char =
  if character.isPC:
    '@'
  else:
    character.ch

proc attrs*(cell: Cell): seq[int64] =
proc fullColorName*(cell: Cell): string =
  case cell.kind:
    of ckWall: @[COLOR_PAIR 0]
    of ckWall: "gray"
    of ckFloor:
      case cell.floorKind:
        of fkGrass: @[COLOR_PAIR 4]
        of fkStone: @[COLOR_PAIR 0]
        of fkGrass: "green"
        of fkStone: "gray"

proc attrs*(character: Character): seq[int64] =
proc fullColorName*(character: Character): string =
  let
    level = case character.level:
      of elevated: some(A_BOLD)
      of divine: some(A_UNDERLINE)
      of normal: none(int)
    levelModifier = case character.level:
      of elevated: "lighter"
      of divine: "light"
      of normal: ""
  if level.isSome:
    @[character.color, level.get]
  else:
    @[character.color]
  levelModifier & " " & character.colorName

proc randomPC*(): Character =
  Character(
    class: random(PLAYER_CLASSES),
    health: random(20..60),
    speed: random(40..60)
  )
proc randomNPC*(): Character =
  Character(
    class: random(BEAST_CLASSES),
    health: random(10..30),
    speed: random(40..60)
  )

Changes to noguepkg/map.nim.

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28








-
+











-
+







import sequtils, random, math, options

type
  CellKind* = enum
    ckWall, ckFloor
  FloorKind* = enum
    fkGrass, fkStone
  Cell* = object
    point: Point
    point*: Point
    case kind*: CellKind
    of ckWall: discard
    of ckFloor:
      floorKind*: FloorKind
  Matrix*[T] = seq[seq[T]]
  Map* = ref MapMatrix
  MapMatrix = Matrix[Cell]
  Dim* = enum
    y, x
  Point* = array[y..x, int]

proc `[]`*[T](matrix: Matrix[T], y, x: int): T =
proc `[]`* [T](matrix: Matrix[T], y, x: int): T =
  matrix[y][x]
proc `[]`*[T](matrix: Matrix[T], p: Point): T =
  matrix[p[y]][p[x]]
proc `[]=`*[T](matrix: var Matrix[T], y, x: int, t: T) =
  matrix[y][x] = t
proc `[]=`*[T](matrix: var Matrix[T], p: Point, t: T) =
  matrix[p[y]][p[x]] = t

Changes to noguepkg/ui.nim.

1


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20









21
22
23
24

25
26
27
28
29
30
31




32
33
34
35
36
37
38
39





40
41
42
43
44
45
46
47


48
49
50


51
52
53
54
55
56
57
58

59
60
61
62



63
64
65
66
67
68
69
70
71
72
73
74

75
76
77
78


79
80
81
82

83
84

85
86
87
88
89
90
91
92
93


94
95
96
97
98
99
100
101
102
103
104





105
106


107
108
109
110
111

112
113
114
115

116
117

118
119

120
121
122
123
124
125
126

127
128
129
130
131

132
133

134
135
136
137
138
139
140
141
142
143







144
145
146
147
148
149
150

1
2
3
4
5
6
7
8
9
10
11
12









13
14
15
16
17
18
19
20
21
22
23
24

25
26
27
28




29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46







47
48



49
50
51







52




53
54
55


56









57
58
59


60
61




62
63

64
65
66
67
68
69
70
71


72
73

74
75
76
77
78





79
80
81
82
83

84
85
86
87
88
89


90

91
92

93
94

95
96
97
98
99





100
101
102
103
104
105

106


107
108
109
110
111






112
113
114
115
116
117
118
119
120
121
122
123
124
125
-
+
+










-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+



-
+



-
-
-
-
+
+
+
+








+
+
+
+
+

-
-
-
-
-
-
-
+
+
-
-
-
+
+

-
-
-
-
-
-
-
+
-
-
-
-
+
+
+
-
-

-
-
-
-
-
-
-
-
-
+


-
-
+
+
-
-
-
-
+

-
+







-
-
+
+
-





-
-
-
-
-
+
+
+
+
+
-

+
+



-
-
+
-


-
+

-
+


+

-
-
-
-
-

+




-
+
-
-
+




-
-
-
-
-
-
+
+
+
+
+
+
+







import ncurses, tables, deques, strutils, sequtils
import tables, deques, strutils, sequtils
import blt
import map, base, characters, classes

type
  UiCommand = enum
    ucExamine
  ExamineCommand = enum
    ecPrev, ecNext, ecNormal

const
  gameCommands = {
    'l': Command.up,
    'k': Command.down,
    'h': Command.left,
    'e': Command.right,
    'j': Command.upleft,
    'u': Command.upright,
    'b': Command.downleft,
    'm': Command.downright,
    'Q': quitGame
    TK_U: Command.up,
    TK_K: Command.down,
    TK_H: Command.left,
    TK_E: Command.right,
    TK_L: Command.upleft,
    TK_Y: Command.upright,
    TK_B: Command.downleft,
    TK_M: Command.downright,
    TK_Q: quitGame
  }.toTable

  uiCommands = {
    'x': ucExamine
    TK_X: ucExamine
  }.toTable

  examineCommands = {
    '<': ecPrev,
    '>': ecNext,
    27.char: ecNormal,
    'x': ecNormal
    TK_COMMA: ecPrev,
    TK_PERIOD: ecNext,
    TK_ESCAPE: ecNormal,
    TK_X: ecNormal
  }.toTable

var
  examineTarget: Point

proc statusRow(game: Game): int = game.y
proc messageRow(game: Game): int = game.y + 1

const
  MAXY = 25
  MAXX = 80
  WINDOWY = 35

proc init*(game: Game) =
  var maxX, maxY, cropY: int
  let gameWindow = initscr()
  getMaxYX(gameWindow, maxY, maxX)
  cropY = (maxX.float / 1.77).int
  discard startColor()
  discard cbreak()

  discard terminal_open()
  discard terminal_set("window.title='nogue'; window.size=80x35")
  game.y = cropY
  game.x = maxX
  game.gameWindow = gameWindow
  game.x = MAXX
  game.y = MAXY

  init_pair(0, COLOR_WHITE, COLOR_BLACK)
  init_pair(1, COLOR_RED, COLOR_BLACK)
  init_pair(2, COLOR_YELLOW, COLOR_BLACK)
  init_pair(3, COLOR_MAGENTA, COLOR_BLACK)
  init_pair(4, COLOR_GREEN, COLOR_BLACK)
  init_pair(5, COLOR_BLUE, COLOR_BLACK)

let
const
  colorInfo = COLOR_PAIR 0
  colorAlert = COLOR_PAIR 1
  colorSuccess = COLOR_PAIR 4
  colorInfo = color_from_name("white")
  colorAlert = color_from_name("red")
  colorSuccess = color_from_name("green")
  colorPlayer = COLOR_PAIR 2
  colorBeast = COLOR_PAIR 3

converter chToInt(ch: char): chtype = ord(ch)

proc display(cell: Cell, ch: char) =
  addch(ch)

proc display(character: Character, ch: char) =
  mvaddch(character.point[y], character.point[x], ch)

proc render[T](t: T, extraAttrs: seq[int] = @[]) =
proc render[T: Cell | Character](t: T, extraAttrs: seq[int] = @[]) =
  let
    ch = t.displayChar
  var attrs = t.attrs.mapIt(it.int) & extraAttrs
  for attr in attrs:
    colorName = t.fullColorName
  terminal_color colorName.color_from_name
    attron attr
  t.display(ch)
  for attr in attrs:
    attroff attr
  terminal_put(t.point[x].cint, t.point[y].cint, ch.cint)

proc render(msg: Message) =
proc render(x, y: cint, msg: Message) =
  let color = case msg.level:
    of mlInfo:
      colorInfo
    of mlAlert:
      colorAlert
    of mlSuccess:
      colorSuccess
  attron color
  addstr(msg.content)
  terminal_color color
  discard terminal_print(x, y, msg.content.cstring)
  attroff color

proc display(game: Game) =
  ## Render the whole game screen.
  var msgBufferLength {.global.} = 0
  # Render the map.
  move(0, 0)
  for i in 0..game.map[].high:
    let row = game.map[i]
    for cell in row:
      render cell
  terminal_layer(1)
  for y in 0..game.map[].high:
    let row = game.map[y]
    for x in 0..row.high:
      render row[x]
    move(i+1, 0)
  # Populate the map with all characters.
  terminal_layer(2)
  terminal_clear_area(0, 0, 80, 25)
  for character in game.characters:
    render character
  # Render status line.
  move(game.statusRow, 0)
  deleteln()
  terminal_layer(0)
  insertln()
  if game.mode != gmNormal:
    let modeStr = $game.mode
    addstr modeStr
    discard terminal_print(0.cint, game.statusRow.cint, modeStr.cstring)
  else:
    addstr "----------"
    discard terminal_print(0.cint, game.statusRow.cint, "----------".cstring)

  # Message rendering
  var onScreenMessages {.global.} = initDeque[Message]()

  # Clear existing message rows.
  move(game.messageRow, 0)
  while msgBufferLength > 0:
    deleteln()
    msgBufferLength -= 1
  # Render new message rows.
  terminal_clear_area(0.cint, game.messageRow.cint, MAXX.cint, (WINDOWY-MAXY).cint)
  case game.mode:
    of gmExamine:
      let targetCharacter = game.charMap[examineTarget]
      if not targetCharacter.isNil:
        render(targetCharacter, extraAttrs = @[A_BOLD])
        render(targetCharacter)
        move(game.messageRow, 0)
        addstr targetCharacter.charInfo
        discard terminal_print(0.cint, game.messageRow.cint, targetCharacter.charInfo)
        msgBufferLength += 1

    of gmNormal:
      while game.messages.len > 0:
        let
          msgRow = game.messageRow + msgBufferLength
          msg = game.messages.popLast()
        move(msgRow, 0)
        render msg
        msgBufferLength += 1
        if onScreenMessages.len > WINDOWY-MAXY:
          onScreenMessages.popLast()
        onScreenMessages.addFirst(game.messages.popLast())
      var i = 0
      for msg in onScreenMessages:
        render(0.cint, (game.messageRow + i).cint, msg)
        i += 1

proc handle(game: var Game, command: UiCommand) =
  case command:
    of ucExamine:
      game.mode = gmExamine

proc findExamineTarget(game: Game, point: var Point, f: proc(p: var Point, y, x: int)) =
161
162
163
164
165
166
167
168
169
170
171
172
173



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

136
137
138
139
140
141
142


143
144


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

163







-
-


-
-
+
+
+















-
+
      game.findExamineTarget(currentPoint, mPrevPoint)
    of ecNext:
      game.findExamineTarget(currentPoint, mNextPoint)
    of ecNormal:
      game.mode = gmNormal

proc cycle*(game: var Game): Command =
  var
    success: int
  # Refresh display and get input.
  display game
  success = refresh()
  let input = getch().char
  terminal_refresh()
  var input = terminal_read()
  echo "INPUT: ", input
  # Handle player input.
  result = Command.noOp
  case game.mode:
    of gmNormal:
      if input in uiCommands:
        let uiCommand = uiCommands[input]
        game.handle(uiCommand)
      if input in gameCommands:
        result = gameCommands[input]
    of gmExamine:
      if input in examineCommands:
        let examineCommand = examineCommands[input]
        game.handle(examineCommand)

proc cleanUp*() =
  endwin()
  terminal_close()