This repository has been archived on 2021-04-27. You can view files and clone it, but cannot push or open issues or pull requests.
javapool/main.fs

99 lines
3.6 KiB
Forth
Raw Normal View History

2021-03-17 17:58:01 +00:00
\ JAVAPOOL - THE GAME (an incremental game based on the lore of #javapool on tilde.town)
2021-03-17 11:56:45 +00:00
\ this code likely sucks, because I'm bad at forth and figuring stuff out as I go.
2021-03-17 17:58:01 +00:00
\ ANSI escape sequences. gforth already provides at-xy and page, but I also need a word for clearing the current line and displaying the cursor.
: hide-cursor esc[ ." ?25l" ;
: show-cursor esc[ ." ?25h" ;
: clear-line esc[ ." K" ;
: move-right ( x -- ) esc[ 0 .R ." C" ;
2021-03-17 11:56:45 +00:00
\ initialise some variables that encode game state.
\ the variable "counter" holds the count of the current tick. It always starts at 0.
variable counter
2021-03-17 17:58:01 +00:00
\ the variable "devices" holds the amount of devices currently in the pool.
variable devices
\ the variable "devicerate" is the amount of ticks between devices getting added.
variable devicerate
2000 devicerate !
\ the variable "membercost" is the cost of hiring a new chat member. If it is zero, hiring chat members hasn't been unlocked yet.
variable membercost
\ the variable "membercount" is how many chat members you've hired. For each chat member, device gain delay decreases.
variable membercount
\ the word !0= tests if something is non-zero
: !0= 0= 0= ;
2021-03-17 11:56:45 +00:00
2021-03-17 17:58:01 +00:00
\ the world unlock-members tests to see if hiring chat members can be unlocked. if so, they are unlocked.
: unlock-members
devices @ 5 = membercost @ 0= and if
10 membercost ! then ;
\ the word unlock tests to see if any unlock conditions are matched, and unlocks them if they are.
: unlock unlock-members ;
\ the word tick waits a tick. Ticks are 1ms currently.
: tick 1 ms ;
2021-03-17 11:56:45 +00:00
2021-03-17 12:01:30 +00:00
\ the word tick-up ticks the counter up
: tick-up tick 1 counter +! ;
2021-03-17 11:56:45 +00:00
2021-03-17 17:58:01 +00:00
\ the word game-tick runs a single tick of the game
: game-tick tick-up unlock counter @
devicerate @ mod 0= if 1 devices +! then ;
\ TODO saving
\ the word exit-game exits the game.
: exit-game page show-cursor bye ;
2021-03-17 11:56:45 +00:00
2021-03-17 17:58:01 +00:00
\ the word hire-chat-member hires a chat member.
: hire-chat-member
2021-03-17 18:04:15 +00:00
membercost @ dup !0= swap \ checks if member cost is nonzero, leaving member cost on the stack
devices @ <= \ tests if the member cost is less than the amount of devices we have
2021-03-17 17:58:01 +00:00
and if
2021-03-17 18:04:15 +00:00
devices @ membercost @ - devices ! \ remove devices that "pay" for the members
devicerate @ 500 - devicerate ! \ increase the device rate by 500
1 membercount +! \ add a member to the count
membercount @ 2 * 5 + membercost +! \ increase the cost of buying a new member
2021-03-17 17:58:01 +00:00
then ;
\ the word handle-input handles input every time around the game loop.
2021-03-17 11:56:45 +00:00
: handle-input
key? if key case
[char] q of exit-game endof
2021-03-17 17:58:01 +00:00
[char] h of hire-chat-member endof
2021-03-17 11:56:45 +00:00
endcase then ;
2021-03-17 17:58:01 +00:00
\ print-rate prints the current device output rate on the screen.
\ TODO make this be devices/s
: print-rate
2 set-precision
2021-03-17 18:04:15 +00:00
\ first convert to floating point, divide by 1000 to get seconds from milliseconds, then do 1/n to get the devices/second from seconds/device
." (" devicerate @ s>f 1000e f/ 1e fswap f/ f. ." devices/second)" ;
2021-03-17 17:58:01 +00:00
\ draw-devices draws the number of devices on the screen.
: draw-devices
devices @ dup
1 1 at-xy
clear-line
1 = if ." There is 1 device in the javapool." drop else
." There are " . ." devices in the javapool." then
space print-rate ;
\ draw-actions draws a list of the actions you've unlocked.
: draw-actions
1 3 at-xy
2021-03-17 18:04:15 +00:00
membercost @ dup !0= if \ if membercost is non-zero (members have been unlocked)
2021-03-17 17:58:01 +00:00
." (h)ire a chat member [" . ." Devices] (you have " membercount @ . ." chat members helping you)" cr
else drop then
1 move-right ." (q)uit" ;
: draw-screen draw-devices draw-actions ;
2021-03-17 11:56:45 +00:00
\ main game loop
2021-03-17 17:58:01 +00:00
: game-loop begin game-tick handle-input draw-screen 0 until ;
2021-03-17 11:56:45 +00:00
\ clear the screen and start the game
2021-03-17 17:58:01 +00:00
page hide-cursor game-loop