diff --git a/.gitignore b/.gitignore index e34e4b6..0f9fd36 100644 --- a/.gitignore +++ b/.gitignore @@ -26,7 +26,7 @@ luac.out *.exp # Shared objects (inc. Windows DLLs) -*.dll +# *.dll *.so *.so.* *.dylib @@ -40,3 +40,6 @@ luac.out *.hex .vim + +Neohack +build diff --git a/.luarc.json b/.luarc.json index a89b1ac..1858a14 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,6 +1,7 @@ { "diagnostics.globals": [ "describe", - "it" + "it", + "love" ] } diff --git a/Makefile b/Makefile index abd8263..7e3d0a5 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,54 @@ -.PHONY: test +.PHONY: test ci clean dist +.DEFAULT_GOAL := dist + +BUILD = build +TARGET = $(BUILD)/Neohack.love +SRC_DIR = lua +SRC = $(shell find $(SRC_DIR) -type f) +EXE = $(DIST)/Neohack.exe +DIST = Neohack +ZIP = Neohack.zip +DEPS = love/deps +DATA = data test: nvim --headless -u lua/config.lua -c "PlenaryBustedDirectory tests { minimal_init = 'lua/config.lua' }" ci: nvim --headless --noplugin -u scripts/minimal_init.vim -c "PlenaryBustedDirectory tests { minimal_init = './scripts/minimal_init.vim' }" + +clean: + rm -rf $(TARGET) + rm -rf $(DIST) + rm -rf $(BUILD) + rm $(ZIP) + +updateRotLove: + cp -r ../rotLove/src/* lua/lib/rotLove/ + +$(BUILD): + mkdir $(BUILD) + +$(DIST): $(DATA) $(DEPS) + rm -rf $(DIST) + mkdir $(DIST) + cp $(DEPS)/* $(DIST) + mkdir $(DIST)/$(DATA) + cp $(DATA)/* $(DIST)/$(DATA) + +$(TARGET): $(SRC) $(BUILD) + cd $(SRC_DIR) && zip -9 -r ../$(TARGET) * + +run_love: $(TARGET) + love $(TARGET) + +$(EXE): $(TARGET) $(DIST) + cat love/love.exe $(TARGET) > $(EXE) + +$(ZIP): $(EXE) + cd $(DIST) && zip -9 -r ../$(ZIP) * + +dist: $(ZIP) + +run: dist + love $(BUILD)/Neohack.love diff --git a/README.md b/README.md index 6dd18be..1e8f0d0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # neohack.nvim -Roguelike game built inside neovim +Traditional Roguelike game built inside neovim and ported to love2d. Move your cursor to move your player. Because you have vim motions, your player can attack and teleport from a distance. @@ -9,19 +9,32 @@ Insert text to drop items from you inventory, then pick it up to reorder invento Yank to search. Visual mode is sneaking which searches, avoids attacking, and avoids being hit. -Actions -- i show inventory -- l look at item in inventory -- w wield item as weapon -- f fuse multiple items into one item -- k kick in a direction +## Play + +### As a Neovim plugin +Install neovim, Clone the repo, and run start.sh + +This starts neovim with a custom config designed to play the game. + +### On windows +Download the zip, Run the exe on windows + +### On linux +Clone the repo, run `make run` ## TODO +- [x] Proof of Concept + - [x] basic controls + - [x] good enough performance + - [x] explore some features + - [x] enough vim features to try out an exe for windows + - [ ] neovim plugin mode to use your code as the map (reintroduce this mode) - [ ] refactor all the code - [x] add features first - [x] refactor to have types - [ ] have actually nice code - [ ] unit tests + - [ ] performance - [x] basics - [x] track cursor movement - [x] bounce off walls @@ -157,19 +170,40 @@ Actions - [ ] undo? - [x] varied entities - [x] item and enemy generation from dictionary - - [x] item and enemy variations with different colours + - [x] item and enemy variations with different colors - [ ] map generation - [x] generate random map progressively - [x] have some structure to generated maps - [x] new buffer for down - [ ] add enemies and items in a later stage - [ ] pick a selection, not fully random +- [ ] buffer as multidimensional movement + - [ ] :bprev dig lower, :bnext dig higher + - [ ] allow entities to do it too - [ ] performance - [x] write buffer once per tick - [x] store map as 2D array - [ ] vim tutor/tutorial map + - [ ] teach basic vim motions as game mechanics - [ ] easy run - [x] minimal neovim config with single command to run - [ ] slash screen - - [ ] bundled exe? +- [ ] love2d + - [x] win exe + - [x] make run for linux + - [ ] reimplement vim motions + - [x] hjkl + - [x] words + - [x] ftFT + - [ ] counts + - [x] reimplement which-key + - [ ] modes + - [x] v sneak + - [ ] V aiming + - [ ] C-v - other TODO: what should this mode do? + - [x] colored cursor and status line for different modes + - [ ] try different timers for each entity + - [ ] async movement + - [ ] change events + - [ ] insert for commands diff --git a/love/deps/LICENSE.TXT b/love/deps/LICENSE.TXT new file mode 100644 index 0000000..8279836 --- /dev/null +++ b/love/deps/LICENSE.TXT @@ -0,0 +1,1355 @@ +Licensing information +===================== + +This distribution contains code from the following projects (full license text below): + + - LOVE + Website: https://love2d.org/ + License: zlib + Copyright (c) 2006-2024 LOVE Development Team + + - ENet + Website: http://enet.bespin.org/index.html + License: MIT/Expat + Copyright (c) 2002-2016 Lee Salzman + + - FreeType + Website: https://freetype.org/ + License: FreeType License + Copyright (c) 2006-2017 David Turner, Robert Wilhelm, and Werner Lemberg. + + - GLAD + Website: http://glad.dav1d.de/ + License: MIT/Expat + Copyright (c) 2013 David Herberth, modified by Sasha Szpakowski + + - glslang + Website: https://github.com/KhronosGroup/glslang + License: 3-Clause BSD + Copyright (C) 2002-2005 3Dlabs Inc. Ltd. + Copyright (C) 2013-2016 LunarG, Inc. + + - Kepler Project's lua-compat-5.3 + Website: https://github.com/keplerproject/lua-compat-5.3 + License: MIT/Expat + Copyright (c) 2015 Kepler Project. + + - lua-enet + Website: http://leafo.net/lua-enet/ + License: MIT/Expat + Copyright (C) 2011 by Leaf Corcoran + + - LuaJIT + Website: http://luajit.org/ + License: MIT/Expat + LuaJIT is Copyright (c) 2005-2016 Mike Pall + + - Lua's UTF-8 module + Website: https://www.lua.org/ + License: MIT/Expat + Copyright (C) 1994-2015 Lua.org, PUC-Rio, 2015 LOVE Development Team. + + - LuaSocket + Website: http://w3.impa.br/~diego/software/luasocket/home.html + License: MIT/Expat + Copyright (C) 2004-2013 Diego Nehab + + - LZ4 + Website: https://lz4.github.io/lz4/ + License: 2-Clause BSD + Copyright (C) 2011-2015, Yann Collet. + You can contact the author at : + - LZ4 source repository : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c + + - LodePNG + Website: https://lodev.org/lodepng/ + Source download: https://github.com/lvandeve/lodepng + License: zlib + Copyright (c) 2005-2020 Lode Vandevenne + + - TinyEXR + Website: https://github.com/syoyo/tinyexr + License: 3-Clause BSD + Copyright (c) 2014 - 2016, Syoyo Fujita + + - UTF8-CPP + Website: https://github.com/nemtrif/utfcpp + License: Unknown, MIT/Expat-like (listed as UTF8-CPP) + Copyright 2006 Nemanja Trifunovic + + - xxHash + Website: https://cyan4973.github.io/xxHash/ + License: 2-Clause BSD + Copyright (C) 2012-2016, Yann Collet. + You can contact the author at : + - xxHash source repository : https://github.com/Cyan4973/xxHash + + - dr_flac + Website: https://github.com/mackron/dr_libs + Source download: https://github.com/mackron/dr_libs/blob/c5e5355/dr_flac.h + License: MIT/Expat + Copyright 2018 David Reid + + - stb_image + Website: https://github.com/nothings/stb + Source download: https://github.com/nothings/stb/blob/e140649ccf40818781b7e408f6228a486f6d254b/stb_image.h + License: MIT/Expat + Copyright (c) 2017 Sean Barrett + + - libmpg123 + Website: http://www.mpg123.de/ + Source download: http://sourceforge.net/projects/mpg123/files/latest/download + License: LGPL 2.1 + Copyright (c) 1995-2013 by Michael Hipp and others, free software under the terms of the LGPL v2.1 + Detailed information from the debian project: + Copyright 1995-2016 by the mpg123 project + Copyright 2009-2011 by Malcolm Boczek + Copyright 2008 Christian Weisgerber + Copyright 2006-2007 by Zuxy Meng + Copyright 2000-2002 David Olofson + Copyright 1998 Fabrice Bellard + Copyright 1997 Mikko Tommila + + - OpenAL Soft + Website: https://openal-soft.org/ + Source download: https://openal-soft.org/#download + License: Mixed, licensing information obtained from the debian project + - Alc/backends/opensl.c + License: Apache 2.0 + Copyright 2011 The Android Open Source Project + - examples/alhrtf.c examples/allatency.c examples/alloopback.c examples/alreverb.c examples/alstream.c examples/altonegen.c examples/common/alhelpers.c examples/common/sdl_sound.c utils/openal-info.c + License: MIT/Expat + Copyright © 2010, 2015 Chris Robinson + - examples/alffplay.c + License: unclear, presumed LGPL 2.1 or higher + Copyright © 2003 Fabrice Bellard + Copyright © Martin Bohme + - Alc/bs2b.c OpenAL32/Include/bs2b.h + License: MIT/Expat + Copyright 2005 by Boris Mikhaylov + - cmake/FindALSA.cmake cmake/FindFFmpeg.cmake cmake/FindJACK.cmake cmake/FindSDL2.cmake + License: 3-Clause BSD + Copyright © 2006 Matthias Kretz + Copyright © 2008 Alexander Neundorf + Copyright © 2003-2011 Kitware, Inc. + Copyright © 2009-2011 Philip Lowman + Copyright © 2011 Michael Jansen + Copyright © 2012 Benjamin Eikel + - utils/makehrtf.c (not included in distribution) + License: GPL 2 or higher (2 listed below) + Copyright 2011-2014 Christopher Fitzgerald + - Everything else: + License: LGPL 2.0 or higher (2.1 listed below) + Copyright © 1999-2014 the OpenAL team + Copyright © 2008-2015 Christopher Fitzgerald + Copyright © 2009-2015 Chris Robinson + Copyright © 2013 Anis A. Hireche + Copyright © 2013 Nasca Octavian Paul + Copyright © 2013 Mike Gorchak + Copyright © 2014 Timothy Arceri + +License text +============ + +zlib license + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + +MIT/Expat + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +FreeType License + The FreeType Project LICENSE + ---------------------------- + + 2006-Jan-27 + + Copyright 1996-2002, 2006 by + David Turner, Robert Wilhelm, and Werner Lemberg + + Introduction + ============ + + The FreeType Project is distributed in several archive packages + some of them may contain, in addition to the FreeType font engine, + various tools and contributions which rely on, or relate to, the + FreeType Project. + + This license applies to all files found in such packages, and + which do not fall under their own explicit license. The license + affects thus the FreeType font engine, the test programs, + documentation and makefiles, at the very least. + + This license was inspired by the BSD, Artistic, and IJG + (Independent JPEG Group) licenses, which all encourage inclusion + and use of free software in commercial and freeware products + alike. As a consequence, its main points are that: + + o We don't promise that this software works. However, we will be + interested in any kind of bug reports. (`as is' distribution) + + o You can use this software for whatever you want, in parts or + full form, without having to pay us. (`royalty-free' usage) + + o You may not pretend that you wrote this software. If you use + it, or only parts of it, in a program, you must acknowledge + somewhere in your documentation that you have used the + FreeType code. (`credits') + + We specifically permit and encourage the inclusion of this + software, with or without modifications, in commercial products. + We disclaim all warranties covering The FreeType Project and + assume no liability related to The FreeType Project. + + + Finally, many people asked us for a preferred form for a + credit/disclaimer to use in compliance with this license. We thus + encourage you to use the following text: + + """ + Portions of this software are copyright © The FreeType + Project (www.freetype.org). All rights reserved. + """ + + Please replace with the value from the FreeType version you + actually use. + + + Legal Terms + =========== + + 0. Definitions + -------------- + + Throughout this license, the terms `package', `FreeType Project', + and `FreeType archive' refer to the set of files originally + distributed by the authors (David Turner, Robert Wilhelm, and + Werner Lemberg) as the `FreeType Project', be they named as alpha, + beta or final release. + + `You' refers to the licensee, or person using the project, where + `using' is a generic term including compiling the project's source + code as well as linking it to form a `program' or `executable'. + This program is referred to as `a program using the FreeType + engine'. + + This license applies to all files distributed in the original + FreeType Project, including all source code, binaries and + documentation, unless otherwise stated in the file in its + original, unmodified form as distributed in the original archive. + If you are unsure whether or not a particular file is covered by + this license, you must contact us to verify this. + + The FreeType Project is copyright (C) 1996-2000 by David Turner, + Robert Wilhelm, and Werner Lemberg. All rights reserved except as + specified below. + + 1. No Warranty + -------------- + + THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY + KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO + USE, OF THE FREETYPE PROJECT. + + 2. Redistribution + ----------------- + + This license grants a worldwide, royalty-free, perpetual and + irrevocable right and license to use, execute, perform, compile, + display, copy, create derivative works of, distribute and + sublicense the FreeType Project (in both source and object code + forms) and derivative works thereof for any purpose and to + authorize others to exercise some or all of the rights granted + herein, subject to the following conditions: + + o Redistribution of source code must retain this license file + (`FTL.TXT') unaltered any additions, deletions or changes to + the original files must be clearly indicated in accompanying + documentation. The copyright notices of the unaltered, + original files must be preserved in all copies of source + files. + + o Redistribution in binary form must provide a disclaimer that + states that the software is based in part of the work of the + FreeType Team, in the distribution documentation. We also + encourage you to put an URL to the FreeType web page in your + documentation, though this isn't mandatory. + + These conditions apply to any software derived from or based on + the FreeType Project, not just the unmodified files. If you use + our work, you must acknowledge us. However, no fee need be paid + to us. + + 3. Advertising + -------------- + + Neither the FreeType authors and contributors nor you shall use + the name of the other for commercial, advertising, or promotional + purposes without specific prior written permission. + + We suggest, but do not require, that you use one or more of the + following phrases to refer to this software in your documentation + or advertising materials: `FreeType Project', `FreeType Engine', + `FreeType library', or `FreeType Distribution'. + + As you have not signed this license, you are not required to + accept it. However, as the FreeType Project is copyrighted + material, only this license, or another one contracted with the + authors, grants you the right to use, distribute, and modify it. + Therefore, by using, distributing, or modifying the FreeType + Project, you indicate that you understand and accept all the terms + of this license. + + 4. Contacts + ----------- + + There are two mailing lists related to FreeType: + + o freetype@nongnu.org + + Discusses general use and applications of FreeType, as well as + future and wanted additions to the library and distribution. + If you are looking for support, start in this list if you + haven't found anything to help you in the documentation. + + o freetype-devel@nongnu.org + + Discusses bugs, as well as engine internals, design issues, + specific licenses, porting, etc. + + Our home page can be found at + + http://www.freetype.org + +3-Clause BSD + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + Neither the name of 3Dlabs Inc. Ltd. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES + LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +2-Clause BSD + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, + DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +UTF8-CPP + Permission is hereby granted, free of charge, to any person or organization + obtaining a copy of the software and accompanying documentation covered by + this license (the "Software") to use, reproduce, display, distribute, + execute, and transmit the Software, and to prepare derivative works of the + Software, and to permit third-parties to whom the Software is furnished to + do so, all subject to the following: + + The copyright notices in the Software and this entire statement, including + the above license grant, this restriction and the following disclaimer, + must be included in all copies of the Software, in whole or in part, and + all derivative works of the Software, unless such copies or derivative + works are solely in the form of machine-executable object code generated by + a source language processor. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT + SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE + FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + +LGPL 2.1 + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + [This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your + freedom to share and change it. By contrast, the GNU General Public + Licenses are intended to guarantee your freedom to share and change + free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some + specially designated software packages--typically libraries--of the + Free Software Foundation and other authors who decide to use it. You + can use it too, but we suggest you first think carefully about whether + this license or the ordinary General Public License is the better + strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, + not price. Our General Public Licenses are designed to make sure that + you have the freedom to distribute copies of free software (and charge + for this service if you wish) that you receive source code or can get + it if you want it that you can change the software and use pieces of + it in new free programs and that you are informed that you can do + these things. + + To protect your rights, we need to make restrictions that forbid + distributors to deny you these rights or to ask you to surrender these + rights. These restrictions translate to certain responsibilities for + you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis + or for a fee, you must give the recipients all the rights that we gave + you. You must make sure that they, too, receive or can get the source + code. If you link other code with the library, you must provide + complete object files to the recipients, so that they can relink them + with the library after making changes to the library and recompiling + it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the + library, and (2) we offer you this license, which gives you legal + permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that + there is no warranty for the free library. Also, if the library is + modified by someone else and passed on, the recipients should know + that what they have is not the original version, so that the original + author's reputation will not be affected by problems that might be + introduced by others. + + Finally, software patents pose a constant threat to the existence of + any free program. We wish to make sure that a company cannot + effectively restrict the users of a free program by obtaining a + restrictive license from a patent holder. Therefore, we insist that + any patent license obtained for a version of the library must be + consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the + ordinary GNU General Public License. This license, the GNU Lesser + General Public License, applies to certain designated libraries, and + is quite different from the ordinary General Public License. We use + this license for certain libraries in order to permit linking those + libraries into non-free programs. + + When a program is linked with a library, whether statically or using + a shared library, the combination of the two is legally speaking a + combined work, a derivative of the original library. The ordinary + General Public License therefore permits such linking only if the + entire combination fits its criteria of freedom. The Lesser General + Public License permits more lax criteria for linking other code with + the library. + + We call this license the "Lesser" General Public License because it + does Less to protect the user's freedom than the ordinary General + Public License. It also provides other free software developers Less + of an advantage over competing non-free programs. These disadvantages + are the reason we use the ordinary General Public License for many + libraries. However, the Lesser license provides advantages in certain + special circumstances. + + For example, on rare occasions, there may be a special need to + encourage the widest possible use of a certain library, so that it becomes + a de-facto standard. To achieve this, non-free programs must be + allowed to use the library. A more frequent case is that a free + library does the same job as widely used non-free libraries. In this + case, there is little to gain by limiting the free library to free + software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free + programs enables a greater number of people to use a large body of + free software. For example, permission to use the GNU C Library in + non-free programs enables many more people to use the whole GNU + operating system, as well as its variant, the GNU/Linux operating + system. + + Although the Lesser General Public License is Less protective of the + users' freedom, it does ensure that the user of a program that is + linked with the Library has the freedom and the wherewithal to run + that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and + modification follow. Pay close attention to the difference between a + "work based on the library" and a "work that uses the library". The + former contains code derived from the library, whereas the latter must + be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other + program which contains a notice placed by the copyright holder or + other authorized party saying it may be distributed under the terms of + this Lesser General Public License (also called "this License"). + Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data + prepared so as to be conveniently linked with application programs + (which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work + which has been distributed under these terms. A "work based on the + Library" means either the Library or any derivative work under + copyright law: that is to say, a work containing the Library or a + portion of it, either verbatim or with modifications and/or translated + straightforwardly into another language. (Hereinafter, translation is + included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for + making modifications to it. For a library, complete source code means + all the source code for all modules it contains, plus any associated + interface definition files, plus the scripts used to control compilation + and installation of the library. + + Activities other than copying, distribution and modification are not + covered by this License they are outside its scope. The act of + running a program using the Library is not restricted, and output from + such a program is covered only if its contents constitute a work based + on the Library (independent of the use of the Library in a tool for + writing it). Whether that is true depends on what the Library does + and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's + complete source code as you receive it, in any medium, provided that + you conspicuously and appropriately publish on each copy an + appropriate copyright notice and disclaimer of warranty keep intact + all the notices that refer to this License and to the absence of any + warranty and distribute a copy of this License along with the + Library. + + You may charge a fee for the physical act of transferring a copy, + and you may at your option offer warranty protection in exchange for a + fee. + + 2. You may modify your copy or copies of the Library or any portion + of it, thus forming a work based on the Library, and copy and + distribute such modifications or work under the terms of Section 1 + above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + + These requirements apply to the modified work as a whole. If + identifiable sections of that work are not derived from the Library, + and can be reasonably considered independent and separate works in + themselves, then this License, and its terms, do not apply to those + sections when you distribute them as separate works. But when you + distribute the same sections as part of a whole which is a work based + on the Library, the distribution of the whole must be on the terms of + this License, whose permissions for other licensees extend to the + entire whole, and thus to each and every part regardless of who wrote + it. + + Thus, it is not the intent of this section to claim rights or contest + your rights to work written entirely by you rather, the intent is to + exercise the right to control the distribution of derivative or + collective works based on the Library. + + In addition, mere aggregation of another work not based on the Library + with the Library (or with a work based on the Library) on a volume of + a storage or distribution medium does not bring the other work under + the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public + License instead of this License to a given copy of the Library. To do + this, you must alter all the notices that refer to this License, so + that they refer to the ordinary GNU General Public License, version 2, + instead of to this License. (If a newer version than version 2 of the + ordinary GNU General Public License has appeared, then you can specify + that version instead if you wish.) Do not make any other change in + these notices. + + Once this change is made in a given copy, it is irreversible for + that copy, so the ordinary GNU General Public License applies to all + subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of + the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or + derivative of it, under Section 2) in object code or executable form + under the terms of Sections 1 and 2 above provided that you accompany + it with the complete corresponding machine-readable source code, which + must be distributed under the terms of Sections 1 and 2 above on a + medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy + from a designated place, then offering equivalent access to copy the + source code from the same place satisfies the requirement to + distribute the source code, even though third parties are not + compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the + Library, but is designed to work with the Library by being compiled or + linked with it, is called a "work that uses the Library". Such a + work, in isolation, is not a derivative work of the Library, and + therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library + creates an executable that is a derivative of the Library (because it + contains portions of the Library), rather than a "work that uses the + library". The executable is therefore covered by this License. + Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file + that is part of the Library, the object code for the work may be a + derivative work of the Library even though the source code is not. + Whether this is true is especially significant if the work can be + linked without the Library, or if the work is itself a library. The + threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data + structure layouts and accessors, and small macros and small inline + functions (ten lines or less in length), then the use of the object + file is unrestricted, regardless of whether it is legally a derivative + work. (Executables containing this object code plus portions of the + Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may + distribute the object code for the work under the terms of Section 6. + Any executables containing that work also fall under Section 6, + whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or + link a "work that uses the Library" with the Library to produce a + work containing portions of the Library, and distribute that work + under terms of your choice, provided that the terms permit + modification of the work for the customer's own use and reverse + engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the + Library is used in it and that the Library and its use are covered by + this License. You must supply a copy of this License. If the work + during execution displays copyright notices, you must include the + copyright notice for the Library among them, as well as a reference + directing the user to the copy of this License. Also, you must do one + of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above) and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the + Library" must include any data and utility programs needed for + reproducing the executable from it. However, as a special exception, + the materials to be distributed need not include anything that is + normally distributed (in either source or binary form) with the major + components (compiler, kernel, and so on) of the operating system on + which the executable runs, unless that component itself accompanies + the executable. + + It may happen that this requirement contradicts the license + restrictions of other proprietary libraries that do not normally + accompany the operating system. Such a contradiction means you cannot + use both them and the Library together in an executable that you + distribute. + + 7. You may place library facilities that are a work based on the + Library side-by-side in a single library together with other library + facilities not covered by this License, and distribute such a combined + library, provided that the separate distribution of the work based on + the Library and of the other library facilities is otherwise + permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute + the Library except as expressly provided under this License. Any + attempt otherwise to copy, modify, sublicense, link with, or + distribute the Library is void, and will automatically terminate your + rights under this License. However, parties who have received copies, + or rights, from you under this License will not have their licenses + terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not + signed it. However, nothing else grants you permission to modify or + distribute the Library or its derivative works. These actions are + prohibited by law if you do not accept this License. Therefore, by + modifying or distributing the Library (or any work based on the + Library), you indicate your acceptance of this License to do so, and + all its terms and conditions for copying, distributing or modifying + the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the + Library), the recipient automatically receives a license from the + original licensor to copy, distribute, link with or modify the Library + subject to these terms and conditions. You may not impose any further + restrictions on the recipients' exercise of the rights granted herein. + You are not responsible for enforcing compliance by third parties with + this License. + + 11. If, as a consequence of a court judgment or allegation of patent + infringement or for any other reason (not limited to patent issues), + conditions are imposed on you (whether by court order, agreement or + otherwise) that contradict the conditions of this License, they do not + excuse you from the conditions of this License. If you cannot + distribute so as to satisfy simultaneously your obligations under this + License and any other pertinent obligations, then as a consequence you + may not distribute the Library at all. For example, if a patent + license would not permit royalty-free redistribution of the Library by + all those who receive copies directly or indirectly through you, then + the only way you could satisfy both it and this License would be to + refrain entirely from distribution of the Library. + + If any portion of this section is held invalid or unenforceable under any + particular circumstance, the balance of the section is intended to apply, + and the section as a whole is intended to apply in other circumstances. + + It is not the purpose of this section to induce you to infringe any + patents or other property right claims or to contest validity of any + such claims this section has the sole purpose of protecting the + integrity of the free software distribution system which is + implemented by public license practices. Many people have made + generous contributions to the wide range of software distributed + through that system in reliance on consistent application of that + system it is up to the author/donor to decide if he or she is willing + to distribute software through any other system and a licensee cannot + impose that choice. + + This section is intended to make thoroughly clear what is believed to + be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in + certain countries either by patents or by copyrighted interfaces, the + original copyright holder who places the Library under this License may add + an explicit geographical distribution limitation excluding those countries, + so that distribution is permitted only in or among countries not thus + excluded. In such case, this License incorporates the limitation as if + written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new + versions of the Lesser General Public License from time to time. + Such new versions will be similar in spirit to the present version, + but may differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the Library + specifies a version number of this License which applies to it and + "any later version", you have the option of following the terms and + conditions either of that version or of any later version published by + the Free Software Foundation. If the Library does not specify a + license version number, you may choose any version ever published by + the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free + programs whose distribution conditions are incompatible with these, + write to the author to ask for permission. For software which is + copyrighted by the Free Software Foundation, write to the Free + Software Foundation we sometimes make exceptions for this. Our + decision will be guided by the two goals of preserving the free status + of all derivatives of our free software and of promoting the sharing + and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO + WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. + EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR + OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY + KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE + LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME + THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN + WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY + AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU + FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR + CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE + LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING + RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A + FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF + SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest + possible use to the public, we recommend making it free software that + everyone can redistribute and change. You can do so by permitting + redistribution under these terms (or, alternatively, under the terms of the + ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is + safest to attach them to the start of each source file to most effectively + convey the exclusion of warranty and each file should have at least the + "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + Also add information on how to contact you by electronic and paper mail. + + You should also get your employer (if you work as a programmer) or your + school, if any, to sign a "copyright disclaimer" for the library, if + necessary. Here is a sample alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + + That's all there is to it! + +GPL 2 + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your + freedom to share and change it. By contrast, the GNU General Public + License is intended to guarantee your freedom to share and change free + software--to make sure the software is free for all its users. This + General Public License applies to most of the Free Software + Foundation's software and to any other program whose authors commit to + using it. (Some other Free Software Foundation software is covered by + the GNU Lesser General Public License instead.) You can apply it to + your programs, too. + + When we speak of free software, we are referring to freedom, not + price. Our General Public Licenses are designed to make sure that you + have the freedom to distribute copies of free software (and charge for + this service if you wish), that you receive source code or can get it + if you want it, that you can change the software or use pieces of it + in new free programs and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid + anyone to deny you these rights or to ask you to surrender the rights. + These restrictions translate to certain responsibilities for you if you + distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether + gratis or for a fee, you must give the recipients all the rights that + you have. You must make sure that they, too, receive or can get the + source code. And you must show them these terms so they know their + rights. + + We protect your rights with two steps: (1) copyright the software, and + (2) offer you this license which gives you legal permission to copy, + distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain + that everyone understands that there is no warranty for this free + software. If the software is modified by someone else and passed on, we + want its recipients to know that what they have is not the original, so + that any problems introduced by others will not reflect on the original + authors' reputations. + + Finally, any free program is threatened constantly by software + patents. We wish to avoid the danger that redistributors of a free + program will individually obtain patent licenses, in effect making the + program proprietary. To prevent this, we have made it clear that any + patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and + modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains + a notice placed by the copyright holder saying it may be distributed + under the terms of this General Public License. The "Program", below, + refers to any such program or work, and a "work based on the Program" + means either the Program or any derivative work under copyright law: + that is to say, a work containing the Program or a portion of it, + either verbatim or with modifications and/or translated into another + language. (Hereinafter, translation is included without limitation in + the term "modification".) Each licensee is addressed as "you". + + Activities other than copying, distribution and modification are not + covered by this License they are outside its scope. The act of + running the Program is not restricted, and the output from the Program + is covered only if its contents constitute a work based on the + Program (independent of having been made by running the Program). + Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's + source code as you receive it, in any medium, provided that you + conspicuously and appropriately publish on each copy an appropriate + copyright notice and disclaimer of warranty keep intact all the + notices that refer to this License and to the absence of any warranty + and give any other recipients of the Program a copy of this License + along with the Program. + + You may charge a fee for the physical act of transferring a copy, and + you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion + of it, thus forming a work based on the Program, and copy and + distribute such modifications or work under the terms of Section 1 + above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + + These requirements apply to the modified work as a whole. If + identifiable sections of that work are not derived from the Program, + and can be reasonably considered independent and separate works in + themselves, then this License, and its terms, do not apply to those + sections when you distribute them as separate works. But when you + distribute the same sections as part of a whole which is a work based + on the Program, the distribution of the whole must be on the terms of + this License, whose permissions for other licensees extend to the + entire whole, and thus to each and every part regardless of who wrote it. + + Thus, it is not the intent of this section to claim rights or contest + your rights to work written entirely by you rather, the intent is to + exercise the right to control the distribution of derivative or + collective works based on the Program. + + In addition, mere aggregation of another work not based on the Program + with the Program (or with a work based on the Program) on a volume of + a storage or distribution medium does not bring the other work under + the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, + under Section 2) in object code or executable form under the terms of + Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + + The source code for a work means the preferred form of the work for + making modifications to it. For an executable work, complete source + code means all the source code for all modules it contains, plus any + associated interface definition files, plus the scripts used to + control compilation and installation of the executable. However, as a + special exception, the source code distributed need not include + anything that is normally distributed (in either source or binary + form) with the major components (compiler, kernel, and so on) of the + operating system on which the executable runs, unless that component + itself accompanies the executable. + + If distribution of executable or object code is made by offering + access to copy from a designated place, then offering equivalent + access to copy the source code from the same place counts as + distribution of the source code, even though third parties are not + compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program + except as expressly provided under this License. Any attempt + otherwise to copy, modify, sublicense or distribute the Program is + void, and will automatically terminate your rights under this License. + However, parties who have received copies, or rights, from you under + this License will not have their licenses terminated so long as such + parties remain in full compliance. + + 5. You are not required to accept this License, since you have not + signed it. However, nothing else grants you permission to modify or + distribute the Program or its derivative works. These actions are + prohibited by law if you do not accept this License. Therefore, by + modifying or distributing the Program (or any work based on the + Program), you indicate your acceptance of this License to do so, and + all its terms and conditions for copying, distributing or modifying + the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the + Program), the recipient automatically receives a license from the + original licensor to copy, distribute or modify the Program subject to + these terms and conditions. You may not impose any further + restrictions on the recipients' exercise of the rights granted herein. + You are not responsible for enforcing compliance by third parties to + this License. + + 7. If, as a consequence of a court judgment or allegation of patent + infringement or for any other reason (not limited to patent issues), + conditions are imposed on you (whether by court order, agreement or + otherwise) that contradict the conditions of this License, they do not + excuse you from the conditions of this License. If you cannot + distribute so as to satisfy simultaneously your obligations under this + License and any other pertinent obligations, then as a consequence you + may not distribute the Program at all. For example, if a patent + license would not permit royalty-free redistribution of the Program by + all those who receive copies directly or indirectly through you, then + the only way you could satisfy both it and this License would be to + refrain entirely from distribution of the Program. + + If any portion of this section is held invalid or unenforceable under + any particular circumstance, the balance of the section is intended to + apply and the section as a whole is intended to apply in other + circumstances. + + It is not the purpose of this section to induce you to infringe any + patents or other property right claims or to contest validity of any + such claims this section has the sole purpose of protecting the + integrity of the free software distribution system, which is + implemented by public license practices. Many people have made + generous contributions to the wide range of software distributed + through that system in reliance on consistent application of that + system it is up to the author/donor to decide if he or she is willing + to distribute software through any other system and a licensee cannot + impose that choice. + + This section is intended to make thoroughly clear what is believed to + be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in + certain countries either by patents or by copyrighted interfaces, the + original copyright holder who places the Program under this License + may add an explicit geographical distribution limitation excluding + those countries, so that distribution is permitted only in or among + countries not thus excluded. In such case, this License incorporates + the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions + of the General Public License from time to time. Such new versions will + be similar in spirit to the present version, but may differ in detail to + address new problems or concerns. + + Each version is given a distinguishing version number. If the Program + specifies a version number of this License which applies to it and "any + later version", you have the option of following the terms and conditions + either of that version or of any later version published by the Free + Software Foundation. If the Program does not specify a version number of + this License, you may choose any version ever published by the Free Software + Foundation. + + 10. If you wish to incorporate parts of the Program into other free + programs whose distribution conditions are different, write to the author + to ask for permission. For software which is copyrighted by the Free + Software Foundation, write to the Free Software Foundation we sometimes + make exceptions for this. Our decision will be guided by the two goals + of preserving the free status of all derivatives of our free software and + of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY + FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN + OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES + PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED + OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS + TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE + PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, + REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING + WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR + REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, + INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING + OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED + TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY + YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER + PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE + POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest + possible use to the public, the best way to achieve this is to make it + free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest + to attach them to the start of each source file to most effectively + convey the exclusion of warranty and each file should have at least + the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + Also add information on how to contact you by electronic and paper mail. + + If the program is interactive, make it output a short notice like this + when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions type `show c' for details. + + The hypothetical commands `show w' and `show c' should show the appropriate + parts of the General Public License. Of course, the commands you use may + be called something other than `show w' and `show c' they could even be + mouse-clicks or menu items--whatever suits your program. + + You should also get your employer (if you work as a programmer) or your + school, if any, to sign a "copyright disclaimer" for the program, if + necessary. Here is a sample alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + + This General Public License does not permit incorporating your program into + proprietary programs. If your program is a subroutine library, you may + consider it more useful to permit linking proprietary applications with the + library. If this is what you want to do, use the GNU Lesser General + Public License instead of this License. + +Apache 2.0 + Apache License + + Version 2.0, January 2004 + + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + + "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + + You must give any other recipients of the Work or Derivative Works a copy of this License and + You must cause any modified files to carry prominent notices stating that You changed the files and + You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works and + If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works within the Source form or documentation, if provided along with the Derivative Works or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + + You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + APPENDIX: How to apply the Apache License to your work + + To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License") + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/love/deps/LUA51.dll b/love/deps/LUA51.dll new file mode 100644 index 0000000..bc0c9c6 Binary files /dev/null and b/love/deps/LUA51.dll differ diff --git a/love/deps/MSVCP120.dll b/love/deps/MSVCP120.dll new file mode 100644 index 0000000..4ea1efa Binary files /dev/null and b/love/deps/MSVCP120.dll differ diff --git a/love/deps/MSVCR120.dll b/love/deps/MSVCR120.dll new file mode 100644 index 0000000..d711c92 Binary files /dev/null and b/love/deps/MSVCR120.dll differ diff --git a/love/deps/OpenAL32.dll b/love/deps/OpenAL32.dll new file mode 100644 index 0000000..c8f637b Binary files /dev/null and b/love/deps/OpenAL32.dll differ diff --git a/love/deps/SDL2.dll b/love/deps/SDL2.dll new file mode 100644 index 0000000..2317a7c Binary files /dev/null and b/love/deps/SDL2.dll differ diff --git a/love/deps/love.dll b/love/deps/love.dll new file mode 100644 index 0000000..2f5e67a Binary files /dev/null and b/love/deps/love.dll differ diff --git a/love/deps/mpg123.dll b/love/deps/mpg123.dll new file mode 100644 index 0000000..f6f1842 Binary files /dev/null and b/love/deps/mpg123.dll differ diff --git a/lua/config.lua b/lua/config.lua index c1d3a9f..6d67fa7 100644 --- a/lua/config.lua +++ b/lua/config.lua @@ -57,6 +57,7 @@ require("lazy").setup({ }, }, config = function() + -- this must be call after every keymap cleanup require("which-key").setup({ triggers = { { "", mode = { "n", "v" } }, @@ -64,14 +65,16 @@ require("lazy").setup({ }) end, }, + { + "nvim-lua/plenary.nvim", + lazy = true, -- only load when needed + init = function() + vim.notify = function() end -- suppress notifications + end, + }, { -- "caspersg/neohack.nvim", dir = "~/projects/neohack.nvim", - dependencies = { - -- "sphamba/smear-cursor.nvim", - - "nvim-lua/plenary.nvim", - }, config = function() require("neohack").setup({}) vim.cmd("normal! i ") diff --git a/lua/lib/rotLove/img/cp437.png b/lua/lib/rotLove/img/cp437.png new file mode 100644 index 0000000..d400b05 Binary files /dev/null and b/lua/lib/rotLove/img/cp437.png differ diff --git a/lua/lib/rotLove/rot.lua b/lua/lib/rotLove/rot.lua new file mode 100644 index 0000000..73d7c67 --- /dev/null +++ b/lua/lib/rotLove/rot.lua @@ -0,0 +1,106 @@ +local ROTLOVE_PATH = (...) .. "." +local Class = require(ROTLOVE_PATH .. "class") + +local ROT = Class:extend("ROT", { + + DIRS = { + FOUR = { + { 0, -1 }, + { 1, 0 }, + { 0, 1 }, + { -1, 0 }, + }, + EIGHT = { + { 0, -1 }, + { 1, -1 }, + { 1, 0 }, + { 1, 1 }, + { 0, 1 }, + { -1, 1 }, + { -1, 0 }, + { -1, -1 }, + }, + }, +}) +package.loaded[...] = ROT + +-- Concatenating assert function +-- see http://lua.space/general/assert-usage-caveat +function ROT.assert(pass, ...) + if pass then + return pass, ... + elseif select("#", ...) > 0 then + error(table.concat({ ... }), 2) + else + error("assertion failed!", 2) + end +end + +ROT.Class = Class + +ROT.RNG = require(ROTLOVE_PATH .. "rng") + +-- bind a function to a class instance +function Class:bind(func) + return function(...) + return func(self, ...) + end +end + +-- get/set RNG instance for a class +-- used by maps, noise, dice, etc. +Class._rng = ROT.RNG +function Class:getRNG() + return self._rng +end +function Class:setRNG(rng) + self._rng = rng or ROT.RNG + return self +end + +require(ROTLOVE_PATH .. "newFuncs") + +ROT.Type = {} -- collection types tuned for various use cases +ROT.Type.PointSet = require(ROTLOVE_PATH .. "type.pointSet") +ROT.Type.Grid = require(ROTLOVE_PATH .. "type.grid") + +ROT.Dice = require(ROTLOVE_PATH .. "dice") +ROT.Display = require(ROTLOVE_PATH .. "display") +ROT.TextDisplay = require(ROTLOVE_PATH .. "textDisplay") +ROT.StringGenerator = require(ROTLOVE_PATH .. "stringGenerator") +ROT.EventQueue = require(ROTLOVE_PATH .. "eventQueue") +ROT.Scheduler = require(ROTLOVE_PATH .. "scheduler") +ROT.Scheduler.Simple = require(ROTLOVE_PATH .. "scheduler.simple") +ROT.Scheduler.Speed = require(ROTLOVE_PATH .. "scheduler.speed") +ROT.Scheduler.Action = require(ROTLOVE_PATH .. "scheduler.action") +ROT.Engine = require(ROTLOVE_PATH .. "engine") +ROT.Map = require(ROTLOVE_PATH .. "map") +ROT.Map.Arena = require(ROTLOVE_PATH .. "map.arena") +ROT.Map.DividedMaze = require(ROTLOVE_PATH .. "map.dividedMaze") +ROT.Map.IceyMaze = require(ROTLOVE_PATH .. "map.iceyMaze") +ROT.Map.EllerMaze = require(ROTLOVE_PATH .. "map.ellerMaze") +ROT.Map.Cellular = require(ROTLOVE_PATH .. "map.cellular") +ROT.Map.Dungeon = require(ROTLOVE_PATH .. "map.dungeon") +ROT.Map.Feature = require(ROTLOVE_PATH .. "map.feature") +ROT.Map.Room = require(ROTLOVE_PATH .. "map.room") +ROT.Map.Corridor = require(ROTLOVE_PATH .. "map.corridor") +ROT.Map.Digger = require(ROTLOVE_PATH .. "map.digger") +ROT.Map.Uniform = require(ROTLOVE_PATH .. "map.uniform") +ROT.Map.Rogue = require(ROTLOVE_PATH .. "map.rogue") +ROT.Map.BrogueRoom = require(ROTLOVE_PATH .. "map.brogueRoom") +ROT.Map.Brogue = require(ROTLOVE_PATH .. "map.brogue") +ROT.Noise = require(ROTLOVE_PATH .. "noise") +ROT.Noise.Simplex = require(ROTLOVE_PATH .. "noise.simplex") +ROT.FOV = require(ROTLOVE_PATH .. "fov") +ROT.FOV.Precise = require(ROTLOVE_PATH .. "fov.precise") +ROT.FOV.Bresenham = require(ROTLOVE_PATH .. "fov.bresenham") +ROT.FOV.Recursive = require(ROTLOVE_PATH .. "fov.recursive") +ROT.Color = require(ROTLOVE_PATH .. "color") +ROT.Lighting = require(ROTLOVE_PATH .. "lighting") +ROT.Path = require(ROTLOVE_PATH .. "path") +ROT.Path.Dijkstra = require(ROTLOVE_PATH .. "path.dijkstra") +ROT.Path.DijkstraMap = require(ROTLOVE_PATH .. "path.dijkstraMap") +ROT.Path.AStar = require(ROTLOVE_PATH .. "path.astar") +ROT.Text = require(ROTLOVE_PATH .. "text") + +return ROT diff --git a/lua/lib/rotLove/rot/class.lua b/lua/lib/rotLove/rot/class.lua new file mode 100644 index 0000000..2dc2b42 --- /dev/null +++ b/lua/lib/rotLove/rot/class.lua @@ -0,0 +1,24 @@ +---@class Class +---@field new function +---@field extend function +---@field init function +---@field super table +local Class = {} + +function Class:new(...) + local t = setmetatable({}, self) + t:init(...) + return t +end + +function Class:extend(name, t) + t = t or {} + t.__index = t + t.super = self + t.__name = name + return setmetatable(t, { __call = self.new, __index = self }) +end + +function Class:init() end + +return Class diff --git a/lua/lib/rotLove/rot/color.lua b/lua/lib/rotLove/rot/color.lua new file mode 100644 index 0000000..ea3ffa8 --- /dev/null +++ b/lua/lib/rotLove/rot/color.lua @@ -0,0 +1,553 @@ +--- The Color Toolkit. +-- Color is a color handler that treats any +-- objects intended to represent a color as a +-- table of the following schema: +-- @module ROT.Color + +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Color +local Color = ROT.Class:extend("Color") + +function Color:init(r, g, b, a) + self[1], self[2], self[3], self[4] = r or 0, g or 0, b or 0, a +end + +--- Get color from string. +-- Convert one of several formats of string to what +-- Color interperets as a color object +-- @tparam string str Accepted formats 'rgb(0..1, 0..1, 0..1)', '#5fe', '#5FE', '#254eff', 'goldenrod' +function Color.fromString(str) + local cached = Color._cached[str] + if cached then + return cached + end + local values = { 0, 0, 0 } + if str:sub(1, 1) == "#" then + local j = 1 + for s in str:gmatch("[%da-fA-F]") do + values[j] = tonumber(s, 16) + j = j + 1 + end + if #values == 3 then + for i = 1, 3 do + values[i] = values[i] * 17 / 255 + end + else + for i = 1, 3 do + values[i + 1] = values[i + 1] + (16 * values[i]) / 255 + table.remove(values, i) + end + end + elseif str:gmatch("rgb") then + local i = 1 + for s in str:gmatch("(%d*%.?%d+)") do + values[i] = tonumber(s) + i = i + 1 + end + end + Color._cached[str] = values + return values +end + +local function add(t, color, ...) + if not color then + return t + end + for i = 1, #color do + t[i] = (t[i] or 0) + color[i] + end + return add(t, ...) +end + +local function multiply(t, color, ...) + if not color then + return t + end + for i = 1, #color do + t[i] = ((t[i] or 1.0) * color[i]) + end + return multiply(t, ...) +end + +--- Add two or more colors. +-- @tparam table color1 A color table +-- @tparam table color2 A color table +-- @tparam table ... More color tables +-- @treturn table new color +function Color.add(...) + return add({}, ...) +end + +--- Add two or more colors. Modifies first color in-place. +-- @tparam table color1 A color table +-- @tparam table color2 A color table +-- @tparam table ... More color tables +-- @treturn table modified color +function Color.add_(...) + return add(...) +end + +-- Multiply (mix) two or more colors. +-- @tparam table color1 A color table +-- @tparam table color2 A color table +-- @tparam table ... More color tables +-- @treturn table new color +function Color.multiply(...) + return multiply({}, ...) +end + +-- Multiply (mix) two or more colors. Modifies first color in-place. +-- @tparam table color1 A color table +-- @tparam table color2 A color table +-- @tparam table ... More color tables +-- @treturn table modified color +function Color.multiply_(...) + return multiply(...) +end + +--- Interpolate (blend) two colors with a given factor. +-- @tparam table color1 A color table +-- @tparam table color2 A color table +-- @tparam float factor A number from 0 to 1. <0.5 favors color1, >0.5 favors color2. +-- @treturn table resulting color +function Color.interpolate(color1, color2, factor) + factor = factor or 0.5 + local result = {} + for i = 1, math.max(#color1, #color2) do + local a, b = color2[i] or color1[i], color1[i] or color2[i] + result[i] = (b + factor * (a - b) + 0.005) + end + return result +end + +--- Interpolate (blend) two colors with a given factor in HSL mode. +-- @tparam table color1 A color table +-- @tparam table color2 A color table +-- @tparam float factor A number from 0 to 1. <0.5 favors color1, >0.5 favors color2. +-- @treturn table resulting color +function Color.interpolateHSL(color1, color2, factor) + factor = factor or 0.5 + local result = {} + local hsl1, hsl2 = Color.rgb2hsl(color1), Color.rgb2hsl(color2) + for i = 1, math.max(#hsl1, #hsl2) do + local a, b = hsl2[i] or hsl1[i], hsl1[i] or hsl2[i] + result[i] = b + factor * (a - b) + end + return Color.hsl2rgb(result) +end + +--- Create a new random color based on this one +-- @tparam table color A color table +-- @tparam int|table diff One or more numbers to use for a standard deviation +function Color.randomize(color, diff, rng) + rng = rng or color._rng or ROT.RNG + local result = {} + if type(diff) ~= "table" then + local r = rng:random(0, diff * 100) / 100 + for i = 1, #color do + result[i] = color[i] + r + end + else + for i = 1, #color do + result[i] = color[i] + rng:random(0, diff[i] * 100) / 100 + end + end + return result +end + +-- Convert rgb color to hsl +function Color.rgb2hsl(color) + local r = color[1] + local g = color[2] + local b = color[3] + local a = color[4] and color[4] + local max = math.max(r, g, b) + local min = math.min(r, g, b) + local h, s, l = 0, 0, (max + min) / 2 + + if max ~= min then + local d = max - min + s = l > 0.5 and d / (2 - max - min) or d / (max + min) + if max == r then + h = (g - b) / d + (g < b and 6 or 0) + elseif max == g then + h = (b - r) / d + 2 + elseif max == b then + h = (r - g) / d + 4 + end + h = h / 6 + end + + return { math.floor(h + 0.005), math.floor(s + 0.005), math.floor(l + 0.005), math.floor(a + 0.005) } +end + +local function hue2rgb(p, q, t) + if t < 0 then + t = t + 1 + end + if t > 1 then + t = t - 1 + end + if t < 1 / 6 then + return (p + (q - p) * 6 * t) + end + if t < 1 / 2 then + return q + end + if t < 2 / 3 then + return (p + (q - p) * (2 / 3 - t) * 6) + end + return p +end + +-- Convert hsl color to rgb +function Color.hsl2rgb(color) + local h, s, l = color[1], color[2], color[3] + local result = {} + result[4] = color[4] + if s == 0 then + local value = (l + 0.005) + for i = 1, 3 do + result[i] = value + end + else + local q = l < 0.5 and l * (1 + s) or l + s - l * s + local p = 2 * l - q + result[1] = math.floor(hue2rgb(p, q, h + 1 / 3) * 255 + 0.5) / 256 + result[2] = math.floor(hue2rgb(p, q, h) * 255 + 0.5) / 256 + result[3] = math.floor(hue2rgb(p, q, h - 1 / 3) * 255 + 0.5) / 256 + end + return result +end + +--- Convert color to RGB string. +-- Get a string that can be fed to Color.fromString() +-- @tparam table color A color table +function Color.toRGB(color) + return ("rgb(%f,%f,%f)"):format(Color._clamp(color[1]), Color._clamp(color[2]), Color._clamp(color[3])) +end + +--- Convert color to Hex string +-- Get a string that can be fed to Color.fromString() +-- @tparam table color A color table +function Color.toHex(color) + return ("#%02x%02x%02x"):format( + Color._clamp(color[1]) * 255, + Color._clamp(color[2]) * 255, + Color._clamp(color[3]) * 255 + ) +end + +-- limit a number to 0..1 +function Color._clamp(n) + return n < 0 and 0 or n > 1.0 and 1.0 or n +end + +function Color.__add(a, b) + return add({}, a, b) +end +function Color.__mul(a, b) + return mul({}, a, b) +end + +--- Color cache +-- A table of predefined color tables +-- These keys can be passed to Color.fromString() +-- @field black { 0/255, 0/255, 0/255 } +-- @field navy { 0/255, 0/255, 128/255 } +-- @field darkblue { 0/255, 0/255, 139/255 } +-- @field mediumblue { 0/255, 0/255, 205/255 } +-- @field blue { 0/255, 0/255, 255/255 } +-- @field darkgreen { 0/255, 100/255, 0/255 } +-- @field green { 0/255, 128/255, 0/255 } +-- @field teal { 0/255, 128/255, 128/255 } +-- @field darkcyan { 0/255, 139/255, 139/255 } +-- @field deepskyblue { 0/255, 191/255, 255/255 } +-- @field darkturquoise { 0/255, 206/255, 209/255 } +-- @field mediumspringgreen { 0/255, 250/255, 154/255 } +-- @field lime { 0/255, 255/255, 0/255 } +-- @field springgreen { 0/255, 255/255, 127/255 } +-- @field aqua { 0/255, 255/255, 255/255 } +-- @field cyan { 0/255, 255/255, 255/255 } +-- @field midnightblue { 25/255, 25/255, 112/255 } +-- @field dodgerblue { 30/255, 144/255, 255/255 } +-- @field forestgreen { 34/255, 139/255, 34/255 } +-- @field seagreen { 46/255, 139/255, 87/255 } +-- @field darkslategray { 47/255, 79/255, 79/255 } +-- @field darkslategrey { 47/255, 79/255, 79/255 } +-- @field limegreen { 50/255, 205/255, 50/255 } +-- @field mediumseagreen { 60/255, 179/255, 113/255 } +-- @field turquoise { 64/255, 224/255, 208/255 } +-- @field royalblue { 65/255, 105/255, 225/255 } +-- @field steelblue { 70/255, 130/255, 180/255 } +-- @field darkslateblue { 72/255, 61/255, 139/255 } +-- @field mediumturquoise { 72/255, 209/255, 204/255 } +-- @field indigo { 75/255, 0/255, 130/255 } +-- @field darkolivegreen { 85/255, 107/255, 47/255 } +-- @field cadetblue { 95/255, 158/255, 160/255 } +-- @field cornflowerblue { 100/255, 149/255, 237/255 } +-- @field mediumaquamarine { 102/255, 205/255, 170/255 } +-- @field dimgray { 105/255, 105/255, 105/255 } +-- @field dimgrey { 105/255, 105/255, 105/255 } +-- @field slateblue { 106/255, 90/255, 205/255 } +-- @field olivedrab { 107/255, 142/255, 35/255 } +-- @field slategray { 112/255, 128/255, 144/255 } +-- @field slategrey { 112/255, 128/255, 144/255 } +-- @field lightslategray { 119/255, 136/255, 153/255 } +-- @field lightslategrey { 119/255, 136/255, 153/255 } +-- @field mediumslateblue { 123/255, 104/255, 238/255 } +-- @field lawngreen { 124/255, 252/255, 0/255 } +-- @field chartreuse { 127/255, 255/255, 0/255 } +-- @field aquamarine { 127/255, 255/255, 212/255 } +-- @field maroon { 128/255, 0/255, 0/255 } +-- @field purple { 128/255, 0/255, 128/255 } +-- @field olive { 128/255, 128/255, 0/255 } +-- @field gray { 128/255, 128/255, 128/255 } +-- @field grey { 128/255, 128/255, 128/255 } +-- @field skyblue { 135/255, 206/255, 235/255 } +-- @field lightskyblue { 135/255, 206/255, 250/255 } +-- @field blueviolet { 138/255, 43/255, 226/255 } +-- @field darkred { 139/255, 0/255, 0/255 } +-- @field darkmagenta { 139/255, 0/255, 139/255 } +-- @field saddlebrown { 139/255, 69/255, 19/255 } +-- @field darkseagreen { 143/255, 188/255, 143/255 } +-- @field lightgreen { 144/255, 238/255, 144/255 } +-- @field mediumpurple { 147/255, 112/255, 216/255 } +-- @field darkviolet { 148/255, 0/255, 211/255 } +-- @field palegreen { 152/255, 251/255, 152/255 } +-- @field darkorchid { 153/255, 50/255, 204/255 } +-- @field yellowgreen { 154/255, 205/255, 50/255 } +-- @field sienna { 160/255, 82/255, 45/255 } +-- @field brown { 165/255, 42/255, 42/255 } +-- @field darkgray { 169/255, 169/255, 169/255 } +-- @field darkgrey { 169/255, 169/255, 169/255 } +-- @field lightblue { 173/255, 216/255, 230/255 } +-- @field greenyellow { 173/255, 255/255, 47/255 } +-- @field paleturquoise { 175/255, 238/255, 238/255 } +-- @field lightsteelblue { 176/255, 196/255, 222/255 } +-- @field powderblue { 176/255, 224/255, 230/255 } +-- @field firebrick { 178/255, 34/255, 34/255 } +-- @field darkgoldenrod { 184/255, 134/255, 11/255 } +-- @field mediumorchid { 186/255, 85/255, 211/255 } +-- @field rosybrown { 188/255, 143/255, 143/255 } +-- @field darkkhaki { 189/255, 183/255, 107/255 } +-- @field silver { 192/255, 192/255, 192/255 } +-- @field mediumvioletred { 199/255, 21/255, 133/255 } +-- @field indianred { 205/255, 92/255, 92/255 } +-- @field peru { 205/255, 133/255, 63/255 } +-- @field chocolate { 210/255, 105/255, 30/255 } +-- @field tan { 210/255, 180/255, 140/255 } +-- @field lightgray { 211/255, 211/255, 211/255 } +-- @field lightgrey { 211/255, 211/255, 211/255 } +-- @field palevioletred { 216/255, 112/255, 147/255 } +-- @field thistle { 216/255, 191/255, 216/255 } +-- @field orchid { 218/255, 112/255, 214/255 } +-- @field goldenrod { 218/255, 165/255, 32/255 } +-- @field crimson { 220/255, 20/255, 60/255 } +-- @field gainsboro { 220/255, 220/255, 220/255 } +-- @field plum { 221/255, 160/255, 221/255 } +-- @field burlywood { 222/255, 184/255, 135/255 } +-- @field lightcyan { 224/255, 255/255, 255/255 } +-- @field lavender { 230/255, 230/255, 250/255 } +-- @field darksalmon { 233/255, 150/255, 122/255 } +-- @field violet { 238/255, 130/255, 238/255 } +-- @field palegoldenrod { 238/255, 232/255, 170/255 } +-- @field lightcoral { 240/255, 128/255, 128/255 } +-- @field khaki { 240/255, 230/255, 140/255 } +-- @field aliceblue { 240/255, 248/255, 255/255 } +-- @field honeydew { 240/255, 255/255, 240/255 } +-- @field azure { 240/255, 255/255, 255/255 } +-- @field sandybrown { 244/255, 164/255, 96/255 } +-- @field wheat { 245/255, 222/255, 179/255 } +-- @field beige { 245/255, 245/255, 220/255 } +-- @field whitesmoke { 245/255, 245/255, 245/255 } +-- @field mintcream { 245/255, 255/255, 250/255 } +-- @field ghostwhite { 248/255, 248/255, 255/255 } +-- @field salmon { 250/255, 128/255, 114/255 } +-- @field antiquewhite { 250/255, 235/255, 215/255 } +-- @field linen { 250/255, 240/255, 230/255 } +-- @field lightgoldenrodyellow { 250/255, 250/255, 210/255 } +-- @field oldlace { 253/255, 245/255, 230/255 } +-- @field red { 255/255, 0/255, 0/255 } +-- @field fuchsia { 255/255, 0/255, 255/255 } +-- @field magenta { 255/255, 0/255, 255/255 } +-- @field deeppink { 255/255, 20/255, 147/255 } +-- @field orangered { 255/255, 69/255, 0/255 } +-- @field tomato { 255/255, 99/255, 71/255 } +-- @field hotpink { 255/255, 105/255, 180/255 } +-- @field coral { 255/255, 127/255, 80/255 } +-- @field darkorange { 255/255, 140/255, 0/255 } +-- @field lightsalmon { 255/255, 160/255, 122/255 } +-- @field orange { 255/255, 165/255, 0/255 } +-- @field lightpink { 255/255, 182/255, 193/255 } +-- @field pink { 255/255, 192/255, 203/255 } +-- @field gold { 255/255, 215/255, 0/255 } +-- @field peachpuff { 255/255, 218/255, 185/255 } +-- @field navajowhite { 255/255, 222/255, 173/255 } +-- @field moccasin { 255/255, 228/255, 181/255 } +-- @field bisque { 255/255, 228/255, 196/255 } +-- @field mistyrose { 255/255, 228/255, 225/255 } +-- @field blanchedalmond { 255/255, 235/255, 205/255 } +-- @field papayawhip { 255/255, 239/255, 213/255 } +-- @field lavenderblush { 255/255, 240/255, 245/255 } +-- @field seashell { 255/255, 245/255, 238/255 } +-- @field cornsilk { 255/255, 248/255, 220/255 } +-- @field lemonchiffon { 255/255, 250/255, 205/255 } +-- @field floralwhite { 255/255, 250/255, 240/255 } +-- @field snow { 255/255, 250/255, 250/255 } +-- @field yellow { 255/255, 255/255, 0/255 } +-- @field lightyellow { 255/255, 255/255, 224/255 } +-- @field ivory { 255/255, 255/255, 240/255 } +-- @field white { 255/255, 255/255, 255/255 } +-- @table Color._cache + +Color._cached = { + black = { 0.000, 0.000, 0.000 }, + navy = { 0.000, 0.000, 0.502 }, + darkblue = { 0.000, 0.000, 0.545 }, + mediumblue = { 0.000, 0.000, 0.804 }, + blue = { 0.000, 0.000, 1.000 }, + darkgreen = { 0.000, 0.392, 0.000 }, + green = { 0.000, 0.502, 0.000 }, + teal = { 0.000, 0.502, 0.502 }, + darkcyan = { 0.000, 0.545, 0.545 }, + deepskyblue = { 0.000, 0.749, 1.000 }, + darkturquoise = { 0.000, 0.808, 0.820 }, + mediumspringgreen = { 0.000, 0.980, 0.604 }, + lime = { 0.000, 1.000, 0.000 }, + springgreen = { 0.000, 1.000, 0.498 }, + aqua = { 0.000, 1.000, 1.000 }, + cyan = { 0.000, 1.000, 1.000 }, + midnightblue = { 0.098, 0.098, 0.439 }, + dodgerblue = { 0.118, 0.565, 1.000 }, + forestgreen = { 0.133, 0.545, 0.133 }, + seagreen = { 0.180, 0.545, 0.341 }, + darkslategray = { 0.184, 0.310, 0.310 }, + darkslategrey = { 0.184, 0.310, 0.310 }, + limegreen = { 0.196, 0.804, 0.196 }, + mediumseagreen = { 0.235, 0.702, 0.443 }, + turquoise = { 0.251, 0.878, 0.816 }, + royalblue = { 0.255, 0.412, 0.882 }, + steelblue = { 0.275, 0.510, 0.706 }, + darkslateblue = { 0.282, 0.239, 0.545 }, + mediumturquoise = { 0.282, 0.820, 0.800 }, + indigo = { 0.294, 0.000, 0.510 }, + darkolivegreen = { 0.333, 0.420, 0.184 }, + cadetblue = { 0.373, 0.620, 0.627 }, + cornflowerblue = { 0.392, 0.584, 0.929 }, + mediumaquamarine = { 0.400, 0.804, 0.667 }, + dimgray = { 0.412, 0.412, 0.412 }, + dimgrey = { 0.412, 0.412, 0.412 }, + slateblue = { 0.416, 0.353, 0.804 }, + olivedrab = { 0.420, 0.557, 0.137 }, + slategray = { 0.439, 0.502, 0.565 }, + slategrey = { 0.439, 0.502, 0.565 }, + lightslategray = { 0.467, 0.533, 0.600 }, + lightslategrey = { 0.467, 0.533, 0.600 }, + mediumslateblue = { 0.482, 0.408, 0.933 }, + lawngreen = { 0.486, 0.988, 0.000 }, + chartreuse = { 0.498, 1.000, 0.000 }, + aquamarine = { 0.498, 1.000, 0.831 }, + maroon = { 0.502, 0.000, 0.000 }, + purple = { 0.502, 0.000, 0.502 }, + olive = { 0.502, 0.502, 0.000 }, + gray = { 0.502, 0.502, 0.502 }, + grey = { 0.502, 0.502, 0.502 }, + skyblue = { 0.529, 0.808, 0.922 }, + lightskyblue = { 0.529, 0.808, 0.980 }, + blueviolet = { 0.541, 0.169, 0.886 }, + darkred = { 0.545, 0.000, 0.000 }, + darkmagenta = { 0.545, 0.000, 0.545 }, + saddlebrown = { 0.545, 0.271, 0.075 }, + darkseagreen = { 0.561, 0.737, 0.561 }, + lightgreen = { 0.565, 0.933, 0.565 }, + mediumpurple = { 0.576, 0.439, 0.847 }, + darkviolet = { 0.580, 0.000, 0.827 }, + palegreen = { 0.596, 0.984, 0.596 }, + darkorchid = { 0.600, 0.196, 0.800 }, + yellowgreen = { 0.604, 0.804, 0.196 }, + sienna = { 0.627, 0.322, 0.176 }, + brown = { 0.647, 0.165, 0.165 }, + darkgray = { 0.663, 0.663, 0.663 }, + darkgrey = { 0.663, 0.663, 0.663 }, + lightblue = { 0.678, 0.847, 0.902 }, + greenyellow = { 0.678, 1.000, 0.184 }, + paleturquoise = { 0.686, 0.933, 0.933 }, + lightsteelblue = { 0.690, 0.769, 0.871 }, + powderblue = { 0.690, 0.878, 0.902 }, + firebrick = { 0.698, 0.133, 0.133 }, + darkgoldenrod = { 0.722, 0.525, 0.043 }, + mediumorchid = { 0.729, 0.333, 0.827 }, + rosybrown = { 0.737, 0.561, 0.561 }, + darkkhaki = { 0.741, 0.718, 0.420 }, + silver = { 0.753, 0.753, 0.753 }, + mediumvioletred = { 0.780, 0.082, 0.522 }, + indianred = { 0.804, 0.361, 0.361 }, + peru = { 0.804, 0.522, 0.247 }, + chocolate = { 0.824, 0.412, 0.118 }, + tan = { 0.824, 0.706, 0.549 }, + lightgray = { 0.827, 0.827, 0.827 }, + lightgrey = { 0.827, 0.827, 0.827 }, + palevioletred = { 0.847, 0.439, 0.576 }, + thistle = { 0.847, 0.749, 0.847 }, + orchid = { 0.855, 0.439, 0.839 }, + goldenrod = { 0.855, 0.647, 0.125 }, + crimson = { 0.863, 0.078, 0.235 }, + gainsboro = { 0.863, 0.863, 0.863 }, + plum = { 0.867, 0.627, 0.867 }, + burlywood = { 0.871, 0.722, 0.529 }, + lightcyan = { 0.878, 1.000, 1.000 }, + lavender = { 0.902, 0.902, 0.980 }, + darksalmon = { 0.914, 0.588, 0.478 }, + violet = { 0.933, 0.510, 0.933 }, + palegoldenrod = { 0.933, 0.910, 0.667 }, + lightcoral = { 0.941, 0.502, 0.502 }, + khaki = { 0.941, 0.902, 0.549 }, + aliceblue = { 0.941, 0.973, 1.000 }, + honeydew = { 0.941, 1.000, 0.941 }, + azure = { 0.941, 1.000, 1.000 }, + sandybrown = { 0.957, 0.643, 0.376 }, + wheat = { 0.961, 0.871, 0.702 }, + beige = { 0.961, 0.961, 0.863 }, + whitesmoke = { 0.961, 0.961, 0.961 }, + mintcream = { 0.961, 1.000, 0.980 }, + ghostwhite = { 0.973, 0.973, 1.000 }, + salmon = { 0.980, 0.502, 0.447 }, + antiquewhite = { 0.980, 0.922, 0.843 }, + linen = { 0.980, 0.941, 0.902 }, + lightgoldenrodyellow = { 0.980, 0.980, 0.824 }, + oldlace = { 0.992, 0.961, 0.902 }, + red = { 1.000, 0.000, 0.000 }, + fuchsia = { 1.000, 0.000, 1.000 }, + magenta = { 1.000, 0.000, 1.000 }, + deeppink = { 1.000, 0.078, 0.576 }, + orangered = { 1.000, 0.271, 0.000 }, + tomato = { 1.000, 0.388, 0.278 }, + hotpink = { 1.000, 0.412, 0.706 }, + coral = { 1.000, 0.498, 0.314 }, + darkorange = { 1.000, 0.549, 0.000 }, + lightsalmon = { 1.000, 0.627, 0.478 }, + orange = { 1.000, 0.647, 0.000 }, + lightpink = { 1.000, 0.714, 0.757 }, + pink = { 1.000, 0.753, 0.796 }, + gold = { 1.000, 0.843, 0.000 }, + peachpuff = { 1.000, 0.855, 0.725 }, + navajowhite = { 1.000, 0.871, 0.678 }, + moccasin = { 1.000, 0.894, 0.710 }, + bisque = { 1.000, 0.894, 0.769 }, + mistyrose = { 1.000, 0.894, 0.882 }, + blanchedalmond = { 1.000, 0.922, 0.804 }, + papayawhip = { 1.000, 0.937, 0.835 }, + lavenderblush = { 1.000, 0.941, 0.961 }, + seashell = { 1.000, 0.961, 0.933 }, + cornsilk = { 1.000, 0.973, 0.863 }, + lemonchiffon = { 1.000, 0.980, 0.804 }, + floralwhite = { 1.000, 0.980, 0.941 }, + snow = { 1.000, 0.980, 0.980 }, + yellow = { 1.000, 1.000, 0.000 }, + lightyellow = { 1.000, 1.000, 0.878 }, + ivory = { 1.000, 1.000, 0.941 }, + white = { 1.000, 1.000, 1.000 }, +} + +return Color diff --git a/lua/lib/rotLove/rot/dice.lua b/lua/lib/rotLove/rot/dice.lua new file mode 100644 index 0000000..b3fae3b --- /dev/null +++ b/lua/lib/rotLove/rot/dice.lua @@ -0,0 +1,246 @@ +--- A module used to roll and manipulate roguelike based dice +-- Based off the RL-Dice library at https://github.com/timothymtorres/RL-Dice +-- @module ROT.Dice + +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Dice +local Dice = ROT.Class:extend("Dice", { minimum = 1 }) -- class default lowest possible roll is 1 (can set to nil to allow negative rolls) + +--- Constructor that creates a new dice instance +-- @tparam ?int|string dice_notation Can be either a dice string, or int +-- @tparam[opt] int minimum Sets dice instance roll's minimum result boundaries +-- @treturn dice +function Dice:init(dice_notation, minimum) + -- If dice_notation is a number, we must convert it into the proper dice string format + if type(dice_notation) == "number" then + dice_notation = "1d" .. dice_notation + end + + local dice_pattern = "[(]?%d+[d]%d+[+-]?[+-]?%d*[%^]?[+-]?[+-]?%d*[)]?[x]?%d*" + assert(dice_notation == string.match(dice_notation, dice_pattern), "Dice string incorrectly formatted.") + + self.num = tonumber(string.match(dice_notation, "%d+")) + self.faces = tonumber(string.match(dice_notation, "[d](%d+)")) + + local double_bonus = string.match(dice_notation, "[d]%d+([+-]?[+-])%d+") + local bonus = string.match(dice_notation, "[d]%d+[+-]?([+-]%d+)") + self.is_bonus_plural = double_bonus == "++" or double_bonus == "--" + self.bonus = tonumber(bonus) or 0 + + local double_reroll = string.match(dice_notation, "[%^]([+-]?[+-])%d+") + local reroll = string.match(dice_notation, "[%^][+-]?([+-]%d+)") + self.is_reroll_plural = double_reroll == "++" or double_reroll == "--" + self.rerolls = tonumber(reroll) or 0 + + self.sets = tonumber(string.match(dice_notation, "[x](%d+)")) or 1 + + self.minimum = minimum +end + +--- Sets dice minimum result boundaries (if nil, no minimum result) +function Dice:setMin(value) + self.minimum = value +end + +--- Get number of total dice +function Dice:getNum() + return self.num +end + +--- Get number of total faces on a dice +function Dice:getFaces() + return self.faces +end + +--- Get bonus to be added to the dice total +function Dice:getBonus() + return self.bonus +end + +--- Get rerolls to be added to the dice +function Dice:getRerolls() + return self.rerolls +end + +--- Get number of total dice sets +function Dice:getSets() + return self.sets +end + +--- Get bonus to be added to all dice (if double bonus enabled) otherwise regular bonus +function Dice:getTotalBonus() + return (self.is_bonus_plural and self.bonus * self.num) or self.bonus +end + +--- Get rerolls to be added to all dice (if double reroll enabled) otherwise regular reroll +function Dice:getTotalRerolls() + return (self.is_reroll_plural and self.rerolls * self.num) or self.rerolls +end + +--- Returns boolean that checks if all dice are to be rerolled together or individually +function Dice:isDoubleReroll() + return self.is_reroll_plural +end + +--- Returns boolean that checks if all dice are to apply a bonus together or individually +function Dice:isDoubleBonus() + return self.is_bonus_plural +end + +--- Modifies bonus +function Dice:__add(value) + self.bonus = self.bonus + value + return self +end + +--- Modifies bonus +function Dice:__sub(value) + self.bonus = self.bonus - value + return self +end + +--- Modifies number of dice +function Dice:__mul(value) + self.num = self.num + value + return self +end + +--- Modifies amount of dice faces +function Dice:__div(value) + self.faces = self.faces + value + return self +end + +--- Modifies rerolls +function Dice:__pow(value) + self.rerolls = self.rerolls + value + return self +end + +--- Modifies dice sets +function Dice:__mod(value) + self.sets = self.sets + value + return self +end + +--- Returns a formatted dice string in roguelike notation +function Dice:__tostring() + local num_dice, dice_faces, bonus, is_bonus_plural, rerolls, is_reroll_plural, sets = + self.num, self.faces, self.bonus, self.is_bonus_plural, self.rerolls, self.is_reroll_plural, self.sets + + -- num_dice & dice_faces default to 1 if negative or 0! + sets, num_dice, dice_faces = math.max(sets, 1), math.max(num_dice, 1), math.max(dice_faces, 1) + + local double_bonus = is_bonus_plural and (bonus >= 0 and "+" or "-") or "" + bonus = (bonus ~= 0 and double_bonus .. string.format("%+d", bonus)) or "" + + local double_reroll = is_reroll_plural and (rerolls >= 0 and "+" or "-") or "" + rerolls = (rerolls ~= 0 and "^" .. double_reroll .. string.format("%+d", rerolls)) or "" + + if sets > 1 then + return "(" .. num_dice .. "d" .. dice_faces .. bonus .. rerolls .. ")x" .. sets + else + return num_dice .. "d" .. dice_faces .. bonus .. rerolls + end +end + +--- Modifies whether reroll or bonus applies to individual dice or all of them (pluralism_notation string must be one of the following operators `- + ^` The operator may be double signed to indicate pluralism) +function Dice:__concat(pluralism_notation) + local str_b = string.match(pluralism_notation, "[+-][+-]?") or "" + local bonus = ((str_b == "++" or str_b == "--") and "double") + or ((str_b == "+" or str_b == "-") and "single") + or nil + + local str_r = string.match(pluralism_notation, "[%^][%^]?") or "" + local reroll = (str_r == "^^" and "double") or (str_r == "^" and "single") or nil + + if bonus == "double" then + self.is_bonus_plural = true + elseif bonus == "single" then + self.is_bonus_plural = false + end + + if reroll == "double" then + self.is_reroll_plural = true + elseif reroll == "single" then + self.is_reroll_plural = false + end + return self +end + +--- Rolls the dice +-- @tparam ?int|dice|str self +-- @tparam[opt] int minimum +-- @tparam[opt] ROT.RNG rng When called directly as ROT.Dice.roll, is used +-- in call to ROT.Dice.new. Not used when called on an instance of ROT.Dice. +-- +-- i.e.: `ROT.Dice.roll('3d6', 1, rng) -- rng arg used` +-- +-- `d = ROT.Dice:new('3d6', 1); d:roll(nil, rng) -- rng arg not used` +-- +-- +function Dice.roll(self, minimum, rng) + if type(self) ~= "table" then + self = ROT.Dice:new(self, minimum):setRNG(rng) + end + local num_dice, dice_faces = self.num, self.faces + local bonus, rerolls = self.bonus, self.rerolls + local is_bonus_plural, is_reroll_plural = self.is_bonus_plural, self.is_reroll_plural + local sets, minimum = self.sets, self.minimum + + sets = math.max(sets, 1) -- Minimum of 1 needed + local set_rolls = {} + + local bonus_all = is_bonus_plural and bonus or 0 + rerolls = is_reroll_plural and rerolls * num_dice or rerolls + + -- num_dice & dice_faces CANNOT be negative! + num_dice, dice_faces = math.max(num_dice, 1), math.max(dice_faces, 1) + + for i = 1, sets do + local rolls = {} + for ii = 1, num_dice + math.abs(rerolls) do + rolls[ii] = self._rng:random(1, dice_faces) + bonus_all -- if is_bonus_plural then bonus_all gets added to every roll, otherwise bonus_all = 0 + end + + if rerolls ~= 0 then + -- sort and if reroll is + then remove lowest rolls, if reroll is - then remove highest rolls + if rerolls > 0 then + table.sort(rolls, function(a, b) + return a > b + end) + else + table.sort(rolls) + end + for index = num_dice + 1, #rolls do + rolls[index] = nil + end + end + + -- bonus gets added to the last roll if it is not plural + if not is_bonus_plural then + rolls[#rolls] = rolls[#rolls] + bonus + end + + local total = 0 + for _, number in ipairs(rolls) do + total = total + number + end + set_rolls[i] = total + end + + -- if minimum is empty then use dice class default min + if minimum == nil then + minimum = Dice.minimum + end + + if minimum then + for i = 1, sets do + set_rolls[i] = math.max(set_rolls[i], minimum) + end + end + + return unpack(set_rolls) +end + +return Dice diff --git a/lua/lib/rotLove/rot/display.lua b/lua/lib/rotLove/rot/display.lua new file mode 100644 index 0000000..2ccdfd3 --- /dev/null +++ b/lua/lib/rotLove/rot/display.lua @@ -0,0 +1,380 @@ +--- Visual Display. +-- A Code Page 437 terminal emulator based on AsciiPanel. +-- @module ROT.Display +local Display_Path = (...):gsub((".[^./\\]*"):rep(2) .. "$", ""):gsub("[./\\]", "/") .. "/" +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Display +local Display = ROT.Class:extend("Display") + +--- Constructor. +-- The display constructor. Called when ROT.Display:new() is called. +-- @tparam[opt=80] int w Width of display in number of characters +-- @tparam[opt=24] int h Height of display in number of characters +-- @tparam[opt=1] float scale Window scale modifier applied to glyph dimensions +-- @tparam[opt] table dfg Default foreground color as a table defined as {r,g,b,a} +-- @tparam[opt] table dbg Default background color +-- @tparam[opt=false] boolean fullOrFlags In Love 0.8.0: Use fullscreen In Love 0.9.0: a table defined for love.graphics.setMode +-- @tparam[opt=false] boolean vsync Use vsync +-- @tparam[opt=0] int fsaa Number of fsaa passes +-- @return nil +function Display:init(w, h, scale, dfg, dbg, fullOrFlags, vsync, fsaa) + self.__name = "Display" + self.widthInChars = w and w or 80 + self.heightInChars = h and h or 24 + self.scale = scale or 1 + self.charWidth = 9 * self.scale + self.charHeight = 16 * self.scale + self.glyphs = {} + self.chars = { {} } + self.backgroundColors = { {} } + self.foregroundColors = { {} } + self.oldChars = { {} } + self.oldBackgroundColors = { {} } + self.oldForegroundColors = { {} } + self.graphics = love.graphics + if love.window then + love.window.setMode(self.charWidth * self.widthInChars, self.charHeight * self.heightInChars, fullOrFlags) + self.drawQ = self.graphics.draw + else + self.graphics.setMode( + self.charWidth * self.widthInChars, + self.charHeight * self.heightInChars, + fullOrFlags, + vsync, + fsaa + ) + self.drawQ = self.graphics.drawq + end + + self.defaultForegroundColor = dfg and dfg or { 0.9, 0.9, 0.9 } + self.defaultBackgroundColor = dbg and dbg or { 0.1, 0.1, 0.1 } + + self.graphics.setBackgroundColor(self.defaultBackgroundColor) + + self.canvas = self.graphics.newCanvas(self.charWidth * self.widthInChars, self.charHeight * self.heightInChars) + + self.glyphSprite = self.graphics.newImage(Display_Path .. "img/cp437.png") + for i = 0, 255 do + local sx = (i % 32) * 9 + local sy = math.floor(i / 32) * 16 + self.glyphs[i] = self.graphics.newQuad(sx, sy, 9, 16, self.glyphSprite:getWidth(), self.glyphSprite:getHeight()) + end + + for i = 1, self.widthInChars do + self.chars[i] = {} + self.backgroundColors[i] = {} + self.foregroundColors[i] = {} + self.oldChars[i] = {} + self.oldBackgroundColors[i] = {} + self.oldForegroundColors[i] = {} + for j = 1, self.heightInChars do + self.chars[i][j] = 32 + self.backgroundColors[i][j] = self.defaultBackgroundColor + self.foregroundColors[i][j] = self.defaultForegroundColor + self.oldChars[i][j] = nil + self.oldBackgroundColors[i][j] = nil + self.oldForegroundColors[i][j] = nil + end + end +end + +--- Draw. +-- The main draw function. This should be called from love.draw() to display any written characters to screen +function Display:draw() + self.graphics.setCanvas(self.canvas) + for x = 1, self.widthInChars do + for y = 1, self.heightInChars do + local c = self.chars[x][y] + local bg = self.backgroundColors[x][y] + local fg = self.foregroundColors[x][y] + local px = (x - 1) * self.charWidth + local py = (y - 1) * self.charHeight + if + self.oldChars[x][y] ~= c + or self.oldBackgroundColors[x][y] ~= bg + or self.oldForegroundColors[x][y] ~= fg + then + self:_setColor(bg) + self.graphics.rectangle("fill", px, py, self.charWidth, self.charHeight) + if c ~= 32 and c ~= 255 then + local qd = self.glyphs[c] + self:_setColor(fg) + self.drawQ(self.glyphSprite, qd, px, py, nil, self.scale) + end + + self.oldChars[x][y] = c + self.oldBackgroundColors[x][y] = bg + self.oldForegroundColors[x][y] = fg + end + end + end + self.graphics.setCanvas() + self.graphics.setColor(1.0, 1.0, 1.0, 1.0) + self.graphics.draw(self.canvas) +end + +--- Contains point. +-- Returns true if point x,y can be drawn to display. +function Display:contains(x, y) + return x > 0 and x <= self:getWidth() and y > 0 and y <= self:getHeight() +end + +function Display:getCharHeight() + return self.charHeight +end +function Display:getCharWidth() + return self.charWidth +end +function Display:getWidth() + return self:getWidthInChars() +end +function Display:getHeight() + return self:getHeightInChars() +end +function Display:getHeightInChars() + return self.heightInChars +end +function Display:getWidthInChars() + return self.widthInChars +end +function Display:getDefaultBackgroundColor() + return self.defaultBackgroundColor +end +function Display:getDefaultForegroundColor() + return self.defaultForegroundColor +end + +--- Get a character. +-- returns the character being displayed at position x, y +-- @tparam int x The x-position of the character +-- @tparam int y The y-position of the character +-- @treturn string The character +function Display:getCharacter(x, y) + local c = self.chars[x][y] + return c and string.char(c) or nil +end + +--- Get a background color. +-- returns the current background color of the character written to position x, y +-- @tparam int x The x-position of the character +-- @tparam int y The y-position of the character +-- @treturn table The background color as a table defined as {r,g,b,a} +function Display:getBackgroundColor(x, y) + return self.backgroundColors[x][y] +end + +--- Get a foreground color. +-- returns the current foreground color of the character written to position x, y +-- @tparam int x The x-position of the character +-- @tparam int y The y-position of the character +-- @treturn table The foreground color as a table defined as {r,g,b,a} +function Display:getForegroundColor(x, y) + return self.foregroundColors[x][y] +end + +--- Set Default Background Color. +-- Sets the background color to be used when it is not provided +-- @tparam table c The background color as a table defined as {r,g,b,a} +function Display:setDefaultBackgroundColor(c) + self.defaultBackgroundColor = c and c or self.defaultBackgroundColor +end + +--- Set Defaul Foreground Color. +-- Sets the foreground color to be used when it is not provided +-- @tparam table c The foreground color as a table defined as {r,g,b,a} +function Display:setDefaultForegroundColor(c) + self.defaultForegroundColor = c and c or self.defaultForegroundColor +end + +--- Clear the screen. +-- By default wipes the screen to the default background color. +-- You can provide a character, x-position, y-position, width, height, fore-color and back-color +-- and write the same character to a portion of the screen +-- @tparam[opt=' '] string c A character to write to the screen - may fail for strings with a length > 1 +-- @tparam[opt=1] int x The x-position from which to begin the wipe +-- @tparam[opt=1] int y The y-position from which to begin the wipe +-- @tparam[opt] int w The number of chars to wipe in the x direction +-- @tparam[opt] int h Then number of chars to wipe in the y direction +-- @tparam[opt] table fg The color used to write the provided character +-- @tparam[opt] table bg the color used to fill in the background of the cleared space +function Display:clear(c, x, y, w, h, fg, bg) + c = c or " " + w = w or self.widthInChars + local s = c:rep(self.widthInChars) + x = self:_validateX(x, s) + y = self:_validateY(y) + h = self:_validateHeight(y, h) + fg = self:_validateForegroundColor(fg) + bg = self:_validateBackgroundColor(bg) + for i = 0, h - 1 do + self:_writeValidatedString(s, x, y + i, fg, bg) + end +end + +--- Clear canvas. +-- runs the clear method of the Love2D canvas object being used to write to the screen +function Display:clearCanvas() + self.canvas:clear() +end + +--- Write. +-- Writes a string to the screen +-- @tparam string s The string to be written +-- @tparam[opt=1] int x The x-position where the string will be written +-- @tparam[opt=1] int y The y-position where the string will be written +-- @tparam[opt] table fg The color used to write the provided string +-- @tparam[opt] table bg the color used to fill in the string's background +function Display:write(s, x, y, fg, bg) + ROT.assert(s, "Display:write() must have string as param") + x = self:_validateX(x, s) + y = self:_validateY(y, s) + fg = self:_validateForegroundColor(fg) + bg = self:_validateBackgroundColor(bg) + + self:_writeValidatedString(s, x, y, fg, bg) +end + +--- Write Center. +-- write a string centered on the middle of the screen +-- @tparam string s The string to be written +-- @tparam[opt=1] int y The y-position where the string will be written +-- @tparam[opt] table fg The color used to write the provided string +-- @tparam[opt] table bg the color used to fill in the string's background +function Display:writeCenter(s, y, fg, bg) + ROT.assert(s, "Display:writeCenter() must have string as param") + ROT.assert(#s < self.widthInChars, "Length of ", s, " is greater than screen width") + y = y and y or math.floor((self:getHeightInChars() - 1) / 2) + y = self:_validateY(y, s) + fg = self:_validateForegroundColor(fg) + bg = self:_validateBackgroundColor(bg) + + local x = math.floor((self.widthInChars - #s) / 2) + self:_writeValidatedString(s, x, y, fg, bg) +end + +function Display:_writeValidatedString(s, x, y, fg, bg) + for i = 1, #s do + self.backgroundColors[x + i - 1][y] = bg + self.foregroundColors[x + i - 1][y] = fg + self.chars[x + i - 1][y] = s:byte(i) + end +end + +function Display:_validateX(x, s) + x = x and x or 1 + ROT.assert(x > 0 and x <= self.widthInChars, "X value must be between 0 and ", self.widthInChars) + ROT.assert( + (x + #s) - 1 <= self.widthInChars, + "X value plus length of String must be between 0 and ", + self.widthInChars + ) + return x +end +function Display:_validateY(y) + y = y and y or 1 + ROT.assert(y > 0 and y <= self.heightInChars, "Y value must be between 0 and ", self.heightInChars) + return y +end +function Display:_validateForegroundColor(c) + c = c or self.defaultForegroundColor + ROT.assert(#c > 2, "Foreground Color must have at least 3 components") + for i = 1, #c do + c[i] = self:_clamp(c[i]) + end + return c +end +function Display:_validateBackgroundColor(c) + c = c or self.defaultBackgroundColor + ROT.assert(#c > 2, "Background Color must have at least 3 components") + for i = 1, #c do + c[i] = self:_clamp(c[i]) + end + return c +end +function Display:_validateHeight(y, h) + h = h and h or self.heightInChars - y + 1 + ROT.assert(h > 0, "Height must be greater than 0. Height provided: ", h) + ROT.assert( + y + h - 1 <= self.heightInChars, + "Height + y value must be less than screen height. y, height: ", + y, + ", ", + h + ) + return h +end +function Display:_setColor(c) + love.graphics.setColor(c or self.defaultForegroundColor) +end +function Display:_clamp(n) + return n < 0 and 0 or n > 1.0 and 1.0 or n +end + +--- Draw text. +-- Draws a text at given position. Optionally wraps at a maximum length. +-- @tparam number x +-- @tparam number y +-- @tparam string text May contain color/background format specifiers, %c{name}/%b{name}, both optional. %c{}/%b{} resets to default. +-- @tparam number maxWidth wrap at what width (optional)? +-- @treturn number lines drawn +function Display:drawText(x, y, text, maxWidth) + local fg + local bg + local cx = x + local cy = y + local lines = 1 + if not maxWidth then + maxWidth = self.widthInChars - x + end + + local tokens = ROT.Text.tokenize(text, maxWidth) + + while #tokens > 0 do -- interpret tokenized opcode stream + local token = table.remove(tokens, 1) + if token.type == ROT.Text.TYPE_TEXT then + local isSpace, isPrevSpace, isFullWidth, isPrevFullWidth + for i = 1, #token.value do + local cc = token.value:byte(i) + local c = token.value:sub(i, i) + -- TODO: chars will never be full-width without special handling + -- TODO: ... so the next 15 lines or so do some pointless stuff + -- Assign to `true` when the current char is full-width. + isFullWidth = (cc > 0xff00 and cc < 0xff61) or (cc > 0xffdc and cc < 0xffe8) or cc > 0xffee + -- Current char is space, whatever full-width or half-width both are OK. + isSpace = c:byte() == 0x20 or c:byte() == 0x3000 + -- The previous char is full-width and + -- current char is nether half-width nor a space. + if isPrevFullWidth and not isFullWidth and not isSpace then + cx = cx + 1 -- add an extra position + end + -- The current char is full-width and + -- the previous char is not a space. + if isFullWidth and not isPrevSpace then + cx = cx + 1 -- add an extra position + end + fg = (fg == "" or not fg) and self.defaultForegroundColor + or type(fg) == "string" and ROT.Color.fromString(fg) + or fg + bg = (bg == "" or not bg) and self.defaultBackgroundColor + or type(bg) == "string" and ROT.Color.fromString(bg) + or bg + self:_writeValidatedString(c, cx, cy, fg, bg) + cx = cx + 1 + isPrevSpace = isSpace + isPrevFullWidth = isFullWidth + end + elseif token.type == ROT.Text.TYPE_FG then + fg = token.value or nil + elseif token.type == ROT.Text.TYPE_BG then + bg = token.value or nil + elseif token.type == ROT.Text.TYPE_NEWLINE then + cx = x + cy = cy + 1 + lines = lines + 1 + end + end + + return lines +end + +return Display diff --git a/lua/lib/rotLove/rot/engine.lua b/lua/lib/rotLove/rot/engine.lua new file mode 100644 index 0000000..356c305 --- /dev/null +++ b/lua/lib/rotLove/rot/engine.lua @@ -0,0 +1,31 @@ +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Engine +local Engine = ROT.Class:extend("Engine") + +function Engine:init(scheduler) + self._scheduler = scheduler + self._lock = 1 +end + +function Engine:start() + return self:unlock() +end + +function Engine:lock() + self._lock = self._lock + 1 +end + +function Engine:unlock() + assert(self._lock > 0, "Cannot unlock unlocked Engine") + self._lock = self._lock - 1 + while self._lock < 1 do + local actor = self._scheduler:next() + if not actor then + return self:lock() + end + actor:act() + end + return self +end + +return Engine diff --git a/lua/lib/rotLove/rot/eventQueue.lua b/lua/lib/rotLove/rot/eventQueue.lua new file mode 100644 index 0000000..67e93b7 --- /dev/null +++ b/lua/lib/rotLove/rot/eventQueue.lua @@ -0,0 +1,95 @@ +--- Stores and retrieves events based on time. +-- @module ROT.EventQueue +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class EventQueue +local EventQueue = ROT.Class:extend("EventQueue") + +function EventQueue:init() + self._time = 0 + self._events = {} + self._eventTimes = {} +end + +--- Get Time. +-- Get time counted since start +-- @treturn int elapsed time +function EventQueue:getTime() + return self._time +end + +--- Clear. +-- Remove all events from queue +-- @treturn ROT.EventQueue self +function EventQueue:clear() + self._events = {} + self._eventTimes = {} + return self +end + +--- Add. +-- Add an event +-- @tparam any event Any object +-- @tparam int time The number of time units that will elapse before this event is returned +function EventQueue:add(event, time) + local index = 1 + if self._eventTimes then + for i = 1, #self._eventTimes do + if self._eventTimes[i] > time then + index = i + break + end + index = i + 1 + end + end + table.insert(self._events, index, event) + table.insert(self._eventTimes, index, time) +end + +--- Get. +-- Get the next event from the queue and advance the appropriate amount time +-- @treturn event|nil The event previously added by .add() or nil if none are queued +function EventQueue:get() + if #self._events < 1 then + return nil + end + local time = table.remove(self._eventTimes, 1) + if time > 0 then + self._time = self._time + time + for i = 1, #self._eventTimes do + self._eventTimes[i] = self._eventTimes[i] - time + end + end + return table.remove(self._events, 1) +end + +--- Get event time. +-- Get the time associated with the given event +-- @tparam any event +-- @treturn number time +function EventQueue:getEventTime(event) + local index = table.indexOf(self._events, event) + if index == 0 then + return nil + end + return self._eventTimes[index] +end + +--- Remove. +-- Find and remove an event from the queue +-- @tparam any event The previously added event to be removed +-- @treturn boolean true if an event was removed from the queue +function EventQueue:remove(event) + local index = table.indexOf(self._events, event) + if index == 0 then + return false + end + self:_remove(index) + return true +end + +function EventQueue:_remove(index) + table.remove(self._events, index) + table.remove(self._eventTimes, index) +end + +return EventQueue diff --git a/lua/lib/rotLove/rot/fov.lua b/lua/lib/rotLove/rot/fov.lua new file mode 100644 index 0000000..2ee4110 --- /dev/null +++ b/lua/lib/rotLove/rot/fov.lua @@ -0,0 +1,61 @@ +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class FOV +local FOV = ROT.Class:extend("FOV") + +function FOV:init(lightPassesCallback, options) + self._lightPasses = lightPassesCallback + self._options = { topology = 8 } + if options then + for k, _ in pairs(options) do + self._options[k] = options[k] + end + end +end + +function FOV:compute() end + +function FOV:_getCircle(cx, cy, r) + local result = {} + local dirs, countFactor, startOffset + local topo = self._options.topology + if topo == 4 then + countFactor = 1 + startOffset = { 0, 1 } + dirs = { + ROT.DIRS.EIGHT[8], + ROT.DIRS.EIGHT[2], + ROT.DIRS.EIGHT[4], + ROT.DIRS.EIGHT[6], + } + elseif topo == 8 then + dirs = ROT.DIRS.FOUR + countFactor = 2 + startOffset = { -1, 1 } + end + + local x = cx + startOffset[1] * r + local y = cy + startOffset[2] * r + + for i = 1, #dirs do + for _ = 1, r * countFactor do + table.insert(result, { x, y }) + x = x + dirs[i][1] + y = y + dirs[i][2] + end + end + return result +end + +function FOV:_getRealCircle(cx, cy, r) + local i = 0 + local result = {} + while i < 2 * math.pi do + i = i + 0.05 + local x = cx + r * math.cos(i) + local y = cy + r * math.sin(i) + table.insert(result, { x, y }) + end + return result +end + +return FOV diff --git a/lua/lib/rotLove/rot/fov/bresenham.lua b/lua/lib/rotLove/rot/fov/bresenham.lua new file mode 100644 index 0000000..1930679 --- /dev/null +++ b/lua/lib/rotLove/rot/fov/bresenham.lua @@ -0,0 +1,239 @@ +--- Bresenham Based Ray-Casting FOV calculator. +-- See http://en.wikipedia.org/wiki/Bresenham's_line_algorithm. +-- Included for sake of having options. Provides three functions for computing FOV +-- @module ROT.FOV.Bresenham +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Bresenham +local Bresenham = ROT.FOV:extend("Bresenham") + +-- internal Point class + +local Point = ROT.Class:extend("Point") + +function Point:init(x, y) + self.isPoint = true + self.x = x + self.y = y +end + +function Point:hashCode() + local prime = 31 + local result = 1 + result = prime * result + self.x + result = prime * result + self.y + return result +end + +function Point:equals(other) + if self == other then + return true + end + if other == nil or not other.isPoint or (other.x and other.x ~= self.x) or (other.y and other.y ~= self.y) then + return false + end + return true +end + +function Point:adjacentPoints() + local points = {} + local i = 1 + for ox = -1, 1 do + for oy = -1, 1 do + points[i] = Point(self.x + ox, self.y + oy) + i = i + 1 + end + end + return points +end + +-- internal Line class + +local Line = ROT.Class:extend("Line") + +function Line:init(x1, y1, x2, y2) + self.x1 = x1 + self.y1 = y1 + self.x2 = x2 + self.y2 = y2 + self.points = {} +end +function Line:getPoints() + local dx = math.abs(self.x2 - self.x1) + local dy = math.abs(self.y2 - self.y1) + local sx = self.x1 < self.x2 and 1 or -1 + local sy = self.y1 < self.y2 and 1 or -1 + local err = dx - dy + + while true do + table.insert(self.points, Point(self.x1, self.y1)) + if self.x1 == self.x2 and self.y1 == self.y2 then + break + end + local e2 = err * 2 + if e2 > -dx then + err = err - dy + self.x1 = self.x1 + sx + end + if e2 < dx then + err = err + dx + self.y1 = self.y1 + sy + end + end + return self +end + +--- Constructor. +-- Called with ROT.FOV.Bresenham:new() +-- @tparam function lightPassesCallback A function with two parameters (x, y) that returns true if a map cell will allow light to pass through +-- @tparam table options Options +-- @tparam int options.topology Direction for light movement Accepted values: (4 or 8) +-- @tparam boolean options.useDiamond If true, the FOV will be a diamond shape as opposed to a circle shape. +function Bresenham:init(lightPassesCallback, options) + Bresenham.super.init(self, lightPassesCallback, options) +end + +--- Compute. +-- Get visibility from a given point. +-- This method cast's rays from center to points on a circle with a radius 3-units longer than the provided radius. +-- A list of cell's within the radius is kept. This list is checked at the end to verify that each cell has been passed to the callback. +-- @tparam int cx x-position of center of FOV +-- @tparam int cy y-position of center of FOV +-- @tparam int r radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam number callback.visibility The cell's visibility rating (from 0-1). How well can you see this cell? +function Bresenham:compute(cx, cy, r, callback) + local notvisited = {} + for x = -r, r do + for y = -r, r do + notvisited[Point(cx + x, cy + y):hashCode()] = { cx + x, cy + y } + end + end + + callback(cx, cy, 1, 1) + notvisited[Point(cx, cy):hashCode()] = nil + + local thePoints = self:_getCircle(cx, cy, r + 3) + for _, p in pairs(thePoints) do + local x, y = p[1], p[2] + local line = Line(cx, cy, x, y):getPoints() + for i = 2, #line.points do + local point = line.points[i] + if self:_oob(cx - point.x, cy - point.y, r) then + break + end + if notvisited[point:hashCode()] then + callback(point.x, point.y, i, 1 - (i / r)) + notvisited[point:hashCode()] = nil + end + if not self:_lightPasses(point.x, point.y) then + break + end + end + end + + for _, v in pairs(notvisited) do + local x, y = v[1], v[2] + local line = Line(cx, cy, x, y):getPoints() + for i = 2, #line.points do + local point = line.points[i] + if self:_oob(cx - point.x, cy - point.y, r) then + break + end + if notvisited[point:hashCode()] then + callback(point.x, point.y, i, 1 - (i / r)) + notvisited[point:hashCode()] = nil + end + if not self:_lightPasses(point.x, point.y) then + break + end + end + end +end + +--- Compute Thorough. +-- Get visibility from a given point. +-- This method cast's rays from center to every cell within the given radius. +-- This method is much slower, but is more likely to not generate any anomalies within the field. +-- @tparam int cx x-position of center of FOV +-- @tparam int cy y-position of center of FOV +-- @tparam int r radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam number callback.visibility The cell's visibility rating (from 0-1). How well can you see this cell? +function Bresenham:computeThorough(cx, cy, r, callback) + local visited = {} + callback(cx, cy, r) + visited[Point(cx, cy):hashCode()] = 0 + for x = -r, r do + for y = -r, r do + local line = Line(cx, cy, x + cx, y + cy):getPoints() + for i = 2, #line.points do + local point = line.points[i] + if self:_oob(cx - point.x, cy - point.y, r) then + break + end + if not visited[point:hashCode()] then + callback(point.x, point.y, r) + visited[point:hashCode()] = 0 + end + if not self:_lightPasses(point.x, point.y) then + break + end + end + end + end +end + +--- Compute Thorough. +-- Get visibility from a given point. The quickest method provided. +-- This method cast's rays from center to points on a circle with a radius 3-units longer than the provided radius. +-- Unlike compute() this method stops at that point. It will likely miss cell's for fields with a large radius. +-- @tparam int cx x-position of center of FOV +-- @tparam int cy y-position of center of FOV +-- @tparam int r radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam number callback.visibility The cell's visibility rating (from 0-1). How well can you see this cell? +function Bresenham:computeQuick(cx, cy, r, callback) + local visited = {} + callback(cx, cy, 1, 1) + visited[Point(cx, cy):hashCode()] = 0 + + local thePoints = self:_getCircle(cx, cy, r + 3) + for _, p in pairs(thePoints) do + local x, y = p[1], p[2] + local line = Line(cx, cy, x, y):getPoints() + for i = 2, #line.points do + local point = line.points[i] + if self:_oob(cx - point.x, cy - point.y, r) then + break + end + if not visited[point:hashCode()] then + callback(point.x, point.y, i, 1 - (i * i) / (r * r)) + visited[point:hashCode()] = 0 + end + if not self:_lightPasses(point.x, point.y) then + break + end + end + end +end + +function Bresenham:_oob(x, y, r) + if not self._options.useDiamond then + local ab = ((x * x) + (y * y)) + local c = (r * r) + return ab > c + else + return math.abs(x) + math.abs(y) > r + end +end + +return Bresenham diff --git a/lua/lib/rotLove/rot/fov/precise.lua b/lua/lib/rotLove/rot/fov/precise.lua new file mode 100644 index 0000000..825266e --- /dev/null +++ b/lua/lib/rotLove/rot/fov/precise.lua @@ -0,0 +1,157 @@ +--- Precise Shadowcasting Field of View calculator. +-- The Precise shadow casting algorithm developed by Ondřej Žára for rot.js. +-- See http://roguebasin.roguelikedevelopment.org/index.php?title=Precise_Shadowcasting_in_JavaScript +-- @module ROT.FOV.Precise +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Precise +local Precise = ROT.FOV:extend("Precise") +--- Constructor. +-- Called with ROT.FOV.Precise:new() +-- @tparam function lightPassesCallback A function with two parameters (x, y) that returns true if a map cell will allow light to pass through +-- @tparam table options Options +-- @tparam int options.topology Direction for light movement Accepted values: (4 or 8) +function Precise:init(lightPassesCallback, options) + Precise.super.init(self, lightPassesCallback, options) +end + +--- Compute. +-- Get visibility from a given point +-- @tparam int x x-position of center of FOV +-- @tparam int y y-position of center of FOV +-- @tparam int R radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam number callback.visibility The cell's visibility rating (from 0-1). How well can you see this cell? +function Precise:compute(x, y, R, callback) + callback(x, y, 0, 1) + local SHADOWS = {} + + local blocks, A1, A2, visibility + + for r = 1, R do + local neighbors = self:_getCircle(x, y, r) + local neighborCount = #neighbors + + for i = 0, neighborCount - 1 do + local cx = neighbors[i + 1][1] + local cy = neighbors[i + 1][2] + A1 = { i > 0 and 2 * i - 1 or 2 * neighborCount - 1, 2 * neighborCount } + A2 = { 2 * i + 1, 2 * neighborCount } + + blocks = not self:_lightPasses(cx, cy) + visibility = self:_checkVisibility(A1, A2, blocks, SHADOWS) + if visibility > 0 then + callback(cx, cy, r, visibility) + end + if #SHADOWS == 2 and SHADOWS[1][1] == 0 and SHADOWS[2][1] == SHADOWS[2][2] then + break + end + end + end +end + +local function splice(t, i, rn, it) -- table, index, numberToRemove, insertTable + if rn > 0 then + for _ = 1, rn do + table.remove(t, i) + end + end + if it and #it > 0 then + for idx = i, i + #it - 1 do + local el = table.remove(it, 1) + if el then + table.insert(t, idx, el) + end + end + end +end + +function Precise:_checkVisibility(A1, A2, blocks, SHADOWS) + if A1[1] > A2[1] then + local v1 = self:_checkVisibility(A1, { A1[2], A1[2] }, blocks, SHADOWS) + local v2 = self:_checkVisibility({ 0, 1 }, A2, blocks, SHADOWS) + return (v1 + v2) / 2 + end + local index1 = 1 + local edge1 = false + while index1 <= #SHADOWS do + local old = SHADOWS[index1] + local diff = old[1] * A1[2] - A1[1] * old[2] + if diff >= 0 then + if diff == 0 and index1 % 2 == 1 then + edge1 = true + end + break + end + index1 = index1 + 1 + end + + local index2 = #SHADOWS + local edge2 = false + while index2 > 0 do + local old = SHADOWS[index2] + local diff = A2[1] * old[2] - old[1] * A2[2] + if diff >= 0 then + if diff == 0 and index2 % 2 == 0 then + edge2 = true + end + break + end + index2 = index2 - 1 + end + local visible = true + if index1 == index2 and (edge1 or edge2) then + visible = false + elseif edge1 and edge2 and index1 + 1 == index2 and index2 % 2 == 0 then + visible = false + elseif index1 > index2 and index1 % 2 == 0 then + visible = false + end + if not visible then + return 0 + end + local visibleLength = 0 + local remove = index2 - index1 + 1 + if remove % 2 == 1 then + if index1 % 2 == 0 then + if #SHADOWS > 0 then + local P = SHADOWS[index1] + visibleLength = (A2[1] * P[2] - P[1] * A2[2]) / (P[2] * A2[2]) + end + if blocks then + splice(SHADOWS, index1, remove, { A2 }) + end + else + if #SHADOWS > 0 then + local P = SHADOWS[index2] + visibleLength = (P[1] * A1[2] - A1[1] * P[2]) / (A1[2] * P[2]) + end + if blocks then + splice(SHADOWS, index1, remove, { A1 }) + end + end + else + if index1 % 2 == 0 then + if #SHADOWS > 0 then + local P1 = SHADOWS[index1] + local P2 = SHADOWS[index2] + visibleLength = (P2[1] * P1[2] - P1[1] * P2[2]) / (P1[2] * P2[2]) + end + if blocks then + splice(SHADOWS, index1, remove) + end + else + if blocks then + splice(SHADOWS, index1, remove, { A1, A2 }) + end + return 1 + end + end + + local arcLength = (A2[1] * A1[2] - A1[1] * A2[2]) / (A1[2] * A2[2]) + return visibleLength / arcLength +end + +return Precise diff --git a/lua/lib/rotLove/rot/fov/recursive.lua b/lua/lib/rotLove/rot/fov/recursive.lua new file mode 100644 index 0000000..03328f7 --- /dev/null +++ b/lua/lib/rotLove/rot/fov/recursive.lua @@ -0,0 +1,147 @@ +--- Recursive Shadowcasting Field of View calculator. +-- The Recursive shadow casting algorithm developed by Ondřej Žára for rot.js. +-- See http://roguebasin.roguelikedevelopment.org/index.php?title=Recursive_Shadowcasting_in_JavaScript +-- @module ROT.FOV.Recursive +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Recursive +local Recursive = ROT.FOV:extend("Recursive") +--- Constructor. +-- Called with ROT.FOV.Recursive:new() +-- @tparam function lightPassesCallback A function with two parameters (x, y) that returns true if a map cell will allow light to pass through +-- @tparam table options Options +-- @tparam int options.topology Direction for light movement Accepted values: (4 or 8) +function Recursive:init(lightPassesCallback, options) + Recursive.super.init(self, lightPassesCallback, options) +end + +Recursive._octants = { + { -1, 0, 0, 1 }, + { 0, -1, 1, 0 }, + { 0, -1, -1, 0 }, + { -1, 0, 0, -1 }, + { 1, 0, 0, -1 }, + { 0, 1, -1, 0 }, + { 0, 1, 1, 0 }, + { 1, 0, 0, 1 }, +} + +--- Compute. +-- Get visibility from a given point +-- @tparam int x x-position of center of FOV +-- @tparam int y y-position of center of FOV +-- @tparam int R radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam boolean callback.visibility Indicates if the cell is seen +function Recursive:compute(x, y, R, callback) + callback(x, y, 0, true) + for i = 1, #self._octants do + self:_renderOctant(x, y, self._octants[i], R, callback) + end +end + +--- Compute 180. +-- Get visibility from a given point for a 180 degree arc +-- @tparam int x x-position of center of FOV +-- @tparam int y y-position of center of FOV +-- @tparam int R radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam int dir viewing direction (use ROT.DIR index for values) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam boolean callback.visibility Indicates if the cell is seen +function Recursive:compute180(x, y, R, dir, callback) + callback(x, y, 0, true) + local prev = ((dir - 2 + 8) % 8) + 1 + local nPre = ((dir - 3 + 8) % 8) + 1 + local next = ((dir + 8) % 8) + 1 + + self:_renderOctant(x, y, self._octants[nPre], R, callback) + self:_renderOctant(x, y, self._octants[prev], R, callback) + self:_renderOctant(x, y, self._octants[dir], R, callback) + self:_renderOctant(x, y, self._octants[next], R, callback) +end +--- Compute 90. +-- Get visibility from a given point for a 90 degree arc +-- @tparam int x x-position of center of FOV +-- @tparam int y y-position of center of FOV +-- @tparam int R radius of FOV (i.e.: At most, I can see for R cells) +-- @tparam int dir viewing direction (use ROT.DIR index for values) +-- @tparam function callback A function that is called for every cell in view. Must accept four parameters. +-- @tparam int callback.x x-position of cell that is in view +-- @tparam int callback.y y-position of cell that is in view +-- @tparam int callback.r The cell's distance from center of FOV +-- @tparam boolean callback.visibility Indicates if the cell is seen +function Recursive:compute90(x, y, R, dir, callback) + callback(x, y, 0, true) + local prev = ((dir - 2 + 8) % 8) + 1 + + self:_renderOctant(x, y, self._octants[dir], R, callback) + self:_renderOctant(x, y, self._octants[prev], R, callback) +end + +function Recursive:_renderOctant(x, y, octant, R, callback) + self:_castVisibility(x, y, 1, 1.0, 0.0, R + 1, octant[1], octant[2], octant[3], octant[4], callback) +end + +function Recursive:_castVisibility(startX, startY, row, visSlopeStart, visSlopeEnd, radius, xx, xy, yx, yy, callback) + if visSlopeStart < visSlopeEnd then + return + end + for i = row, radius do + local dx = -i - 1 + local dy = -i + local blocked = false + local newStart = 0 + + while dx <= 0 do + dx = dx + 1 + local slopeStart = (dx - 0.5) / (dy + 0.5) + local slopeEnd = (dx + 0.5) / (dy - 0.5) + + if slopeEnd <= visSlopeStart then + if slopeStart < visSlopeEnd then + break + end + local mapX = startX + dx * xx + dy * xy + local mapY = startY + dx * yx + dy * yy + + if dx * dx + dy * dy < radius * radius then + callback(mapX, mapY, i, true) + end + if not blocked then + if not self:_lightPasses(mapX, mapY) and i < radius then + blocked = true + self:_castVisibility( + startX, + startY, + i + 1, + visSlopeStart, + slopeStart, + radius, + xx, + xy, + yx, + yy, + callback + ) + newStart = slopeEnd + end + elseif not self:_lightPasses(mapX, mapY) then + newStart = slopeEnd + else + blocked = false + visSlopeStart = newStart + end + end + end + if blocked then + break + end + end +end + +return Recursive diff --git a/lua/lib/rotLove/rot/lighting.lua b/lua/lib/rotLove/rot/lighting.lua new file mode 100644 index 0000000..83ad45d --- /dev/null +++ b/lua/lib/rotLove/rot/lighting.lua @@ -0,0 +1,161 @@ +--- Lighting Calculator. +-- based on a traditional FOV for multiple light sources and multiple passes. +-- @module ROT.Lighting +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Lighting +local Lighting = ROT.Class:extend("Lighting") + +local PointSet = ROT.Type.PointSet +local Grid = ROT.Type.Grid + +--- Constructor. +-- @tparam function reflectivityCallback Callback to retrieve cell reflectivity must return float(0..1) +-- @tparam int reflectivityCallback.x x-position of cell +-- @tparam int reflectivityCallback.y y-position of cell +-- @tparam table options Options +-- @tparam[opt=1] int options.passes Number of passes. 1 equals to simple FOV of all light sources, >1 means a *highly simplified* radiosity-like algorithm. +-- @tparam[opt=100] int options.emissionThreshold Cells with emissivity > threshold will be treated as light source in the next pass. +-- @tparam[opt=10] int options.range Max light range +function Lighting:init(reflectivityCallback, options) + self._reflectivityCallback = reflectivityCallback + self._options = { passes = 1, emissionThreshold = 100, range = 10 } + self._fov = nil + self._lights = Grid() + self._reflectivityCache = Grid() + self._fovCache = Grid() + + if options then + for k, _ in pairs(options) do + self._options[k] = options[k] + end + end +end + +--- Set FOV +-- Set the Field of View algorithm used to calculate light emission +-- @tparam userdata fov Class/Module used to calculate fov Must have compute(x, y, range, cb) method. Typically you would supply ROT.FOV.Precise:new() here. +-- @treturn ROT.Lighting self +-- @see ROT.FOV.Precise +-- @see ROT.FOV.Bresenham +function Lighting:setFOV(fov) + self._fov = fov + self._fovCache = Grid() + return self +end + +--- Add or remove a light source +-- @tparam int x x-position of light source +-- @tparam int y y-position of light source +-- @tparam nil|string|table color An string accepted by Color:fromString(str) or a color table. A nil value here will remove the light source at x, y +-- @treturn ROT.Lighting self +-- @see ROT.Color +function Lighting:setLight(x, y, color) + self._lights:setCell(x, y, type(color) == "string" and ROT.Color.fromString(color) or color or nil) + return self +end + +--- Compute. +-- Compute the light sources and lit cells +-- @tparam function lightingCallback Will be called with (x, y, color) for every lit cell +-- @treturn ROT.Lighting self +function Lighting:compute(lightingCallback) + local doneCells = PointSet() + local emittingCells = Grid() + local litCells = Grid() + + for _, x, y, light in self._lights:each() do + local emitted = emittingCells:getCell(x, y) + if not emitted then + emitted = { 0, 0, 0 } + emittingCells:setCell(x, y, emitted) + end + ROT.Color.add_(emitted, light) + end + + for i = 1, self._options.passes do + self:_emitLight(emittingCells, litCells, doneCells) + if i < self._options.passes then + emittingCells = self:_computeEmitters(litCells, doneCells) + end + end + + for _, x, y, value in litCells:each() do + lightingCallback(x, y, value) + end + + return self +end + +function Lighting:_emitLight(emittingCells, litCells, doneCells) + for _, x, y, v in emittingCells:each() do + self:_emitLightFromCell(x, y, v, litCells) + doneCells:push(x, y) + end + return self +end + +function Lighting:_computeEmitters(litCells, doneCells) + local result = Grid() + if not litCells then + return nil + end + for _, x, y, color in litCells:each() do + if not doneCells:find(x, y) then + local reflectivity = self._reflectivityCache:getCell(x, y) + if not reflectivity then + reflectivity = self:_reflectivityCallback(x, y) + self._reflectivityCache:setCell(x, y, reflectivity) + end + + if reflectivity > 0 then + local emission = {} + local intensity = 0 + for l, c in ipairs(color) do + if l < 4 then + local part = c * reflectivity + emission[l] = part + intensity = intensity + part + end + end + if intensity > self._options.emissionThreshold then + result:setCell(x, y, emission) + end + end + end + end + + return result +end + +function Lighting:_emitLightFromCell(x, y, color, litCells) + local fov = self._fovCache:getCell(x, y) or self:_updateFOV(x, y) + for _, x, y, formFactor in fov:each() do + local cellColor = litCells:getCell(x, y) + if not cellColor then + cellColor = { 0, 0, 0 } + litCells:setCell(x, y, cellColor) + end + for l = 1, 3 do + cellColor[l] = cellColor[l] + (color[l] * formFactor) + end + end + return self +end + +function Lighting:_updateFOV(x, y) + local cache = Grid() + self._fovCache:setCell(x, y, cache) + local range = self._options.range + local function cb(x, y, r, vis) + local formFactor = vis * (1 - r / range) + if formFactor == 0 then + return + end + cache:setCell(x, y, formFactor) + end + self._fov:compute(x, y, range, cb) + + return cache +end + +return Lighting diff --git a/lua/lib/rotLove/rot/map.lua b/lua/lib/rotLove/rot/map.lua new file mode 100644 index 0000000..a63d1a8 --- /dev/null +++ b/lua/lib/rotLove/rot/map.lua @@ -0,0 +1,27 @@ +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) + +---@class Map:Class +local Map = ROT.Class:extend("Map") + +Map.DEFAULT_WIDTH = 80 +Map.DEFAULT_HEIGHT = 24 + +function Map:init(width, height) + self._width = width or Map.DEFAULT_WIDTH + self._height = height or Map.DEFAULT_HEIGHT +end + +function Map:create() end + +function Map:_fillMap(value) + local map = {} + for x = 1, self._width do + map[x] = {} + for y = 1, self._height do + map[x][y] = value + end + end + return map +end + +return Map diff --git a/lua/lib/rotLove/rot/map/arena.lua b/lua/lib/rotLove/rot/map/arena.lua new file mode 100644 index 0000000..23daf7c --- /dev/null +++ b/lua/lib/rotLove/rot/map/arena.lua @@ -0,0 +1,35 @@ +--- The Arena map generator. +-- Generates an arena style map. All cells except for the extreme borders are floors. The borders are walls. +-- @module ROT.Map.Arena +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Arena +local Arena = ROT.Map:extend("Arena") +--- Constructor. +-- Called with ROT.Map.Arena:new(width, height) +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +function Arena:init(width, height) + Arena.super.init(self, width, height) +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.Arena self +function Arena:create(callback) + local w, h = self._width, self._height + if not callback then + return self + end + for y = 1, h do + for x = 1, w do + callback(x, y, x > 1 and y > 1 and x < w and y < h and 0 or 1) + end + end + return self +end + +return Arena diff --git a/lua/lib/rotLove/rot/map/brogue.lua b/lua/lib/rotLove/rot/map/brogue.lua new file mode 100644 index 0000000..55c4abe --- /dev/null +++ b/lua/lib/rotLove/rot/map/brogue.lua @@ -0,0 +1,344 @@ +--- The Brogue Map Generator. +-- Based on the description of Brogues level generation at http://brogue.wikia.com/wiki/Level_Generation +-- @module ROT.Map.Brogue +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Brogue +local Brogue = ROT.Map.Dungeon:extend("Brogue") + +local PointSet = ROT.Type.PointSet + +--- Constructor. +-- Called with ROT.Map.Brogue:new(). A note: Brogue's map is 79x29. Consider using those dimensions for Display if you're looking to build a brogue-like. +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +-- @tparam[opt] table options Options +-- @tparam[opt={4,20}] table options.roomWidth Room width for rectangle one of cross rooms +-- @tparam[opt={3,7}] table options.roomHeight Room height for rectangle one of cross rooms +-- @tparam[opt={3,12}] table options.crossWidth Room width for rectangle two of cross rooms +-- @tparam[opt={2,5}] table options.crossHeight Room height for rectangle two of cross rooms +-- @tparam[opt={3,12}] table options.corridorWidth Length of east-west corridors +-- @tparam[opt={2,5}] table options.corridorHeight Length of north-south corridors +function Brogue:init(width, height, options) + Brogue.super.init(self, width, height) + + self._digCallback = self:bind(self._digCallback) + self._canBeDugCallback = self:bind(self._canBeDugCallback) + self._isWallCallback = self:bind(self._isWallCallback) + + self._options = { + roomWidth = { 4, 20 }, + roomHeight = { 3, 7 }, + crossWidth = { 3, 12 }, + crossHeight = { 2, 5 }, + corridorWidth = { 2, 12 }, + corridorHeight = { 2, 5 }, + caveChance = 0.33, + corridorChance = 0.8, + } + + if options then + for k, v in pairs(options) do + self._options[k] = v + end + end + + self._walls = PointSet():setRNG(self._rng) + self._rooms = {} + self._loops = 30 + self._loopAttempts = 300 + self._maxrooms = 99 + self._roomAttempts = 600 + self._dirs = ROT.DIRS.FOUR +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall, 2==door +-- @tparam boolean firstFloorBehavior If true will put an upside T (9x10v and 20x4h) at the bottom center of the map. +-- @treturn ROT.Map.Brogue self +function Brogue:create(callback, firstFloorBehavior) + self._map = self:_fillMap(1) + self._rooms = {} + self._walls = PointSet():setRNG(self._rng) + + self:_buildFirstRoom(firstFloorBehavior) + self:_generateRooms() + self:_generateLoops() + self:_closeDiagonalOpenings() + self:_addDoors() + + if not callback then + return self + end + for y = 1, self._height do + for x = 1, self._width do + callback(x, y, self._map[x][y]) + end + end + return self +end + +function Brogue:_buildFirstRoom(firstFloorBehavior) + while true do + if firstFloorBehavior then + local room = ROT.Map.BrogueRoom:createEntranceRoom(self._width, self._height) + if room:isValid(self._isWallCallback, self._canBeDugCallback) then + table.insert(self._rooms, room) + room:create(self._digCallback) + self:_insertWalls(room._walls) + return room + end + elseif self._rng:random() < self._options.caveChance then + return self:_buildCave() + else + local room = ROT.Map.BrogueRoom:createRandom(self._width, self._height, self._options, self._rng) + if room:isValid(self._isWallCallback, self._canBeDugCallback) then + table.insert(self._rooms, room) + room:create(self._digCallback) + self:_insertWalls(room._walls) + return room + end + end + end +end + +function Brogue:_buildCave() + local cl = ROT.Map.Cellular:new(self._width, self._height, nil, self._rng) + cl:randomize(0.55) + for _ = 1, 5 do + cl:create() + end + local map = cl._map + local id = 2 + local largest = 2 + local bestBlob = { size = 0, walls = PointSet() } + + for x = 1, self._width do + for y = 1, self._height do + if map[x][y] == 1 then + local blobData = self:_fillBlob(x, y, map, id) + if blobData.size > bestBlob.size then + largest = id + bestBlob = blobData + end + id = id + 1 + end + end + end + + for _, x, y in bestBlob.walls:each() do + self._walls:push(x, y) + end + + for x = 2, self._width - 1 do + for y = 2, self._height - 1 do + if map[x][y] == largest then + self._map[x][y] = 0 + else + self._map[x][y] = 1 + end + end + end +end + +function Brogue:_fillBlob(x, y, map, id) + map[x][y] = id + local todo = PointSet() + local dirs = ROT.DIRS.EIGHT + local size = 1 + local walls = PointSet() + todo:push(x, y) + repeat + local px, py = todo:pluck(1) + for i = 1, #dirs do + local rx = px + dirs[i][1] + local ry = py + dirs[i][2] + if rx < 1 or rx > self._width or ry < 1 or ry > self._height then + elseif map[rx][ry] == 1 then + map[rx][ry] = id + todo:push(rx, ry) + size = size + 1 + elseif map[rx][ry] == 0 then + walls:push(rx, ry) + end + end + until #todo == 0 + return { size = size, walls = walls } +end + +function Brogue:_generateRooms() + local rooms = 0 + for i = 1, 1000 do + if rooms > self._maxrooms then + break + end + if self:_buildRoom(i > 375) then + rooms = rooms + 1 + end + end +end + +function Brogue:_buildRoom(forceNoCorridor) + --local p=table.remove(self._walls,self._rng:random(1,#self._walls)) + -- local p=self._walls[self._rng:random(1,#self._walls)] + local x, y = self._walls:getRandom() + if not x then + return false + end + local d = self:_getDiggingDirection(x, y) + if d then + if self._rng:random() < self._options.corridorChance and not forceNoCorridor then + local cd + if d[1] ~= 0 then + cd = self._options.corridorWidth + else + cd = self._options.corridorHeight + end + local corridor = + ROT.Map.Corridor:createRandomAt(x + d[1], y + d[2], d[1], d[2], { corridorLength = cd }, self._rng) + if corridor:isValid(self._isWallCallback, self._canBeDugCallback) then + local dx = corridor._endX + local dy = corridor._endY + + local room = ROT.Map.BrogueRoom:createRandomAt(dx, dy, d[1], d[2], self._options, self._rng) + + if room:isValid(self._isWallCallback, self._canBeDugCallback) then + corridor:create(self._digCallback) + table.insert(self._corridors, corridor) + room:create(self._digCallback) + table.insert(self._rooms, room) + self:_insertWalls(room._walls) + self._map[x][y] = 0 + self._map[dx][dy] = 0 + return true + end + end + else + local room = ROT.Map.BrogueRoom:createRandomAt(x, y, d[1], d[2], self._options, self._rng) + if room:isValid(self._isWallCallback, self._canBeDugCallback) then + room:create(self._digCallback) + table.insert(self._rooms, room) + self:_insertWalls(room._walls) + return true + end + end + end + return false +end + +function Brogue:_getDiggingDirection(cx, cy) + local deltas = ROT.DIRS.FOUR + local result = nil + + for i = 1, #deltas do + local delta = deltas[i] + local x = cx + delta[1] + local y = cy + delta[2] + if x < 1 or y < 1 or x > self._width or y > self._height then + return nil + end + if self._map[x][y] == 0 then + if result and #result > 0 then + return nil + end + result = delta + end + end + if not result or #result < 1 then + return nil + end + + return { -result[1], -result[2] } +end + +function Brogue:_insertWalls(wt) + for _, x, y in wt:each() do + self._walls:push(x, y) + end +end + +function Brogue:_generateLoops() + local dirs = ROT.DIRS.FOUR + local count = 0 + local wd = self._width + local hi = self._height + local m = self._map + local function cb() + count = count + 1 + end + local function pass(x, y) + return m[x][y] ~= 1 + end + for _ = 1, 300 do + if #self._walls < 1 then + return + end + local wx, wy = self._walls:getRandom() + self._walls:prune(wx, wy) + + for j = 1, 2 do + local x = wx + dirs[j][1] + local y = wy + dirs[j][2] + local x2 = wx + dirs[j + 2][1] + local y2 = wy + dirs[j + 2][2] + if + x > 1 + and x2 > 1 + and y > 1 + and y2 > 1 + and x < wd + and x2 < wd + and y < hi + and y2 < hi + and m[x][y] == 0 + and m[x2][y2] == 0 + then + local path = ROT.Path.AStar(x, y, pass) + path:compute(x2, y2, cb) + if count > 20 then + m[wx][wy] = 0 -- 2 + end + count = 0 + end + end + end +end + +function Brogue:_closeDiagonalOpenings() end + +function Brogue:_digCallback(x, y, value) + self._map[x][y] = value == 2 and 0 or value +end + +function Brogue:_isWallCallback(x, y) + if x < 1 or y < 1 or x > self._width or y > self._height then + return false + end + return self._map[x][y] == 1 +end + +function Brogue:_canBeDugCallback(x, y) + if x < 2 or y < 2 or x >= self._width or y >= self._height then + return false + end + local drs = ROT.DIRS.FOUR + for i = 1, #drs do + if self._map[x + drs[i][1]][y + drs[i][2]] == 0 then + return false + end + end + return true +end + +function Brogue:_addDoors() + for i = 1, #self._rooms do + local room = self._rooms[i] + room:clearDoors() + room:addDoors(self._isWallCallback) + end +end + +return Brogue diff --git a/lua/lib/rotLove/rot/map/brogueRoom.lua b/lua/lib/rotLove/rot/map/brogueRoom.lua new file mode 100644 index 0000000..4d9c3e6 --- /dev/null +++ b/lua/lib/rotLove/rot/map/brogueRoom.lua @@ -0,0 +1,338 @@ +--- BrogueRoom object. +-- Used by ROT.Map.Brogue to create maps with 'cross rooms' +-- @module ROT.Map.BrogueRoom +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class BrogueRoom +local BrogueRoom = ROT.Map.Room:extend("BrogueRoom") + +local PointSet = ROT.Type.PointSet + +--- Constructor. +-- creates a new BrogueRoom object with the assigned values +-- @tparam table dims Represents dimensions and positions of the rooms two rectangles +-- @tparam[opt] int doorX x-position of door +-- @tparam[opt] int doorY y-position of door +function BrogueRoom:init(dims, doorX, doorY) + self._dims = dims + self._doors = PointSet() + self._walls = PointSet() + if doorX then + self._doors:push(doorX, doorY) + end +end + +--- Create room at bottom center with dims 9x10 and 20x4 +-- @tparam int availWidth Typically the width of the map. +-- @tparam int availHeight Typically the height of the map +-- @tparam[opt] userData rng A user defined object with a .random(self, min, max) method +function BrogueRoom:createEntranceRoom(availWidth, availHeight, rng) + local dims = {} + dims.w1 = 9 + dims.h1 = 10 + dims.w2 = 20 + dims.h2 = 4 + + dims.x1 = math.floor(availWidth / 2 - dims.w1 / 2) + dims.y1 = math.floor(availHeight - dims.h1 - 1) + dims.x2 = math.floor(availWidth / 2 - dims.w2 / 2) + dims.y2 = math.floor(availHeight - dims.h2 - 1) + + return BrogueRoom:new(dims):setRNG(rng) +end + +--- Create Random with position. +-- @tparam int x x-position of room +-- @tparam int y y-position of room +-- @tparam int dx x-direction in which to build room 1==right -1==left +-- @tparam int dy y-direction in which to build room 1==down -1==up +-- @tparam table options Options +-- @tparam table options.roomWidth minimum/maximum width for room {min,max} +-- @tparam table options.roomHeight minimum/maximum height for room {min,max} +-- @tparam[opt] userData rng A user defined object with a .random(self, min, max) method +function BrogueRoom:createRandomAt(x, y, dx, dy, options, rng) + rng = rng or self._rng + local dims = {} + + local min = options.roomWidth[1] + local max = options.roomWidth[2] + dims.w1 = math.floor(rng:random(min, max)) + + min = options.roomHeight[1] + max = options.roomHeight[2] + dims.h1 = math.floor(rng:random(min, max)) + + min = options.crossWidth[1] + max = options.crossWidth[2] + dims.w2 = math.floor(rng:random(min, max)) + + min = options.crossHeight[1] + max = options.crossHeight[2] + dims.h2 = math.floor(rng:random(min, max)) + + if dx == 1 then + -- wider rect gets x+1 + -- wider gets y-math.floor(rng:random()*widersHeight) + if dims.w1 > dims.w2 then + dims.x1 = x + 1 + dims.y1 = y - math.floor(rng:random() * dims.h1) + dims.x2 = math.floor(rng:random(dims.x1, (dims.x1 + dims.w1) - dims.w2)) + dims.y2 = math.floor(rng:random(dims.y1, (dims.y1 + dims.h1) - dims.h2)) + else + dims.x2 = x + 1 + dims.y2 = y - math.floor(rng:random() * dims.h2) + dims.x1 = math.floor(rng:random(dims.x2, (dims.x2 + dims.w2) - dims.w1)) + dims.y1 = math.floor(rng:random(dims.y2, (dims.y2 + dims.h2) - dims.h1)) + end + elseif dx == -1 then + -- wider rect gets x-widersWidth + -- wider gets y-math.floor(rng:random()*widersHeight) + if dims.w1 > dims.w2 then + dims.x1 = x - dims.w1 - 1 + dims.y1 = y - math.floor(rng:random() * dims.h1) + dims.x2 = math.floor(rng:random(dims.x1, (dims.x1 + dims.w1) - dims.w2)) + dims.y2 = math.floor(rng:random(dims.y1, (dims.y1 + dims.h1) - dims.h2)) + else + dims.x2 = x - dims.w2 - 1 + dims.y2 = y - math.floor(rng:random() * dims.h2) + dims.x1 = math.floor(rng:random(dims.x2, (dims.x2 + dims.w2) - dims.w1)) + dims.y1 = math.floor(rng:random(dims.y2, (dims.y2 + dims.h2) - dims.h1)) + end + elseif dy == 1 then + -- taller gets y+1 + -- taller gets x-math.floor(rng:random()*width) + if dims.h1 > dims.h2 then + dims.y1 = y + 1 + dims.x1 = x - math.floor(rng:random() * dims.w1) + dims.x2 = math.floor(rng:random(dims.x1, (dims.x1 + dims.w1) - dims.w2)) + dims.y2 = math.floor(rng:random(dims.y1, (dims.y1 + dims.h1) - dims.h2)) + else + dims.y2 = y + 1 + dims.x2 = x - math.floor(rng:random() * dims.w2) + dims.x1 = math.floor(rng:random(dims.x2, (dims.x2 + dims.w2) - dims.w1)) + dims.y1 = math.floor(rng:random(dims.y2, (dims.y2 + dims.h2) - dims.h1)) + end + elseif dy == -1 then + -- taller gets y-tallersHeight + -- taller gets x-math.floor(rng:random()*width) + if dims.h1 > dims.h2 then + dims.y1 = y - dims.h1 - 1 + dims.x1 = x - math.floor(rng:random() * dims.w1) + dims.x2 = math.floor(rng:random(dims.x1, (dims.x1 + dims.w1) - dims.w2)) + dims.y2 = math.floor(rng:random(dims.y1, (dims.y1 + dims.h1) - dims.h2)) + else + dims.y2 = y - dims.h2 - 1 + dims.x2 = x - math.floor(rng:random() * dims.w2) + dims.x1 = math.floor(rng:random(dims.x2, (dims.x2 + dims.w2) - dims.w1)) + dims.y1 = math.floor(rng:random(dims.y2, (dims.y2 + dims.h2) - dims.h1)) + end + else + assert(false, "dx or dy must be 1 or -1") + end + --if dims.x2~=dims.x2 then dims.x2=dims.x1 end + --if dims.y2~=dims.y2 then dims.y2=dims.y1 end + --if dims.x1~=dims.x1 then dims.x1=dims.x2 end + --if dims.y1~=dims.y1 then dims.y1=dims.y2 end + return BrogueRoom:new(dims, x, y):setRNG(rng) +end + +--- Create Random with center position. +-- @tparam int cx x-position of room's center +-- @tparam int cy y-position of room's center +-- @tparam table options Options +-- @tparam table options.roomWidth minimum/maximum width for room {min,max} +-- @tparam table options.roomHeight minimum/maximum height for room {min,max} +-- @tparam table options.crossWidth minimum/maximum width for rectangleTwo {min,max} +-- @tparam table options.crossHeight minimum/maximum height for rectangleTwo {min,max} +-- @tparam[opt] userData rng A user defined object with a .random(min, max) method +function BrogueRoom:createRandomCenter(cx, cy, options, rng) + rng = rng or self._rng + local dims = {} + --- Generate Rectangle One dimensions + local min = options.roomWidth[1] + local max = options.roomWidth[2] + dims.w1 = math.floor(rng:random(min, max)) + + min = options.roomHeight[1] + max = options.roomHeight[2] + dims.h1 = math.floor(rng:random(min, max)) + + dims.x1 = cx - math.floor(rng:random() * dims.w1) + dims.y1 = cy - math.floor(rng:random() * dims.h1) + + --- Generate Rectangle Two dimensions + min = options.roomWidth[1] + max = options.roomWidth[2] + dims.w2 = math.floor(rng:random(min, max)) + + min = options.roomHeight[1] + max = options.roomHeight[2] + dims.h2 = math.floor(rng:random(min, max)) + + dims.x2 = math.floor(rng:random(dims.x1, (dims.x1 + dims.w1) - dims.w2)) + dims.y2 = math.floor(rng:random(dims.y1, (dims.y1 + dims.h1) - dims.h2)) + if dims.x2 ~= dims.x2 then + dims.x2 = dims.x1 + end + if dims.y2 ~= dims.y2 then + dims.y2 = dims.y1 + end + + return BrogueRoom:new(dims):setRNG(rng) +end + +--- Create random with no position. +-- @tparam int availWidth Typically the width of the map. +-- @tparam int availHeight Typically the height of the map +-- @tparam table options Options +-- @tparam table options.roomWidth minimum/maximum width for rectangleOne {min,max} +-- @tparam table options.roomHeight minimum/maximum height for rectangleOne {min,max} +-- @tparam table options.crossWidth minimum/maximum width for rectangleTwo {min,max} +-- @tparam table options.crossHeight minimum/maximum height for rectangleTwo {min,max} +-- @tparam[opt] userData rng A user defined object with a .random(min, max) method +function BrogueRoom:createRandom(availWidth, availHeight, options, rng) + rng = rng or self._rng + local dims = {} + --- Generate Rectangle One dimensions + local min = options.roomWidth[1] + local max = options.roomWidth[2] + dims.w1 = math.floor(rng:random(min, max)) + + min = options.roomHeight[1] + max = options.roomHeight[2] + dims.h1 = math.floor(rng:random(min, max)) + + -- Consider moving these to aw-(w1+w2) and ah-(h1+h2) + local left = availWidth - dims.w1 + local top = availHeight - dims.h1 + + dims.x1 = math.floor(rng:random() * left) + dims.y1 = math.floor(rng:random() * top) + + --- Generate Rectangle Two dimensions + min = options.crossWidth[1] + max = options.crossWidth[2] + dims.w2 = math.floor(rng:random(min, max)) + + min = options.crossHeight[1] + max = options.crossHeight[2] + dims.h2 = math.floor(rng:random(min, max)) + + dims.x2 = math.floor(rng:random(dims.x1, (dims.x1 + dims.w1) - dims.w2)) + dims.y2 = math.floor(rng:random(dims.y1, (dims.y1 + dims.h1) - dims.h2)) + if dims.x2 ~= dims.x2 then + dims.x2 = dims.x1 + end + if dims.y2 ~= dims.y2 then + dims.y2 = dims.y1 + end + return BrogueRoom:new(dims):setRNG(rng) +end + +--- Use two callbacks to confirm room validity. +-- @tparam function isWallCallback A function with two parameters (x, y) that will return true if x, y represents a wall space in a map. +-- @tparam function canBeDugCallback A function with two parameters (x, y) that will return true if x, y represents a map cell that can be made into floorspace. +-- @treturn boolean true if room is valid. +function BrogueRoom:isValid(isWallCallback, canBeDugCallback) + local dims = self._dims + if dims.x2 ~= dims.x2 or dims.y2 ~= dims.y2 or dims.x1 ~= dims.x1 or dims.y1 ~= dims.y1 then + return false + end + + local left = self:getLeft() - 1 + local right = self:getRight() + 1 + local top = self:getTop() - 1 + local bottom = self:getBottom() + 1 + for x = left, right do + for y = top, bottom do + if self:_coordIsFloor(x, y) then + if not isWallCallback(x, y) or not canBeDugCallback(x, y) then + return false + end + elseif self:_coordIsWall(x, y) then + self._walls:push(x, y) + end + end + end + + return true +end + +--- Create. +-- Function runs a callback to dig the room into a map +-- @tparam function digCallback The function responsible for digging the room into a map. +function BrogueRoom:create(digCallback) + local value = 0 + local left = self:getLeft() - 1 + local right = self:getRight() + 1 + local top = self:getTop() - 1 + local bottom = self:getBottom() + 1 + for x = left, right do + for y = top, bottom do + if self._doors:find(x, y) then + value = 2 + elseif self:_coordIsFloor(x, y) then + value = 0 + else + value = 1 + end + digCallback(x, y, value) + end + end +end + +function BrogueRoom:_coordIsFloor(x, y) + local d = self._dims + if x >= d.x1 and x <= d.x1 + d.w1 and y >= d.y1 and y <= d.y1 + d.h1 then + return true + elseif x >= d.x2 and x <= d.x2 + d.w2 and y >= d.y2 and y <= d.y2 + d.h2 then + return true + end + return false +end + +function BrogueRoom:_coordIsWall(x, y) + local dirs = ROT.DIRS.EIGHT + for i = 1, #dirs do + local dir = dirs[i] + if self:_coordIsFloor(x + dir[1], y + dir[2]) then + return true + end + end + return false +end + +function BrogueRoom:getLeft() + return math.min(self._dims.x1, self._dims.x2) +end +function BrogueRoom:getRight() + return math.max(self._dims.x1 + self._dims.w1, self._dims.x2 + self._dims.w2) +end +function BrogueRoom:getTop() + return math.min(self._dims.y1, self._dims.y2) +end +function BrogueRoom:getBottom() + return math.max(self._dims.y1 + self._dims.h1, self._dims.y2 + self._dims.h2) +end + +function BrogueRoom:debug() + local str = "" + for k, v in pairs(self._dims) do + str = str .. k .. "=" .. v .. "," + end + io.write(str) + io.flush() +end + +function BrogueRoom:_checkHorizontalEdge(isWallCallback, x, y) + return self._walls:find(x, y) and self._walls:find(x - 1, y) and self._walls:find(x + 1, y) + -- and isWallCallback(x - 1, y) + -- and isWallCallback(x + 1, y) +end + +function BrogueRoom:_checkVerticalEdge(isWallCallback, x, y) + return self._walls:find(x, y) and self._walls:find(x, y - 1) and self._walls:find(x, y + 1) + -- and isWallCallback(x, y - 1) + -- and isWallCallback(x, y + 1) +end + +return BrogueRoom diff --git a/lua/lib/rotLove/rot/map/cellular.lua b/lua/lib/rotLove/rot/map/cellular.lua new file mode 100644 index 0000000..c69f205 --- /dev/null +++ b/lua/lib/rotLove/rot/map/cellular.lua @@ -0,0 +1,216 @@ +--- Cellular Automaton Map Generator +-- @module ROT.Map.Cellular +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Cellular +local Cellular = ROT.Map:extend("Cellular") +--- Constructor. +-- Called with ROT.Map.Cellular:new() +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +-- @tparam[opt] table options Options +-- @tparam table options.born List of neighbor counts for a new cell to be born in empty space +-- @tparam table options.survive List of neighbor counts for an existing cell to survive +-- @tparam int options.topology Topology. Accepted values: 4, 8 +-- @tparam boolean options.connected Set to true to connect open areas on create +-- @tparam int options.minimumZoneArea Unconnected zones with fewer tiles than this will be turned to wall instead of being connected +function Cellular:init(width, height, options) + Cellular.super.init(self, width, height) + self._options = { + born = { 5, 6, 7, 8 }, + survive = { 4, 5, 6, 7, 8 }, + topology = 8, + connected = false, + minimumZoneArea = 8, + } + if options then + for k, v in pairs(options) do + self._options[k] = v + end + end + local t = self._options.topology + assert(t == 8 or t == 4, "topology must be 8 or 4") + self._dirs = t == 8 and ROT.DIRS.EIGHT or t == 4 and ROT.DIRS.FOUR +end + +--- Randomize cells. +-- Random fill map with 0 or 1. Call this first when creating a map. +-- @tparam number prob Probability that a cell will be a floor (0). Accepts values between 0 and 1 +-- @treturn ROT.Map.Cellular self +function Cellular:randomize(prob) + if not self._map then + self._map = self:_fillMap(0) + end + for i = 1, self._width do + for j = 1, self._height do + self._map[i][j] = self._rng:random() < prob and 1 or 0 + end + end + return self +end + +--- Set. +-- Assign a value (0 or 1) to a cell on the map +-- @tparam int x x-position of the cell +-- @tparam int y y-position of the cell +-- @tparam int value Value to be assigned 0-Floor 1-Wall +function Cellular:set(x, y, value) + self._map[x][y] = value +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.Cellular self +function Cellular:create(callback) + local newMap = self:_fillMap(0) + local born = self._options.born + local survive = self._options.survive + local changed = false + + for j = 1, self._height do + for i = 1, self._width do + local cur = self._map[i][j] + local ncount = self:_getNeighbors(i, j) + if cur > 0 and table.indexOf(survive, ncount) > 0 then + newMap[i][j] = 1 + elseif cur <= 0 and table.indexOf(born, ncount) > 0 then + newMap[i][j] = 1 + end + if not changed and newMap[i][j] ~= self._map[i][j] then + changed = true + end + end + end + self._map = newMap + + if self._options.connected then + self:_completeMaze() + end + if callback then + for i = 1, self._width do + for j = 1, self._height do + if callback then + callback(i, j, newMap[i][j]) + end + end + end + end + self.changed = changed + return self +end + +function Cellular:_getNeighbors(cx, cy) + local rst = 0 + for i = 1, #self._dirs do + local dir = self._dirs[i] + local x = cx + dir[1] + local y = cy + dir[2] + if x > 0 and x <= self._width and y > 0 and y <= self._height then + rst = self._map[x][y] == 1 and rst + 1 or rst + end + end + return rst +end + +function Cellular:_completeMaze() + -- Collect all zones + local zones = {} + for i = 1, self._width do + for j = 1, self._height do + if self._map[i][j] == 0 then + self:_addZoneFrom(i, j, zones) + end + end + end + -- overwrite zones below a certain size + -- and connect zones + for i = 1, #zones do + if #zones[i] < self._options.minimumZoneArea then + for _, v in pairs(zones[i]) do + self._map[v[1]][v[2]] = 1 + end + else + local rx = self._rng:random(1, self._width) + local ry = self._rng:random(1, self._height) + while self._map[rx][ry] ~= 1 and self._map[rx][ry] ~= i do + rx = self._rng:random(1, self._width) + ry = self._rng:random(1, self._height) + end + local t = zones[i][self._rng:random(1, #zones[i])] + self:_tunnel(t[1], t[2], rx, ry) + -- re-establish floors as 0 for this zone + for _, v in pairs(zones[i]) do + self._map[v[1]][v[2]] = 0 + end + end + end +end + +function Cellular:_addZoneFrom(x, y, zones) + local dirs = self._dirs + local todo = { { x, y } } + table.insert(zones, {}) + local zId = #zones + 1 + self._map[x][y] = zId + table.insert(zones[#zones], { x, y }) + while #todo > 0 do + local t = table.remove(todo) + local tx = t[1] + local ty = t[2] + for _, v in pairs(dirs) do + local nx = tx + v[1] + local ny = ty + v[2] + if self._map[nx] and self._map[nx][ny] and self._map[nx][ny] == 0 then + self._map[nx][ny] = zId + table.insert(zones[#zones], { nx, ny }) + table.insert(todo, { nx, ny }) + end + end + end +end + +function Cellular:_tunnel(sx, sy, ex, ey) + local xOffset = ex - sx + local yOffset = ey - sy + local xpos = sx + local ypos = sy + local moves = {} + local xAbs = math.abs(xOffset) + local yAbs = math.abs(yOffset) + local firstHalf = self._rng:random() + local secondHalf = 1 - firstHalf + local xDir = xOffset > 0 and 3 or 7 + local yDir = yOffset > 0 and 5 or 1 + if xAbs < yAbs then + local tempDist = math.ceil(yAbs * firstHalf) + table.insert(moves, { yDir, tempDist }) + table.insert(moves, { xDir, xAbs }) + tempDist = math.floor(yAbs * secondHalf) + table.insert(moves, { yDir, tempDist }) + else + local tempDist = math.ceil(xAbs * firstHalf) + table.insert(moves, { xDir, tempDist }) + table.insert(moves, { yDir, yAbs }) + tempDist = math.floor(xAbs * secondHalf) + table.insert(moves, { xDir, tempDist }) + end + + local dirs = ROT.DIRS.EIGHT + self._map[xpos][ypos] = 0 + while #moves > 0 do + local move = table.remove(moves) + if move and move[1] and move[1] < 9 and move[1] > 0 then + while move[2] > 0 do + xpos = xpos + dirs[move[1]][1] + ypos = ypos + dirs[move[1]][2] + self._map[xpos][ypos] = 0 + move[2] = move[2] - 1 + end + end + end +end + +return Cellular diff --git a/lua/lib/rotLove/rot/map/corridor.lua b/lua/lib/rotLove/rot/map/corridor.lua new file mode 100644 index 0000000..a912593 --- /dev/null +++ b/lua/lib/rotLove/rot/map/corridor.lua @@ -0,0 +1,158 @@ +--- Corridor object. +-- Used by ROT.Map.Uniform and ROT.Map.Digger to create maps +-- @module ROT.Map.Corridor +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Corridor +local Corridor = ROT.Map.Feature:extend("Corridor") +--- Constructor. +-- Called with ROT.Map.Corridor:new() +-- @tparam int startX x-position of first floospace in corridor +-- @tparam int startY y-position of first floospace in corridor +-- @tparam int endX x-position of last floospace in corridor +-- @tparam int endY y-position of last floospace in corridor +function Corridor:init(startX, startY, endX, endY) + self._startX = startX + self._startY = startY + self._endX = endX + self._endY = endY + self._endsWithAWall = true +end + +--- Create random with position. +-- @tparam int x x-position of first floospace in corridor +-- @tparam int y y-position of first floospace in corridor +-- @tparam int dx x-direction of corridor (-1, 0, 1) for (left, none, right) +-- @tparam int dy y-direction of corridor (-1, 0, 1) for (up, none, down) +-- @tparam table options Options +-- @tparam table options.corridorLength a table for the min and max corridor lengths {min, max} +-- @tparam[opt] userData rng A user defined object with a .random(min, max) method +function Corridor:createRandomAt(x, y, dx, dy, options, rng) + rng = rng and rng or math.random + local min = options.corridorLength[1] + local max = options.corridorLength[2] + local length = math.floor(rng:random(min, max)) + return Corridor:new(x, y, x + dx * length, y + dy * length):setRNG(rng) +end + +--- Write various information about this corridor to the console. +function Corridor:debug() + local debugString = "corridor: " .. self._startX .. "," .. self._startY .. "," .. self._endX .. "," .. self._endY + io.write(debugString) + io.flush() +end + +--- Use two callbacks to confirm corridor validity. +-- @tparam function isWallCallback A function with two parameters (x, y) that will return true if x, y represents a wall space in a map. +-- @tparam function canBeDugCallback A function with two parameters (x, y) that will return true if x, y represents a map cell that can be made into floorspace. +-- @treturn boolean true if corridor is valid. +function Corridor:isValid(isWallCallback, canBeDugCallback) + local sx = self._startX + local sy = self._startY + local dx = self._endX - sx + local dy = self._endY - sy + local length = 1 + math.max(math.abs(dx), math.abs(dy)) + + if dx ~= 0 then + dx = dx / math.abs(dx) + end + if dy ~= 0 then + dy = dy / math.abs(dy) + end + local nx = dy + local ny = -dx + + local ok = true + + for i = 0, length - 1 do + local x = sx + i * dx + local y = sy + i * dy + + if not canBeDugCallback(x, y) then + ok = false + end + if not isWallCallback(x + nx, y + ny) then + ok = false + end + if not isWallCallback(x - nx, y - ny) then + ok = false + end + + if not ok then + length = i + self._endX = x - dx + self._endY = y - dy + break + end + end + + if length == 0 then + return false + end + if length == 1 and isWallCallback(self._endX + dx, self._endY + dy) then + return false + end + + local firstCornerBad = not isWallCallback(self._endX + dx + nx, self._endY + dy + ny) + local secondCornrBad = not isWallCallback(self._endX + dx - nx, self._endY + dy - ny) + self._endsWithAWall = isWallCallback(self._endX + dx, self._endY + dy) + if (firstCornerBad or secondCornrBad) and self._endsWithAWall then + return false + end + + return true +end + +--- Create. +-- Function runs a callback to dig the corridor into a map +-- @tparam function digCallback The function responsible for digging the corridor into a map. +function Corridor:create(digCallback) + local sx = self._startX + local sy = self._startY + local dx = self._endX - sx + local dy = self._endY - sy + + local length = 1 + math.max(math.abs(dx), math.abs(dy)) + if dx ~= 0 then + dx = dx / math.abs(dx) + end + if dy ~= 0 then + dy = dy / math.abs(dy) + end + + for i = 0, length - 1 do + local x = sx + i * dx + local y = sy + i * dy + digCallback(x, y, 0) + end + return true +end + +--- Mark walls as priority for a future feature. +-- Use this for storing the three points at the end of the corridor that you probably want to make sure gets a room attached. +-- @tparam userdata gen The map generator calling this function. Passed as self to the digCallback +-- @tparam function priorityWallCallback The function responsible for receiving and processing the priority walls +function Corridor:createPriorityWalls(priorityWallCallback) + if not self._endsWithAWall then + return + end + + local sx = self._startX + local sy = self._startY + local dx = self._endX - sx + local dy = self._endY - sy + + if dx ~= 0 then + dx = dx / math.abs(dx) + end + if dy ~= 0 then + dy = dy / math.abs(dy) + end + local nx = dy + local ny = -dx + + priorityWallCallback(self._endX + dx, self._endY + dy) + priorityWallCallback(self._endX + nx, self._endY + ny) + priorityWallCallback(self._endX - nx, self._endY - ny) +end + +return Corridor diff --git a/lua/lib/rotLove/rot/map/digger.lua b/lua/lib/rotLove/rot/map/digger.lua new file mode 100644 index 0000000..98a75e1 --- /dev/null +++ b/lua/lib/rotLove/rot/map/digger.lua @@ -0,0 +1,235 @@ +--- The Digger Map Generator. +-- See http://www.roguebasin.roguelikedevelopment.org/index.php?title=Dungeon-Building_Algorithm. +-- @module ROT.Map.Digger +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Digger +local Digger = ROT.Map.Dungeon:extend("Digger") +--- Constructor. +-- Called with ROT.Map.Digger:new() +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +-- @tparam[opt] table options Options +-- @tparam[opt={3,8}] table options.roomWidth room minimum and maximum width +-- @tparam[opt={3,5}] table options.roomHeight room minimum and maximum height +-- @tparam[opt={3,7}] table options.corridorLength corridor minimum and maximum length +-- @tparam[opt=0.2] number options.dugPercentage we stop after this percentage of level area has been dug out +-- @tparam[opt=1000] int options.timeLimit stop after this much time has passed (msec) +-- @tparam[opt=false] boolean options.nocorridorsmode If true, do not use corridors to generate this map +function Digger:init(width, height, options) + Digger.super.init(self, width, height) + + self._digCallback = self:bind(self._digCallback) + self._canBeDugCallback = self:bind(self._canBeDugCallback) + self._isWallCallback = self:bind(self._isWallCallback) + self._priorityWallCallback = self:bind(self._priorityWallCallback) + + self._options = { + roomWidth = { 3, 8 }, + roomHeight = { 3, 5 }, + corridorLength = { 3, 7 }, + dugPercentage = 0.2, + timeLimit = 1000, + nocorridorsmode = false, + } + if options then + for k, _ in pairs(options) do + self._options[k] = options[k] + end + end + + self._features = { Room = 4, Corridor = 4 } + if self._options.nocorridorsmode then + self._features.Corridor = nil + end + self._featureAttempts = 20 + self._walls = {} +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.Digger self +function Digger:create(callback) + self._rooms = {} + self._corridors = {} + self._map = self:_fillMap(1) + self._walls = {} + self._dug = 0 + local area = (self._width - 2) * (self._height - 2) + + self:_firstRoom() + + local t1 = os.clock() * 1000 + local priorityWalls = 0 + repeat + local t2 = os.clock() * 1000 + if t2 - t1 > self._options.timeLimit then + break + end + + local wall = self:_findWall() + if not wall then + break + end + + local x = wall.x + local y = wall.y + local dir = self:_getDiggingDirection(x, y) + if dir then + local featureAttempts = 0 + repeat + featureAttempts = featureAttempts + 1 + if self:_tryFeature(x, y, dir[1], dir[2]) then + self:_removeSurroundingWalls(x, y) + self:_removeSurroundingWalls(x - dir[1], y - dir[2]) + break + end + until featureAttempts >= self._featureAttempts + priorityWalls = 0 + for i = 1, #self._walls do + local wall = self._walls[i] + if wall.value > 1 then + priorityWalls = priorityWalls + 1 + end + end + end + until self._dug / area > self._options.dugPercentage and priorityWalls < 1 + + self:_addDoors() + + if not callback then + return self + end + for y = 1, self._height do + for x = 1, self._width do + callback(x, y, self._map[x][y]) + end + end + return self +end + +function Digger:_digCallback(x, y, value) + if value == 0 or value == 2 then + self._map[x][y] = 0 + self._dug = self._dug + 1 + else + self:setWall(x, y, 1) + end +end + +function Digger:_isWallCallback(x, y) + if x < 1 or y < 1 or x > self._width or y > self._height then + return false + end + return self._map[x][y] == 1 +end + +function Digger:_canBeDugCallback(x, y) + if x < 2 or y < 2 or x >= self._width or y >= self._height then + return false + end + return self._map[x][y] == 1 +end + +function Digger:_priorityWallCallback(x, y) + self:setWall(x, y, 2) +end + +function Digger:_firstRoom() + local cx = math.floor(self._width / 2) + local cy = math.floor(self._height / 2) + local room = ROT.Map.Room:new():createRandomCenter(cx, cy, self._options, self._rng) + table.insert(self._rooms, room) + room:create(self._digCallback) +end + +function Digger:_findWall() + local prio1 = {} + local prio2 = {} + for i = 1, #self._walls do + local wall = self._walls[i] + if wall.value > 1 then + prio2[#prio2 + 1] = wall + else + prio1[#prio1 + 1] = wall + end + end + local arr = #prio2 > 0 and prio2 or prio1 + if #arr < 1 then + return nil + end + local wall = table.random(arr) + self:setWall(wall.x, wall.y, nil) + return wall +end + +function Digger:_tryFeature(x, y, dx, dy) + local type = self._rng:getWeightedValue(self._features) + local feature = ROT.Map[type]:createRandomAt(x, y, dx, dy, self._options, self._rng) + + if not feature:isValid(self._isWallCallback, self._canBeDugCallback) then + return false + end + + feature:create(self._digCallback) + + if type == "Room" then + table.insert(self._rooms, feature) + elseif type == "Corridor" then + feature:createPriorityWalls(self._priorityWallCallback) + table.insert(self._corridors, feature) + end + + return true +end + +function Digger:_removeSurroundingWalls(cx, cy) + local deltas = ROT.DIRS.FOUR + for i = 1, #deltas do + local delta = deltas[i] + local x = cx + delta[1] + local y = cy + delta[2] + self:setWall(x, y, nil) + x = 2 * delta[1] + y = 2 * delta[2] + self:setWall(x, y, nil) + end +end + +function Digger:_getDiggingDirection(cx, cy) + if cx < 2 or cy < 2 or cx > self._width - 1 or cy > self._height - 1 then + return nil + end + local deltas = ROT.DIRS.FOUR + local result = nil + + for i = 1, #deltas do + local delta = deltas[i] + local x = cx + delta[1] + local y = cy + delta[2] + if self._map[x][y] == 0 then + if result then + return nil + end + result = delta + end + end + if not result then + return nil + end + + return { -result[1], -result[2] } +end + +function Digger:_addDoors() + for i = 1, #self._rooms do + local room = self._rooms[i] + room:clearDoors() + room:addDoors(self._isWallCallback) + end +end + +return Digger diff --git a/lua/lib/rotLove/rot/map/dividedMaze.lua b/lua/lib/rotLove/rot/map/dividedMaze.lua new file mode 100644 index 0000000..71fde9c --- /dev/null +++ b/lua/lib/rotLove/rot/map/dividedMaze.lua @@ -0,0 +1,123 @@ +--- The Divided Maze Map Generator. +-- Recursively divided maze, http://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_division_method +-- @module ROT.Map.DividedMaze +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class DividedMaze +local DividedMaze = ROT.Map:extend("DividedMaze") +--- Constructor. +-- Called with ROT.Map.DividedMaze:new(width, height) +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +function DividedMaze:init(width, height) + DividedMaze.super.init(self, width, height) +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.DividedMaze self +function DividedMaze:create(callback) + local w = self._width + local h = self._height + self._map = {} + + for i = 1, w do + table.insert(self._map, {}) + for j = 1, h do + local border = i == 1 or j == 1 or i == w or j == h + table.insert(self._map[i], border and 1 or 0) + end + end + self._stack = { { 2, 2, w - 1, h - 1 } } + self:_process() + if not callback then + return self + end + for y = 1, h do + for x = 1, w do + callback(x, y, self._map[x][y]) + end + end + return self +end + +function DividedMaze:_process() + while #self._stack > 0 do + local room = table.remove(self._stack, 1) + self:_partitionRoom(room) + end +end + +function DividedMaze:_partitionRoom(room) + local availX = {} + local availY = {} + + for i = room[1] + 1, room[3] - 1 do + local top = self._map[i][room[2] - 1] + local bottom = self._map[i][room[4] + 1] + if top > 0 and bottom > 0 and i % 2 == 0 then + table.insert(availX, i) + end + end + + for j = room[2] + 1, room[4] - 1 do + local left = self._map[room[1] - 1][j] + local right = self._map[room[3] + 1][j] + if left > 0 and right > 0 and j % 2 == 0 then + table.insert(availY, j) + end + end + + if #availX == 0 or #availY == 0 then + return + end + + local x = table.random(availX) + local y = table.random(availY) + + self._map[x][y] = 1 + + local walls = {} + + table.insert(walls, {}) + for i = room[1], x - 1, 1 do + self._map[i][y] = 1 + table.insert(walls[#walls], { i, y }) + end + + table.insert(walls, {}) + for i = x + 1, room[3], 1 do + self._map[i][y] = 1 + table.insert(walls[#walls], { i, y }) + end + + table.insert(walls, {}) + for j = room[2], y - 1, 1 do + self._map[x][j] = 1 + table.insert(walls[#walls], { x, j }) + end + + table.insert(walls, {}) + for j = y + 1, room[4] do + self._map[x][j] = 1 + table.insert(walls[#walls], { x, j }) + end + + local solid = table.random(walls) + for i = 1, #walls do + local w = walls[i] + if w ~= solid then + local hole = table.random(w) + self._map[hole[1]][hole[2]] = 0 + end + end + table.insert(self._stack, { room[1], room[2], x - 1, y - 1 }) + table.insert(self._stack, { x + 1, room[2], room[3], y - 1 }) + table.insert(self._stack, { room[1], y + 1, x - 1, room[4] }) + table.insert(self._stack, { x + 1, y + 1, room[3], room[4] }) +end + +return DividedMaze diff --git a/lua/lib/rotLove/rot/map/dungeon.lua b/lua/lib/rotLove/rot/map/dungeon.lua new file mode 100644 index 0000000..3966317 --- /dev/null +++ b/lua/lib/rotLove/rot/map/dungeon.lua @@ -0,0 +1,82 @@ +--- The Dungeon-style map Prototype. +-- This class is extended by ROT.Map.Digger and ROT.Map.Uniform +-- @module ROT.Map.Dungeon +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Dungeon +local Dungeon = ROT.Map:extend("Dungeon") +--- Constructor. +-- Called with ROT.Map.Cellular:new() +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +function Dungeon:init(width, height) + Dungeon.super.init(self, width, height) + self._rooms = {} + self._corridors = {} +end + +--- Get rooms +-- Get a table of rooms on the map +-- @treturn table A table containing objects of the type ROT.Map.Room +function Dungeon:getRooms() + return self._rooms +end + +--- Get doors +-- Get a table of doors on the map +-- @treturn table A table {{x=int, y=int},...} for doors. + +-- FIXME: This could be problematic; it accesses an internal member of another +-- class (room._doors). Will break if underlying implementation changes. +-- Should probably take a callback instead like Room:getDoors(). + +function Dungeon:getDoors() + local result = {} + for _, room in ipairs(self._rooms) do + for _, x, y in room._doors:each() do + result[#result + 1] = { x = x, y = y } + end + end + return result +end + +--- Get corridors +-- Get a table of corridors on the map +-- @treturn table A table containing objects of the type ROT.Map.Corridor +function Dungeon:getCorridors() + return self._corridors +end + +function Dungeon:_getDetail(name, x, y) + local t = self[name] + for i = 1, #t do + if t[i].x == x and t[i].y == y then + return t[i], i + end + end +end + +function Dungeon:_setDetail(name, x, y, value) + local detail, i = self:_getDetail(name, x, y) + if detail then + if value then + detail.value = value + else + table.remove(self[name], i) + end + elseif value then + local t = self[name] + detail = { x = x, y = y, value = value } + t[#t + 1] = detail + end + return detail +end + +function Dungeon:getWall(x, y) + return self:_getDetail("_walls", x, y) +end + +function Dungeon:setWall(x, y, value) + return self:_setDetail("_walls", x, y, value) +end + +return Dungeon diff --git a/lua/lib/rotLove/rot/map/ellerMaze.lua b/lua/lib/rotLove/rot/map/ellerMaze.lua new file mode 100644 index 0000000..36f95ba --- /dev/null +++ b/lua/lib/rotLove/rot/map/ellerMaze.lua @@ -0,0 +1,96 @@ +--- The Eller Maze Map Generator. +-- See http://homepages.cwi.nl/~tromp/maze.html for explanation +-- @module ROT.Map.EllerMaze +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class EllerMaze +local EllerMaze = ROT.Map:extend("EllerMaze") + +--- Constructor. +-- Called with ROT.Map.EllerMaze:new(width, height) +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +function EllerMaze:init(width, height) + EllerMaze.super.init(self, width, height) +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.EllerMaze self +function EllerMaze:create(callback) + local map = ROT.Type.Grid() + local w = math.ceil((self._width - 2) / 2) + local rand = 9 / 24 + local L = {} + local R = {} + + for i = 1, w do + table.insert(L, i) + table.insert(R, i) + end + table.insert(L, w) + local j = 2 + while j < self._height - 2 do + for i = 1, w do + local x = 2 * i + local y = j + map:setCell(x, y, 0) + + if i ~= L[i + 1] and self._rng:random() > rand then + self:_addToList(i, L, R) + map:setCell(x + 1, y, 0) + end + + if i ~= L[i] and self._rng:random() > rand then + self:_removeFromList(i, L, R) + else + map:setCell(x, y + 1, 0) + end + end + j = j + 2 + end + --j=self._height%2==1 and self._height-2 or self._height-3 + for i = 1, w do + local x = 2 * i + local y = j + map:setCell(x, y, 0) + + if i ~= L[i + 1] and (i == L[i] or self._rng:random() > rand) then + self:_addToList(i, L, R) + map:setCell(x + 1, y, 0) + end + + self:_removeFromList(i, L, R) + end + + if not callback then + return self + end + + for y = 1, self._height do + for x = 1, self._width do + callback(x, y, map:getCell(x, y) or 1) + end + end + + return self +end + +function EllerMaze:_removeFromList(i, L, R) + R[L[i]] = R[i] + L[R[i]] = L[i] + R[i] = i + L[i] = i +end + +function EllerMaze:_addToList(i, L, R) + R[L[i + 1]] = R[i] + L[R[i]] = L[i + 1] + R[i] = i + 1 + L[i + 1] = i +end + +return EllerMaze diff --git a/lua/lib/rotLove/rot/map/feature.lua b/lua/lib/rotLove/rot/map/feature.lua new file mode 100644 index 0000000..84f7ed0 --- /dev/null +++ b/lua/lib/rotLove/rot/map/feature.lua @@ -0,0 +1,9 @@ +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Feature +local Feature = ROT.Class:extend("Feature") + +function Feature:isValid() end +function Feature:create() end +function Feature:debug() end +function Feature:createRandomAt() end +return Feature diff --git a/lua/lib/rotLove/rot/map/iceyMaze.lua b/lua/lib/rotLove/rot/map/iceyMaze.lua new file mode 100644 index 0000000..c85947a --- /dev/null +++ b/lua/lib/rotLove/rot/map/iceyMaze.lua @@ -0,0 +1,118 @@ +--- The Icey Maze Map Generator. +-- See http://www.roguebasin.roguelikedevelopment.org/index.php?title=Simple_maze for explanation +-- @module ROT.Map.IceyMaze +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class IceyMaze +local IceyMaze = ROT.Map:extend("IceyMaze") +--- Constructor. +-- Called with ROT.Map.IceyMaze:new(width, height, regularity) +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +-- @tparam int[opt=0] regularity A value used to determine the 'randomness' of the map, 0= more random +function IceyMaze:init(width, height, regularity) + IceyMaze.super.init(self, width, height) + self._regularity = regularity and regularity or 0 +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.IceyMaze self +function IceyMaze:create(callback) + local w = self._width + local h = self._height + local map = self:_fillMap(1) + w = w % 2 == 1 and w - 1 or w - 2 + h = h % 2 == 1 and h - 1 or h - 2 + + local cx, cy, nx, ny = 1, 1, 1, 1 + local done = 0 + local blocked = false + local dirs = { + { 0, 0 }, + { 0, 0 }, + { 0, 0 }, + { 0, 0 }, + } + repeat + cx = 2 + 2 * math.floor(self._rng:random() * (w - 1) / 2) + cy = 2 + 2 * math.floor(self._rng:random() * (h - 1) / 2) + if done == 0 then + map[cx][cy] = 0 + end + if map[cx][cy] == 0 then + self:_randomize(dirs) + repeat + if math.floor(self._rng:random() * (self._regularity + 1)) == 0 then + self:_randomize(dirs) + end + blocked = true + for i = 1, 4 do + nx = cx + dirs[i][1] * 2 + ny = cy + dirs[i][2] * 2 + if self:_isFree(map, nx, ny, w, h) then + map[nx][ny] = 0 + map[cx + dirs[i][1]][cy + dirs[i][2]] = 0 + + cx = nx + cy = ny + blocked = false + done = done + 1 + break + end + end + until blocked + end + until done + 1 >= w * h / 4 + + if not callback then + return self + end + for y = 1, self._height do + for x = 1, self._width do + callback(x, y, map[x][y]) + end + end + return self +end + +function IceyMaze:_randomize(dirs) + for i = 1, 4 do + dirs[i][1] = 0 + dirs[i][2] = 0 + end + local rand = math.floor(self._rng:random() * 4) + if rand == 0 then + dirs[1][1] = -1 + dirs[3][2] = -1 + dirs[2][1] = 1 + dirs[4][2] = 1 + elseif rand == 1 then + dirs[4][1] = -1 + dirs[2][2] = -1 + dirs[3][1] = 1 + dirs[1][2] = 1 + elseif rand == 2 then + dirs[3][1] = -1 + dirs[1][2] = -1 + dirs[4][1] = 1 + dirs[2][2] = 1 + elseif rand == 3 then + dirs[2][1] = -1 + dirs[4][2] = -1 + dirs[1][1] = 1 + dirs[3][2] = 1 + end +end + +function IceyMaze:_isFree(map, x, y, w, h) + if x < 2 or y < 2 or x > w or y > h then + return false + end + return map[x][y] ~= 0 +end + +return IceyMaze diff --git a/lua/lib/rotLove/rot/map/rogue.lua b/lua/lib/rotLove/rot/map/rogue.lua new file mode 100644 index 0000000..e6a0229 --- /dev/null +++ b/lua/lib/rotLove/rot/map/rogue.lua @@ -0,0 +1,355 @@ +--- Rogue Map Generator. +-- A map generator based on the original Rogue map gen algorithm +-- See http://kuoi.com/~kamikaze/GameDesign/art07_rogue_dungeon.php +-- @module ROT.Map.Rogue +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Rogue:Map +local Rogue = ROT.Map:extend("Rogue") + +local function calculateRoomSize(size, cell) + local max = math.floor((size / cell) * 0.8) + local min = math.floor((size / cell) * 0.25) + min = min < 2 and 2 or min + max = max < 2 and 2 or max + return { min, max } +end + +--- Constructor. +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +-- @tparam[opt] table options Options +-- @tparam int options.cellWidth Number of cells to create on the horizontal (number of rooms horizontally) +-- @tparam int options.cellHeight Number of cells to create on the vertical (number of rooms vertically) +-- @tparam int options.roomWidth Room min and max width +-- @tparam int options.roomHeight Room min and max height +function Rogue:init(width, height, options) + Rogue.super.init(self, width, height) + self._doors = {} + self._options = { cellWidth = math.floor(width * 0.0375), cellHeight = math.floor(height * 0.125) } + if options then + for k, _ in pairs(options) do + self._options[k] = options[k] + end + end + + if not self._options.roomWidth then + self._options.roomWidth = calculateRoomSize(width, self._options.cellWidth) + end + + if not self._options.roomHeight then + self._options.roomHeight = calculateRoomSize(height, self._options.cellHeight) + end +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: +-- @tparam int callback.x The x-position of a cell in the map +-- @tparam int callback.y The y-position of a cell in the map +-- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.Cellular|nil self or nil if time limit is reached +function Rogue:create(callback) + self.map = self:_fillMap(1) + self._rooms = {} + self.connectedCells = {} + + self:_initRooms() + self:_connectRooms() + self:_connectUnconnectedRooms() + self:_createRandomRoomConnections() + self:_createRooms() + self:_createCorridors() + if not callback then + return self + end + for y = 1, self._height do + for x = 1, self._width do + callback(x, y, self.map[x][y]) + end + end + return self +end + +function Rogue:_getRandomInt(min, max) + min = min and min or 0 + max = max and max or 1 + return math.floor(self._rng:random(min, max)) +end + +function Rogue:_initRooms() + for i = 1, self._options.cellWidth do + self._rooms[i] = {} + for j = 1, self._options.cellHeight do + self._rooms[i][j] = { x = 0, y = 0, width = 0, height = 0, connections = {}, cellx = i, celly = j } + end + end +end + +function Rogue:_connectRooms() + local cgx = self:_getRandomInt(1, self._options.cellWidth) + local cgy = self:_getRandomInt(1, self._options.cellHeight) + local idx, ncgx, ncgy + local found = false + local room, otherRoom + local dirToCheck = 0 + repeat + dirToCheck = { 1, 3, 5, 7 } + dirToCheck = table.randomize(dirToCheck) + repeat + found = false + idx = table.remove(dirToCheck) + ncgx = cgx + ROT.DIRS.EIGHT[idx][1] + ncgy = cgy + ROT.DIRS.EIGHT[idx][2] + + if (ncgx > 0 and ncgx <= self._options.cellWidth) and (ncgy > 0 and ncgy <= self._options.cellHeight) then + room = self._rooms[cgx][cgy] + + if #room.connections > 0 then + if room.connections[1][1] == ncgx and room.connections[1][2] == ncgy then + break + end + end + + otherRoom = self._rooms[ncgx][ncgy] + + if #otherRoom.connections == 0 then + table.insert(otherRoom.connections, { cgx, cgy }) + table.insert(self.connectedCells, { ncgx, ncgy }) + cgx = ncgx + cgy = ncgy + found = true + end + end + until #dirToCheck < 1 or found + until #dirToCheck < 1 +end + +function Rogue:_connectUnconnectedRooms() + local cw = self._options.cellWidth + local ch = self._options.cellHeight + + self.connectedCells = table.randomize(self.connectedCells) + local room, otherRoom, validRoom + + for i = 1, cw do + for j = 1, ch do + room = self._rooms[i][j] + + if #room.connections == 0 then + local dirs = { 1, 3, 5, 7 } + dirs = table.randomize(dirs) + validRoom = false + repeat + local dirIdx = table.remove(dirs) + local newI = i + ROT.DIRS.EIGHT[dirIdx][1] + local newJ = j + ROT.DIRS.EIGHT[dirIdx][2] + + if newI > 0 and newI <= cw and newJ > 0 and newJ <= ch then + otherRoom = self._rooms[newI][newJ] + validRoom = true + + if #otherRoom.connections == 0 then + break + end + + for k = 1, #otherRoom.connections do + if otherRoom.connections[k][1] == i and otherRoom.connections[k][2] == j then + validRoom = false + break + end + end + + if validRoom then + break + end + end + until #dirs < 1 + if validRoom then + table.insert(room.connections, { otherRoom.cellx, otherRoom.celly }) + else + io.write("-- Unable to connect room.") + io.flush() + end + end + end + end +end + +function Rogue:_createRandomRoomConnections() + return +end + +function Rogue:_createRooms() + local w = self._width + local h = self._height + local cw = self._options.cellWidth + local ch = self._options.cellHeight + local cwp = math.floor(self._width / cw) + local chp = math.floor(self._height / ch) + + local roomw, roomh + local roomWidth = self._options.roomWidth + local roomHeight = self._options.roomHeight + local sx, sy + local otherRoom + + for i = 1, cw do + for j = 1, ch do + sx = cwp * (i - 1) + sy = chp * (j - 1) + sx = sx < 2 and 2 or sx + sy = sy < 2 and 2 or sy + roomw = self:_getRandomInt(roomWidth[1], roomWidth[2]) + roomh = self:_getRandomInt(roomHeight[1], roomHeight[2]) + + if j > 1 then + otherRoom = self._rooms[i][j - 1] + while sy - (otherRoom.y + otherRoom.height) < 3 do + sy = sy + 1 + end + end + + if i > 1 then + otherRoom = self._rooms[i - 1][j] + while sx - (otherRoom.x + otherRoom.width) < 3 do + sx = sx + 1 + end + end + local sxOffset = math.round(self:_getRandomInt(0, cwp - roomw) / 2) + local syOffset = math.round(self:_getRandomInt(0, chp - roomh) / 2) + while sx + sxOffset + roomw > w do + if sxOffset > 0 then + sxOffset = sxOffset - 1 + else + roomw = roomw - 1 + end + end + + while sy + syOffset + roomh > h do + if syOffset > 0 then + syOffset = syOffset - 1 + else + roomh = roomh - 1 + end + end + + sx = sx + sxOffset + sy = sy + syOffset + + self._rooms[i][j].x = sx + self._rooms[i][j].y = sy + self._rooms[i][j].width = roomw + self._rooms[i][j].height = roomh + + for ii = sx, sx + roomw - 1 do + for jj = sy, sy + roomh - 1 do + self.map[ii][jj] = 0 + end + end + end + end +end + +function Rogue:_getWallPosition(aRoom, aDirection) + local rx, ry, door + if aDirection == 1 or aDirection == 3 then + local maxRx = aRoom.x + aRoom.width - 1 + rx = self:_getRandomInt(aRoom.x, maxRx > aRoom.x and maxRx or aRoom.x) + if aDirection == 1 then + ry = aRoom.y - 2 + door = ry + 1 + else + ry = aRoom.y + aRoom.height + 1 + door = ry - 1 + end + self.map[rx][door] = 0 + table.insert(self._doors, { x = rx, y = door }) + elseif aDirection == 2 or aDirection == 4 then + local maxRy = aRoom.y + aRoom.height - 1 + ry = self:_getRandomInt(aRoom.y, maxRy > aRoom.y and maxRy or aRoom.y) + if aDirection == 2 then + rx = aRoom.x + aRoom.width + 1 + door = rx - 1 + else + rx = aRoom.x - 2 + door = rx + 1 + end + self.map[door][ry] = 0 + table.insert(self._doors, { x = door, y = ry }) + end + return { rx, ry } +end + +function Rogue:_drawCorridor(startPosition, endPosition) + local xOffset = endPosition[1] - startPosition[1] + local yOffset = endPosition[2] - startPosition[2] + local xpos = startPosition[1] + local ypos = startPosition[2] + local moves = {} + local xAbs = math.abs(xOffset) + local yAbs = math.abs(yOffset) + local firstHalf = self._rng:random() + local secondHalf = 1 - firstHalf + local xDir = xOffset > 0 and 3 or 7 + local yDir = yOffset > 0 and 5 or 1 + if xAbs < yAbs then + local tempDist = math.ceil(yAbs * firstHalf) + table.insert(moves, { yDir, tempDist }) + table.insert(moves, { xDir, xAbs }) + tempDist = math.floor(yAbs * secondHalf) + table.insert(moves, { yDir, tempDist }) + else + local tempDist = math.ceil(xAbs * firstHalf) + table.insert(moves, { xDir, tempDist }) + table.insert(moves, { yDir, yAbs }) + tempDist = math.floor(xAbs * secondHalf) + table.insert(moves, { xDir, tempDist }) + end + + local dirs = ROT.DIRS.EIGHT + self.map[xpos][ypos] = 0 + while #moves > 0 do + local move = table.remove(moves) + if move and move[1] and move[1] < 9 and move[1] > 0 then + while move[2] > 0 do + xpos = xpos + dirs[move[1]][1] + ypos = ypos + dirs[move[1]][2] + self.map[xpos][ypos] = 0 + move[2] = move[2] - 1 + end + end + end +end + +function Rogue:_createCorridors() + local cw = self._options.cellWidth + local ch = self._options.cellHeight + local room, connection, otherRoom, wall, otherWall + + for i = 1, cw do + for j = 1, ch do + room = self._rooms[i][j] + for k = 1, #room.connections do + connection = room.connections[k] + otherRoom = self._rooms[connection[1]][connection[2]] + + if otherRoom.cellx > room.cellx then + wall = 2 + otherWall = 4 + elseif otherRoom.cellx < room.cellx then + wall = 4 + otherWall = 2 + elseif otherRoom.celly > room.celly then + wall = 3 + otherWall = 1 + elseif otherRoom.celly < room.celly then + wall = 1 + otherWall = 3 + end + self:_drawCorridor(self:_getWallPosition(room, wall), self:_getWallPosition(otherRoom, otherWall)) + end + end + end +end + +return Rogue diff --git a/lua/lib/rotLove/rot/map/room.lua b/lua/lib/rotLove/rot/map/room.lua new file mode 100644 index 0000000..5349594 --- /dev/null +++ b/lua/lib/rotLove/rot/map/room.lua @@ -0,0 +1,274 @@ +--- Room object. +-- Used by ROT.Map.Uniform and ROT.Map.Digger to create maps +-- @module ROT.Map.Room +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Room +local Room = ROT.Map.Feature:extend("Room") + +local PointSet = ROT.Type.PointSet + +--- Constructor. +-- creates a new room object with the assigned values +-- @tparam int x1 Left wall +-- @tparam int y1 Upper wall +-- @tparam int x2 Right wall +-- @tparam int y2 Bottom wall +-- @tparam[opt] int doorX x-position of door +-- @tparam[opt] int doorY y-position of door +function Room:init(x1, y1, x2, y2, doorX, doorY) + self._x1 = x1 + self._x2 = x2 + self._y1 = y1 + self._y2 = y2 + self._doors = PointSet() + if doorX and doorY then + self:addDoor(doorX, doorY) + end +end + +--- Create Random with position. +-- @tparam int x x-position of room +-- @tparam int y y-position of room +-- @tparam int dx x-direction in which to build room 1==right -1==left +-- @tparam int dy y-direction in which to build room 1==down -1==up +-- @tparam table options Options +-- @tparam table options.roomWidth minimum/maximum width for room {min,max} +-- @tparam table options.roomHeight minimum/maximum height for room {min,max} +-- @tparam[opt] userData rng A user defined object with a .random(self, min, max) method +function Room:createRandomAt(x, y, dx, dy, options, rng) + rng = rng or self._rng + local min = options.roomWidth[1] + local max = options.roomWidth[2] + local width = rng:getUniformInt(min, max) + + min = options.roomHeight[1] + max = options.roomHeight[2] + local height = rng:getUniformInt(min, max) + + if dx == 1 then + local y2 = y - math.floor(rng:getUniform() * height) + return Room:new(x + 1, y2, x + width, y2 + height - 1, x, y):setRNG(rng) + end + if dx == -1 then + local y2 = y - math.floor(rng:getUniform() * height) + return Room:new(x - width, y2, x - 1, y2 + height - 1, x, y):setRNG(rng) + end + if dy == 1 then + local x2 = x - math.floor(rng:getUniform() * width) + return Room:new(x2, y + 1, x2 + width - 1, y + height, x, y):setRNG(rng) + end + if dy == -1 then + local x2 = x - math.floor(rng:getUniform() * width) + return Room:new(x2, y - height, x2 + width - 1, y - 1, x, y):setRNG(rng) + end +end + +--- Create Random with center position. +-- @tparam int cx x-position of room's center +-- @tparam int cy y-position of room's center +-- @tparam table options Options +-- @tparam table options.roomWidth minimum/maximum width for room {min,max} +-- @tparam table options.roomHeight minimum/maximum height for room {min,max} +-- @tparam[opt] userData rng A user defined object with a .random(min, max) method +function Room:createRandomCenter(cx, cy, options, rng) + rng = rng or self._rng + local min = options.roomWidth[1] + local max = options.roomWidth[2] + local width = rng:getUniformInt(min, max) + + min = options.roomHeight[1] + max = options.roomHeight[2] + local height = rng:getUniformInt(min, max) + + local x1 = cx - math.floor(rng:random() * width) + local y1 = cy - math.floor(rng:random() * height) + local x2 = x1 + width - 1 + local y2 = y1 + height - 1 + + return Room:new(x1, y1, x2, y2):setRNG(rng) +end + +--- Create random with no position. +-- @tparam int availWidth Typically the width of the map. +-- @tparam int availHeight Typically the height of the map +-- @tparam table options Options +-- @tparam table options.roomWidth minimum/maximum width for room {min,max} +-- @tparam table options.roomHeight minimum/maximum height for room {min,max} +-- @tparam[opt] userData rng A user defined object with a .random(min, max) method +function Room:createRandom(availWidth, availHeight, options, rng) + rng = rng or self._rng + local min = options.roomWidth[1] + local max = options.roomWidth[2] + local width = rng:getUniformInt(min, max) + + min = options.roomHeight[1] + max = options.roomHeight[2] + local height = rng:getUniformInt(min, max) + + local left = availWidth - width + local top = availHeight - height + + local x1 = math.floor(rng:random() * left) + local y1 = math.floor(rng:random() * top) + local x2 = x1 + width + local y2 = y1 + height + return Room:new(x1, y1, x2, y2):setRNG(rng) +end + +--- Place a door. +-- adds an element to this rooms _doors table +-- @tparam int x the x-position of the door +-- @tparam int y the y-position of the door +function Room:addDoor(x, y) + self._doors:push(x, y) +end + +--- Get all doors. +-- Runs the provided callback on all doors for this room +-- @tparam function callback A function with two parameters (x, y) representing the position of the door. +function Room:getDoors(callback) + for _, x, y in self._doors:each() do + callback(x, y) + end +end + +--- Reset the room's _doors table. +-- @treturn ROT.Map.Room self +function Room:clearDoors() + self._doors = PointSet() + return self +end + +function Room:_checkHorizontalEdge(isWallCallback, x, y) + local top = self:getTop() - 1 + local bottom = self:getBottom() + 1 + return y == top or y == bottom and not isWallCallback(x, y + 1) and not isWallCallback(x, y - 1) +end + +function Room:_checkVerticalEdge(isWallCallback, x, y) + local left = self:getLeft() - 1 + local right = self:getRight() + 1 + return x == left or x == right and not isWallCallback(x + 1, y) and not isWallCallback(x - 1, y) +end + +function Room:_checkEdge(isWallCallback, x, y) + local v = self:_checkVerticalEdge(isWallCallback, x, y) + local h = self:_checkHorizontalEdge(isWallCallback, x, y) + return (v or h) -- and not (v and h) +end + +--- Add all doors based on available walls. +-- @tparam function isWallCallback +-- @treturn ROT.Map.Room self +function Room:addDoors(isWallCallback) + local left = self:getLeft() - 1 + local right = self:getRight() + 1 + local top = self:getTop() - 1 + local bottom = self:getBottom() + 1 + for x = left, right do + for y = top, bottom do + if isWallCallback(x, y) then + elseif self:_checkEdge(isWallCallback, x, y) then + self:addDoor(x, y) + end + end + end + return self +end + +--- Write various information about this room to the console. +function Room:debug() + local door = "doors" + for _, x, y in self._doors:each() do + door = door .. "; " .. x .. "," .. y + end + local debugString = "room : " + .. (self._x1 and self._x1 or "not available") + .. "," + .. (self._y1 and self._y1 or "not available") + .. "," + .. (self._x2 and self._x2 or "not available") + .. "," + .. (self._y2 and self._y2 or "not available") + .. "," + .. door + io.write(debugString) + io.flush() +end + +--- Use two callbacks to confirm room validity. +-- @tparam function isWallCallback A function with two parameters (x, y) that will return true if x, y represents a wall space in a map. +-- @tparam function canBeDugCallback A function with two parameters (x, y) that will return true if x, y represents a map cell that can be made into floorspace. +-- @treturn boolean true if room is valid. +function Room:isValid(isWallCallback, canBeDugCallback) + local left = self:getLeft() - 1 + local right = self:getRight() + 1 + local top = self:getTop() - 1 + local bottom = self:getBottom() + 1 + for x = left, right do + for y = top, bottom do + if x == left or x == right or y == top or y == bottom then + if not isWallCallback(x, y) then + return false + end + else + if not canBeDugCallback(x, y) then + return false + end + end + end + end + return true +end + +--- Create. +-- Function runs a callback to dig the room into a map +-- @tparam function digCallback The function responsible for digging the room into a map. +function Room:create(digCallback) + local left = self:getLeft() - 1 + local top = self:getTop() - 1 + local right = self:getRight() + 1 + local bottom = self:getBottom() + 1 + local value = 0 + for x = left, right do + for y = top, bottom do + if self._doors:find(x, y) then + value = 2 + elseif x == left or x == right or y == top or y == bottom then + value = 1 + else + value = 0 + end + digCallback(x, y, value) + end + end +end + +--- Get center cell of room +-- @treturn table {x-position, y-position} +function Room:getCenter() + return { math.ceil((self:getLeft() + self:getRight()) / 2), math.ceil((self:getTop() + self:getBottom()) / 2) } +end + +--- Get Left most floor space. +-- @treturn int left-most floor +function Room:getLeft() + return self._x1 +end +--- Get right-most floor space. +-- @treturn int right-most floor +function Room:getRight() + return self._x2 +end +--- Get top most floor space. +-- @treturn int top-most floor +function Room:getTop() + return self._y1 +end +--- Get bottom-most floor space. +-- @treturn int bottom-most floor +function Room:getBottom() + return self._y2 +end + +return Room diff --git a/lua/lib/rotLove/rot/map/uniform.lua b/lua/lib/rotLove/rot/map/uniform.lua new file mode 100644 index 0000000..f9ff048 --- /dev/null +++ b/lua/lib/rotLove/rot/map/uniform.lua @@ -0,0 +1,296 @@ +--- The Uniform Map Generator. +-- See http://www.roguebasin.roguelikedevelopment.org/index.php?title=Dungeon-Building_Algorithm. +-- @module ROT.Map.Uniform +local ROT = require((...):gsub(('.[^./\\]*'):rep(2) .. '$', '')) +local Uniform=ROT.Map.Dungeon:extend("Uniform") + +--- Constructor. +-- Called with ROT.Map.Uniform:new() +-- @tparam int width Width in cells of the map +-- @tparam int height Height in cells of the map +-- @tparam[opt] table options Options + -- @tparam[opt={4,9}] table options.roomWidth room minimum and maximum width + -- @tparam[opt={4,6}] table options.roomHeight room minimum and maximum height + -- @tparam[opt=0.2] number options.dugPercentage we stop after this percentage of level area has been dug out + -- @tparam[opt=1000] int options.timeLimit stop after this much time has passed (msec) +-- @tparam userdata rng Userdata with a .random(self, min, max) function +function Uniform:init(width, height, options) + Uniform.super.init(self, width, height) + + self._digCallback = self:bind(self._digCallback) + self._canBeDugCallback = self:bind(self._canBeDugCallback) + self._isWallCallback = self:bind(self._isWallCallback) + + self._options={ + roomWidth={4,9}, + roomHeight={4,6}, + roomDugPercentage=0.2, + timeLimit=1000 + } + if options then + for k,_ in pairs(options) do + self._options[k]=options[k] + end + end + self._roomAttempts=20 + self._corridorAttempts=20 + self._connected={} + self._unconnected={} +end + +--- Create. +-- Creates a map. +-- @tparam function callback This function will be called for every cell. It must accept the following parameters: + -- @tparam int callback.x The x-position of a cell in the map + -- @tparam int callback.y The y-position of a cell in the map + -- @tparam int callback.value A value representing the cell-type. 0==floor, 1==wall +-- @treturn ROT.Map.Uniform self +function Uniform:create(callback) + local t1=os.clock()*1000 + while true do + local t2=os.clock()*1000 + if t2-t1>self._options.timeLimit then return nil end + self._map=self:_fillMap(1) + self._dug=0 + self._rooms={} + self._unconnected={} + self:_generateRooms() + if self:_generateCorridors() then break end + end + + if not callback then return self end + for y = 1, self._height do + for x = 1, self._width do + callback(x, y, self._map[x][y]) + end + end + return self +end + +function Uniform:_generateRooms() + local w=self._width-4 + local h=self._height-4 + local room=nil + repeat + room=self:_generateRoom() + if self._dug/(w*h)>self._options.roomDugPercentage then break end + until not room +end + +function Uniform:_generateRoom() + local count=0 + while count0 and 3 or 1 + dirIndex2=(dirIndex1+1)%4+1 + min =room2:getLeft() + max =room2:getRight() + index =1 + else + dirIndex1=diffX>0 and 2 or 4 + dirIndex2=(dirIndex1+1)%4+1 + min =room2:getTop() + max =room2:getBottom() + index =2 + end + + local index2=(index%2)+1 + + local start=self:_placeInWall(room1, dirIndex1) + if not start or #start<1 then return false end + local endTbl={} + + if start[index] >= min and start[index] <= max then + endTbl=table.slice(start) + local value=nil + if dirIndex2==1 then value=room2:getTop() -1 + elseif dirIndex2==2 then value=room2:getRight() +1 + elseif dirIndex2==3 then value=room2:getBottom()+1 + elseif dirIndex2==4 then value=room2:getLeft() -1 + end + endTbl[index2]=value + self:_digLine({start, endTbl}) + elseif start[index] < min-1 or start[index] > max+1 then + local diff=start[index]-center2[index] + local rotation=0 + if dirIndex2==1 or dirIndex2==2 then rotation=diff<0 and 2 or 4 + elseif dirIndex2==3 or dirIndex2==4 then rotation=diff<0 and 4 or 2 end + if rotation==0 then assert(false, 'failed to rotate') end + dirIndex2=(dirIndex2+rotation)%4+1 + + endTbl=self:_placeInWall(room2, dirIndex2) + if not endTbl then return false end + + local mid={0,0} + mid[index]=start[index] + mid[index2]=endTbl[index2] + self:_digLine({start, mid, endTbl}) + else + endTbl=self:_placeInWall(room2, dirIndex2) + if #endTbl<1 then return false end + local mid =math.round((endTbl[index2]+start[index2])/2) + + local mid1={0,0} + local mid2={0,0} + mid1[index] = start[index]; + mid1[index2] = mid; + mid2[index] = endTbl[index]; + mid2[index2] = mid; + self:_digLine({start, mid1, mid2, endTbl}); + end + + room1:addDoor(start[1],start[2]) + room2:addDoor(endTbl[1], endTbl[2]) + + index=table.indexOf(self._unconnected, room1) + if index>0 then + table.insert(self._connected, table.remove(self._unconnected, index)) + end + + return true +end + +function Uniform:_placeInWall(room, dirIndex) + local start ={0,0} + local dir ={0,0} + local length=0 + local retTable={} + + if dirIndex==1 then + dir ={1,0} + start ={room:getLeft()-1, room:getTop()-1} + length= room:getRight()-room:getLeft() + elseif dirIndex==2 then + dir ={0,1} + start ={room:getRight()+1, room:getTop()} + length=room:getBottom()-room:getTop() + elseif dirIndex==3 then + dir ={1,0} + start ={room:getLeft()-1, room:getBottom()+1} + length=room:getRight()-room:getLeft() + elseif dirIndex==4 then + dir ={0,1} + start ={room:getLeft()-1, room:getTop()-1} + length=room:getBottom()-room:getTop() + end + local avail={} + local lastBadIndex=-1 + local null=string.char(0) + for i=1,length do + local x=start[1]+i*dir[1] + local y=start[2]+i*dir[2] + table.insert(avail, null) + if self._map[x][y]==1 then --is a wall + if lastBadIndex ~=i-1 then + avail[i]={x, y} + end + else + lastBadIndex=i + if i>1 then avail[i-1]=null end + end + end + + for i=1,#avail do + if avail[i]~=string.char(0) then + table.insert(retTable, avail[i]) + i=i-1 + end + end + return #retTable>0 and table.random(retTable) or nil +end + +function Uniform:_digLine(points) + for i=2,#points do + local start=points[i-1] + local endPt=points[i] + local corridor=ROT.Map.Corridor:new(start[1], start[2], endPt[1], endPt[2]) + corridor:create(self._digCallback) + table.insert(self._corridors, corridor) + end +end + +function Uniform:_digCallback(x, y, value) + self._map[x][y]=value + if value==0 then self._dug=self._dug+1 end +end + +function Uniform:_isWallCallback(x, y) + if x<1 or y<1 or x>self._width or y>self._height then return false end + return self._map[x][y]==1 +end + +function Uniform:_canBeDugCallback(x, y) + if x<2 or y<2 or x>=self._width or y>=self._height then return false end + return self._map[x][y]==1 +end + +return Uniform diff --git a/lua/lib/rotLove/rot/newFuncs.lua b/lua/lib/rotLove/rot/newFuncs.lua new file mode 100644 index 0000000..49bea0c --- /dev/null +++ b/lua/lib/rotLove/rot/newFuncs.lua @@ -0,0 +1,145 @@ +local ROT = require((...):gsub(('.[^./\\]*'):rep(1) .. '$', '')) + +-- asserts the type of 'theTable' is table +local function isATable(theTable) + ROT.assert(type(theTable)=='table', "bad argument #1 to 'random' (table expected got ",type(theTable),")") +end + +-- returns string of length n consisting of only char c +local function charNTimes(c, n) + ROT.assert(#c==1, 'character must be a string of length 1') + local s='' + for _=1,n and n or 2 do + s=s..c + end + return s +end + +-- New Table Functions +-- returns random table element, nil if length is 0 +function table.random(theTable) + isATable(theTable) + if #theTable==0 then return nil end + return theTable[math.floor(ROT.RNG:random(#theTable))] +end +-- returns random valid index, nil if length is 0 +function table.randomi(theTable) + isATable(theTable) + if #theTable==0 then return nil end + return math.floor(ROT.RNG:random(#theTable)) +end +-- randomly reorders the elements of the provided table and returns the result +function table.randomize(theTable) + isATable(theTable) + local result={} + while #theTable>0 do + table.insert(result, table.remove(theTable, table.randomi(theTable))) + end + return result +end +-- add js slice function +function table.slice (values,i1,i2) + local res = {} + local n = #values + -- default values for range + i1 = i1 or 1 + i2 = i2 or n + if i2 < 0 then + i2 = n + i2 + 1 + elseif i2 > n then + i2 = n + end + if i1 < 1 or i1 > n then + return {} + end + local k = 1 + for i = i1,i2 do + res[k] = values[i] + k = k + 1 + end + return res +end +-- add js indexOf function +function table.indexOf(values,value) + if values then + for i=1,#values do + if values[i] == value then return i end + end + end + if type(value)=='table' then return table.indexOfTable(values, value) end + return 0 +end + +-- extended for use with tables of tables +function table.indexOfTable(values, value) + if type(value)~='table' then return 0 end + for k,v in ipairs(values) do + if #v==#value then + local match=true + for i=1,#v do + if v[i]~=value[i] then match=false end + end + if match then return k end + end + end + return 0 +end + +-- New String functions +-- first letter capitalized +function string:capitalize() + return self:sub(1,1):upper() .. self:sub(2) +end +-- left pad with c char, repeated n times +function string:lpad(c, n) + c=c and c or '0' + n=n and n or 2 + local s='' + while #s < n-#self do s=s..c end + return s..self +end +-- right pad with c char, repeated n times +function string:rpad(c, n) + c=c and c or '0' + n=n and n or 2 + local s='' + while #s < n-#self do s=s..c end + return self..s +end +-- add js split function +function string:split(delim, maxNb) + -- Eliminate bad cases... + if string.find(self, delim) == nil then + return { self } + end + local result = {} + if delim == '' or not delim then + for i=1,#self do + result[i]=self:sub(i,i) + end + return result + end + if maxNb == nil or maxNb < 1 then + maxNb = 0 -- No limit + end + local pat = "(.-)" .. delim .. "()" + local nb = 0 + local lastPos + for part, pos in string.gmatch(self, pat) do + nb = nb + 1 + result[nb] = part + lastPos = pos + if nb == maxNb then break end + end + -- Handle the last field + if nb ~= maxNb then + result[nb + 1] = string.sub(self, lastPos) + end + return result +end + +function math.round(n, mult) + mult = mult or 1 + return math.floor((n + mult/2)/mult) * mult +end + diff --git a/lua/lib/rotLove/rot/noise.lua b/lua/lib/rotLove/rot/noise.lua new file mode 100644 index 0000000..79d1fe9 --- /dev/null +++ b/lua/lib/rotLove/rot/noise.lua @@ -0,0 +1,7 @@ +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Noise +local Noise = ROT.Class:extend("Noise") + +function Noise:get() end + +return Noise diff --git a/lua/lib/rotLove/rot/noise/simplex.lua b/lua/lib/rotLove/rot/noise/simplex.lua new file mode 100644 index 0000000..8d058af --- /dev/null +++ b/lua/lib/rotLove/rot/noise/simplex.lua @@ -0,0 +1,109 @@ +--- Simplex Noise Generator. +-- Based on a simple 2d implementation of simplex noise by Ondrej Zara +-- Which is based on a speed-improved simplex noise algorithm for 2D, 3D and 4D in Java. +-- Which is based on example code by Stefan Gustavson (stegu@itn.liu.se). +-- With Optimisations by Peter Eastman (peastman@drizzle.stanford.edu). +-- Better rank ordering method by Stefan Gustavson in 2012. +-- @module ROT.Noise.Simplex +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Simplex +local Simplex = ROT.Noise:extend("Simplex") +--- Constructor. +-- 2D simplex noise generator. +-- @tparam int gradients The random values for the noise. +function Simplex:init(gradients) + self._F2 = 0.5 * (math.sqrt(3) - 1) + self._G2 = (3 - math.sqrt(3)) / 6 + + self._gradients = { + { 0, -1 }, + { 1, -1 }, + { 1, 0 }, + { 1, 1 }, + { 0, 1 }, + { -1, 1 }, + { -1, 0 }, + { -1, -1 }, + } + local permutations = {} + local count = gradients and gradients or 256 + for i = 1, count do + table.insert(permutations, i) + end + + permutations = table.randomize(permutations) + + self._perms = {} + self._indexes = {} + + for i = 1, 2 * count do + table.insert(self._perms, permutations[i % count + 1]) + table.insert(self._indexes, self._perms[i] % #self._gradients + 1) + end +end + +--- Get noise for a cell +-- Iterate over this function to retrieve noise values +-- @tparam int xin x-position of noise value +-- @tparam int yin y-position of noise value +function Simplex:get(xin, yin) + local perms = self._perms + local indexes = self._indexes + local count = #perms / 2 + local G2 = self._G2 + + local n0, n1, n2, gi = 0, 0, 0 + + local s = (xin + yin) * self._F2 + local i = math.floor(xin + s) + local j = math.floor(yin + s) + local t = (i + j) * G2 + local X0 = i - t + local Y0 = j - t + local x0 = xin - X0 + local y0 = yin - Y0 + + local i1, j1 + if x0 > y0 then + i1 = 1 + j1 = 0 + else + i1 = 0 + j1 = 1 + end + + local x1 = x0 - i1 + G2 + local y1 = y0 - j1 + G2 + local x2 = x0 - 1 + 2 * G2 + local y2 = y0 - 1 + 2 * G2 + + local ii = i % count + 1 + local jj = j % count + 1 + + local t0 = 0.5 - x0 * x0 - y0 * y0 + if t0 >= 0 then + t0 = t0 * t0 + gi = indexes[ii + perms[jj]] + local grad = self._gradients[gi] + n0 = t0 * t0 * (grad[1] * x0 + grad[2] * y0) + end + + local t1 = 0.5 - x1 * x1 - y1 * y1 + if t1 >= 0 then + t1 = t1 * t1 + gi = indexes[ii + i1 + perms[jj + j1]] + local grad = self._gradients[gi] + n1 = t1 * t1 * (grad[1] * x1 + grad[2] * y1) + end + + local t2 = 0.5 - x2 * x2 - y2 * y2 + if t2 >= 0 then + t2 = t2 * t2 + gi = indexes[ii + 1 + perms[jj + 1]] + local grad = self._gradients[gi] + n2 = t2 * t2 * (grad[1] * x2 + grad[2] * y2) + end + return 70 * (n0 + n1 + n2) +end + +return Simplex diff --git a/lua/lib/rotLove/rot/path.lua b/lua/lib/rotLove/rot/path.lua new file mode 100644 index 0000000..5b36509 --- /dev/null +++ b/lua/lib/rotLove/rot/path.lua @@ -0,0 +1,49 @@ +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Path +local Path = ROT.Class:extend("Path") + +function Path:init(toX, toY, passableCallback, options) + self._toX = toX + self._toY = toY + self._fromX = nil + self._fromY = nil + self._passableCallback = passableCallback + self._options = { topology = 8 } + + if options then + for k, _ in pairs(options) do + self._options[k] = options[k] + end + end + + self._dirs = self._options.topology == 8 and ROT.DIRS.EIGHT or ROT.DIRS.FOUR + if self._options.topology == 8 then + self._dirs = { + self._dirs[1], + self._dirs[3], + self._dirs[5], + self._dirs[7], + self._dirs[2], + self._dirs[4], + self._dirs[6], + self._dirs[8], + } + end +end + +function Path:compute() end + +function Path:_getNeighbors(cx, cy) + local result = {} + for i = 1, #self._dirs do + local dir = self._dirs[i] + local x = cx + dir[1] + local y = cy + dir[2] + if self._passableCallback(x, y) then + table.insert(result, { x, y }) + end + end + return result +end + +return Path diff --git a/lua/lib/rotLove/rot/path/astar.lua b/lua/lib/rotLove/rot/path/astar.lua new file mode 100644 index 0000000..f6f7308 --- /dev/null +++ b/lua/lib/rotLove/rot/path/astar.lua @@ -0,0 +1,95 @@ +--- A* Pathfinding. +-- Simplified A* algorithm: all edges have a value of 1 +-- @module ROT.Path.AStar +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class AStar +local AStar = ROT.Path:extend("AStar") +--- Constructor. +-- @tparam int toX x-position of destination cell +-- @tparam int toY y-position of destination cell +-- @tparam function passableCallback Function with two parameters (x, y) that returns true if the cell at x,y is able to be crossed +-- @tparam table options Options +-- @tparam[opt=8] int options.topology Directions for movement Accepted values (4 or 8) +function AStar:init(toX, toY, passableCallback, options) + AStar.super.init(self, toX, toY, passableCallback, options) + self._todo = {} + self._done = {} + self._fromX = nil + self._fromY = nil +end + +--- Compute the path from a starting point +-- @tparam int fromX x-position of starting point +-- @tparam int fromY y-position of starting point +-- @tparam function callback Will be called for every path item with arguments "x" and "y" +function AStar:compute(fromX, fromY, callback) + self._todo = {} + self._done = {} + self._fromX = tonumber(fromX) + self._fromY = tonumber(fromY) + self._done[self._toX] = {} + self:_add(self._toX, self._toY, nil) + + while #self._todo > 0 do + local item = table.remove(self._todo, 1) + if item.x == fromX and item.y == fromY then + break + end + local neighbors = self:_getNeighbors(item.x, item.y) + + for i = 1, #neighbors do + local x = neighbors[i][1] + local y = neighbors[i][2] + if not self._done[x] then + self._done[x] = {} + end + if not self._done[x][y] then + self:_add(x, y, item) + end + end + end + + local item = self._done[self._fromX] and self._done[self._fromX][self._fromY] or nil + if not item then + return + end + + while item do + callback(tonumber(item.x), tonumber(item.y)) + item = item.prev + end +end + +function AStar:_add(x, y, prev) + local h = self:_distance(x, y) + local obj = {} + obj.x = x + obj.y = y + obj.prev = prev + obj.g = prev and prev.g + 1 or 0 + obj.h = h + self._done[x][y] = obj + + local f = obj.g + obj.h + + for i = 1, #self._todo do + local item = self._todo[i] + local itemF = item.g + item.h + if f < itemF or (f == itemF and h < item.h) then + table.insert(self._todo, i, obj) + return + end + end + + table.insert(self._todo, obj) +end + +function AStar:_distance(x, y) + if self._options.topology == 4 then + return math.abs(x - self._fromX) + math.abs(y - self._fromY) + elseif self._options.topology == 8 then + return math.max(math.abs(x - self._fromX), math.abs(y - self._fromY)) + end +end + +return AStar diff --git a/lua/lib/rotLove/rot/path/dijkstra.lua b/lua/lib/rotLove/rot/path/dijkstra.lua new file mode 100644 index 0000000..6981268 --- /dev/null +++ b/lua/lib/rotLove/rot/path/dijkstra.lua @@ -0,0 +1,67 @@ +--- Dijkstra Pathfinding. +-- Simplified Dijkstra's algorithm: all edges have a value of 1 +-- @module ROT.Path.Dijkstra +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Dijkstra +local Dijkstra = ROT.Path:extend("Dijkstra") + +local Grid = ROT.Type.Grid + +--- Constructor. +-- @tparam int toX x-position of destination cell +-- @tparam int toY y-position of destination cell +-- @tparam function passableCallback Function with two parameters (x, y) that returns true if the cell at x,y is able to be crossed +-- @tparam table options Options +-- @tparam[opt=8] int options.topology Directions for movement Accepted values (4 or 8) +function Dijkstra:init(toX, toY, passableCallback, options) + toX, toY = tonumber(toX), tonumber(toY) + Dijkstra.super.init(self, toX, toY, passableCallback, options) + + self._computed = Grid() + self._todo = {} + + self:_add(toX, toY) +end + +--- Compute the path from a starting point +-- @tparam int fromX x-position of starting point +-- @tparam int fromY y-position of starting point +-- @tparam function callback Will be called for every path item with arguments "x" and "y" +function Dijkstra:compute(fromX, fromY, callback) + fromX, fromY = tonumber(fromX), tonumber(fromY) + + local item = self._computed:getCell(fromX, fromY) or self:_compute(fromX, fromY) + + while item do + callback(item.x, item.y) + item = item.prev + end +end + +function Dijkstra:_compute(fromX, fromY) + while #self._todo > 0 do + local item = table.remove(self._todo, 1) + if item.x == fromX and item.y == fromY then + return item + end + + local neighbors = self:_getNeighbors(item.x, item.y) + + for i = 1, #neighbors do + local x = neighbors[i][1] + local y = neighbors[i][2] + if not self._computed:getCell(x, y) then + self:_add(x, y, item) + end + end + end +end + +function Dijkstra:_add(x, y, prev) + local obj = { x = x, y = y, prev = prev } + + self._computed:setCell(x, y, obj) + table.insert(self._todo, obj) +end + +return Dijkstra diff --git a/lua/lib/rotLove/rot/path/dijkstraMap.lua b/lua/lib/rotLove/rot/path/dijkstraMap.lua new file mode 100644 index 0000000..8963aa7 --- /dev/null +++ b/lua/lib/rotLove/rot/path/dijkstraMap.lua @@ -0,0 +1,183 @@ +--- DijkstraMap Pathfinding. +-- Based on the DijkstraMap Article on RogueBasin, http://roguebasin.roguelikedevelopment.org/index.php?title=The_Incredible_Power_of_Dijkstra_Maps +-- @module ROT.DijkstraMap +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class DijkstraMap +local DijkstraMap = ROT.Path:extend("DijkstraMap") + +local PointSet = ROT.Type.PointSet +local Grid = ROT.Type.Grid + +--- Constructor. +-- @tparam int goalX x-position of cell that map will 'roll down' to +-- @tparam int goalY y-position of cell that map will 'roll down' to +-- @tparam function passableCallback a function with two parameters (x, y) that returns true if a map cell is passable +-- @tparam table options Options +-- @tparam[opt=8] int options.topology Directions for movement Accepted values (4 or 8) +function DijkstraMap:init(goalX, goalY, passableCallback, options) + DijkstraMap.super.init(self, goalX, goalY, passableCallback, options) + self._map = Grid() + self._goals = PointSet() + self._dirty = true + if goalX and goalY then + self:addGoal(goalX, goalY) + end +end + +--- Establish values for all cells in map. +-- call after ROT.DijkstraMap:new(goalX, goalY, passableCallback) +function DijkstraMap:compute(x, y, callback, topology) + self:_rebuild() + local dx, dy = self:dirTowardsGoal(x, y, topology) + if dx then + callback(x, y) + end + while dx do + x, y = x + dx, y + dy + callback(x, y) + dx, dy = self:dirTowardsGoal(x, y, topology) + end +end + +--- Run a callback function on every cell in the map +-- @tparam function callback A function with x and y parameters that will be run on every cell in the map +function DijkstraMap:create(callback) + self:_rebuild() + for _, x, y, v in self._map:each() do + callback(x, y, v) + end +end + +--- Check if a goal exists at a position. +-- @tparam int x the x-value to check +-- @tparam int y the y-value to check +function DijkstraMap:hasGoal(x, y) + return not not self._goals:find(x, y) +end + +--- Add new goal. +-- @tparam int x the x-value of the new goal cell +-- @tparam int y the y-value of the new goal cell +function DijkstraMap:addGoal(x, y) + if self._goals:push(x, y) then + self._dirty = true + end + return self +end + +--- Remove a goal. +-- @tparam int gx the x-value of the goal cell +-- @tparam int gy the y-value of the goal cell +function DijkstraMap:removeGoal(x, y) + if self._goals:prune(x, y) then + self._dirty = true + end + return self +end + +--- Remove all goals. +function DijkstraMap:clearGoals() + self._goals = PointSet() + self._dirty = true + return self +end + +--- Get the direction of the goal from a given position +-- @tparam int x x-value of current position +-- @tparam int y y-value of current position +-- @treturn int xDir X-Direction towards goal. Either -1, 0, or 1 +-- @treturn int yDir Y-Direction towards goal. Either -1, 0, or 1 +function DijkstraMap:dirTowardsGoal(x, y, topology) + local low = self._map:getCell(x, y) + if not low or low == 0 or low == math.huge then + return + end + local dir = nil + for i = 1, topology or self._options.topology do + local v = ROT.DIRS.FOUR[i] or ROT.DIRS.EIGHT[(i - 4) * 2] + local tx = (x + v[1]) + local ty = (y + v[2]) + local val = self._map:getCell(tx, ty) + if val and i < 5 and val <= low or val < low then + low = val + dir = v + end + end + if dir then + return dir[1], dir[2] + end +end + +--- Output map values to console. +-- For debugging, will send a comma separated output of cell values to the console. +-- @tparam boolean[opt=false] returnString Will return the output in addition to sending it to console if true. +function DijkstraMap:debug(returnString) + self:_rebuild() + local ls + + if returnString then + ls = "" + end + for y = 1, self._dimensions.h do + local s = "" + for x = 1, self._dimensions.w do + s = s .. self._map:getCell(x, y) .. "," + end + io.write(s) + io.flush() + if returnString then + ls = ls .. s .. "\n" + end + end + if returnString then + return ls + end +end + +function DijkstraMap:_addCell(x, y, value) + self._nextCells:push(x, y) + self._map:setCell(x, y, value) + return value +end + +function DijkstraMap:_visitAdjacent(x, y) + if not self._passableCallback(x, y) then + return + end + + local low = math.huge + + for i = 1, #self._dirs do + local tx = x + self._dirs[i][1] + local ty = y + self._dirs[i][2] + local value = self._map:getCell(tx, ty) or self:_addCell(tx, ty, math.huge) + + low = math.min(low, value) + end + + if self._map:getCell(x, y) > low + 2 then + self._map:setCell(x, y, low + 1) + end +end + +function DijkstraMap:_rebuild(callback) + if not self._dirty then + return + end + self._dirty = false + + self._nextCells = PointSet() + self._map = Grid() + + for _, x, y in self._goals:each() do + self:_addCell(x, y, 0) + end + + while #self._nextCells > 0 do + for i in self._nextCells:each() do + self:_visitAdjacent(self._nextCells:pluck(i)) + end + end +end + +return DijkstraMap diff --git a/lua/lib/rotLove/rot/rng.lua b/lua/lib/rotLove/rot/rng.lua new file mode 100644 index 0000000..a84f96f --- /dev/null +++ b/lua/lib/rotLove/rot/rng.lua @@ -0,0 +1,165 @@ +--- The RNG Class. +-- A Lua port of Johannes Baagøe's Alea +-- From http://baagoe.com/en/RandomMusings/javascript/ +-- Johannes Baagøe , 2010 +-- Mirrored at: +-- https://github.com/nquinlan/better-random-numbers-for-javascript-mirror +-- @module ROT.RNG +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class RNG +local RNG = ROT.Class:extend("RNG") + +local function Mash() + local n = 0xefc8249d + + local function mash(data) + data = tostring(data) + + for i = 1, data:len() do + n = n + data:byte(i) + local h = 0.02519603282416938 * n + n = math.floor(h) + h = h - n + h = h * n + n = math.floor(h) + h = h - n + n = n + h * 0x100000000 -- 2^32 + end + return math.floor(n) * 2.3283064365386963e-10 -- 2^-32 + end + + return mash +end + +function RNG:init(seed) + self.s0 = 0 + self.s1 = 0 + self.s2 = 0 + self.c = 1 + self:setSeed(seed) +end + +--- Seed. +-- seed the rng +-- @tparam[opt=os.clock()] number s A number to base the rng from +function RNG:getSeed() + return self.seed +end + +--- Seed. +-- seed the rng +-- @tparam[opt=os.clock()] number s A number to base the rng from +function RNG:setSeed(seed) + self.c = 1 + self.seed = seed or os.time() + + local mash = Mash() + self.s0 = mash(" ") + self.s1 = mash(" ") + self.s2 = mash(" ") + + self.s0 = self.s0 - mash(self.seed) + if self.s0 < 0 then + self.s0 = self.s0 + 1 + end + self.s1 = self.s1 - mash(self.seed) + if self.s1 < 0 then + self.s1 = self.s1 + 1 + end + self.s2 = self.s2 - mash(self.seed) + if self.s2 < 0 then + self.s2 = self.s2 + 1 + end + mash = nil +end + +function RNG:getUniform() + -- return self.implementation() + local t = 2091639 * self.s0 + self.c * 2.3283064365386963e-10 -- 2^-32 + self.s0 = self.s1 + self.s1 = self.s2 + self.c = math.floor(t) + self.s2 = t - self.c + return self.s2 +end + +function RNG:getUniformInt(lowerBound, upperBound) + local max = math.max(lowerBound, upperBound) + local min = math.min(lowerBound, upperBound) + return math.floor(self:getUniform() * (max - min + 1)) + min +end + +function RNG:getNormal(mean, stddev) + repeat + local u = 2 * self:getUniform() - 1 + local v = 2 * self:getUniform() - 1 + local r = u * u + v * v + until r > 1 or r == 0 + + local gauss = u * math.sqrt(-2 * math.log(r) / r) + return (mean or 0) + gauss * (stddev or 1) +end + +function RNG:getPercentage() + return 1 + math.floor(self:getUniform() * 100) +end + +function RNG:getWeightedValue(tbl) + local total = 0 + for _, v in pairs(tbl) do + total = total + v + end + local rand = self:getUniform() * total + local part = 0 + for k, v in pairs(tbl) do + part = part + v + if rand < part then + return k + end + end + return nil +end + +--- Get current rng state +-- Returns a table that can be given to the rng to return it to this state. +-- Any RNG of the same type will always produce the same values from this state. +-- @treturn table A table that represents the current state of the rng +function RNG:getState() + return { self.s0, self.s1, self.s2, self.c, self.seed } +end + +--- Set current rng state +-- used to return an rng to a known/previous state +-- @tparam table stateTable The table retrieved from .getState() +function RNG:setState(t) + self.s0, self.s1, self.s2, self.c, self.seed = t[1], t[2], t[3], t[4], t[5] +end + +function RNG:clone() + local clone = self:extend() + clone:setState(self:getState()) + return clone +end + +-- Methods below mirror Lua's math.random and math.randomseed + +--- Random. +-- get a random number +-- @tparam[opt=0] int a lower threshold for random numbers +-- @tparam[opt=1] int b upper threshold for random numbers +-- @treturn number a random number +function RNG:random(a, b) + if not a then + return self:getUniform() + elseif not b then + return self:getUniformInt(1, tonumber(a)) + else + return self:getUniformInt(tonumber(a), tonumber(b)) + end +end + +RNG.randomseed = RNG.setSeed + +RNG:init() + +return RNG diff --git a/lua/lib/rotLove/rot/scheduler.lua b/lua/lib/rotLove/rot/scheduler.lua new file mode 100644 index 0000000..e87039d --- /dev/null +++ b/lua/lib/rotLove/rot/scheduler.lua @@ -0,0 +1,74 @@ +--- The Scheduler Prototype +-- @module ROT.Scheduler +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class Scheduler +local Scheduler = ROT.Class:extend("Scheduler") + +function Scheduler:init() + self._queue = ROT.EventQueue:new() + self._repeat = {} + self._current = nil +end + +--- Get Time. +-- Get time counted since start +-- @treturn int elapsed time +function Scheduler:getTime() + return self._queue:getTime() +end + +--- Add. +-- Add an item to the schedule +-- @tparam any item +-- @tparam boolean repeating If true, this item will be rescheduled once it is returned by .next() +-- @treturn ROT.Scheduler self +function Scheduler:add(item, repeating) + if repeating then + table.insert(self._repeat, item) + end + return self +end + +--- Get scheduled time. +-- Get the time the given item is scheduled for +-- @tparam any item +-- @treturn number time +function Scheduler:getTimeOf(item) + return self._queue:getEventTime(item) +end + +--- Clear. +-- Remove all items from scheduler +-- @treturn ROT.Scheduler self +function Scheduler:clear() + self._queue:clear() + self._repeat = {} + self._current = nil + return self +end + +--- Remove. +-- Find and remove an item from the scheduler +-- @tparam any item The previously added item to be removed +-- @treturn boolean true if an item was removed from the scheduler +function Scheduler:remove(item) + local result = self._queue:remove(item) + local index = table.indexOf(self._repeat, item) + if index ~= 0 then + table.remove(self._repeat, index) + end + if self._current == item then + self._current = nil + end + return result +end + +--- Next. +-- Get the next event from the scheduler and advance the appropriate amount time +-- @treturn event|nil The event previously added by .add() or nil if none are queued +function Scheduler:next() + self._current = self._queue:get() + return self._current +end + +return Scheduler diff --git a/lua/lib/rotLove/rot/scheduler/action.lua b/lua/lib/rotLove/rot/scheduler/action.lua new file mode 100644 index 0000000..cb2d25b --- /dev/null +++ b/lua/lib/rotLove/rot/scheduler/action.lua @@ -0,0 +1,63 @@ +--- Action based turn scheduler. +-- @module ROT.Scheduler.Action +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Action +local Action = ROT.Scheduler:extend("Action") +function Action:init() + Action.super.init(self) + self._defaultDuration = 1 + self._duration = self._defaultDuration +end + +--- Add. +-- Add an item to the scheduler. +-- @tparam any item The item that is returned when this turn comes up +-- @tparam boolean repeating If true, when this turn comes up, it will be added to the queue again +-- @tparam[opt=1] int time an initial delay time +-- @treturn ROT.Scheduler.Action self +function Action:add(item, repeating, time) + self._queue:add(item, time and time or self._defaultDuration) + return Action.super.add(self, item, repeating) +end + +--- Clear. +-- empties this scheduler's event queue, no items will be returned by .next() until more are added with .add() +-- @treturn ROT.Scheduler.Action self +function Action:clear() + self._duration = self._defaultDuration + return Action.super.clear(self) +end + +--- Remove. +-- Looks for the next instance of item in the event queue +-- @treturn ROT.Scheduler.Action self +function Action:remove(item) + if item == self._current then + self._duration = self._defaultDuration + end + return Action.super.remove(self, item) +end + +--- Next. +-- returns the next item based on that item's last action's duration +-- @return item +function Action:next() + if self._current and table.indexOf(self._repeat, self._current) ~= 0 then + self._queue:add(self._current, self._duration and self._duration or self._defaultDuration) + self._duration = self._defaultDuration + end + return Action.super.next(self) +end + +--- set duration for the active item +-- after calling next() this function defines the duration of that item's action +-- @tparam int time The amount of time that the current item's action should last. +-- @treturn ROT.Scheduler.Action self +function Action:setDuration(time) + if self._current then + self._duration = time + end + return self +end + +return Action diff --git a/lua/lib/rotLove/rot/scheduler/simple.lua b/lua/lib/rotLove/rot/scheduler/simple.lua new file mode 100644 index 0000000..6876f02 --- /dev/null +++ b/lua/lib/rotLove/rot/scheduler/simple.lua @@ -0,0 +1,43 @@ +--- The simple scheduler. +-- @module ROT.Scheduler.Simple +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Simple +local Simple = ROT.Scheduler:extend("Simple") + +--- Add. +-- Add an item to the schedule +-- @tparam any item +-- @tparam boolean repeating If true, this item will be rescheduled once it is returned by .next() +-- @treturn ROT.Scheduler.Simple self +function Simple:add(item, repeating) + self._queue:add(item, 0) + return Simple.super.add(self, item, repeating) +end + +--- Next. +-- Get the next item from the scheduler and advance the appropriate amount time +-- @treturn item|nil The item previously added by .add() or nil if none are queued +function Simple:next() + if self._current and table.indexOf(self._repeat, self._current) ~= 0 then + self._queue:add(self._current, 0) + end + return Simple.super.next(self) +end + +return Simple + +--- Get Time. +-- Get time counted since start +-- @treturn int elapsed time +-- @function Simple:getTime() + +--- Clear. +-- Remove all items from scheduler +-- @treturn ROT.Scheduler.Simple self +-- @function Simple:clear() + +--- Remove. +-- Find and remove an item from the scheduler +-- @tparam any item The previously added item to be removed +-- @treturn boolean true if an item was removed from the scheduler +-- @function Simple:remove(item) diff --git a/lua/lib/rotLove/rot/scheduler/speed.lua b/lua/lib/rotLove/rot/scheduler/speed.lua new file mode 100644 index 0000000..9a4453e --- /dev/null +++ b/lua/lib/rotLove/rot/scheduler/speed.lua @@ -0,0 +1,43 @@ +--- The Speed based scheduler +-- @module ROT.Scheduler.Speed +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Speed +local Speed = ROT.Scheduler:extend("Speed") +--- Add. +-- Add an item to the schedule +-- @tparam userdata item Any class/module/userdata with a :getSpeed() function. The value returned by getSpeed() should be a number. +-- @tparam boolean repeating If true, this item will be rescheduled once it is returned by .next() +-- @tparam number time Initial time offset, defaults to 1/item:getSpeed() +-- @treturn ROT.Scheduler.Speed self +function Speed:add(item, repeating, time) + self._queue:add(item, time or (1 / item:getSpeed())) + return Speed.super.add(self, item, repeating) +end + +--- Next. +-- Get the next item from the scheduler and advance the appropriate amount time +-- @treturn item|nil The item previously added by .add() or nil if none are queued +function Speed:next() + if self._current and table.indexOf(self._repeat, self._current) ~= 0 then + self._queue:add(self._current, 1 / self._current:getSpeed()) + end + return Speed.super.next(self) +end + +return Speed + +--- Get Time. +-- Get time counted since start +-- @treturn int elapsed time +-- @function Speed:getTime() + +--- Clear. +-- Remove all items from scheduler +-- @treturn ROT.Scheduler.Speed self +-- @function Speed:clear() + +--- Remove. +-- Find and remove an item from the scheduler +-- @tparam any item The previously added item to be removed +-- @treturn boolean true if an item was removed from the scheduler +-- @function Speed:remove(item) diff --git a/lua/lib/rotLove/rot/stringGenerator.lua b/lua/lib/rotLove/rot/stringGenerator.lua new file mode 100644 index 0000000..eb1d689 --- /dev/null +++ b/lua/lib/rotLove/rot/stringGenerator.lua @@ -0,0 +1,169 @@ +--- Random String Generator. +-- Learns from provided strings, and generates similar strings. +-- @module ROT.StringGenerator +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class StringGenerator +local StringGenerator = ROT.Class:extend("StringGenerator") + +--- Constructor. +-- Called with ROT.StringGenerator:new() +-- @tparam table options A table with the following fields: +-- @tparam[opt=false] boolean options.words Use word mode +-- @tparam[opt=3] int options.order Number of letters/words to be used as context +-- @tparam[opt=0.001] number options.prior A default priority for characters/words +function StringGenerator:init(options) + self._options = { words = false, order = 3, prior = 0.001 } + self._boundary = string.char(0) + self._suffix = string.char(0) + self._prefix = {} + self._priorValues = {} + self._data = {} + if options then + for k, v in pairs(options) do + self._options[k] = v + end + end + for _ = 1, self._options.order do + table.insert(self._prefix, self._boundary) + end + self._priorValues[self._boundary] = self._options.prior +end + +--- Remove all learned data +function StringGenerator:clear() + self._data = {} + self._priorValues = {} +end + +--- Generate a string +-- @treturn string The generated string +function StringGenerator:generate() + local result = { self:_sample(self._prefix) } + while result[#result] ~= self._boundary do + table.insert(result, self:_sample(result)) + end + table.remove(result) + return table.concat(result) +end + +--- Observe +-- Learn from a string +-- @tparam string s The string to observe +function StringGenerator:observe(s) + local tokens = self:_split(s) + for i = 1, #tokens do + self._priorValues[tokens[i]] = self._options.prior + end + local i = 1 + for _, v in pairs(self._prefix) do + table.insert(tokens, i, v) + i = i + 1 + end + table.insert(tokens, self._suffix) + for i = self._options.order, #tokens - 1 do + local context = table.slice(tokens, i - self._options.order + 1, i) + local evt = tokens[i + 1] + for j = 1, #context do + local subcon = table.slice(context, j) + self:_observeEvent(subcon, evt) + end + end +end + +--- get Stats +-- Get info about learned strings +-- @treturn string Number of observed strings, number of contexts, number of possible characters/words +function StringGenerator:getStats() + local parts = {} + local prC = 0 + for _ in pairs(self._priorValues) do + prC = prC + 1 + end + prC = prC - 1 + table.insert(parts, "distinct samples: " .. prC) + local dataC = 0 + local evtCount = 0 + for k, _ in pairs(self._data) do + dataC = dataC + 1 + for _, _ in pairs(self._data[k]) do + evtCount = evtCount + 1 + end + end + table.insert(parts, "dict size(cons): " .. dataC) + table.insert(parts, "dict size(evts): " .. evtCount) + return table.concat(parts, ", ") +end + +function StringGenerator:_split(str) + return str:split(self._options.words and " " or "") +end + +function StringGenerator:_join(arr) + return table.concat(arr, self._options.words and " " or "") +end + +function StringGenerator:_observeEvent(context, event) + local key = self:_join(context) + if not self._data[key] then + self._data[key] = {} + end + if not self._data[key][event] then + self._data[key][event] = 0 + end + self._data[key][event] = self._data[key][event] + 1 +end +function StringGenerator:_sample(context) + context = self:_backoff(context) + local key = self:_join(context) + local data = self._data[key] + local avail = {} + if self._options.prior then + for k, _ in pairs(self._priorValues) do + avail[k] = self._priorValues[k] + end + for k, _ in pairs(data) do + avail[k] = avail[k] + data[k] + end + else + avail = data + end + return self:_pickRandom(avail) +end + +function StringGenerator:_backoff(context) + local ctx = {} + for i = 1, #context do + ctx[i] = context[i] + end + if #ctx > self._options.order then + while #ctx > self._options.order do + table.remove(ctx, 1) + end + elseif #ctx < self._options.order then + while #ctx < self._options.order do + table.insert(ctx, 1, self._boundary) + end + end + while not self._data[self:_join(ctx)] and #ctx > 0 do + ctx = table.slice(ctx, 2) + end + + return ctx +end + +function StringGenerator:_pickRandom(data) + local total = 0 + for k, _ in pairs(data) do + total = total + data[k] + end + local rand = self._rng:random() * total + local i = 0 + for k, _ in pairs(data) do + i = i + data[k] + if rand < i then + return k + end + end +end + +return StringGenerator diff --git a/lua/lib/rotLove/rot/text.lua b/lua/lib/rotLove/rot/text.lua new file mode 100644 index 0000000..5c34818 --- /dev/null +++ b/lua/lib/rotLove/rot/text.lua @@ -0,0 +1,199 @@ +--- Text tokenization and breaking routines. +-- @module ROT.Text +---@class Text +local Text = {} + +Text.RE_COLORS = "()(%%([bc]){([^}]*)})" + +-- token types +Text.TYPE_TEXT = 0 +Text.TYPE_NEWLINE = 1 +Text.TYPE_FG = 2 +Text.TYPE_BG = 3 + +--- Measure size of a resulting text block. +function Text.measure(str, maxWidth) + local width, height = 0, 1 + local tokens = Text.tokenize(str, maxWidth) + local lineWidth = 0 + + for i = 1, #tokens do + local token = tokens[i] + if token.type == Text.TYPE_TEXT then + lineWidth = lineWidth + #token.value + elseif token.type == Text.TYPE_NEWLINE then + height = height + 1 + width = math.max(width, lineWidth) + lineWidth = 0 + end + end + width = math.max(width, lineWidth) + + return width, height +end + +--- Convert string to a series of a formatting commands. +function Text.tokenize(str, maxWidth) + local result = {} + + -- first tokenization pass - split texts and color formatting commands + local offset = 1 + str:gsub(Text.RE_COLORS, function(index, match, type, name) + -- string before + local part = str:sub(offset, index - 1) + if #part then + result[#result + 1] = { + type = Text.TYPE_TEXT, + value = part, + } + end + + -- color command + result[#result + 1] = { + type = type == "c" and Text.TYPE_FG or Text.TYPE_BG, + value = name:gsub("^ +", ""):gsub(" +$", ""), + } + + offset = index + #match + return "" + end) + + -- last remaining part + local part = str:sub(offset) + if #part > 0 then + result[#result + 1] = { + type = Text.TYPE_TEXT, + value = part, + } + end + + return (Text._breakLines(result, maxWidth)) +end + +-- insert line breaks into first-pass tokenized data +function Text._breakLines(tokens, maxWidth) + maxWidth = maxWidth or math.huge + + local i = 1 + local lineLength = 0 + local lastTokenWithSpace + + -- This contraption makes `break` work like `continue`. + -- A `break` in the `repeat` loop will continue the outer loop. + while i <= #tokens do + repeat + -- take all text tokens, remove space, apply linebreaks + local token = tokens[i] + if token.type == Text.TYPE_NEWLINE then -- reset + lineLength = 0 + lastTokenWithSpace = nil + end + if token.type ~= Text.TYPE_TEXT then -- skip non-text tokens + i = i + 1 + break -- continue + end + + -- remove spaces at the beginning of line + if lineLength == 0 then + token.value = token.value:gsub("^ +", "") + end + + -- forced newline? insert two new tokens after this one + local index = token.value:find("\n") + if index then + token.value = Text._breakInsideToken(tokens, i, index, true) + + -- if there are spaces at the end, we must remove them + -- (we do not want the line too long) + token.value = token.value:gsub(" +$", "") + end + + -- token degenerated? + if token.value == "" then + table.remove(tokens, i) + break -- continue + end + + if lineLength + #token.value > maxWidth then + -- line too long, find a suitable breaking spot + + -- is it possible to break within this token? + local index = 0 + while 1 do + local nextIndex = token.value:find(" ", index + 1) + if not nextIndex then + break + end + if lineLength + nextIndex > maxWidth then + break + end + index = nextIndex + end + + if index > 0 then -- break at space within this one + token.value = Text._breakInsideToken(tokens, i, index, true) + elseif lastTokenWithSpace then + -- is there a previous token where a break can occur? + local token = tokens[lastTokenWithSpace] + local breakIndex = token.value:find(" [^ ]-$") + token.value = Text._breakInsideToken(tokens, lastTokenWithSpace, breakIndex, true) + i = lastTokenWithSpace + else -- force break in this token + token.value = Text._breakInsideToken(tokens, i, maxWidth - lineLength + 1, false) + end + else -- line not long, continue + lineLength = lineLength + #token.value + if token.value:find(" ") then + lastTokenWithSpace = i + end + end + + i = i + 1 -- advance to next token + until true + end + -- end of "continue contraption" + + -- insert fake newline to fix the last text line + tokens[#tokens + 1] = { type = Text.TYPE_NEWLINE } + + -- remove trailing space from text tokens before newlines + local lastTextToken + for i = 1, #tokens do + local token = tokens[i] + if token.type == Text.TYPE_TEXT then + lastTextToken = token + elseif token.type == Text.TYPE_NEWLINE then + if lastTextToken then -- remove trailing space + lastTextToken.value = lastTextToken.value:gsub(" +$", "") + end + lastTextToken = nil + end + end + + tokens[#tokens] = nil -- remove fake token + + return tokens +end + +--- Create new tokens and insert them into the stream +-- @tparam table tokens +-- @tparam number tokenIndex Token being processed +-- @tparam number breakIndex Index within current token's value +-- @tparam boolean removeBreakChar Do we want to remove the breaking character? +-- @treturn string remaining unbroken token value +function Text._breakInsideToken(tokens, tokenIndex, breakIndex, removeBreakChar) + local newBreakToken = { + type = Text.TYPE_NEWLINE, + } + local newTextToken = { + type = Text.TYPE_TEXT, + value = tokens[tokenIndex].value:sub(breakIndex + (removeBreakChar and 1 or 0)), + } + + table.insert(tokens, tokenIndex + 1, newTextToken) + table.insert(tokens, tokenIndex + 1, newBreakToken) + + return tokens[tokenIndex].value:sub(1, breakIndex - 1) +end + +return Text diff --git a/lua/lib/rotLove/rot/textDisplay.lua b/lua/lib/rotLove/rot/textDisplay.lua new file mode 100644 index 0000000..28291d6 --- /dev/null +++ b/lua/lib/rotLove/rot/textDisplay.lua @@ -0,0 +1,367 @@ +--- Visual Display. +-- A UTF-8 based text display. +-- @module ROT.TextDisplay +local ROT = require((...):gsub((".[^./\\]*"):rep(1) .. "$", "")) +---@class TextDisplay +local TextDisplay = ROT.Class:extend("TextDisplay") + +--- Constructor. +-- The display constructor. Called when ROT.TextDisplay:new() is called. +-- @tparam[opt=80] int w Width of display in number of characters +-- @tparam[opt=24] int h Height of display in number of characters +-- @tparam[opt] string|file|data font Any valid object accepted by love.graphics.newFont +-- @tparam[opt=10] int size font size +-- @tparam[opt] table dfg Default foreground color as a table defined as {r,g,b,a} +-- @tparam[opt] table dbg Default background color +-- @tparam[opt=false] boolean fullOrFlags In Love 0.8.0: Use fullscreen In Love 0.9.0: a table defined for love.graphics.setMode +-- @tparam[opt=false] boolean vsync Use vsync +-- @tparam[opt=0] int fsaa Number of fsaa passes +-- @return nil +function TextDisplay:init(w, h, font, size, dfg, dbg, fullOrFlags, vsync, fsaa) + self._widthInChars = w and w or 80 + self._heightInChars = h and h or 24 + local window = love.window or self.graphics + window.setMode(self._widthInChars, self._heightInChars, fullOrFlags, vsync, fsaa) + self.graphics = love.graphics + self._fontSize = size or 10 + self._font = font and self.graphics.newFont(font, size) or self.graphics.newFont(self._fontSize) + self.graphics.setFont(self._font) + self._charWidth = self._font:getWidth("W") + self._charHeight = self._font:getHeight() + window.setMode( + self._charWidth * self._widthInChars, + self._charHeight * self._heightInChars, + fullOrFlags, + vsync, + fsaa + ) + + self.defaultForegroundColor = dfg and dfg or { 235 / 255, 235 / 255, 235 / 255 } + self.defaultBackgroundColor = dbg and dbg or { 15 / 255, 15 / 255, 15 / 255 } + + self.graphics.setBackgroundColor(self.defaultBackgroundColor) + + self._canvas = self.graphics.newCanvas(self._charWidth * self._widthInChars, self._charHeight * self._heightInChars) + + self._chars = {} + self._backgroundColors = {} + self._foregroundColors = {} + self._oldChars = {} + self._oldBackgroundColors = {} + self._oldForegroundColors = {} + + for x = 1, self._widthInChars do + self._chars[x] = {} + self._backgroundColors[x] = {} + self._foregroundColors[x] = {} + self._oldChars[x] = {} + self._oldBackgroundColors[x] = {} + self._oldForegroundColors[x] = {} + for y = 1, self._heightInChars do + self._chars[x][y] = " " + self._backgroundColors[x][y] = self.defaultBackgroundColor + self._foregroundColors[x][y] = self.defaultForegroundColor + self._oldChars[x][y] = nil + self._oldBackgroundColors[x][y] = nil + self._oldForegroundColors[x][y] = nil + end + end +end + +function TextDisplay:draw() + self.graphics.setCanvas(self._canvas) + for x = 1, self._widthInChars do + for y = 1, self._heightInChars do + local c = self._chars[x][y] + local bg = self._backgroundColors[x][y] + local fg = self._foregroundColors[x][y] + local px = (x - 1) * self._charWidth + local py = (y - 1) * self._charHeight + if + self._oldChars[x][y] ~= c + or self._oldBackgroundColors[x][y] ~= bg + or self._oldForegroundColors[x][y] ~= fg + then + self:_setColor(bg) + self.graphics.rectangle("fill", px, py, self._charWidth, self._charHeight) + self:_setColor(fg) + self.graphics.print(c, px, py) + self._oldChars[x][y] = c + self._oldBackgroundColors[x][y] = bg + self._oldForegroundColors[x][y] = fg + end + end + end + self.graphics.setCanvas() + self.graphics.setColor(1, 1, 1, 1) + self.graphics.draw(self._canvas) +end + +--- Contains point. +-- Returns true if point x,y can be drawn to display. +function TextDisplay:contains(x, y) + return x > 0 and x <= self:getWidth() and y > 0 and y <= self:getHeight() +end + +function TextDisplay:getCharHeight() + return self._charHeight +end +function TextDisplay:getCharWidth() + return self._charWidth +end +function TextDisplay:getWidth() + return self:getWidthInChars() +end +function TextDisplay:getHeight() + return self:getHeightInChars() +end +function TextDisplay:getHeightInChars() + return self._heightInChars +end +function TextDisplay:getWidthInChars() + return self._widthInChars +end +function TextDisplay:getDefaultBackgroundColor() + return self.defaultBackgroundColor +end +function TextDisplay:getDefaultForegroundColor() + return self.defaultForegroundColor +end + +--- Get a character. +-- returns the character being displayed at position x, y +-- @tparam int x The x-position of the character +-- @tparam int y The y-position of the character +-- @treturn string The character +function TextDisplay:getCharacter(x, y) + local c = self._chars[x][y] + return c and string.char(c) or nil +end +--- Get a background color. +-- returns the current background color of the character written to position x, y +-- @tparam int x The x-position of the character +-- @tparam int y The y-position of the character +-- @treturn table The background color as a table defined as {r,g,b,a} +function TextDisplay:getBackgroundColor(x, y) + return self._backgroundColors[x][y] +end + +--- Get a foreground color. +-- returns the current foreground color of the character written to position x, y +-- @tparam int x The x-position of the character +-- @tparam int y The y-position of the character +-- @treturn table The foreground color as a table defined as {r,g,b,a} +function TextDisplay:getForegroundColor(x, y) + return self._foregroundColors[x][y] +end + +--- Set Default Background Color. +-- Sets the background color to be used when it is not provided +-- @tparam table c The background color as a table defined as {r,g,b,a} +function TextDisplay:setDefaultBackgroundColor(c) + self.defaultBackgroundColor = c and c or self.defaultBackgroundColor +end + +--- Set Defaul Foreground Color. +-- Sets the foreground color to be used when it is not provided +-- @tparam table c The foreground color as a table defined as {r,g,b,a} +function TextDisplay:setDefaultForegroundColor(c) + self.defaultForegroundColor = c and c or self.defaultForegroundColor +end + +--- Clear the screen. +-- By default wipes the screen to the default background color. +-- You can provide a character, x-position, y-position, width, height, fore-color and back-color +-- and write the same character to a portion of the screen +-- @tparam[opt=' '] string c A character to write to the screen - may fail for strings with a length > 1 +-- @tparam[opt=1] int x The x-position from which to begin the wipe +-- @tparam[opt=1] int y The y-position from which to begin the wipe +-- @tparam[opt] int w The number of chars to wipe in the x direction +-- @tparam[opt] int h Then number of chars to wipe in the y direction +-- @tparam[opt] table fg The color used to write the provided character +-- @tparam[opt] table bg the color used to fill in the background of the cleared space +function TextDisplay:clear(c, x, y, w, h, fg, bg) + c = c or " " + w = w or self._widthInChars + local s = c:rep(self._widthInChars) + x = self:_validateX(x, s) + y = self:_validateY(y) + h = self:_validateHeight(y, h) + fg = self:_validateForegroundColor(fg) + bg = self:_validateBackgroundColor(bg) + for i = 0, h - 1 do + self:_writeValidatedString(s, x, y + i, fg, bg) + end +end + +--- Clear canvas. +-- runs the clear method of the Love2D canvas object being used to write to the screen +function TextDisplay:clearCanvas() + self._canvas:clear() +end + +--- Write. +-- Writes a string to the screen +-- @tparam string s The string to be written +-- @tparam[opt=1] int x The x-position where the string will be written +-- @tparam[opt=1] int y The y-position where the string will be written +-- @tparam[opt] table fg The color used to write the provided string +-- @tparam[opt] table bg the color used to fill in the string's background +function TextDisplay:write(s, x, y, fg, bg) + ROT.assert(s, "Display:write() must have string as param") + x = self:_validateX(x, s) + y = self:_validateY(y, s) + fg = self:_validateForegroundColor(fg) + bg = self:_validateBackgroundColor(bg) + + self:_writeValidatedString(s, x, y, fg, bg) +end + +--- Write Center. +-- write a string centered on the middle of the screen +-- @tparam string s The string to be written +-- @tparam[opt=1] int y The y-position where the string will be written +-- @tparam[opt] table fg The color used to write the provided string +-- @tparam[opt] table bg the color used to fill in the string's background +function TextDisplay:writeCenter(s, y, fg, bg) + ROT.assert(s, "Display:writeCenter() must have string as param") + ROT.assert(#s < self._widthInChars, "Length of ", s, " is greater than screen width") + y = y and y or math.floor((self:getHeightInChars() - 1) / 2) + y = self:_validateY(y, s) + fg = self:_validateForegroundColor(fg) + bg = self:_validateBackgroundColor(bg) + + local x = math.floor((self._widthInChars - #s) / 2) + self:_writeValidatedString(s, x, y, fg, bg) +end + +function TextDisplay:_writeValidatedString(s, x, y, fg, bg) + for i = 1, #s do + self._backgroundColors[x + i - 1][y] = bg + self._foregroundColors[x + i - 1][y] = fg + self._chars[x + i - 1][y] = s:sub(i, i) + end +end + +function TextDisplay:_validateX(x, s) + x = x and x or 1 + ROT.assert(x > 0 and x <= self._widthInChars, "X value must be between 0 and ", self._widthInChars) + ROT.assert( + (x + #s) - 1 <= self._widthInChars, + "X value plus length of String must be between 0 and ", + self._widthInChars, + " string: ", + s, + "; x:", + x + ) + return x +end +function TextDisplay:_validateY(y) + y = y and y or 1 + ROT.assert(y > 0 and y <= self._heightInChars, "Y value must be between 0 and ", self._heightInChars) + return y +end +function TextDisplay:_validateForegroundColor(c) + c = c or self.defaultForegroundColor + ROT.assert(#c > 2, "Foreground Color must have at least 3 components") + for i = 1, #c do + c[i] = self:_clamp(c[i]) + end + return c +end +function TextDisplay:_validateBackgroundColor(c) + c = c or self.defaultBackgroundColor + ROT.assert(#c > 2, "Background Color must have at least 3 components") + for i = 1, #c do + c[i] = self:_clamp(c[i]) + end + return c +end +function TextDisplay:_validateHeight(y, h) + h = h and h or self._heightInChars - y + 1 + ROT.assert(h > 0, "Height must be greater than 0. Height provided: ", h) + ROT.assert( + y + h - 1 <= self._heightInChars, + "Height + y value must be less than screen height. y, height: ", + y, + ", ", + h + ) + return h +end +function TextDisplay:_setColor(c) + love.graphics.setColor(c or self.defaultForegroundColor) +end +function TextDisplay:_clamp(n) + return n < 0 and 0 or n > 1 and 1 or n +end + +--- Draw text. +-- Draws a text at given position. Optionally wraps at a maximum length. +-- @tparam number x +-- @tparam number y +-- @tparam string text May contain color/background format specifiers, %c{name}/%b{name}, both optional. %c{}/%b{} resets to default. +-- @tparam number maxWidth wrap at what width (optional)? +-- @treturn number lines drawn +function TextDisplay:drawText(x, y, text, maxWidth) + local fg + local bg + local cx = x + local cy = y + local lines = 1 + if not maxWidth then + maxWidth = self._widthInChars - x + end + + local tokens = ROT.Text.tokenize(text, maxWidth) + + while #tokens > 0 do -- interpret tokenized opcode stream + local token = table.remove(tokens, 1) + if token.type == ROT.Text.TYPE_TEXT then + local isSpace, isPrevSpace, isFullWidth, isPrevFullWidth + for i = 1, #token.value do + local cc = token.value:byte(i) + local c = token.value:sub(i, i) + -- TODO: chars will never be full-width without special handling + -- TODO: ... so the next 15 lines or so do some pointless stuff + -- Assign to `true` when the current char is full-width. + isFullWidth = (cc > 0xff00 and cc < 0xff61) or (cc > 0xffdc and cc < 0xffe8) or cc > 0xffee + -- Current char is space, whatever full-width or half-width both are OK. + isSpace = c:byte() == 0x20 or c:byte() == 0x3000 + -- The previous char is full-width and + -- current char is nether half-width nor a space. + if isPrevFullWidth and not isFullWidth and not isSpace then + cx = cx + 1 -- add an extra position + end + -- The current char is full-width and + -- the previous char is not a space. + if isFullWidth and not isPrevSpace then + cx = cx + 1 -- add an extra position + end + fg = (fg == "" or not fg) and self.defaultForegroundColor + or type(fg) == "string" and ROT.Color.fromString(fg) + or fg + bg = (bg == "" or not bg) and self.defaultBackgroundColor + or type(bg) == "string" and ROT.Color.fromString(bg) + or bg + self:_writeValidatedString(c, cx, cy, fg, bg) + cx = cx + 1 + isPrevSpace = isSpace + isPrevFullWidth = isFullWidth + end + elseif token.type == ROT.Text.TYPE_FG then + fg = token.value or nil + elseif token.type == ROT.Text.TYPE_BG then + bg = token.value or nil + elseif token.type == ROT.Text.TYPE_NEWLINE then + cx = x + cy = cy + 1 + lines = lines + 1 + end + end + + return lines +end + +return TextDisplay diff --git a/lua/lib/rotLove/rot/type/grid.lua b/lua/lib/rotLove/rot/type/grid.lua new file mode 100644 index 0000000..88b070d --- /dev/null +++ b/lua/lib/rotLove/rot/type/grid.lua @@ -0,0 +1,63 @@ +--- Grid. +-- @module ROT.Type.Grid +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class Grid +local Grid = ROT.Class:extend("Grid") + +-- Grid class + +function Grid:init() + self:clear() +end + +function Grid:clear() + self._points = ROT.Type.PointSet() + self._values = {} +end + +function Grid:removeCell(x, y) + local i = self._points:find(x, y) + if not i then + return + end + local n = #self._points - 1 + local oldValue = self._values[i] + self._points:pluck(i) + self._values[i] = self._values[n] + self._values[n] = nil + return oldValue +end + +function Grid:setCell(x, y, value) + if value == nil then + return self:removeCell(x, y) + end + local i, j = self._points:push(x, y) + local oldValue = j and self._values[j] + self._values[i or j] = value + return oldValue +end + +function Grid:getCell(x, y) + local i = self._points:find(x, y) + return i and self._values[i] +end + +local function iterate(self, i) + i = i - 2 + if i > 0 then + local x, y = self._points:peek(i) + return i, x, y, self._values[i] + end +end + +function Grid:each() + return iterate, self, #self._points + 1 +end + +function Grid:getRandom() + local x, y = self._points:getRandom() + return x, y, self:getCell(x, y) +end + +return Grid diff --git a/lua/lib/rotLove/rot/type/pointSet.lua b/lua/lib/rotLove/rot/type/pointSet.lua new file mode 100644 index 0000000..00c8c01 --- /dev/null +++ b/lua/lib/rotLove/rot/type/pointSet.lua @@ -0,0 +1,75 @@ +--- Pair set. +-- An unordered collection of unique value-pairs. +-- @module ROT.Type.PointSet +local ROT = require((...):gsub((".[^./\\]*"):rep(2) .. "$", "")) +---@class PointSet +local PointSet = ROT.Class:extend("PointSet") + +function PointSet:init() + self._index = {} +end + +local function hash(x, y) + return x and y * 0x4000000 + x or false -- 26-bit x and y +end + +function PointSet:find(x, y) + return self._index[hash(x, y)] +end + +function PointSet:peek(i) + return self[i], self[i + 1] +end + +function PointSet:poke(i, x, y) + self._index[hash(self:peek(i))] = nil + self._index[hash(x, y)] = i + self._index[false] = nil + self[i], self[i + 1] = x, y + return self +end + +function PointSet:push(x, y) + local key = hash(x, y) + local i = self._index[key] + if i then + return nil, i + end + i = #self + 1 + self:poke(i, x, y) + self._index[key] = i + self._index[false] = nil + return i +end + +function PointSet:pluck(i) + local last, x, y = #self - 1, self:peek(i) + self:poke(i, self:peek(last)):poke(last) + self._index[hash(x, y)] = nil + self._index[hash(self:peek(i))] = i + self._index[false] = nil + return x, y +end + +function PointSet:prune(x, y) + local i = self:find(x, y) + return i and self:pluck(i) +end + +local function iterate(self, i) + i = i - 2 + if i > 0 then + return i, self:peek(i) + end +end + +function PointSet:each() + return iterate, self, #self + 1 +end + +function PointSet:getRandom() + local i = self._rng:random(1, #self / 2) * 2 - 1 + return self:peek(i) +end + +return PointSet diff --git a/lua/main.lua b/lua/main.lua new file mode 100644 index 0000000..68710e7 --- /dev/null +++ b/lua/main.lua @@ -0,0 +1,167 @@ +--- love2d main entry point +--- equivalent to lua/neohack/init.lua + +local buffer = require("neohack.buffer") +-- local utils = require("neohack.utils") + +-- fake a global vim for now +vim = { + api = { + nvim_create_namespace = function() end, + nvim_create_augroup = function() end, + nvim_command = function() end, + }, +} + +local state = require("neohack.state") +local love_render = require("neohack.love2d.render") +local game = require("neohack.game") +local actions = require("neohack.actions") +local api = require("neohack.api") +local love_cursor = require("neohack.love2d.cursor") +local love_keymaps = require("neohack.love2d.keymaps") +local love_async = require("neohack.love2d.async") +local love_edits = require("neohack.love2d.edits") +local message = require("neohack.message") + +---@type Rogue +local rog = nil + +local profile = false + +local paused = false + +-- these numbers just happen to fit with 1080p fullscreen and scale 1 + +local count = 0 +function love.draw() + love_render.f:draw() + love_render.write_messages() + love_render.write_menu() + love_cursor.write_cursor() + love_render.write_status_line(game.status_line()) + love_render.draw_ui() + + if profile then + count = count + 1 + if count % 10 == 0 then + -- love.graphics.setColor(0, 1, 1, 1) + api.debug(love.report or "Please wait...") + end + end +end + +local function init() + local rows, cols, messageX, scale = love_render.derive_grid() + local generations = cols * 100 + + -- equivalent to lua/neohack/init.lua + love_render.init(rows, cols, messageX, scale) + -- TODO: setup love2d equivalent + game.open_ui = function() end + + api.notify = love_render.notify + api.debug = love_render.debug + api.cursor = love_cursor + api.render = love_render + api.keymaps = love_keymaps + api.async = love_async + api.edits = love_edits + + state.rows = rows + state.cols = cols + game.generations = generations + + actions.leader_key = "" + state.debug = false + state.trace = false + + love_cursor.init() +end + +local function start_game() + -- equivalent to lua/neohack/game.lua:22 + game.start_game() + love_keymaps.notify_keys() -- show basic movement keys + game.turn_logic() + state.update = true +end + +function love.load() + init() + start_game() + + if profile then + love.profiler = require("profile") + love.profiler.start() + end +end + +local last_tick_s = 0 +function love.update(dt) + if not state.running then + return + end + + local update = false + if not paused then + last_tick_s = last_tick_s + dt + if state.realtime and last_tick_s > (state.tick_ms / 1000) then + -- api.debug("tick", last_tick_s) + last_tick_s = 0 + local this_turn_ms = api.async.get_time() + update = game.realtime_tick(this_turn_ms) + end + end + + if state.update or update then + buffer.write_buf() + -- with this flashes are gone, but without it picked up items linger + -- love_cursor.write_cursor() + buffer.animations() + state.update = false + + if profile then + love.report = love.profiler.report(20) + love.profiler.reset() + end + end + love_async.on_update() +end + +local function handle_input(update, is_paused) + if update and not is_paused then + if state.running then + game.turn_logic() + end + state.update = true + end + paused = is_paused + -- state change only, no game tick +end + +local function dead(key) + if not state.running then + if key == "escape" or key == "q" then + love.event.quit() + else + message.notify("The game is over. 'esc' to quit.") + end + return true + else + return false + end +end + +--- for special characters only +function love.keypressed(key) + if not dead(key) then + handle_input(love_keymaps.keypressed(key)) + end +end + +function love.textinput(key) + if not dead(key) then + handle_input(love_keymaps.textinput(key)) + end +end diff --git a/lua/neohack/actions.lua b/lua/neohack/actions.lua index a540631..aad0c39 100644 --- a/lua/neohack/actions.lua +++ b/lua/neohack/actions.lua @@ -2,6 +2,7 @@ local chat = require("neohack.chat") local state = require("neohack.state") local message = require("neohack.message") local utils = require("neohack.utils") +local api = require("neohack.api") local M = { leader_key = nil, @@ -11,7 +12,8 @@ local M = { M.actions = { ---comment inventory = function() - message.notify("You have " .. state.player.inventory:get_inventory()) + message.notify_lines(unpack(state.player.inventory:get_inventory())) + message.debug(unpack(state.player.inventory:get_inventory())) end, ---comment @@ -26,9 +28,12 @@ M.actions = { M.tick() end, + --TODO: disarm + ---comment wear = function(request) state.player.inventory:wear(request.object, request.target) + M.tick() end, ---comment @@ -46,7 +51,7 @@ M.actions = { ---comment look = function(request) - state.player.inventory:look({ request.object }) + state.player.inventory:look(request.object) end, ---comment @@ -73,12 +78,13 @@ M.actions = { end, ---comment - wait = function(request) - local count = utils.string_to_int(request.object) + rest = function(request) + local count = request and utils.string_to_int(request.object) or 1 + local action = request and utils.capitalize_first_letter(request.action) or "rest" if count then - message.notify(utils.capitalize_first_letter(request.action) .. "ing " .. count .. " turns") + message.notify(action .. "ing " .. count .. " turns") for i = 1, count do - message.notify(utils.capitalize_first_letter(request.action) .. "ing turn", i) + -- message.notify(utils.capitalize_first_letter(request.action) .. "ing turn", i) --TODO: keeps being off by 1 state.animating = false -- ignore animations, otherwise turns are skipped M.tick() @@ -88,20 +94,23 @@ M.actions = { ---comment help = function() - message.notify( - ":EndGame to quit.\n" - .. ":StartGame to restart.\n" - .. "Move cursor in normal mode to move and attack.\n" - .. "Yank to search for items.\n" - .. "Visual mode to sneak.\n" - .. "Delete to pickup items.\n" - .. "Insert to type an action. eg 'wear helmet head'\n" - .. "Or use the action keymaps, which start with '" + message.notify_lines( + -- ":EndGame to quit.\n" + -- .. ":StartGame to restart.\n" + + "Move cursor in vim normal mode to move and attack.", + "Yank to search for items.", + "Visual mode to sneak.", + "Delete to pickup items.", + -- .. "Insert to type an action. eg 'wear helmet head'\n" + "Open menu with '" .. M.leader_key - .. "'\n" - .. "Actions: " - .. "i to show inventory, w to wear, k to kick, f to fuse, r to rename, l to look, p to pilfer, e to eat, s to say, d to drop, to wait, ? for help" + .. "'" + -- .. "'\n" + -- .. "Actions: " + -- .. "i to show inventory, w to wear, k to kick, f to fuse, r to rename, l to look, p to pilfer, e to eat, s to say, d to drop, R to rest, ? for help" ) + return false end, } @@ -134,6 +143,7 @@ M.synonyms = { take = "pilfer", P = "pick", + Pick = "pick", pick = "pick", pickpocket = "pick", @@ -146,141 +156,150 @@ M.synonyms = { yell = "say", intone = "say", - sleep = "wait", - sit = "wait", - rest = "wait", + sleep = "rest", + sit = "rest", + wait = "rest", + R = "rest", + Rest = "rest", } ----Insert the action command ----@param action_string string -M.insert_action = function(action_string) - vim.cmd("normal! i" .. action_string) -end +-- ---Insert the action command +-- ---@param action_string string +-- M.insert_action = function(action_string) +-- vim.cmd("normal! i" .. action_string) +-- end ---comment -M.prompt_kick = function() - local direction = M.prompt_one_char("Kick direction? hjkl") - if direction then - -- M.insert_action("kick " .. direction) - M.actions.kick({ object = direction }) - end -end +-- M.prompt_kick = function() +-- local direction = M.prompt_one_char("Kick direction? hjkl") +-- if direction then +-- -- M.insert_action("kick " .. direction) +-- M.actions.kick({ object = direction }) +-- end +-- end ---comment -M.prompt_wear = function() - message.notify("You have:\n0:nothing\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local object = M.prompt_one_word("Wear what?") - if object then - local slot = M.prompt_one_word("Wear on? h:head r:right_hand l:left_hand b:body f:feet") - if slot then - -- M.insert_action("wear " .. object .. " " .. slot) - M.actions.wear({ object = object, target = slot }) - end - end - end, 50) -end +-- M.prompt_wear = function() +-- message.notify("You have:\n0:nothing\n" .. state.player.inventory:get_inventory_item_with_index()) +-- api.async.defer(function() +-- local object = M.prompt_one_word("Wear what?") +-- if object then +-- local slot = M.prompt_one_word("Wear on? h:head r:right_hand l:left_hand b:body f:feet") +-- if slot then +-- -- M.insert_action("wear " .. object .. " " .. slot) +-- M.actions.wear({ object = object, target = slot }) +-- end +-- end +-- end, 50) +-- end ---comment -M.prompt_fuse = function() - message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local index = M.prompt("Fuse what?") - if index then - -- M.insert_action("fuse " .. index) - local words = utils.split_words(index) - if words then - M.actions.fuse({ object = words[1], target = words[2] }) - end - end - end, 50) -end +-- M.prompt_fuse = function() +-- message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) +-- api.async.defer(function() +-- local index = M.prompt("Fuse what?") +-- if index then +-- -- M.insert_action("fuse " .. index) +-- local words = utils.split_words(index) +-- if words then +-- M.actions.fuse({ object = words[1], target = words[2] }) +-- end +-- end +-- end, 50) +-- end ---comment -M.prompt_rename = function() - message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local index = M.prompt_one_word("Rename what?") - if index then - local name = M.prompt_one_word("New name?") - if name then - M.actions.rename({ object = index, target = name }) - end - end - end, 50) +-- M.prompt_rename = function() +-- message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) +-- api.async.defer(function() +-- local index = M.prompt_one_word("Rename what?") +-- if index then +-- local name = M.prompt_one_word("New name?") +-- if name then +-- M.actions.rename({ object = index, target = name }) +-- end +-- end +-- end, 50) +-- end + +M.prompt_rename_to = function(request) + local name = M.prompt_one_word("New name?") + if name then + M.actions.rename({ object = request.object, target = name }) + end end ---comment -M.prompt_look = function() - message.notify("You have:\n0:self\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local index = M.prompt_one_word("Look at what?") - if index then - -- M.insert_action("look " .. index) - M.actions.look({ object = index }) - end - end, 50) -end +-- M.prompt_look = function() +-- message.notify("You have:\n0:self\n" .. state.player.inventory:get_inventory_item_with_index()) +-- api.async.defer(function() +-- local index = M.prompt_one_word("Look at what?") +-- if index then +-- -- M.insert_action("look " .. index) +-- M.actions.look({ object = index }) +-- end +-- end, 50) +-- end ---comment -M.prompt_pilfer = function() - message.notify("You have:\n0:self\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local index = M.prompt_one_word("Pilfer from what?") - if index then - M.actions.pilfer({ object = index }) - end - end, 50) -end +-- M.prompt_pilfer = function() +-- message.notify("You have:\n0:self\n" .. state.player.inventory:get_inventory_item_with_index()) +-- api.async.defer(function() +-- local index = M.prompt_one_word("Pilfer from what?") +-- if index then +-- M.actions.pilfer({ object = index }) +-- end +-- end, 50) +-- end ---comment -M.prompt_eat = function() - message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local index = M.prompt_one_word("Eat what?") - if index then - -- M.insert_action("eat " .. index) - M.actions.eat({ object = index }) - end - end, 50) -end +-- M.prompt_eat = function() +-- message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) +-- api.async.defer(function() +-- local index = M.prompt_one_word("Eat what?") +-- if index then +-- -- M.insert_action("eat " .. index) +-- M.actions.eat({ object = index }) +-- end +-- end, 50) +-- end ---comment -M.prompt_drop = function() - message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) - vim.defer_fn(function() - local index = M.prompt_one_word("Drop what?") - if index then - -- M.insert_action("drop " .. index) - M.actions.drop({ object = index }) - end - end, 50) -end +-- M.prompt_drop = function() +-- message.notify("You have:\n" .. state.player.inventory:get_inventory_item_with_index()) +-- vim.defer_fn(function() +-- local index = M.prompt_one_word("Drop what?") +-- if index then +-- -- M.insert_action("drop " .. index) +-- M.actions.drop({ object = index }) +-- end +-- end, 50) +-- end ---comment -M.prompt_say = function() - vim.defer_fn(function() - local words = M.prompt("Say what?") - if words then - -- M.insert_action("say " .. words) - local w = utils.split_words(words) - if w then - M.actions.say({ object = w[1], target = w[2] }) - end - end - end, 50) -end +-- M.prompt_say = function() +-- api.async.defer(function() +-- local words = M.prompt("Say what?") +-- if words then +-- -- M.insert_action("say " .. words) +-- local w = utils.split_words(words) +-- if w then +-- M.actions.say({ object = w[1], target = w[2] }) +-- end +-- end +-- end, 50) +-- end ---comment -M.prompt_wait = function() - vim.defer_fn(function() - local str = M.prompt_one_word("Wait how long?") - if str then - -- M.insert_action("wait " .. str) - M.actions.wait({ action = "wait", object = str }) - end - end, 50) -end +-- M.prompt_rest = function() +-- api.async.defer(function() +-- local str = M.prompt_one_word("Rest how long?") +-- if str then +-- -- M.insert_action("rest " .. str) +-- M.actions.rest({ action = "rest", object = str }) +-- end +-- end, 50) +-- end ---parse a string into an action and the request ---@param inserted_chars string @@ -309,71 +328,16 @@ M.execute_action = function(inserted_chars) end end -local directions = { - { key = "h", desc = "left" }, - { key = "j", desc = "down" }, - { key = "k", desc = "up" }, - { key = "l", desc = "right" }, -} - ---comment ----@param bufnr number ---@param tick function -M.setup = function(bufnr, tick) - -- TODO: this doesn't work well do something to clean up other keymaps - -- M.remove_keymaps(bufnr) - +M.setup = function(tick) M.tick = tick - local function map(key, func, desc) - vim.keymap.set({ "n", "v" }, M.leader_key .. key, func, { buffer = bufnr, desc = desc }) - end - - local function direction_keymaps(key, func, name) - for _, map_info in ipairs(directions) do - map(key .. map_info.key, function() - func({ object = map_info.key }) - end, name .. " " .. map_info.desc) - end - end - - map("?", M.actions.help, "help") - -- map("?", function() M.insert_action("help") end, "help") - map("i", M.actions.inventory, "inventory") - -- map("i", function() M.insert_action("inventory") end, "inventory") - map("w", M.prompt_wear, "wear") - map("k", M.prompt_kick, "kick") - map("f", M.prompt_fuse, "fuse") - map("r", M.prompt_rename, "rename") - map("l", M.prompt_look, "look") - map("p", M.prompt_pilfer, "pilfer") - --TODO:make it operator pending - direction_keymaps("P", M.actions.pick, "Pick") - map("e", M.prompt_eat, "eat") - map("d", M.prompt_drop, "drop") - map("s", M.prompt_say, "say") - map(" ", M.prompt_wait, "wait") -end - ----remove all existing keymaps for the leader ----@param bufnr number -M.remove_keymaps = function(bufnr) - local global_keymaps = vim.api.nvim_get_keymap("n") - for _, keymap in ipairs(global_keymaps) do - if keymap.lhs:sub(1, 1) == M.leader_key then - -- vim.api.nvim_del_keymap("n", keymap.lhs) - vim.api.nvim_buf_set_keymap(bufnr, "n", keymap.lhs, "", { noremap = true, silent = true }) - end - end - local buffer_keymaps = vim.api.nvim_buf_get_keymap(bufnr, "n") - for _, keymap in ipairs(buffer_keymaps) do - if keymap.lhs:sub(1, 1) == M.leader_key then - vim.api.nvim_del_keymap("n", keymap.lhs) - end - end + api.keymaps.dynamic_keymaps() end +--- DEPRECATED M.prompt_one_char = function(prompt) - local result = M.prompt(prompt) + local result = api.keymaps.prompt(prompt) if result and #result ~= 1 then message.notify("1 only") return nil @@ -381,11 +345,12 @@ M.prompt_one_char = function(prompt) return result end +--- TODO: only used for rename ---comment ---@param prompt string ---@return string|nil M.prompt_one_word = function(prompt) - local words = utils.split_words(M.prompt(prompt)) + local words = utils.split_words(api.keymaps.prompt(prompt)) if words then if #words ~= 1 then message.notify("1 only") @@ -397,15 +362,4 @@ M.prompt_one_word = function(prompt) return nil end ----comment ----@param prompt string ----@return string -M.prompt = function(prompt) - local result - vim.ui.input({ prompt = prompt .. " " }, function(input) - result = input - end) - return result -end - return M diff --git a/lua/neohack/animate.lua b/lua/neohack/animate.lua index f025e04..4c3bf09 100644 --- a/lua/neohack/animate.lua +++ b/lua/neohack/animate.lua @@ -1,6 +1,7 @@ -local view_buffer = require("neohack.view_buffer") local Def = require("neohack.def") local state = require("neohack.state") +local api = require("neohack.api") +local constant_defs = require("neohack.constant_defs") ---@class Animation ---@field attacker Entity @@ -9,29 +10,37 @@ local state = require("neohack.state") local M = { ---@type Animation[] animations = {}, + cursor_bounce = nil, } ---comment ---@param attacker Entity ---@param target Entity M.attack = function(attacker, target) - -- view_buffer.show_char_at_pos(target.movement.row, target.movement.col, attacker.movement.char) + -- api.render.write_pos(target.movement.row, target.movement.col, attacker.movement.char) -- message.notify(attacker.name, "hitting", target.name) -- -- utils.sleep(0.5) - -- vim.defer_fn(function() - -- view_buffer.show_char_at_pos(target.movement.row, target.movement.col, target.movement.char) + -- api.async.defer(function() + -- api.render.write_pos(target.movement.row, target.movement.col, target.movement.char) -- end, 800) -- error("hittttt") if state.animations_enabled then - table.insert(M.animations, { - attacker = attacker, - target = target, - }) + -- table.insert(M.animations, { + -- attacker = attacker, + -- target = target, + -- }) end end +M.bounce_cursor = function(fromRow, fromCol, toRow, toCol) + M.cursor_bounce = { + from = { row = fromRow, col = fromCol }, + to = { row = toRow, col = toCol }, + } +end + M.show_time = 200 ---comment @@ -47,22 +56,23 @@ M.attack_animation = function(animation, get_entity_at_pos, animation_completed) local attacker_col = attacker.movement.col if attacker.type ~= Def.DefType.player then - view_buffer.show_char_at_pos(target_row, target_col, attacker.movement.char) - view_buffer.show_char_at_pos(attacker_row, attacker_col, " ") + api.render.write_pos(target_row, target_col, attacker.movement.char) + api.render.write_pos(attacker_row, attacker_col, constant_defs.floor_char) end if attacker.combat.hit_highlight then - view_buffer.highlight_hit(target_row, target_col, attacker.combat.hit_highlight, M.show_time) + -- view_buffer.highlight_hit(target_row, target_col, attacker.combat.hit_highlight, M.show_time) + api.render.set_highlight(target_row, target_col, attacker.combat.hit_highlight) end attacker.combat.hit_highlight = nil -- message.debug("anmiate", ani.attacker.name, "hitting", ani.target.name) - vim.defer_fn(function() + api.async.defer(function() -- target could be player, so get the original entity local original_target = get_entity_at_pos(target_row, target_col, true) local original_attacker = get_entity_at_pos(attacker_row, attacker_col, true) - view_buffer.show_char_at_pos(target_row, target_col, original_target.movement.char) - view_buffer.show_char_at_pos(attacker_row, attacker_col, original_attacker.movement.char) + api.render.write_pos(target_row, target_col, original_target.movement.char) + api.render.write_pos(attacker_row, attacker_col, original_attacker.movement.char) target.combat.hit_highlight = nil animation_completed() end, M.show_time) @@ -91,6 +101,16 @@ M.show_animations = function(get_entity_at_pos, callback) end end + if M.cursor_bounce then + local from = M.cursor_bounce.from + local to = M.cursor_bounce.to + api.cursor.animate(from.row, from.col, to.row, to.col) + api.async.defer(function() + api.cursor.animate(to.row, to.col, from.row, from.col) + M.cursor_bounce = nil + end, 100) + end + if #M.animations == 0 then animation_completed() end diff --git a/lua/neohack/api.lua b/lua/neohack/api.lua new file mode 100644 index 0000000..9d2e12e --- /dev/null +++ b/lua/neohack/api.lua @@ -0,0 +1,64 @@ +---@type Color string|RGB either a vim highlight name or list of love2d RGBA values + +---@class Highlight +---@field fg Color|nil +---@field bg Color|nil + +--- Cursor +--- @class Cursor +--- @field get fun(): integer, integer get the current cursor +--- @field set fun(row:integer, col:integer) set the cursor +--- @field animate fun(row:integer, col:integer, oldRow:integer, oldCol:integer) show the cursor +--- @field highlight_cursor fun(highlight:Highlight) highlight the cursor +--- @field write_cursor fun() write the cursor to the buffer + +--- Render +--- @class Render +--- @field write_buf fun(lines:string[]) write the full buffer +--- @field write_pos fun(row:integer, col:integer, char:string) write a single character to the buffer +--- @field new_view fun(level:integer) create a new view for a level +--- @field setup_view fun(opts:table) setup the buffer with event handlers +--- @field unsetup_view fun() tear down the buffer and event handlers +--- @field create_color fun(name:string, rgb:RGB): Highlight create a color +--- @field create_background_color fun(name:string, rgb:RGB): Highlight create a color +--- @field set_highlight fun(row:integer, col:integer, highlight:Highlight) set highlight for a specific position + +--- Keymaps +---@class Keymaps +---@field prompt fun(prompt:string):string get a string from the user +---@field dynamic_keymaps fun() set the keymaps +---@field in_sneak_mode fun(): boolean +---@field in_aim_mode fun(): boolean +---@field in_other_mode fun(): boolean +---@field get_mode fun(): string game mode + +--- Async +---@class Async +---@field later fun(callback:fun()) defer a callback to be called later +---@field defer fun(callback:fun(), timeout:integer) defer a callback to be called after timeout +---@field get_time fun(): integer time in milliseconds + +--- Edits +---@class Edits +---@field handle_insert_enter func() +---@field handle_insert func() +---@field handle_insert_exit func() +---@field handle_changed func() +---@field handle_yanked func() +---@field func() +local M = { + ---@type Cursor + cursor = nil, + ---@type Render + render = nil, + ---@type fun(...) + notify = nil, + ---@type fun(...) + debug = nil, + ---@type Keymaps + keymaps = nil, + ---@type Async + async = nil, +} + +return M diff --git a/lua/neohack/buffer.lua b/lua/neohack/buffer.lua index 875e703..94348ac 100644 --- a/lua/neohack/buffer.lua +++ b/lua/neohack/buffer.lua @@ -1,148 +1,90 @@ --- all the interactions with the buffer that backs the map --- -local view_buffer = require("neohack.view_buffer") local Def = require("neohack.def") local animate = require("neohack.animate") +local constant_defs = require("neohack.constant_defs") ---@class Buffer ---- @field bufnr integer --- @field level integer --- @field cells Entity[][] --- @field buf_settings table ---- @field handle_moved function ---- @field handle_insert function ---- @field handle_insert_enter function ---- @field handle_changed function ---- @field handle_yanked function local M = { - --- bufnr to buffer + --- level to buffer ---@type table buffers = {}, - --- level to bufnr - ---@type table - levels = {}, } local state = require("neohack.state") local message = require("neohack.message") +local api = require("neohack.api") --- Initialize buffer-specific data -local function init_buffer(bufnr, handlers) +---@param level integer +---@return Buffer +local function init_buffer(level) return { - bufnr = bufnr, - level = 0, + level = level, cells = {}, buf_settings = nil, - handle_moved = handlers.handle_moved, - handle_insert = handlers.handle_insert, - handle_insert_enter = handlers.handle_insert_enter, - handle_changed = handlers.handle_changed, - handle_yanked = handlers.handle_yanked, } end -M.create_new_buffer = function(lines, handlers) - local bufnr = lines and view_buffer.new_buffer(lines) or view_buffer.copy_buffer() - M.buffers[bufnr] = init_buffer(bufnr, handlers) - table.insert(M.levels, bufnr) - M.buffers[bufnr].level = #M.levels - -- M.read_buf(bufnr) - return bufnr, #M.levels +--- Setup the view buffer for a specific buffer +---@param level integer +M.setup_new_buffer = function(level) + M.buffers[level] = init_buffer(level) end -M.setup_buffer = function(bufnr, first_floor) - view_buffer.setup_buffer(bufnr, first_floor) - M.add_handlers(bufnr) - M.setup_game_buffer(bufnr) +--- Write the current level to the current view +M.write_buf = function() + local level = state.current_level + api.render.write_buf(M.cells_to_render(M.buffers[level].cells)) end -M.setup_game_buffer = function(bufnr) - view_buffer.setup_game_buffer(bufnr, M.buffers[bufnr]) -end - -M.restore_original_settings = function(bufnr) - view_buffer.restore_original_settings(bufnr, M.buffers[bufnr]) -end - -M.end_game = function() - for bufnr, _ in pairs(M.buffers) do - print("end game " .. bufnr) - -- M.handle_moved = nil - -- M.handle_insert = nil - -- M.handle_changed = nil - -- M.handle_yanked = nil - view_buffer.remove_handlers(bufnr) - M.restore_original_settings(bufnr) - end -end - ---- Add handlers for a specific buffer ----@param bufnr integer -M.add_handlers = function(bufnr) - local buf_data = M.buffers[bufnr] - if - buf_data.handle_moved - and buf_data.handle_insert - and buf_data.handle_insert_enter - and buf_data.handle_changed - and buf_data.handle_yanked - then - view_buffer.add_handler(buf_data, "CursorMoved", buf_data.handle_moved) - view_buffer.add_handler(buf_data, "TextChangedI", buf_data.handle_insert) - view_buffer.add_handler(buf_data, "InsertEnter", buf_data.handle_insert_enter) - -- changed is part of CursorMoved now - -- view_buffer.add_handler(buf_data, "TextChanged", buf_data.handle_changed) - view_buffer.add_handler(buf_data, "TextYankPost", buf_data.handle_yanked) - else - error("missing handlers") - end -end - --- --- Read buffer content and update the cells for a specific buffer --- ---@param bufnr integer --- M.read_buf = function(bufnr) --- -- replace the old frame --- M.buffers[bufnr].cells = view_buffer.read_a_buf(bufnr) --- end - --- Write the buffer content based on the cells for a specific buffer ---comment ----@param bufnr integer | nil -M.write_buf = function(bufnr) - if not bufnr then - bufnr = state.current_bufnr - if not bufnr then - error("no bufnr") +---@param cells Entity[][] +---@return string[] +M.cells_to_render = function(cells) + local lines = {} + for row_index, row in ipairs(cells) do + local value = {} + for _, entity in ipairs(row) do + if entity.movement.visible then + if not state.player.vision:can_see(entity) then + if state.debug then + -- table.insert(value, "^") + -- TODO: randomness causes flashing + table.insert(value, entity.movement.char) + else + table.insert(value, " ") + end + else + table.insert(value, entity.movement.char) + end + elseif entity.movement.seen then + table.insert(value, entity.movement.char) + else + table.insert(value, constant_defs.not_visible) + end end + lines[row_index] = table.concat(value) end - view_buffer.write_buf(bufnr, M.buffers[bufnr].cells) -end - ---- Apply the callback to move to the next frame for a specific buffer ----@param callback function() -M.tick = function(callback) - if state.animating then - message.debug("animating so skip tick") - return - end - callback() - M.write_buf(nil) - M.animations() + return lines end M.animations = function() -- message.debug("animating true") state.animating = true animate.show_animations(M.get_entity_at_pos, function() - vim.defer_fn(function() + api.async.later(function() -- message.debug("animating false") state.animating = false - state.player:handle_bounce() --TODO: what should this timer be? - end, 10) + end) end) end @@ -157,8 +99,8 @@ M.get_entity_at_pos = function(row, col, skip_player) return state.player end end - local bufnr = state.current_bufnr - local line = M.buffers[bufnr].cells[row] + local level = state.current_level + local line = M.buffers[level].cells[row] if not line then -- TODO: is this an actual error? -- message.notify("no line at " .. row) @@ -168,7 +110,7 @@ M.get_entity_at_pos = function(row, col, skip_player) local entity = line[col] if entity then if entity.movement.row ~= row or entity.movement.col ~= col then - message.notify("entity mismatch", entity.movement.char, entity.movement.row, entity.movement.col, row, col) + message.debug("entity mismatch", entity.movement.char, entity.movement.row, entity.movement.col, row, col) end else -- message.notify("nothing at " .. row .. " " .. col) @@ -181,8 +123,7 @@ end ---@return integer -- col 1 indexed ---@return Entity M.get_under_cursor = function() - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - col = col + 1 + local row, col = api.cursor.get() local entity = M.get_entity_at_pos(row, col, true) -- message.notify("under cursor " .. row .. " " .. col .. " " .. entity.movement.char) return row, col, entity @@ -193,11 +134,11 @@ end ---@param col integer 1 indexed ---@param entity Entity M.set_entity_at_cell = function(row, col, entity) - local bufnr = state.current_bufnr + local level = state.current_level entity.movement.row = row entity.movement.col = col -- TODO: deal with nil - local buf = M.buffers[bufnr] + local buf = M.buffers[level] if not buf then error("no buffer") end @@ -214,27 +155,14 @@ end ---@param col integer ---@param entity Entity M.insert_entity_at_cell = function(row, col, entity) - local bufnr = state.current_bufnr + local level = state.current_level entity.movement.row = row entity.movement.col = col - table.insert(M.buffers[bufnr].cells[row], col, entity) + table.insert(M.buffers[level].cells[row], col, entity) -- increment the remaining cols in the row - for i = col + 1, #M.buffers[bufnr].cells[row] do - M.buffers[bufnr].cells[row][i].movement.col = i + for i = col + 1, #M.buffers[level].cells[row] do + M.buffers[level].cells[row][i].movement.col = i end end ---- Highlight cursor line and column with specific settings ----@param highlight table -M.highlight_cursor = function(highlight) - local bufnr = state.current_bufnr - vim.api.nvim_set_hl(0, "CursorLine", highlight) - vim.api.nvim_set_hl(0, "CursorColumn", highlight) - - vim.defer_fn(function() - vim.api.nvim_set_hl(0, "CursorLine", M.buffers[bufnr].buf_settings.cursorline_hl) - vim.api.nvim_set_hl(0, "CursorColumn", M.buffers[bufnr].buf_settings.cursorcolumn_hl) - end, 50) -end - return M diff --git a/lua/neohack/chance.lua b/lua/neohack/chance.lua index d14c8f9..d2a9552 100644 --- a/lua/neohack/chance.lua +++ b/lua/neohack/chance.lua @@ -12,7 +12,7 @@ local state = require("neohack.state") M.action_success = function(skill, advantage, resistance, threshold) -- random is either helpful or harmful local random = math.random() - 0.5 - local level_resistance = resistance + (state.current_floor * 0.1) + local level_resistance = resistance + (state.current_level * 0.1) local success_chance = skill + advantage - level_resistance + random -- message.notify( -- "action_success chance: " @@ -41,7 +41,7 @@ M.action_success_bucket = function(skill, advantage, resistance, thresholds) -- Random is either helpful or harmful local random = math.random() - 0.5 local success_chance = skill + advantage - resistance + random - message.notify( + message.trace( "action_success chance: " .. success_chance .. ", skill: " diff --git a/lua/neohack/components/attributes.lua b/lua/neohack/components/attributes.lua index 2c335ac..655f3f4 100644 --- a/lua/neohack/components/attributes.lua +++ b/lua/neohack/components/attributes.lua @@ -22,6 +22,7 @@ ---Movement ---@field scared number ---@field block_vision boolean +---@field speed number time for a move in seconds ---Sneak ---@field sneak_attribute number ---Vision diff --git a/lua/neohack/components/combat.lua b/lua/neohack/components/combat.lua index bb8cf06..c0526d8 100644 --- a/lua/neohack/components/combat.lua +++ b/lua/neohack/components/combat.lua @@ -8,7 +8,8 @@ local constant_defs = require("neohack.constant_defs") local attributes = require("neohack.attribute_getters") local animate = require("neohack.animate") local Def = require("neohack.def") -local view_buffer = require("neohack.view_buffer") +local api = require("neohack.api") +local highlight = require("neohack.highlight") ---@class Combat ---@field parent Entity @@ -145,7 +146,7 @@ function Combat:try_damage_dealt(target, weapon) local weapon_success = self:try_weapon(target, weapon) if not weapon_success then - message.notify(self.parent.name .. " missed " .. target.parent.name) + message.action(self.parent, "missed", target.parent.name) return 0, false end @@ -159,7 +160,7 @@ function Combat:try_damage_dealt(target, weapon) local damage = weapon.attributes.damage - target:deflect_skill() if damage <= 0 then - message.notify(target.parent.name .. " deflected " .. self.parent.name) + message.action(target.parent, "deflected", self.parent.name) return 0, false end @@ -171,21 +172,21 @@ end function Combat:dodge(attacker) local floor = self.parent.movement:find_floor(false) if not floor then - message.debug(self.parent.name, "has no space to dodge") + message.action(self.parent, "has no space to dodge") return false end if self.parent.type ~= Def.DefType.player then - view_buffer.set_cursor(floor.movement.row, floor.movement.col) + api.cursor.set(floor.movement.row, floor.movement.col) end self.parent.movement:move_to(floor.movement.row, floor.movement.col) - message.notify(self.parent.name .. " dodged " .. attacker.parent.name .. ".") + message.action(self.parent, "dodged", attacker.parent.name) return true end ---comment ---@param target Combat function Combat:apply_highlights(target) - self.hit_highlight = constant_defs.hitting_highlight + self.hit_highlight = highlight.hitting_highlight end ---comment @@ -198,7 +199,7 @@ function Combat:apply_durability(damage, weapon) end weapon.attributes.durability = weapon.attributes.durability - 1 if weapon.attributes.durability <= 0 then - message.notify(self.parent.name .. " broke " .. weapon.name) + message.action(self.parent, "broke", weapon.name) end return damage end @@ -211,11 +212,9 @@ function Combat:apply_damage(damage, target, weapon) target.parent.attributes.health = target.parent.attributes.health - damage if target.parent.health:check_dead() then table.insert(state.bodies, target) - message.notify( - self.parent.name .. " killed " .. target.parent.name .. " with a " .. weapon.name .. " for " .. damage - ) + message.action(self.parent, "killed", target.parent.name, " with a " .. weapon.name .. " for " .. damage) else - message.notify(self.parent.name .. " hit " .. target.parent.name .. " with a " .. weapon.name .. " for " .. damage) + message.action(self.parent, "hit", target.parent.name, " with a " .. weapon.name .. " for " .. damage) end end @@ -241,7 +240,7 @@ function Combat:attack(target) local damage, dealt = self:try_damage_dealt(target, weapon) if not dealt then - self.hit_highlight = constant_defs.missing_highlight + self.hit_highlight = highlight.missing_highlight return false end diff --git a/lua/neohack/components/decision.lua b/lua/neohack/components/decision.lua index 2099a62..db0cf02 100644 --- a/lua/neohack/components/decision.lua +++ b/lua/neohack/components/decision.lua @@ -4,7 +4,6 @@ local Def = require("neohack.def") local message = require("neohack.message") local state = require("neohack.state") -local constant_defs = require("neohack.constant_defs") local Move = require("neohack.move") local utils = require("neohack.utils") @@ -49,7 +48,7 @@ function Decision:find_target(visible_targets) if self.parent.vision:can_see(entity) then return old_target else - message.debug(self.parent.name, "cannot see old target", old_target.name) + message.trace(self.parent.name, "cannot see old target", old_target.name) end end end @@ -86,10 +85,10 @@ function Decision:choose_target(visible_targets) local target = self:find_target(visible_targets) self.parent.attributes.target = target if target then - message.debug(self.parent.name, "targeting", target.name) + message.trace(self.parent.name, "targeting", target.name) return true else - message.debug(self.parent.name, "targeting", "nothing") + message.trace(self.parent.name, "targeting", "nothing") return false end end @@ -113,13 +112,13 @@ function Decision:target_type(target) return target.type end ----@class Action +---@class EntityAction ---@field name string ---@field weight number ---@field act fun(self: Decision): boolean --- available actions ----@type table +---@type table Decision.actions = { attack_player = { name = "attack_player", @@ -162,7 +161,7 @@ Decision.actions = { ---@return boolean act = function(self) local enemies, _ = state.scan_area( - state.current_bufnr, + state.current_level, self.parent.movement.row, self.parent.movement.col, 1 -- only right next to @@ -184,7 +183,7 @@ Decision.actions = { ---@return boolean act = function(self) local _, items = state.scan_area( - state.current_bufnr, + state.current_level, self.parent.movement.row, self.parent.movement.col, 1 -- only right next to @@ -213,7 +212,7 @@ Decision.actions = { -- random spell local spell = spells[math.random(#spells)] local enemies, items = state.scan_area( - state.current_bufnr, + state.current_level, self.parent.movement.row, self.parent.movement.col, self.parent.vision:view_distance() @@ -238,7 +237,7 @@ Decision.actions = { ---@param self Decision ---@return boolean act = function(self) - local targets = state.get_visible_entities(state.current_bufnr, self.parent, self.parent.vision:view_distance()) + local targets = state.get_visible_entities(state.current_level, self.parent, self.parent.vision:view_distance()) self:choose_target(targets) return true end, @@ -293,7 +292,7 @@ Decision.actions = { act = function(self) if self.parent.sneak.sneaking then local enemies, _ = state.scan_area( - state.current_bufnr, + state.current_level, self.parent.movement.row, self.parent.movement.col, 1 -- only right next to @@ -347,17 +346,17 @@ Decision.actions = { end, }, - -- this actions cause there to be fewer total items + -- these actions cause there to be fewer total items --TODO: fuse? --TODO eat? - wait = { - name = "wait", + rest = { + name = "rest", weight = 1, ---@param self Decision ---@return boolean act = function(self) - message.notify(self.parent.name, "is bored") + message.trace(self.parent.name, "is bored") return true end, }, @@ -365,7 +364,7 @@ Decision.actions = { function Decision:ensure_has_target() if not self.parent.attributes.target then - local targets = state.get_visible_entities(state.current_bufnr, self.parent, self.parent.vision:view_distance()) + local targets = state.get_visible_entities(state.current_level, self.parent, self.parent.vision:view_distance()) self:choose_target(targets) end end @@ -380,10 +379,10 @@ function Decision:choose_action() end for i = 1, #list + 1, 1 do local action, index = utils.weighted_random(list) - if action and action ~= Decision.actions.wait then + if action and action ~= Decision.actions.rest then table.remove(list, index) -- don't try this again if action.act(self) then - message.debug(self.parent.name, "chose", action.name) + message.trace(self.parent.name, "chose", action.name) return else -- that action isn't possible this turn @@ -394,8 +393,8 @@ function Decision:choose_action() end end - Decision.actions.wait.act(self) - message.debug(self.parent.name, "fallback to wait") + Decision.actions.rest.act(self) + message.trace(self.parent.name, "fallback to rest") end return Decision diff --git a/lua/neohack/components/eat.lua b/lua/neohack/components/eat.lua index 7ad971d..56d2ffd 100644 --- a/lua/neohack/components/eat.lua +++ b/lua/neohack/components/eat.lua @@ -54,8 +54,8 @@ function Eat:eat(keys) local loot = item.inventory:extract_all() if #loot > 0 then for _, loot_item in ipairs(loot) do - message.notify("Threw up a " .. loot_item.name) - self.parent.inventory:pickup(loot_item) + message.action(self.parent, "threw up a", loot_item.name) + self.parent.inventory:pickup(loot_item, true) end end @@ -74,7 +74,7 @@ function Eat:eat(keys) p.eat_attribute = p.eat_attribute + 0.01 -- TODO what other affects could eating have? - message.notify("Ate a", item.name, ". health:", p.health) --, vim.inspect(p.attributes)) + message.action(self.parent, "ate a", item.name, ". health:", p.health) --, vim.inspect(p.attributes)) if self.parent.health:check_dead() then return true end diff --git a/lua/neohack/components/fuse.lua b/lua/neohack/components/fuse.lua index 1c4f74b..16fb738 100644 --- a/lua/neohack/components/fuse.lua +++ b/lua/neohack/components/fuse.lua @@ -5,6 +5,7 @@ local fuse = require("neohack.fuser") local state = require("neohack.state") local message = require("neohack.message") local attributes = require("neohack.attribute_getters") +local utils = require("neohack.utils") ---@class Fuse ---@field parent Entity @@ -50,7 +51,8 @@ function Fuse:fuse(keys) if not items then return end - message.notify("Fusing " .. vim.inspect(items)) + local name = items[1].name + message.debug("Fusing " .. name .. " and " .. items[2].name) -- got all items, fuse them ---@type Entity @@ -62,9 +64,9 @@ function Fuse:fuse(keys) new_item = latest end end - message.notify("Fused " .. table.concat(keys, " ") .. " into " .. new_item.name) + message.action(self.parent, "fused a", new_item.name, "from " .. name .. " and " .. items[2].name) -- all other items disappear as part of fusing - self.parent.inventory:pickup(new_item) + self.parent.inventory:pickup(new_item, true) end return Fuse diff --git a/lua/neohack/components/inventory.lua b/lua/neohack/components/inventory.lua index 61b427e..7c6ff60 100644 --- a/lua/neohack/components/inventory.lua +++ b/lua/neohack/components/inventory.lua @@ -35,7 +35,7 @@ function Inventory.new(parent) end function Inventory:inspect() - return self:get_inventory() + return utils.concatenate_strings(utils.to_string(self:get_inventory())) end function Inventory:attributes() return {} @@ -69,11 +69,9 @@ end ---@return boolean function Inventory:pickup_off_floor(entity) if self:pickup(entity) then - buffer.set_entity_at_cell( - entity.movement.row, - entity.movement.col, - state.entity_generator.new_floor(entity.movement.row, entity.movement.col) - ) + local new_floor = state.entity_generator.new_floor(entity.movement.row, entity.movement.col) + new_floor.movement.visible = entity.movement.visible + buffer.set_entity_at_cell(entity.movement.row, entity.movement.col, new_floor) return true end return false @@ -81,10 +79,13 @@ end ---comment ---@param entity Entity -function Inventory:pickup(entity) +---@param silent boolean | nil +function Inventory:pickup(entity, silent) if entity.type == Def.DefType.item then table.insert(self.items, 1, entity) - message.notify(self.parent.name, "got a", entity.name) + if not silent then + message.action(self.parent, "got a", entity.name) + end return true else -- do nothing @@ -93,11 +94,11 @@ function Inventory:pickup(entity) end ---comment ----@return string +---@return string[] function Inventory:get_inventory_item_with_index() - local items_str = "" + local items_str = {} for index, item in pairs(self.items) do - items_str = items_str .. index .. ":" .. item.name .. "\n" + table.insert(items_str, index .. ":" .. item.name) end return items_str end @@ -123,40 +124,45 @@ function Inventory:retrieve_item_char(char) if self.items[i].movement.char == char then local entity = table.remove(self.items, i) -- TODO: deal with retrieve for action being too verbose - message.notify(self.parent.name, "retrieved", entity.movement.char) + message.action(self.parent, "retrieved", entity.name) return entity end end - message.notify(self.parent.name("has no"), char, "to retrieve") + message.action(self.parent, "has no", char, "to retrieve") return nil end ----get the named item out of inventory +---get an item from inventory, not removed +---@param key any +function Inventory:get_item(key) + local index = utils.string_to_int(key) + if index then + local item = self.items[index] + return item, index + else + for i, item in ipairs(self.items) do + if item.name == key then + -- matches first found with that name + return item, i + end + end + end + message.action(self.parent, "has no", key) + return nil, nil +end + +---get the named item out of inventory, removes from inventory ---@param keys string[] names or indexes ---@return Entity[]? function Inventory:retrieve_items(keys) local items = {} local indexes = {} for _, name in ipairs(keys) do - local found = false - local index = utils.string_to_int(name) + local item, index = self:get_item(name) if index then - table.insert(items, self.items[index]) + table.insert(items, item) table.insert(indexes, index) - found = true else - for i, v in ipairs(self.items) do - if v.name == name then - -- matches first found - table.insert(items, v) - table.insert(indexes, i) - found = true - break - end - end - end - if not found then - message.notify(self.parent.name, "has no", name, "to retrieve") -- get all or nothing return nil end @@ -169,7 +175,7 @@ function Inventory:retrieve_items(keys) for _, index in ipairs(indexes) do table.remove(self.items, index) end - -- message.notify(self.parent.name,"retrieved", table.concat(indexes, " ")) + -- message.action(self.parent,"retrieved", table.concat(indexes, " ")) return items end @@ -193,7 +199,7 @@ function Inventory:_wield(key, slot) local items = self:retrieve_items({ key }) if items and items[1] then self.slots[slot] = items[1] - message.notify(self.parent.name, "wearing", items[1].name, "on", state.slot_name(slot)) + message.action(self.parent, "wearing", items[1].name, "on", state.slot_name(slot)) else return end @@ -249,10 +255,10 @@ function Inventory:get_wearing() end ---comment ----@return string +---@return string[] function Inventory:get_inventory() local items_str = self:get_inventory_item_with_index() - return "wearing: " .. self:get_wearing() .. "\nitems:\n" .. items_str + return { "wearing: " .. self:get_wearing(), "items:", unpack(items_str) } end ---comment @@ -260,58 +266,41 @@ end ---@param new_name string ---@return boolean function Inventory:rename(key, new_name) - --TODO: don't pull out - local items = self:retrieve_items({ key }) - if not items then - return false + local item = self:get_item(key) + if item then + message.action(self.parent, "renamed", item.name, "to", new_name) + item.name = new_name + return true end - local item = items[1] - message.notify(self.parent.name, "renamed", item.name, "to", new_name) - item.name = new_name - self:pickup(item) - return true + return false end ---comment ----@param keys string[] -function Inventory:look(keys) - for i, key in ipairs(keys) do - if key == "0" or key == "self" then - message.notify(self.parent.name, "looked at self", self.parent:inspect()) - table.remove(keys, i) - end - end - local items = self:retrieve_items(keys) - if not items then +---@param key string +function Inventory:look(key) + if key == "0" or key == "self" then + message.action(self.parent, "looked at", self.parent.name, self.parent:inspect()) return end - for _, item in ipairs(items) do - if item then - self:pickup(item) - message.notify(self.parent.name, "looked at", item:inspect()) - end + local item = self:get_item(key) + if item then + message.action(self.parent, "looked at", item.name, item:inspect()) end end ---pilfer/loot items out of the inventory of another item ---@param key string function Inventory:pilfer(key) - local lootables = self:retrieve_items({ key }) - if not lootables then - return - end - for _, lootable in ipairs(lootables) do - if lootable then - local extracted = "" - local loot = lootable.inventory:extract_all() - for _, item in ipairs(loot) do - self:pickup(item) - extracted = extracted .. item.name .. " " - end - self:pickup(lootable) - if #loot > 0 then - message.notify(self.parent.name, "took", extracted, "from", lootable.name) - end + local lootable = self:get_item(key) + if lootable then + local extracted = "" + local loot = lootable.inventory:extract_all() + for _, item in ipairs(loot) do + self:pickup(item, true) + extracted = extracted .. item.name .. " " + end + if #loot > 0 then + message.action(self.parent, "took", extracted.name, "from", lootable.name) end end end @@ -328,11 +317,12 @@ function Inventory:drop(key) for _, item in ipairs(items) do local floor = self.parent.movement:find_floor(true) if not floor then - message.notify(self.parent.name, "has no space to drop", item.name) + message.action(self.parent, "has no space to drop", item.name) return false end + item.movement.visible = floor.movement.visible buffer.set_entity_at_cell(floor.movement.row, floor.movement.col, item) - message.notify(self.parent.name, "dropped", item.name) + message.action(self.parent, "dropped", item.name) return true end return false diff --git a/lua/neohack/components/movement.lua b/lua/neohack/components/movement.lua index 1371b98..7708ce2 100644 --- a/lua/neohack/components/movement.lua +++ b/lua/neohack/components/movement.lua @@ -5,10 +5,10 @@ local moves = require("neohack.moves") local Move = require("neohack.move") local buffer = require("neohack.buffer") local constant_defs = require("neohack.constant_defs") -local view_buffer = require("neohack.view_buffer") local state = require("neohack.state") local Def = require("neohack.def") local message = require("neohack.message") +local api = require("neohack.api") ---@class Movement ---@field parent Entity @@ -18,6 +18,7 @@ local message = require("neohack.message") ---@field col integer ---@field visible boolean ---@field seen boolean +---@field last_turn_at number time when last moved in ms local Movement = {} Movement.__index = Movement Movement.__name = "Movement" @@ -29,8 +30,9 @@ Movement.__name = "Movement" ---@param row integer ---@param col integer ---@param block_vision boolean +---@param speed number ---@return Movement -function Movement.new(parent, char, moves_set, row, col, block_vision) +function Movement.new(parent, char, moves_set, row, col, block_vision, speed) local instance = { parent = parent, @@ -44,10 +46,12 @@ function Movement.new(parent, char, moves_set, row, col, block_vision) --TODO: could these be attributes? visible = true, seen = false, + last_turn_at = 0, } parent.attributes.scared = 0.01 --TODO: make this a number parent.attributes.block_vision = block_vision + parent.attributes.speed = speed or 0 setmetatable(instance, Movement) return instance end @@ -55,6 +59,7 @@ end function Movement:inspect() return "char=" .. self.char .. " row=" .. self.row .. " col=" .. self.col end + function Movement:attributes() return { -- char = self.char, @@ -65,6 +70,7 @@ function Movement:attributes() seen = self.seen, scared = self.parent.attributes.scared, block_vision = self.parent.attributes.block_vision, + speed = self.parent.attributes.speed, } end @@ -96,7 +102,7 @@ function Movement:make_move(move) self:move_to(new_row, new_col) return true else - -- message.notify("can't move to " .. char or "" .. " " .. vim.inspect(mover) .. " " .. vim.inspect(move)) + -- message.action(self.parent, "can't move to " .. char or "" .. " " .. vim.inspect(mover) .. " " .. vim.inspect(move)) return false end end @@ -104,19 +110,22 @@ end ---@param row integer ---@param col integer function Movement:move_to(row, col) - -- message.notify("making move " .. vim.inspect(self) .. " " .. vim.inspect(move)) + -- message.debug("making move " .. vim.inspect(self) .. " " .. vim.inspect(move)) -- don't delete item under player if self.parent.type ~= Def.DefType.player then - buffer.set_entity_at_cell(self.row, self.col, state.entity_generator.new_floor(self.row, self.col)) - view_buffer.set_highlight(self.row, self.col, nil) + local new_floor = state.entity_generator.new_floor(self.row, self.col) + new_floor.movement.visible = self.parent.movement.visible + buffer.set_entity_at_cell(self.row, self.col, new_floor) + api.render.set_highlight(self.row, self.col, nil) else - -- buffer.highlight_cursor(constant_defs.cursor_hit_highlight) + -- api.cursor.highlight_cursor(constant_defs.cursor_hit_highlight) end buffer.set_entity_at_cell(row, col, self.parent) end ---comment function Movement:move_closer_to_target() + local my_moves = self.moves or moves.eight if self.parent.attributes.target then local target_row = self.parent.attributes.target.movement.row local target_col = self.parent.attributes.target.movement.col @@ -136,7 +145,7 @@ function Movement:move_closer_to_target() if (math.random() * 0.2) < self.parent.attributes.randomness then -- randomly do a random move -- message.notify("random move") - return self:make_move(self.moves[math.random(#self.moves)]) + return self:make_move(my_moves[math.random(#my_moves)]) else -- message.notify("move " .. mover.name .. vim.inspect(move_list)) for _, move in ipairs(move_list) do @@ -147,7 +156,7 @@ function Movement:move_closer_to_target() end else -- move randomly if no target - return self:make_move(self.moves[math.random(#self.moves)]) + return self:make_move(my_moves[math.random(#my_moves)]) end end @@ -181,7 +190,7 @@ function Movement:try_move(move, in_fear) end elseif type == Def.DefType.friend then entity.attributes.health = entity.attributes.health + 0.5 - message.notify(self.parent.name, "hugged", entity.name) + message.action(self.parent, "hugged", entity.name) else if type == Def.DefType.item then -- if cell has an item, pick it up @@ -191,7 +200,7 @@ function Movement:try_move(move, in_fear) end self:make_move(move) if in_fear then - message.notify(self.parent.name .. " is scared") + message.action(self.parent, "is scared") end return true end @@ -211,9 +220,9 @@ function Movement:kick(direction) local target = buffer.get_entity_at_pos(kick_row, kick_col) if target and target.type ~= Def.DefType.floor then if target.movement:make_move(move) then - message.notify(self.parent.name, "kicked", target.name) + message.action(self.parent, "kicked", target.name) else - message.notify(self.parent.name, "kick failed on", target.name) + message.action(self.parent, "kick failed on", target.name) end return true else diff --git a/lua/neohack/components/sneak.lua b/lua/neohack/components/sneak.lua index 4ced107..93412cc 100644 --- a/lua/neohack/components/sneak.lua +++ b/lua/neohack/components/sneak.lua @@ -63,7 +63,7 @@ end function Sneak:pick(direction) local move = Move.directions[direction] if move == nil then - message.debug(self.parent.name, "pick failed without direction") + message.trace(self.parent.name, "pick failed without direction") return false end local row, col = self.parent.movement.row, self.parent.movement.col @@ -86,34 +86,34 @@ function Sneak:pick_entity(target) local success = chance.action_success(self:sneak_skill(), sneak_bonus, target.attributes.vision, self.sneak_threshold) if not success then - message.notify(self.parent.name, "pick failed on", target.name) + message.action(self.parent, "pick failed on", target.name) return true end local index = math.random(#target.inventory.items) local items = target.inventory:retrieve_items({ tostring(index) }) if items and #items > 0 then local item = items[1] - self.parent.inventory:pickup(item) - message.notify(self.parent.name, "picked", item.name, "from", target.name) + self.parent.inventory:pickup(item, true) + message.action(self.parent, "picked", item.name, "from", target.name) else - message.notify(self.parent.name, "picked nothing from", target.name) + message.action(self.parent, "picked nothing from", target.name) end return true else - message.debug(self.parent.name, "picked from non target") + message.trace(self.parent.name, "picked from non target") return false end end function Sneak:sneak_on() self.sneaking = true - message.debug(self.parent.name, "sneaking") + message.action(self.parent, "sneaking") return true end function Sneak:sneak_off() self.sneaking = false - message.debug(self.parent.name, "not sneaking") + message.action(self.parent, "not sneaking") return true end diff --git a/lua/neohack/components/speak.lua b/lua/neohack/components/speak.lua index 49a8dac..b35d629 100644 --- a/lua/neohack/components/speak.lua +++ b/lua/neohack/components/speak.lua @@ -60,7 +60,7 @@ end ---@return Entity[] entities that heard the word function Speak:entities_that_heard(target) local enemies, items = state.scan_area( - state.current_bufnr, + state.current_level, self.parent.movement.row, self.parent.movement.col, self.parent.vision:view_distance() @@ -108,17 +108,17 @@ function Speak:say(word, target) spell.speak.spell.effect.cast(self.parent, 1) end end - message.notify(self.parent.name, "cast", effects, "on themself") + message.action(self.parent, "cast", effects, "on themself") elseif #entities > 0 and #casting > 0 then - message.notify(self.parent.name, "cast", effects, "on", #entities, target .. "s") + message.action(self.parent, "cast", effects, "on", #entities, target .. "s") elseif #entities > 0 then - message.notify(self.parent.name, "said", word, "and was heard by", #entities, target .. "s") + message.action(self.parent, "said", word, "and was heard by", #entities, target .. "s") elseif #casting > 0 then - message.notify(self.parent.name, "cast", effects, "on nothing") + message.action(self.parent, "cast", effects, "on nothing") elseif target == nil then - message.notify(self.parent.name, "said", word, "to themselves") + message.action(self.parent, "said", word, "to themselves") else - message.notify(self.parent.name, "said", word, "but no", target, "heard") + message.action(self.parent, "said", word, "but no", target, "heard") end return true end diff --git a/lua/neohack/constant_defs.lua b/lua/neohack/constant_defs.lua index f18fe13..f9743d7 100644 --- a/lua/neohack/constant_defs.lua +++ b/lua/neohack/constant_defs.lua @@ -14,12 +14,6 @@ M.corpse_suffix = "_corpse" -- M.hidden_highlight = "LineNr" -- gray fg -M.missing_highlight = "NeoHack_missing" -- yellow bg -M.hitting_highlight = "NeoHack_hitting" -- red bg - -M.cursor_hit_highlight = { bg = "#8f1122" } -- dark red bg -M.cursor_dodge_highlight = { bg = "#8f5d11" } -- dark orange bg - M.slap = Def.new({ type = Def.DefType.item, name = "slap", @@ -31,4 +25,15 @@ M.slap = Def.new({ view_distance = 1, }) +M.letters_lower = "abcdefghijklmnopqrstuvwxyz" + +M.letters = M.letters_lower .. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +M.directions = { + { key = "h", name = "left" }, + { key = "j", name = "down" }, + { key = "k", name = "up" }, + { key = "l", name = "right" }, +} + return M diff --git a/lua/neohack/def.lua b/lua/neohack/def.lua index da9ac02..2e2ff86 100644 --- a/lua/neohack/def.lua +++ b/lua/neohack/def.lua @@ -10,6 +10,7 @@ --- @field moves nil | Move[] -- the moves this entity can make --- @field randomness nil | number -- how chaotic entity is --- @field vision nil | number -- how well entity can see movements and attacks +--- @field speed nil | number -- how often a move can be made --- @field view_distance number --- @field spell nil | Spell -- a spell written on the item local Def = {} @@ -27,6 +28,7 @@ Def.__index = Def --- @field moves nil | Move[] -- the moves this entity can make --- @field randomness nil | number -- how chaotic entity is --- @field vision nil | number -- how well entity can see movements and attacks +--- @field speed nil | number -- how often a move can be made --- @field view_distance number --- @field spell nil | Spell -- a spell written on the item @@ -50,6 +52,7 @@ function Def.new(args) instance.moves = args.moves instance.randomness = args.randomness instance.vision = args.vision + instance.speed = args.speed instance.view_distance = args.view_distance instance.spell = args.spell return instance diff --git a/lua/neohack/defs.lua b/lua/neohack/defs.lua index a20cd83..977eb99 100644 --- a/lua/neohack/defs.lua +++ b/lua/neohack/defs.lua @@ -183,6 +183,7 @@ M.enemy_defs = { vision = 0.2, view_distance = 8, durability = 2, + speed = 0.5, }), r = Def.new({ type = Def.DefType.enemy, @@ -197,6 +198,7 @@ M.enemy_defs = { vision = 0.6, view_distance = 12, durability = 2, + speed = 0.3, }), g = Def.new({ type = Def.DefType.enemy, @@ -211,6 +213,7 @@ M.enemy_defs = { vision = 0.7, view_distance = 9, durability = 6, + speed = 3, }), f = Def.new({ type = Def.DefType.enemy, @@ -225,6 +228,7 @@ M.enemy_defs = { vision = 0.3, view_distance = 3, durability = 1, + speed = 1.2, }), b = Def.new({ type = Def.DefType.enemy, @@ -239,6 +243,7 @@ M.enemy_defs = { vision = 0.1, view_distance = 3, durability = 1, + speed = 0.9, }), ["="] = Def.new({ type = Def.DefType.enemy, @@ -253,6 +258,7 @@ M.enemy_defs = { vision = 0.1, view_distance = 1, durability = 10, + speed = 0.5, }), ["("] = Def.new({ type = Def.DefType.enemy, @@ -267,6 +273,7 @@ M.enemy_defs = { vision = 0.01, view_distance = 1, durability = 1, + speed = 2.3, }), [")"] = Def.new({ type = Def.DefType.enemy, @@ -281,6 +288,7 @@ M.enemy_defs = { vision = 0.01, view_distance = 1, durability = 1, + speed = 1.3, }), ['"'] = Def.new({ type = Def.DefType.enemy, @@ -295,6 +303,7 @@ M.enemy_defs = { vision = 0.01, view_distance = 1, durability = 4, + speed = 2.3, }), ["*"] = Def.new({ type = Def.DefType.enemy, @@ -309,6 +318,7 @@ M.enemy_defs = { vision = 0.01, view_distance = 3, durability = 2, + speed = 0.9, }), A = Def.new({ type = Def.DefType.enemy, @@ -323,6 +333,7 @@ M.enemy_defs = { vision = 0.9, view_distance = 5, durability = 6, + speed = 2.9, }), } @@ -447,7 +458,8 @@ end ---@param col integer ---@return Entity M.new_floor = function(row, col) - return Entity.new(M.floor, row, col) + local new_floor = Entity.new(M.floor, row, col) + return new_floor end ---comment diff --git a/lua/neohack/entity.lua b/lua/neohack/entity.lua index 9c621d3..6baff66 100644 --- a/lua/neohack/entity.lua +++ b/lua/neohack/entity.lua @@ -23,7 +23,7 @@ local Attributes = require("neohack.components.attributes") ---@field eat Eat ---@field vision Vision ---@field decision Decision ----@field highlight_group string +---@field highlight_group Highlight|nil local Entity = {} Entity.__index = Entity Entity.__name = "Entity" @@ -43,7 +43,7 @@ function Entity.new(def, row, col) -- components instance.attributes = Attributes.new() - instance.movement = Movement.new(instance, def.char, def.moves, row, col, def.block_vision) + instance.movement = Movement.new(instance, def.char, def.moves, row, col, def.block_vision, def.speed) instance.inventory = Inventory.new(instance) instance.health = Health.new(instance, def.health) instance.combat = Combat.new(instance, def.damage, def.durability, def.hit_rate, Entity.default_weapon) diff --git a/lua/neohack/event.lua b/lua/neohack/event.lua index 4b7ff08..98e779d 100644 --- a/lua/neohack/event.lua +++ b/lua/neohack/event.lua @@ -5,23 +5,14 @@ local M = {} local message = require("neohack.message") local map = require("neohack.map") -local buffer = require("neohack.buffer") local state = require("neohack.state") -local view_buffer = require("neohack.view_buffer") local constant_defs = require("neohack.constant_defs") - ----comment ----@return boolean -M.in_sneak_move = function() - local mode = vim.api.nvim_get_mode().mode - local block_visual = "\22" - --- TODO: should line or block mode be different? - return mode == "v" or mode == "V" or mode == block_visual -end +local api = require("neohack.api") +local buffer = require("neohack.buffer") ---comment M.player_move = function() - if M.in_sneak_move() then + if api.keymaps.in_sneak_mode() then state.player.sneak:sneak_on() state.player:player_sneak_move() else @@ -29,24 +20,70 @@ M.player_move = function() end end ----comment -M.move_entities = function() - -- move enemies - local enemies, friends = map.scan_buf(state.current_bufnr) - M.make_moves(enemies) - M.make_moves(friends) +---@param game_time_ms number time of this turn +---@return boolean true if any moves were made +M.move_entities = function(game_time_ms) + local enemies, friends = map.scan_buf(state.current_level) + return M.make_makes(game_time_ms, enemies) or M.make_makes(game_time_ms, friends) end ---comment +---@param game_time_ms number time of this turn ---@param movers Entity[] -M.make_moves = function(movers) +---@return boolean true if any moves were made +M.make_makes = function(game_time_ms, movers) + local any_moves = false for _, mover in ipairs(movers) do - if mover.health:check_dead() then - message.notify(string.gsub(mover.name, constant_defs.corpse_suffix, "") .. " died") - else - mover.decision:choose_action() + if M.ready_to_move(game_time_ms, mover) then + -- move is due now + M.move(game_time_ms, mover) + any_moves = true end end + return any_moves +end + +---@param game_time_ms number +---@param mover Entity +---@return boolean entity is ready to move +M.ready_to_move = function(game_time_ms, mover) + local expected_turn_delta_ms = mover.attributes.speed * 1000 + local move_time = mover.movement.last_turn_at + expected_turn_delta_ms + + if move_time < game_time_ms then + -- message.debug( + -- "ready to move", + -- game_time_ms, + -- "mover", + -- mover.name, + -- "speed", + -- mover.attributes.speed, + -- move_time, + -- game_time_ms + -- ) + return true + else + -- message.debug( + -- "not ready to move", + -- game_time_ms, + -- "mover", + -- mover.name, + -- "speed", + -- mover.attributes.speed, + -- move_time, + -- game_time_ms + -- ) + return false + end +end + +M.move = function(game_time_ms, mover) + mover.movement.last_turn_at = game_time_ms + if mover.health:check_dead() then + message.action(mover, string.gsub(mover.name, constant_defs.corpse_suffix, "") .. " died") + else + mover.decision:choose_action() + end end return M diff --git a/lua/neohack/game.lua b/lua/neohack/game.lua index f53ef09..7653d12 100644 --- a/lua/neohack/game.lua +++ b/lua/neohack/game.lua @@ -1,37 +1,38 @@ local tick_timer = require("neohack.tick_timer") -local view_buffer = require("neohack.view_buffer") local defs = require("neohack.defs") + --- the overall game --- local M = { game_levels = {}, + open_ui = nil, + generations = 0, } local message = require("neohack.message") -local edits = require("neohack.edits") local Player = require("neohack.player") local map = require("neohack.map") local buffer = require("neohack.buffer") local state = require("neohack.state") local actions = require("neohack.actions") local event = require("neohack.event") -local utils = require("neohack.utils") local generated_defs = require("neohack.generated_defs") +local api = require("neohack.api") ---comment M.start_game = function() - vim.cmd("silent! only") - message.open() state.init_state(Player.new()) generated_defs.init() - vim.fn.setreg('"', "") - if #utils.keys(buffer.buffers) == 0 then - M.create_new_buffer(nil) + M.open_ui() + + local level = 1 + local buff = buffer.buffers[level] + if buff == nil then + M.create_new_buffer(level) else - local bufnr = buffer.levels[1] - M.setup_buffer(buffer.buffers[bufnr].bufnr, 1) + M.setup_buffer(level) end state.entity_generator.new_floor = defs.new_floor @@ -41,70 +42,76 @@ M.start_game = function() state.player.handle_down = M.handle_down state.player.handle_up = M.handle_up - message.notify("New game started.") + message.notify("Welcome to NeoHack!") + message.notify("You are the cursor, explore the dungeon and see how deep it is.") actions.actions.help() + state.running = true end -M.create_new_buffer = function(lines) - local bufnr, level = buffer.create_new_buffer(lines, { - handle_moved = M.handle_moved, - handle_insert = M.handle_insert, - handle_insert_enter = M.handle_insert_enter, - handle_changed = M.handle_changed, - handle_yanked = M.handle_yanked, - }) +--- +---@param level integer +M.create_new_buffer = function(level) + api.render.new_view(level) + buffer.setup_new_buffer(level) -- small map -- buffer.buffers[bufnr].cells = map.generate_new_map(20, 20, 1000) -- large map - buffer.buffers[bufnr].cells = map.generate_new_map(30, 160, 10000) + buffer.buffers[level].cells = map.generate_new_map(state.rows, state.cols, M.generations) + + M.setup_buffer(level) +end - buffer.write_buf(bufnr) - M.setup_buffer(bufnr, level) +M.setup_buffer = function(level) + state.current_level = level + M.setup_view_buffer() + local _, _, first_floor = map.scan_buf(level) + if first_floor then + api.cursor.set(first_floor.row, first_floor.col) + end + actions.setup(M.tick) end -M.setup_buffer = function(bufnr, level) - state.current_bufnr = bufnr - state.current_floor = level - local _, _, first_floor = map.scan_buf(bufnr) - buffer.setup_buffer(bufnr, first_floor) - actions.setup(bufnr, M.tick) - -- write to hide full map - buffer.write_buf(bufnr) +M.setup_view_buffer = function() + buffer.write_buf() + api.render.setup_view({ + handle_moved = M.handle_moved, + handle_insert = M.handle_insert, + handle_insert_enter = M.handle_insert_enter, + handle_changed = M.handle_changed, + handle_yanked = M.handle_yanked, + }) end M.handle_down = function() - local new_level = state.current_floor + 1 - local bufnr = buffer.levels[new_level] - if not bufnr then - M.create_new_buffer({ " " }) + local new_level = state.current_level + 1 + local buff = buffer.buffers[new_level] + if not buff then + M.create_new_buffer(new_level) + -- TODO:Not finding blank space for some reason else - state.current_floor = new_level - state.current_bufnr = bufnr - buffer.setup_buffer(bufnr, nil) + state.current_level = new_level + + M.setup_view_buffer() end - return state.current_floor + return state.current_level end M.handle_up = function() - local new_level = state.current_floor - 1 - local bufnr = buffer.levels[new_level] - if not bufnr then + local new_level = state.current_level - 1 + local buff = buffer.buffers[new_level] + if not buff then return nil else - state.current_floor = new_level - state.current_bufnr = bufnr - vim.api.nvim_set_current_buf(bufnr) - return state.current_floor + state.current_level = new_level + M.setup_view_buffer() + return state.current_level end end ---comment M.handle_moved = function() message.debug("handle_moved") - if state.player.health:is_dead() then - M.end_game() - end M.tick() end @@ -114,12 +121,12 @@ M.handle_insert = function() if state.player.health:is_dead() then M.end_game() end - edits.handle_insert() + api.edits.handle_insert() end ---comment M.handle_insert_enter = function() - edits.handle_insert_enter() + api.edits.handle_insert_enter() end ---TODO: deprecated cursor moved also handles changed @@ -130,82 +137,99 @@ M.handle_changed = function() -- M.end_game() -- end -- message.notify("changed") - -- if not event.in_sneak_move() then - -- edits.handle_changed() + -- if not api.keymaps.in_sneak_mode() then + -- api.edits.handle_changed() -- end end ---comment M.handle_yanked = function() message.debug("handle_yanked") - edits.handle_yanked() + api.edits.handle_yanked() end ---comment M.end_game = function() - vim.schedule(function() - for bufnr, _ in pairs(buffer.buffers) do - local _, _, first_floor = map.scan_buf(bufnr) - view_buffer.setup_buffer(bufnr, first_floor) - map.all_visible(bufnr) - end - buffer.end_game() - end) + -- TODO: is this schedule needed? + -- api.async.later(function() + for level, _ in pairs(buffer.buffers) do + map.all_visible(level) + -- TODO: do we need to write to the other buffers? + -- buffer.write_buf() + api.render.unsetup_view() + end + -- just write the current buffer + buffer.write_buf() + -- end) + state.running = false end ----comment +--- game tick M.tick = function() - -- vim.api.nvim_buf_add_highlight(M.bufnr, vim.api.nvim_create_namespace("NeoHack"), "NeoHackHit", 0, 0, 10) - buffer.tick(function() - --TODO: nvim crashes on long waits or after too many turns - state.turn_counter = state.turn_counter + 1 - message.notify("turn", state.turn_counter) + if state.animating then + message.debug("animating so skip tick") + return + end + M.turn_logic() + buffer.write_buf() + buffer.animations() +end + +---@param game_time_ms number game timer in ms +---@return boolean if any updates were made +M.realtime_tick = function(game_time_ms) + return event.move_entities(game_time_ms) +end + +M.turn_logic = function() + --TODO: nvim crashes on long waits or after too many turns + state.turn_counter = state.turn_counter + 1 + message.debug("turn", state.turn_counter) + + if state.player.health:is_dead() then + M.end_game() + end - state.player:set_position_from_cursor() + api.edits.handle_insert_exit() -- do this before setting player pos + state.player:set_position_from_cursor() - -- map.generate_new_map_sections(state.current_bufnr, state.player.attributes.view_distance) - map.show_visible_line_of_sight(state.current_bufnr, state.player.vision:view_distance()) + map.show_visible_line_of_sight(state.current_level, state.player.vision:view_distance()) - -- detect deleted on every turn - -- state.deleted = map.find_deleted(state.current_bufnr) + -- detect deleted on every turn + -- state.deleted = map.find_deleted() + + -- cursor moved also triggered on change + if not api.keymaps.in_sneak_mode() then + api.edits.handle_changed() + end - -- cursor moved also triggered on change - if not event.in_sneak_move() then - edits.handle_changed() - end + tick_timer.on_tick() - tick_timer.on_tick() + event.player_move() - edits.handle_insert_exit() + if not state.realtime then + M.realtime_tick(api.async.get_time()) + end - event.player_move() + state.player:set_cursor_from_position() - event.move_entities() + if state.player.health:is_dead() then + M.end_game() + end - state.player:set_cursor_from_position() + state.player:handle_bounce() - if state.player.health:is_dead() then - M.end_game() - end - end) + api.keymaps.dynamic_keymaps() end M.status_line = function() - local line, col = unpack(vim.api.nvim_win_get_cursor(0)) - col = col + 1 - - local mode = vim.fn.mode() - local game_mode = { - n = "walking", - i = "action", - v = "sneaking", - V = "sneaking", - ["\22"] = "sneaking", -- CTRL-V - } + local line, col = api.cursor.get() + + local mode = api.keymaps.get_mode() return string.format( " %s | floor %s | turn %s | health %s | position %d,%d ", - game_mode[mode] or mode, - state.current_floor, + mode, + state.current_level, state.turn_counter, state.player.attributes.health, line, diff --git a/lua/neohack/generated_defs.lua b/lua/neohack/generated_defs.lua index a2c55e3..fe79e0d 100644 --- a/lua/neohack/generated_defs.lua +++ b/lua/neohack/generated_defs.lua @@ -1,3 +1,4 @@ +local constant_defs = require("neohack.constant_defs") local M = { ---@type table enemy_list = nil, @@ -13,6 +14,7 @@ local spells = require("neohack.spells") local utils = require("neohack.utils") local message = require("neohack.message") local tick_timer = require("neohack.tick_timer") +local api = require("neohack.api") M.init = function() M.enemy_list = M.read_dictionary("data/enemies.txt") @@ -22,6 +24,14 @@ end M.read_dictionary = function(file_path) local words = {} -- Read the file and categorize words by their starting letter + if not utils.file_exists(file_path) then + error("file not found: " .. file_path) + for i = 1, #constant_defs.letters do + local letter = constant_defs.letters:sub(i, i) + words[letter] = letter + end + return words + end for line in io.lines(file_path) do local first_letter = line:sub(1, 1):lower() if not words[first_letter] then @@ -43,9 +53,8 @@ M.random_word_starting_with = function(words, letter) end M.random_letter = function() - local letters = "abcdefghijklmnopqrstuvwxyz" - local index = math.random(1, #letters) - return letters:sub(index, index) + local index = math.random(1, #constant_defs.letters_lower) + return constant_defs.letters_lower:sub(index, index) end -- Helper function to allocate portions of total points to each stat @@ -58,8 +67,9 @@ M.allocate_portions = function(total_points) local hit_rate = math.random() local randomness = math.random() local vision = math.random() + local speed = math.random() - local total = health + damage + durability + hit_rate + randomness + vision + local total = health + damage + durability + hit_rate + randomness + vision + speed portions.health = (health / total) * total_points portions.damage = (damage / total) * total_points @@ -67,6 +77,7 @@ M.allocate_portions = function(total_points) portions.hit_rate = (hit_rate / total) * total_points portions.randomness = (randomness / total) * total_points portions.vision = (vision / total) * total_points + portions.speed = (speed / total) * total_points return portions end @@ -79,10 +90,14 @@ local function calculate_stat_float(base, level, multiplier) return math.max(0.01, math.min(1.0, base + (level * multiplier))) end +local function calculate_stat_float_max10(base, level, multiplier) + return math.max(0.1, math.min(10.0, base + (level * multiplier))) +end + M.generate_item = function() local char = M.random_letter() local name = M.random_word_starting_with(M.item_list, char) - local total_points = state.current_floor * math.random() * 10 + local total_points = state.current_level * math.random() * 10 -- local total_points = 10 * math.random() * 10 local portions = M.allocate_portions(total_points) @@ -92,6 +107,7 @@ M.generate_item = function() local hit_rate = calculate_stat_float(0.1, portions.hit_rate, 0.05) local randomness = calculate_stat_float(0.2, portions.randomness, 0.5) local vision = calculate_stat_float(0.2, portions.vision, 0.1) + local speed = calculate_stat_float_max10(0.1, portions.speed, 10) local block_vision = math.random() > 0.9 local view_distance = math.random(2, 10) local spell = nil @@ -107,6 +123,7 @@ M.generate_item = function() randomness = randomness, block_vision = block_vision, vision = vision, + speed = speed, spell = spell, -- TODO: random moveset moves = moves.eight, @@ -137,7 +154,7 @@ end M.generate_enemy = function() local char = M.random_letter() local name = M.random_word_starting_with(M.enemy_list, char) - local total_points = state.current_floor * math.random() * 2 + local total_points = state.current_level * math.random() * 2 -- local total_points = 10 * math.random() * 2 local portions = M.allocate_portions(total_points) @@ -147,6 +164,7 @@ M.generate_enemy = function() local hit_rate = calculate_stat_float(0.1, portions.hit_rate, 0.05) local randomness = calculate_stat_float(0.2, portions.randomness, 0.5) local vision = calculate_stat_float(0.2, portions.vision, 0.1) + local speed = calculate_stat_float_max10(0.1, portions.speed, 10) local view_distance = math.random(3, 15) local block_vision = math.random() > 0.5 @@ -161,6 +179,7 @@ M.generate_enemy = function() randomness = randomness, block_vision = block_vision, vision = vision, + speed = speed, view_distance = view_distance, -- TODO: random moveset moves = moves.eight, diff --git a/lua/neohack/highlight.lua b/lua/neohack/highlight.lua index c1c4410..a9e93e1 100644 --- a/lua/neohack/highlight.lua +++ b/lua/neohack/highlight.lua @@ -1,30 +1,30 @@ local Def = require("neohack.def") -local constant_defs = require("neohack.constant_defs") -local message = require("neohack.message") +local api = require("neohack.api") +local utils = require("neohack.utils") -local M = {} +---@class RGB color +---@field red number +---@field green number +---@field blue number +---@field alpha number|nil + +local M = { + ---@type Highlight + missing_highlight = nil, + ---@type Highlight + hitting_highlight = nil, +} ---comment ---@param instance Entity ----@return string +---@return Highlight|nil M.create_entity_highlight = function(instance) - local colour = M.generate_colour(instance) - local highlight_group = instance.name:gsub("[^%w]", "_") .. colour:gsub("#", "_") - -- message.debug("highlight_group", highlight_group, colour) - --TODO: do we really want totally unique highlight group for every entity? - --TODO: memoize - vim.api.nvim_command("highlight " .. highlight_group .. " guifg=" .. colour) - return highlight_group -end - ----comment ----@param name string ----@param colour string ----@return string -M.create_highlight_background = function(name, colour) - -- message.debug("highlight_group", name, colour) - vim.api.nvim_command("highlight " .. name .. " guibg=" .. colour) - return name + local rgb = M.generate_rgb(instance) + if rgb then + return api.render.create_color(instance.name, rgb) + else + return nil + end end --- Convert a number to a value between 0 and 255 for color calculation @@ -34,13 +34,13 @@ local function normalize(num, max_value) if not num then return 0 end - return math.floor((num / max_value) * 255) + return math.max(0, math.floor((num / max_value) * 255)) end --- Generate a color based on the values of a Def --- @param entity Entity ---- @return string -M.generate_colour = function(entity) +--- @return table|nil RGB values +M.generate_rgb = function(entity) -- add type offsets local redOffset = 0 local greenOffset = 0 @@ -54,9 +54,10 @@ M.generate_colour = function(entity) blueOffset = 20 greenOffset = -20 elseif entity.type == Def.DefType.terrain then - blueOffset = 80 - redOffset = 40 - greenOffset = 30 + return nil + -- blueOffset = 80 + -- redOffset = 40 + -- greenOffset = 30 end -- Normalize attributes to create RGB values @@ -76,18 +77,28 @@ M.generate_colour = function(entity) ) -- Calculate RGB values - local red = math.min(255, redAmount) - local green = math.min(255, greenAmount) - local blue = math.min(255, blueAmount) + local red = utils.bounded(redAmount, 0, 255) + local green = utils.bounded(greenAmount, 0, 255) + local blue = utils.bounded(blueAmount, 0, 255) - -- Convert RGB to hex - local color = string.format("#%02X%02X%02X", red, green, blue) - return color + return { + red = red, + green = green, + blue = blue, + } end M.setup_highlights = function() - M.create_highlight_background(constant_defs.hitting_highlight, "#8f1122") -- red bg - M.create_highlight_background(constant_defs.missing_highlight, "#8f6311") -- orange bg + M.hitting_highlight = api.render.create_background_color("NeoHack_hitting", { + red = 143, + green = 17, + blue = 34, + }) -- red bg + M.missing_highlight = api.render.create_background_color("NeoHack_missing", { + red = 143, + green = 99, + blue = 17, + }) -- orange bg end return M diff --git a/lua/neohack/init.lua b/lua/neohack/init.lua index 27e85f0..f28795b 100644 --- a/lua/neohack/init.lua +++ b/lua/neohack/init.lua @@ -1,22 +1,51 @@ --- neohack.nvim ---- +--- neovim main entry point +--- equivalent to lua/main.lua for love2d local M = {} local game = require("neohack.game") -local message = require("neohack.message") +local vim_message = require("neohack.vim.vim_message") local actions = require("neohack.actions") -local state = require("neohack.state") local highlight = require("neohack.highlight") +local render = require("neohack.vim.render") +local vim_cursor = require("neohack.vim.cursor") +local vim_render = require("neohack.vim.render") +local vim_keymaps = require("neohack.vim.keymaps") +local vim_async = require("neohack.vim.async") +local vim_edits = require("neohack.vim.edits") +local api = require("neohack.api") +local state = require("neohack.state") +local buffer = require("neohack.buffer") +local map = require("neohack.map") ---comment M.start_game = function() game.start_game() + + -- hide initial buffer state + map.show_visible_line_of_sight(state.current_level, state.player.vision:view_distance()) + buffer.write_buf() + + if state.realtime then + vim_async.loop(function() + local this_turn_ms = api.async.get_time() + local update = game.realtime_tick(this_turn_ms) + + --- TODO: try this + if update or state.update then + buffer.write_buf() + buffer.animations() + state.update = false + end + end, state.tick_ms) + end end ---comment M.end_game = function() game.end_game() + vim_async.loop_stop() end --- setup the plugin @@ -29,16 +58,25 @@ M.setup = function(opts) --- TODO: separate map generate seed from the rest math.randomseed(12345) - -- state.debug = true -- state.animations_enabled = false -- local leader could be an idea actions.leader_key = "" - -- message.notify_func = print - message.notify_func = message.send_to_message_buf - -- message.notify_func = function(s) - -- vim.notify(s, vim.log.levels.INFO) - -- end + + api.notify = vim_message.send_to_message_buf + api.debug = vim_message.send_to_message_buf + api.cursor = vim_cursor + api.render = vim_render + api.keymaps = vim_keymaps + api.async = vim_async + api.edits = vim_edits + + state.debug = false + state.trace = false + + state.rows = 30 + state.cols = 160 + game.generations = 10000 vim.api.nvim_create_user_command( "StartGame", @@ -49,6 +87,7 @@ M.setup = function(opts) vim.api.nvim_create_user_command("EndGame", M.end_game, { nargs = 0, desc = "End the current NeoHack game" }) highlight.setup_highlights() + game.open_ui = render.open_ui end M.status_line = function() diff --git a/lua/neohack/love2d/async.lua b/lua/neohack/love2d/async.lua new file mode 100644 index 0000000..ad5ea31 --- /dev/null +++ b/lua/neohack/love2d/async.lua @@ -0,0 +1,41 @@ +local api = require("neohack.api") + +---@class DelayedFunc +---@field func fun() function to be executed +---@field execTime float The time when the function should be executed in seconds (float though) + +local M = { + ---@type DelayedFunc[] + delayedFuncs = {}, +} + +---@return integer time in milliseconds +M.get_time = function() + return love.timer.getTime() * 1000 +end + +M.later = function(callback) + M.defer(callback, 200) +end + +M.defer = function(callback, timeout_ms) + table.insert(M.delayedFuncs, { + func = callback, + execTime = love.timer.getTime() + (timeout_ms / 1000), + }) +end + +M.on_update = function() + local currentTime = love.timer.getTime() + for i = #M.delayedFuncs, 1, -1 do + local delayed = M.delayedFuncs[i] + -- api.debug("async", currentTime, delayed.execTime) + if currentTime >= delayed.execTime then + -- api.debug("async >=", currentTime, delayed.execTime) + delayed.func() + table.remove(M.delayedFuncs, i) + end + end +end + +return M diff --git a/lua/neohack/love2d/cursor.lua b/lua/neohack/love2d/cursor.lua new file mode 100644 index 0000000..5d22886 --- /dev/null +++ b/lua/neohack/love2d/cursor.lua @@ -0,0 +1,105 @@ +local utils = require("neohack.utils") +local state = require("neohack.state") +local love_render = require("neohack.love2d.render") +local api = require("neohack.api") + +---@class Pos +---@field row integer +---@field col integer + +local M = { + ---@type Pos + pos = { row = 1, col = 1 }, + + ---@type string + mode = "walking", + + ---@type Highlight + highlight = nil, + ---@type Highlight + default_highlight = nil, +} + +M.init = function() + M.default_highlight = api.render.create_background_color("cursor", { + red = 155, + green = 155, + blue = 155, + alpha = 125, + }) + M.sneak_cursor = api.render.create_background_color("cursor", { + red = 0, + green = 50, + blue = 255, + alpha = 125, + }) + M.aim_cursor = api.render.create_background_color("cursor", { + red = 155, + green = 0, + blue = 0, + alpha = 125, + }) + M.other_cursor = api.render.create_background_color("cursor", { + red = 0, + green = 155, + blue = 0, + alpha = 125, + }) +end + +M.get = function() + return M.pos.row, M.pos.col +end + +M.set = function(row, col, mode) + -- TODO: initial render doesn't have colors + -- TODO: on next floor, pos isn't set correctly + M.pos = { row = row, col = col } +end + +M.set_mode = function(mode) + M.mode = mode +end + +--- just show the cursor for animation purposes +M.animate = function(row, col, oldRow, oldCol) + love_render.highlight_pos(oldRow, oldCol, nil) + love_render.highlight_pos(row, col, M.highlight or M.default_highlight) +end + +M.write_cursor = function() + local hl = M.highlight -- default walking + if M.mode == "sneaking" then + hl = M.sneak_cursor + elseif M.mode == "aiming" then + hl = M.aim_cursor + elseif M.mode == "other" then + hl = M.other_cursor + end + love_render.highlight_pos(M.pos.row, M.pos.col, hl or M.default_highlight) +end + +--- Move the cursor by a specified number of rows or columns +---@param row_delta integer +---@param col_delta integer +M.move = function(row_delta, col_delta) + M.pos = { + row = utils.bounded(M.pos.row + row_delta, 0, state.rows), + col = utils.bounded(M.pos.col + col_delta, 0, state.cols), + } +end + +M.highlight_cursor = function(highlight) + M.highlight = highlight + + api.async.later(function() + M.reset_highlight() + end) +end + +M.reset_highlight = function() + M.highlight = nil +end + +---@type Cursor +return M diff --git a/lua/neohack/love2d/edits.lua b/lua/neohack/love2d/edits.lua new file mode 100644 index 0000000..deed2b0 --- /dev/null +++ b/lua/neohack/love2d/edits.lua @@ -0,0 +1,9 @@ +local M = {} + +M.handle_insert_enter = function() end +M.handle_insert = function() end +M.handle_insert_exit = function() end +M.handle_changed = function() end +M.handle_yanked = function() end + +return M diff --git a/lua/neohack/love2d/keymaps.lua b/lua/neohack/love2d/keymaps.lua new file mode 100644 index 0000000..ee7508c --- /dev/null +++ b/lua/neohack/love2d/keymaps.lua @@ -0,0 +1,389 @@ +local message = require("neohack.message") +local state = require("neohack.state") +local actions = require("neohack.actions") +local vikeys = require("neohack.love2d.vikeys") +local love_render = require("neohack.love2d.render") +local utils = require("neohack.utils") + +---@class Key either an action or a submenu +---@field name string to show in menu +---@field action nil|fun(string?):boolean +---@field menu nil|fun():table +---@field prompt nil|string if following keys are the prompt input +---@field pending nil|integer number of follow up keys needed for processing + +local M = { + ---@type string[] + pending_keys = {}, +} + +---comment +---@return boolean +M.in_sneak_mode = function() + return vikeys.get_mode() == "sneaking" +end + +M.in_aim_mode = function() + return vikeys.get_mode() == "aiming" +end + +M.in_other_mode = function() + return vikeys.get_mode() == "other" +end + +---@return string +M.get_mode = function() + return vikeys.get_mode() +end + +M.esc = function() + message.debug("mode " .. vikeys.get_mode()) + if #M.pending_keys > 1 then + M.exit_menu() + else + vikeys.set_walking() + end + return false +end + +local function single(action) + return function(object) + return function() + action({ object = object }) + return true + end + end +end + +local function double(action) + return function(object) + return function(target) + return function() + action({ object = object, target = target }) + return true + end + end + end +end + +---@param action function +---@return function():table +M.direction_menu = function(action) + return function() + return { + h = { name = "left", action = action("h") }, + j = { name = "down", action = action("j") }, + k = { name = "up", action = action("k") }, + l = { name = "right", action = action("l") }, + } + end +end + +---@param next fun(key:string):fun():boolean +---@param with_self boolean include 0-self option +---@param without nil|integer exclude an inventory index +---@return function():table +M.show_inventory = function(next, with_self, without) + return function() + local menu = {} + for index, item in ipairs(state.player.inventory.items) do + if without == nil or without ~= index then + menu[tostring(index)] = { name = item.name, action = next(tostring(index)) } + end + end + if with_self then + menu["0"] = { name = "self", action = next("0") } + end + return menu + end +end + +---@param next fun(key:string):fun():boolean +---@return function():table +M.two_inventory = function(next) + return function() + local menu = {} + for index, item in ipairs(state.player.inventory.items) do + menu[tostring(index)] = { name = item.name, menu = M.show_inventory(next(tostring(index)), false, index) } + end + return menu + end +end + +---@return function():table +M.wear = function() + local submenu = function(object) + return function() + local keys = {} + for slot_name, slot_key in pairs(state.slot_types) do + keys[slot_key] = { name = slot_name, action = double(actions.actions.wear)(object)(slot_key) } + end + return keys + end + end + + return function() + local menu = {} + for index, item in ipairs(state.player.inventory.items) do + menu[tostring(index)] = { name = item.name, menu = submenu(tostring(index)) } + end + return menu + end +end + +---@return function():table +M.rename = function() + return function() + local menu = {} + for index, item in ipairs(state.player.inventory.items) do + menu[tostring(index)] = { name = item.name, prompt = "new name?", action = M.prompt("rename", index) } + end + return menu + end +end + +M.go_menu = function() + return { + g = { name = "first line", action = vikeys.gg }, + } +end + +M.help = function() + actions.actions.help() + M.notify_keys() +end + +M.notify_keys = function() + message.notify(M.render_menu(M.keys)) +end + +M.main_menu = function() + return { + escape = { name = "esc", action = M.esc }, + ["?"] = { name = "help", action = M.help }, + i = { name = "inv", action = actions.actions.inventory }, + + k = { name = "kick", menu = M.direction_menu(single(actions.actions.kick)) }, + P = { name = "pick", menu = M.direction_menu(single(actions.actions.pick)) }, + + l = { name = "look", menu = M.show_inventory(single(actions.actions.look), true) }, + p = { name = "pilfer", menu = M.show_inventory(single(actions.actions.pilfer), false) }, + e = { name = "eat", menu = M.show_inventory(single(actions.actions.eat), false) }, + d = { name = "drop", menu = M.show_inventory(single(actions.actions.drop), false) }, + + w = { name = "wear", menu = M.wear() }, + f = { name = "fuse", menu = M.two_inventory(double(actions.actions.fuse)) }, + + s = { name = "say", prompt = "say what?", action = M.prompt("say") }, + r = { name = "rest", action = actions.actions.rest }, + q = { name = "quit", action = M.quit }, + R = { name = "rename", menu = M.rename() }, + } +end + +---@type table +M.keys = { + h = { name = "left", action = vikeys.h }, + j = { name = "down", action = vikeys.j }, + k = { name = "up", action = vikeys.k }, + l = { name = "right", action = vikeys.l }, + x = { name = "pickup", action = vikeys.x }, + w = { name = "word", action = vikeys.w }, + b = { name = "back", action = vikeys.b }, + v = { name = "sneak", action = vikeys.v }, + V = { name = "sneak", action = vikeys.V }, + ["C-v"] = { name = "aim", action = vikeys.C_v }, + f = { name = "find", action = vikeys.f, pending = 1 }, + F = { name = "Find prev", action = vikeys.F, pending = 1 }, + t = { name = "til", action = vikeys.t, pending = 1 }, + T = { name = "til prev", action = vikeys.T, pending = 1 }, + y = { name = "yank", action = vikeys.y }, + ["/"] = { name = "search", prompt = "search for what?", action = vikeys.search }, + ["?"] = { name = "search back", prompt = "search backwards for what?", action = vikeys.back_search }, + g = { name = "go", menu = M.go_menu }, + G = { name = "go last line", action = vikeys.G }, + escape = { name = "esc", action = M.esc }, + [" "] = { name = "actions", menu = M.main_menu }, + [":"] = { + name = "command", + menu = function() + return { q = { name = "quit", action = M.quit } } + end, + }, +} + +---@param menu table +---@return string +M.render_menu = function(menu) + local result = "" + for k, v in pairs(menu) do + if k == " " then + k = "space" + end + result = result .. k .. "-" .. v.name .. " " + end + return result +end + +--- find the deepest key +---@return Key|nil the key to handle +---@return integer|nil the offset of pending keys to process as an argument +M._find_key = function() + local keys = M.keys + local key = nil + for index, pressed in ipairs(M.pending_keys) do + key = keys[pressed] + if key then + -- step into inner menu + if key.pending then + return key, index + elseif key.prompt then + -- leave other pending_keys for the prompt + return key, index + elseif key.menu then + keys = key.menu() + else + if index ~= #M.pending_keys then + message.debug( + "missing menu", + "key", + pressed, + "index", + index, + "pending_keys", + utils.concatenate_strings(M.pending_keys) + ) + end + -- break -- no submenus so done + end + else + message.debug("other key '" .. pressed .. "'") + return nil, nil + end + end + return key, nil +end + +--- Handle keys pressed +--- For each key, work through the set of menus and execute the last action +--- So each action was executed only when the key was pressed +---@return boolean if action should trigger an update +M.handle_keys = function() + local key, following_keys = M._find_key() + if not key then + M.exit_menu() + return false + end + + if key.pending then + local pending = table.concat(table.slice(M.pending_keys, following_keys + 1, #M.pending_keys), "") + if key.pending == pending:len() then + message.debug("pending '" .. pending .. "'") + M.exit_menu() + return key.action(pending) + else + message.debug("waiting for pending key") + return false + end + elseif key.prompt then + if M.pending_keys[#M.pending_keys] == "escape" then + M.exit_menu() + return false + elseif M.pending_keys[#M.pending_keys] == "return" then + table.remove(M.pending_keys, #M.pending_keys) + local prompt_keys = table.concat(table.slice(M.pending_keys, following_keys + 1, #M.pending_keys), "") + message.debug("prompt_keys '" .. prompt_keys .. "'") + M.exit_menu() + return key.action(prompt_keys) + else + local prompt_keys = table.concat(table.slice(M.pending_keys, following_keys + 1, #M.pending_keys), "") + M.show_menu_message({ key.prompt, prompt_keys }) + end + return false + end + + -- execute action or show menu for last key + if key.menu then + local menu = key.menu() + menu["backspace"] = { name = "back" } -- always allow backspace to go back up the menu + local result = M.render_menu(menu) + M.show_menu_message({ result }) + end + -- allow both action and menu + if key.action then + local update = key.action() + if key.menu == nil then + M.exit_menu() + end + return update + end + return false +end + +--- For special keys +---@param key any +---@return boolean if key should trigger an update +---@return boolean if paused +M.keypressed = function(key) + if key == "escape" then + return M.textinput(key) + elseif key == "return" then + return M.textinput(key) + elseif key == "backspace" then + table.remove(M.pending_keys, #M.pending_keys) + return M.handle_keys(), M.is_paused() + elseif love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") then + return M.textinput("C-" .. key) + end +end + +---comment +---@param key any +---@return boolean if key should trigger an update +---@return boolean if paused +M.textinput = function(key) + table.insert(M.pending_keys, key) + message.debug("handle_menu", "key '" .. key .. "'", "pending_keys", utils.to_string(M.pending_keys)) + return M.handle_keys(), M.is_paused() +end + +M.is_paused = function() + return #M.pending_keys >= 1 and M.pending_keys[1] == " " +end + +---@param menu string[] +M.show_menu_message = function(menu) + love_render.set_menu(menu) +end + +M.exit_menu = function() + M.pending_keys = {} + love_render.set_menu({}) +end + +---@param action_name string +---@param index nil|integer +M.prompt = function(action_name, index) + return function(prompt_keys) + local action, request = nil, nil + if index then + action, request = actions.parse_action(action_name .. " " .. tostring(index) .. " " .. prompt_keys) + else + action, request = actions.parse_action(action_name .. " " .. prompt_keys) + end + if action then + action(request) + end + return false + end +end + +M.dynamic_keymaps = function() + -- nothing to setup for love2d +end + +M.quit = function() + love.event.quit() +end + +return M diff --git a/lua/neohack/love2d/render.lua b/lua/neohack/love2d/render.lua new file mode 100644 index 0000000..fed07a4 --- /dev/null +++ b/lua/neohack/love2d/render.lua @@ -0,0 +1,264 @@ +local utils = require("neohack.utils") +local ROT = require("lib.rotLove.rot") + +local M = { + ---@type Display + f = nil, + ---@type integer + cols = 0, -- x / width + ---@type integer + rows = 0, -- y / height + ---@type integer + messageRows = 0, + ---@type table> row,col to Highlight + highlights = {}, + ---@type string[] + messages = {}, + --@type string[] + menu = {}, +} + +M.scale = 1 +M.charWidth = 9 * M.scale +M.charHeight = 16 * M.scale +M.messageX = 10 + +M.derive_grid = function() + love.window.setFullscreen(true) + local resX, resY = love.graphics.getDimensions() + local rows = math.floor((resY / M.charHeight) - M.messageX - 1) --56 + local cols = math.floor(resX / M.charWidth) --213 + return rows, cols, M.messageX, M.scale +end + +local default_fg = { 0.9, 0.9, 0.9 } +local default_bg = { 0.1, 0.1, 0.1 } +local invert_fg = default_bg +local invert_bg = default_fg + +M.init = function(rows, cols, messageRows, scale) + -- M.f = ROT.Display(cols, rows + messageRows + 1) + -- for 1080p 1.1 + local vsync = 1 + local fsaa = 0 + local mode = { + fullscreen = true, + vsync = 1, + msaa = fsaa, + } + M.f = ROT.Display(cols, rows + messageRows + 1, scale, default_fg, default_bg, mode, vsync, fsaa) + + M.cols = cols + M.rows = rows + M.messageRows = messageRows +end + +---@param rgb RGB +---@return float[]|nil +M.to_love_color = function(rgb) + if rgb == nil then + return nil + end + return { + rgb.red / 255, + rgb.green / 255, + rgb.blue / 255, + (rgb.alpha and (rgb.alpha / 255)) or 1, + } +end + +--- write the full set of lines to the buffer +---@param lines string[] +M.write_buf = function(lines) + for row_index, row in ipairs(lines) do + for col_index = 1, #row do + local char = string.sub(row, col_index, col_index) + -- TODO: setting highlights is slow + -- M.f:write(char, col_index, row_index, M.get_fg(row_index, col_index), M.get_bg(row_index, col_index)) + -- TODO: skip validation, but background is not correct + M.f:_writeValidatedString( + char, + col_index, + row_index, + M.get_fg(row_index, col_index), + -- default_bg + M.get_bg(row_index, col_index) + ) + -- M.f:write(char, col_index, row_index) + end + end + + -- TODO: is this actually needed? it stops the cursor from showing the fg color + -- M.highlights = {} -- clear color for next frame +end + +M.write_messages = function() + if M.menu ~= nil and #M.menu > 0 then + return + end + M.f:clear(" ", 1, M.rows + 1, M.cols, M.messageRows) + for row, msg in ipairs(M.messages) do + -- TODO: wrap? + -- M.f:drawText(1, M.rows + 1 + row, utils.concatenate_strings(msg), M.cols) + local write_row = M.rows + row + M.f:drawText(1, write_row, msg) + end +end + +--- show a menu, overwriting notify messes temporarily +M.write_menu = function() + -- menu overrides messages and status line + if M.menu == nil or #M.menu == 0 then + return + end + M.f:clear(" ", 1, M.rows + 1, M.cols, M.messageRows + 1) + for row, msg in ipairs(M.menu) do + -- TODO: wrap? + -- M.f:drawText(1, M.rows + 1 + row, utils.concatenate_strings(msg), M.cols) + local write_row = M.rows + row + M.f:drawText(1, write_row, msg) + end + + love.graphics.setLineWidth(1) + local width, height = love.graphics.getDimensions() + local top_of_messages = M.rows * M.charHeight + love.graphics.line( + 0, + top_of_messages, + width - 1, -- why does this look better than width-2 + top_of_messages, + width - 2, + height - 1, + 0, + height - 1, + 0, + top_of_messages + ) +end + +M.write_status_line = function(status) + if M.menu ~= nil and #M.menu > 0 then + return + end + status = status .. string.rep(" ", M.cols - #status) + M.f:write(status, 1, M.rows + M.messageRows + 1, invert_fg, invert_bg) +end + +M.draw_ui = function() + love.graphics.setLineWidth(1) + local width, _ = love.graphics.getDimensions() + local top_of_messages = M.rows * M.charHeight + love.graphics.line(0, top_of_messages, width, top_of_messages) +end + +--- Write a single character to the buffer at the specified position +---@param row integer +---@param col integer +---@param char string a single char +M.write_pos = function(row, col, char) + M.f:write(char, col, row) +end + +--- set the bg highlight at pos, or clear with nil +---@param row integer +---@param col integer +---@param highlight Highlight|nil +M.highlight_pos = function(row, col, highlight) + local bg = M.get_bg(row, col) + if highlight then + bg = highlight.bg + end + if row < 0 or row > M.rows or col < 0 or col > M.cols then + return + end + M.f:write(M.f:getCharacter(col, row), col, row, M.get_fg(row, col), bg) + -- only set the bg for a cell + -- M.f.backgroundColors[col][row] = bg +end + +M.new_view = function(level) end + +--- send a message to a user +---@param ... any +M.notify = function(...) + for _, value in ipairs({ ... }) do + table.insert(M.messages, #M.messages + 1, value) + if #M.messages > M.messageRows then + table.remove(M.messages, 1) + end + end +end + +---@param ... any +M.debug = function(...) + for _, value in ipairs({ ... }) do + print(value) + end +end + +M.set_menu = function(menu) + M.menu = menu + while #M.menu > M.messageRows + 1 do + table.remove(M.menu, 1) + end +end + +M.setup_view = function(handlers) end + +M.unsetup_view = function() end + +--- Create a color +--- @param name string name of color +--- @param rgb RGB +--- @return Highlight +M.create_color = function(name, rgb) + return { + fg = M.to_love_color(rgb), + } +end + +--- Create a background color +--- @param name string name to base the color on +--- @param rgb RGB +--- @return Highlight +M.create_background_color = function(name, rgb) + return { + bg = M.to_love_color(rgb), + } +end + +--- Set highlight for a specific position +--- @param row integer +--- @param col integer +--- @param highlight Highlight +M.set_highlight = function(row, col, highlight) + -- M.f:setDefaultForegroundColor(rgb) + if not M.highlights[row] then + M.highlights[row] = {} + end + M.highlights[row][col] = highlight +end + +--- get the fg highlight at pos +---@param row integer +---@param col integer +---@return float[]|nil +M.get_fg = function(row, col) + if M.highlights[row] and M.highlights[row][col] then + return M.highlights[row][col].fg or default_fg + end + return default_fg +end + +--- get the bg highlight at pos +---@param row integer +---@param col integer +---@return float[]|nil +M.get_bg = function(row, col) + if M.highlights[row] and M.highlights[row][col] then + return M.highlights[row][col].bg or default_bg + end + return default_bg +end + +return M diff --git a/lua/neohack/love2d/vikeys.lua b/lua/neohack/love2d/vikeys.lua new file mode 100644 index 0000000..30a767c --- /dev/null +++ b/lua/neohack/love2d/vikeys.lua @@ -0,0 +1,292 @@ +local buffer = require("neohack.buffer") +local state = require("neohack.state") +local love_cursor = require("neohack.love2d.cursor") +local message = require("neohack.message") +local api = require("neohack.api") +local Def = require("neohack.def") +local chance = require("neohack.chance") + +local M = { + + ---@type string + mode = "walking", +} + +local function get_lines() + return buffer.cells_to_render(buffer.buffers[state.current_level].cells) +end + +---@return string +M.get_mode = function() + return M.mode +end + +M.h = function() + love_cursor.move(0, -1) + return true +end +M.j = function() + love_cursor.move(1, 0) + return true +end +M.k = function() + love_cursor.move(-1, 0) + return true +end +M.l = function() + love_cursor.move(0, 1) + return true +end + +M.x = function() + local row, col = love_cursor.get() + local entity = buffer.get_entity_at_pos(row, col, true) + message.debug("try pickup", entity.name) + if state.player.inventory:can_pickup(entity) then + state.player.inventory:pickup_off_floor(entity) + end + return true +end + +M.w = function() + local row, col = api.cursor.get() + local new_row, new_col = M.word(row, col) + if new_row and new_col then + api.cursor.set(new_row, new_col) + end + return true +end +M.b = function() + local row, col = api.cursor.get() + local new_row, new_col = M.back_word(row, col) + if new_row and new_col then + api.cursor.set(new_row, new_col) + end + return true +end + +M.f = function(char) + local cursor_row, cursor_col = api.cursor.get() + local lines = get_lines() + local line = lines[cursor_row] + for col_index = cursor_col + 1, #line do + local c = string.sub(line, col_index, col_index) + if c == char then + love_cursor.set(cursor_row, col_index) + end + end + return true +end +M.F = function(char) + local cursor_row, cursor_col = api.cursor.get() + local lines = get_lines() + local line = lines[cursor_row] + for col_index = cursor_col - 1, 1, -1 do + local c = string.sub(line, col_index, col_index) + if c == char then + love_cursor.set(cursor_row, col_index) + end + end + return true +end + +M.t = function(char) + local cursor_row, cursor_col = api.cursor.get() + local lines = get_lines() + local line = lines[cursor_row] + for col_index = cursor_col, #line do + local c = string.sub(line, col_index + 1, col_index + 1) + if c == char then + love_cursor.set(cursor_row, col_index) + end + end + return true +end +M.T = function(char) + local cursor_row, cursor_col = api.cursor.get() + local lines = get_lines() + local line = lines[cursor_row] + for col_index = cursor_col, 1, -1 do + local c = string.sub(line, col_index - 1, col_index - 1) + if c == char then + love_cursor.set(cursor_row, col_index) + end + end + return true +end + +M.y = function() + if M.mode ~= "sneaking" then + message.notify("You can only search while sneaking") + return + end + local cursor_row, cursor_col = api.cursor.get() + local entity = buffer.get_entity_at_pos(cursor_row, cursor_col, true) + + if entity ~= nil and entity.type ~= Def.DefType.floor then + local search_success = + chance.action_success(state.player.vision:search_skill(), 0.3, 0.1, state.player.vision.corpse_threshold) + if entity.movement.visible or search_success then + local entry = entity.name + if #entity.inventory:get_wearing() > 0 then + entry = entry .. " (" .. entity.inventory:get_wearing() .. ")" + end + message.notify("Found " .. entry) + else + message.notify("Found unkown") + end + else + message.notify("Found nothing") + end +end + +M.search = function(pattern) + message.debug("searching for '" .. pattern .. "'") + local cursor_row, cursor_col = api.cursor.get() + local new_row, new_col = M.find_pattern(cursor_row, cursor_col, pattern) + if new_row and new_col then + api.cursor.set(new_row, new_col) + end + return true +end + +M.back_search = function(pattern) + message.debug("searching for '" .. pattern .. "'") + local cursor_row, cursor_col = api.cursor.get() + local new_row, new_col = M.back_find_pattern(cursor_row, cursor_col, pattern) + if new_row and new_col then + api.cursor.set(new_row, new_col) + end + return true +end + +M.gg = function() + local _, cursor_col = api.cursor.get() + local lines = get_lines() + love_cursor.set(1, math.min(cursor_col, #lines[1])) + return true +end +M.G = function() + local _, cursor_col = api.cursor.get() + local lines = get_lines() + love_cursor.set(#lines, math.min(cursor_col, #lines[#lines])) + return true +end + +M.v = function() + M.mode = "sneaking" + love_cursor.set_mode(M.mode) +end + +M.V = function() + M.mode = "aiming" + love_cursor.set_mode(M.mode) +end + +M.C_v = function() + M.mode = "other" + love_cursor.set_mode(M.mode) +end + +M.set_walking = function() + M.mode = "walking" + love_cursor.set_mode(M.mode) +end + +M.find_pattern = function(cursor_row, cursor_col, pattern) + local lines = get_lines() + for row_index = cursor_row, #lines do + local line = lines[row_index] + local col_index = 1 + if row_index == cursor_row then + col_index = cursor_col + 1 + end + local sub_line = string.sub(line, col_index, #line) + local relative_col = string.find(sub_line, pattern) + if relative_col then + local col = col_index + relative_col - 1 + message.debug("pattern found at " .. row_index .. " " .. col) + return row_index, col + end + end + message.debug("pattern not found " .. pattern) +end + +M.back_find_pattern = function(cursor_row, cursor_col, pattern) + local lines = get_lines() + for row_index = cursor_row, 1, -1 do + local line = lines[row_index] + local col_index = #line + if row_index == cursor_row then + col_index = cursor_col - 1 + end + for offset = col_index, 1, -1 do + local sub_line = string.sub(line, offset, col_index) + local relative_col = string.find(sub_line, pattern) + if relative_col then + local col = offset + relative_col - 1 + message.debug("pattern found at " .. row_index .. " " .. col) + return row_index, col + end + end + end + message.debug("pattern not found " .. pattern) +end + +--- find start of next word +---@param cursor_row any +---@param cursor_col any +---@return integer|nil +---@return integer|nil +M.word = function(cursor_row, cursor_col) + local lines = get_lines() + local found_space = false + for row_index = cursor_row, #lines do + local line = lines[row_index] + local col_start = 1 + if row_index == cursor_row then + col_start = cursor_col + end + for col_index = col_start, #line do + local char = string.sub(line, col_index, col_index) + if found_space and char ~= " " then + return row_index, col_index + elseif char == " " then + found_space = true + end + end + found_space = true -- newline counts as space + cursor_col = 1 + end + return nil, nil +end + +--- find start of next word +---@param cursor_row any +---@param cursor_col any +---@return integer|nil +---@return integer|nil +M.back_word = function(cursor_row, cursor_col) + local lines = get_lines() + local found_space = false + for row_index = cursor_row, 1, -1 do + local line = lines[row_index] + local col_start = #line + if row_index == cursor_row then + col_start = cursor_col + end + for col_index = col_start, 1, -1 do + local char = string.sub(line, col_index, col_index) + if found_space and char ~= " " then + return row_index, col_index + elseif char == " " then + found_space = true + end + end + found_space = true -- newline counts as space + cursor_col = #line + end + return nil, nil +end + +return M diff --git a/lua/neohack/map.lua b/lua/neohack/map.lua index b5e1f14..aae3d0a 100644 --- a/lua/neohack/map.lua +++ b/lua/neohack/map.lua @@ -7,18 +7,20 @@ local Def = require("neohack.def") local defs = require("neohack.defs") local buffer = require("neohack.buffer") local state = require("neohack.state") -local view_buffer = require("neohack.view_buffer") +local view_buffer = require("neohack.vim.view_buffer") local constant_defs = require("neohack.constant_defs") +local api = require("neohack.api") --- scan the buf, looking for points of interest +---@param level integer ---@return Entity[] enemies ---@return Entity[] friends ---@return table the first floor tile in top left -M.scan_buf = function(bufnr) +M.scan_buf = function(level) local first_floor = nil local enemies = {} local friends = {} - local cells = buffer.buffers[bufnr].cells + local cells = buffer.buffers[level].cells for row_index, line in ipairs(cells) do for col_index, col in ipairs(line) do if col.movement.char == defs.floor.char and not first_floor then @@ -40,16 +42,16 @@ M.scan_buf = function(bufnr) end --- scan an area of the buf, looking for points of interest ----@param bufnr integer +---@param level integer ---@param row integer ---@param col integer ---@param range integer ---@return Entity[] enemies ---@return Entity[] items -M.scan_area = function(bufnr, row, col, range) +M.scan_area = function(level, row, col, range) local enemies = {} local items = {} - local cells = buffer.buffers[bufnr].cells + local cells = buffer.buffers[level].cells for row_index = row - range, row + range do for col_index = col - range, col + range do if cells[row_index] and cells[row_index][col_index] then @@ -128,9 +130,9 @@ end ---set the player's visible area ---@param view_distance integer -M.show_visible_line_of_sight = function(bufnr, view_distance) +M.show_visible_line_of_sight = function(level, view_distance) local player_row, player_col = buffer.get_under_cursor() - local cells = buffer.buffers[bufnr].cells + local cells = buffer.buffers[level].cells for row_index, line in ipairs(cells) do for col_index, entity in ipairs(line) do if @@ -151,7 +153,7 @@ M.show_visible_line_of_sight = function(bufnr, view_distance) local hi = entity.highlight_group if hi then --TODO: highlights sometimes get cleared somehow - view_buffer.set_highlight(row_index, col_index, hi) + api.render.set_highlight(row_index, col_index, hi) end end end @@ -159,18 +161,27 @@ M.show_visible_line_of_sight = function(bufnr, view_distance) end --- find entities that are visible by an entity, doesn't apply sneak ----@param bufnr integer +---@param level integer ---@param viewer_entity Entity ---@param view_distance integer ---@return Entity[] -M.get_visible_entities = function(bufnr, viewer_entity, view_distance) +M.get_visible_entities = function(level, viewer_entity, view_distance) local found = {} - local cells = buffer.buffers[bufnr].cells - for _, line in ipairs(cells) do - for _, entity in ipairs(line) do - local result = M.is_entity_visible(cells, viewer_entity, entity, view_distance) - if result then - table.insert(found, result) + local cells = buffer.buffers[level].cells + -- limit to view_distance first, to limit nested loops + -- for _, line in ipairs(cells) do + for row_index = viewer_entity.movement.row - view_distance, viewer_entity.movement.row + view_distance do + local line = cells[row_index] + -- for _, entity in ipairs(line) do + for col_index = viewer_entity.movement.col - view_distance, viewer_entity.movement.col + view_distance do + if line then + local entity = line[col_index] + if entity then + local result = M.is_entity_visible(cells, viewer_entity, entity, view_distance) + if result then + table.insert(found, result) + end + end end end end @@ -188,8 +199,10 @@ M.is_entity_visible = function(cells, viewer_entity, entity, view_distance) local player_row, player_col = state.player.movement.row, state.player.movement.col local row_index, col_index = entity.movement.row, entity.movement.col if - M.manhattan_distance(row_index, col_index, start_row, start_col) <= view_distance - and M.is_visible_line_of_sight(start_row, start_col, row_index, col_index, cells) + -- already limited to view_distance + -- M.manhattan_distance(row_index, col_index, start_row, start_col) <= view_distance and + M.is_visible_line_of_sight(start_row, start_col, row_index, col_index, cells) + -- true then --visible local is_player = row_index == player_row and col_index == player_col @@ -206,28 +219,27 @@ M.is_entity_visible = function(cells, viewer_entity, entity, view_distance) end ---comment -M.all_visible = function(bufnr) - local cells = buffer.buffers[bufnr].cells +M.all_visible = function(level) + local cells = buffer.buffers[level].cells for _, line in ipairs(cells) do for _, cell in ipairs(line) do cell.movement.visible = true end end - buffer.write_buf(bufnr) end ---comment ---@param view_distance integer ---@return boolean -M.generate_new_map_sections = function(bufnr, view_distance) +M.generate_new_map_sections = function(level, view_distance) local row, col = buffer.get_under_cursor() local new_section_generated = false -- Define the range within which to generate new map sections local range = view_distance - local rowCount = #buffer.buffers[bufnr].cells - local rowOfPlayerColCount = #buffer.buffers[bufnr].cells[row] + local rowCount = #buffer.buffers[level].cells + local rowOfPlayerColCount = #buffer.buffers[level].cells[row] if (row + range) > rowCount then -- message.notify("generating") @@ -237,15 +249,15 @@ M.generate_new_map_sections = function(bufnr, view_distance) for j = 1, rowOfPlayerColCount do table.insert(newLine, defs.new_random_entity(rowCount + i, j)) end - table.insert(buffer.buffers[bufnr].cells, newLine) + table.insert(buffer.buffers[level].cells, newLine) end end if (col + range) > rowOfPlayerColCount then new_section_generated = true - local line_count = #buffer.buffers[bufnr].cells + local line_count = #buffer.buffers[level].cells for i = 1, line_count do - local currentRowColCount = #buffer.buffers[bufnr].cells[i] + local currentRowColCount = #buffer.buffers[level].cells[i] if (col + range) > currentRowColCount and math.abs(row - i) < range then local generateCount = (col + range) - currentRowColCount local newCells = {} @@ -253,7 +265,7 @@ M.generate_new_map_sections = function(bufnr, view_distance) table.insert(newCells, defs.new_random_entity(i, currentRowColCount + j)) end for _, newCel in ipairs(newCells) do - table.insert(buffer.buffers[bufnr].cells[i], newCel) + table.insert(buffer.buffers[level].cells[i], newCel) end end end @@ -271,11 +283,11 @@ end --TODO: deprecated ---comment ----@param bufnr integer +---@param level integer ---@return Entity[][] --- M.find_deleted = function(bufnr) --- local new_buffer = view_buffer.read_a_buf(bufnr) --- local old_buffer = buffer.buffers[bufnr].cells +-- M.find_deleted = function(level) +-- local new_buffer = view_buffer.read_a_buf(level) +-- local old_buffer = buffer.buffers[level].cells -- -- TODO: this doesn't work properly on the last line -- while #new_buffer < #old_buffer do -- -- there are deleted lines @@ -369,16 +381,16 @@ M.find_all_matches = function(grid, pattern_lines) end ---comment ----@param bufnr integer +---@param level integer ---@param pattern_lines string[] a multiline pattern to match ---@param player_row integer ---@param player_col integer ---@return Entity[] | nil -M.find_closest_match = function(bufnr, pattern_lines, player_row, player_col) +M.find_closest_match = function(level, pattern_lines, player_row, player_col) if #pattern_lines == 0 then return nil end - local cells = buffer.buffers[bufnr].cells + local cells = buffer.buffers[level].cells local matches = M.find_all_matches(cells, pattern_lines) if #matches == 0 then return nil @@ -404,14 +416,14 @@ end ---@return Entity[]|nil M.find_closest_to_player = function(patterns) local player_row, player_col = buffer.get_under_cursor() - return M.find_closest_match(state.current_bufnr, patterns, player_row, player_col) + return M.find_closest_match(state.current_level, patterns, player_row, player_col) end ---comment ---@param entities Entity[] ---@return boolean if entities are all the same in the real view buffer M.present_in_buffer = function(entities) - local cells = view_buffer.read_buf_chars(state.current_bufnr) + local cells = view_buffer.read_buf_chars() for _, entity in ipairs(entities) do local char = cells[entity.movement.row][entity.movement.col] if char ~= entity.movement.char then diff --git a/lua/neohack/message.lua b/lua/neohack/message.lua index 700f220..ae7a8d5 100644 --- a/lua/neohack/message.lua +++ b/lua/neohack/message.lua @@ -1,60 +1,46 @@ local state = require("neohack.state") +local api = require("neohack.api") +local utils = require("neohack.utils") -local M = { - notify_func = nil, - message_buf = nil, -} +local M = {} ----comment -M.open = function() - vim.cmd("20 split") - vim.cmd("enew") - M.message_buf = vim.api.nvim_get_current_buf() - vim.api.nvim_command("wincmd p") +M.notify_lines = function(...) + api.notify(...) end ----comment ----comment ----@param ... any +--- Show the user a message +---@param ... any lines M.notify = function(...) - M.notify_func({ ... }) + api.notify(utils.concatenate_strings(...)) end +--- for dev +---@param ... any string parts M.debug = function(...) if state.debug then - local args = { ... } - table.insert(args, 1, "DEBUG:") - M.notify_func(args) + local msg = { ... } + table.insert(msg, 1, "DEBUG:") + api.debug(utils.concatenate_strings(unpack(msg))) end end ----comment ----@param s string ----@return string[] -local function split_string_into_lines(s) - local lines = {} - for line in s:gmatch("([^\n]*)\n?") do - if line ~= "\n" and #line > 0 then - table.insert(lines, line) - end +--- trace game behaviour +---@param ... any string parts +M.trace = function(...) + if state.trace then + api.debug(utils.concatenate_strings("TRACE:", ...)) end - return lines end ----comment ----comment ----@param ... any -M.send_to_message_buf = function(...) - local message = table.concat(..., " ") - vim.schedule(function() - vim.api.nvim_buf_set_lines(M.message_buf, -1, -1, false, split_string_into_lines(message)) - local win_ids = vim.fn.win_findbuf(M.message_buf) - if #win_ids > 0 then - local win_id = win_ids[1] - local line_count = vim.api.nvim_buf_line_count(M.message_buf) - vim.api.nvim_win_set_cursor(win_id, { line_count, 0 }) - end - end) +--- action message +--- @param actor Entity +--- @param ... any +M.action = function(actor, ...) + if actor.type == "player" then + M.notify(actor.name, ...) + else + M.trace(actor.name, ...) + end end return M diff --git a/lua/neohack/player.lua b/lua/neohack/player.lua index c488bdd..457d40f 100644 --- a/lua/neohack/player.lua +++ b/lua/neohack/player.lua @@ -6,8 +6,10 @@ local buffer = require("neohack.buffer") local state = require("neohack.state") local message = require("neohack.message") local Entity = require("neohack.entity") -local view_buffer = require("neohack.view_buffer") +local view_buffer = require("neohack.vim.view_buffer") local constant_defs = require("neohack.constant_defs") +local api = require("neohack.api") +local animate = require("neohack.animate") ---@class Player: Entity ---@field handle_down function @@ -46,6 +48,12 @@ function Player.new() ---@type function instance.handle_up = nil + ---@type Highlight + Player.cursor_hit_highlight = api.render.create_background_color("cursor_hit", { red = 143, green = 17, blue = 34 }) -- dark red bg + ---@type Highlight + Player.cursor_dodge_highlight = + api.render.create_background_color("cursor_dodge", { red = 143, green = 93, blue = 17 }) -- dark orange bg + return instance end @@ -53,10 +61,10 @@ end function Player:player_sneak_move() local _, _, entity = buffer.get_under_cursor() if not entity.vision:can_see(self) then - message.notify(self.name, "sneaked past a", entity.name) + message.action(self, "sneaked past a", entity.name) else self:restore_previous_position() - message.notify(self.name, "sneaked failed on", entity.name) + message.action(self, "sneaked failed on", entity.name) end -- ensure we stay on this position @@ -70,13 +78,16 @@ function Player:player_hit_move() -- bounce is handled in during animations with handle_bounce local row, col, entity = buffer.get_under_cursor() + if entity == nil then + return + end if entity.type == Def.DefType.item then -- do nothing elseif entity.type == Def.DefType.floor then -- do nothing elseif entity.type == Def.DefType.friend then entity.attributes.health = entity.attributes.health + 0.5 - message.notify(self.name, "hugged", entity.name) + message.action(self, "hugged", entity.name) elseif entity.type == Def.DefType.enemy then self:hit_enemy(row, col, entity) else -- assume other characters are terrain @@ -86,14 +97,16 @@ function Player:player_hit_move() end function Player:store_previous_position() - message.debug("store_previous_position") - local prevRow, prevCol = unpack(vim.api.nvim_win_get_cursor(0)) - state.prev_cursor = { row = prevRow, col = prevCol + 1 } + -- message.debug("store_previous_position") + local prevRow, prevCol = api.cursor.get() + state.prev_cursor = { row = prevRow, col = prevCol } end function Player:restore_previous_position() message.debug("restore_previous_position") - view_buffer.move_prev_cursor() + local row, col = api.cursor.get() + animate.bounce_cursor(row, col, state.prev_cursor.row, state.prev_cursor.col) + api.cursor.set(state.prev_cursor.row, state.prev_cursor.col) self:set_position_from_cursor() end @@ -106,15 +119,19 @@ end function Player:set_cursor_from_position() -- message.notify("set cursor from position", cursor_row, cursor_col) - view_buffer.set_cursor(self.movement.row, self.movement.col) + api.cursor.set(self.movement.row, self.movement.col) end function Player:handle_bounce() -- ensure player always bounces off after animations local _, _, entity = buffer.get_under_cursor() + if entity == nil then + return + end if entity.type ~= Def.DefType.floor and entity.type ~= Def.DefType.item then - -- vim.defer_fn(function() + -- api.async.defer(function() state.player:restore_previous_position() + -- message.action(self, "bounced off", entity.name) -- end, 50) else state.player:store_previous_position() @@ -139,18 +156,18 @@ function Player:hit_terrain(row, col, terrain) -- view_buffer.highlight_hit(row, col, constant_defs.hitting_highlight, 150) if terrain.name == "down" then local level = self:handle_down() - message.notify("Went down to " .. level) + message.action(self, "Went down to " .. level) elseif terrain.name == "up" then local level = self:handle_up() if level then - message.notify("Went up to " .. level) + message.action(self, "Went up to " .. level) else - message.notify("Up is blocked. On " .. state.current_floor) + message.action(self, "Up is blocked. On " .. state.current_level) end elseif terrain.attributes.damage then self:hit_by(terrain) else - message.notify("Bumped a " .. terrain.name) + message.action(self, "Bumped a", terrain.name) end end @@ -167,9 +184,9 @@ function Player:hit_by(attacker) -- local cursor_highlight = self.combat:hit_by(attacker) local hit = attacker.combat:attack(self.combat) if hit then - buffer.highlight_cursor(constant_defs.cursor_hit_highlight) + api.cursor.highlight_cursor(Player.cursor_hit_highlight) else - buffer.highlight_cursor(constant_defs.cursor_dodge_highlight) + api.cursor.highlight_cursor(Player.cursor_dodge_highlight) end if self.health:check_dead() then @@ -188,7 +205,7 @@ function Player:died() end local bodies = table.concat(parts, " ") - message.notify(self:inspect(), "\n", self.inventory:get_inventory(), "kills:", bodies) + message.notify_lines(self:inspect(), unpack(self.inventory:get_inventory()), "kills:", bodies) message.notify("YOU DIED") -- put the dead player's corpse at the current cursor location diff --git a/lua/neohack/state.lua b/lua/neohack/state.lua index d82fa29..f571060 100644 --- a/lua/neohack/state.lua +++ b/lua/neohack/state.lua @@ -2,6 +2,21 @@ --- local M = { + ---@type boolean is the game currently running + running = false, + + ---@type boolean are there any updates that require rendering + update = false, + + ---@type boolean if entities will move on a timer. if false moves only occur after player move. + realtime = false, + ---@type number time in ms between moves + tick_ms = 500, + + --- map size + rows = 0, + cols = 0, + --- the player entity ---@type Player player = nil, @@ -13,9 +28,7 @@ local M = { turn_counter = 0, ---@type integer - current_bufnr = nil, - ---@type integer - current_floor = nil, + current_level = nil, -- ---@type Entity[] -- deleted = nil, @@ -45,6 +58,10 @@ local M = { ---@type boolean debug = false, + -- show trace messages + ---@type boolean + trace = false, + ---@type boolean if currently animating, don't do a tick animating = false, @@ -76,7 +93,7 @@ M.init_state = function(player) M.player = player M.prev_cursor = { row = 1, col = 1 } M.turn_counter = 0 - M.current_floor = 1 + M.current_level = 1 -- keep corpse as that's from the previous game end diff --git a/lua/neohack/utils.lua b/lua/neohack/utils.lua index fe15eec..91a7551 100644 --- a/lua/neohack/utils.lua +++ b/lua/neohack/utils.lua @@ -15,6 +15,48 @@ M.table_deep_copy = function(orig) return copy end +--- +---@param ... any +---@return string +M.concatenate_strings = function(...) + local result = "" + for _, str in ipairs({ ... }) do + -- TODO: slow? + -- result = result .. M.to_string(str) .. " " + result = result .. str .. " " + end + return result +end + +--- convert anything to a string +---@param x any +---@return string +M.to_string = function(x) + local result = "" + if type(x) ~= "table" then + return tostring(x) + end + if x[1] then + -- probably an array + result = result .. "{" + for _, value in ipairs(x) do + result = result .. M.to_string(value) .. "," + end + result = result .. "}" + return result + end + + -- else an object + result = result .. "{" + for key, value in pairs(x) do + local formattedKey = tostring(key) + result = result .. formattedKey .. "=" .. M.to_string(value) .. "," + end + result = result .. "}" + + return result +end + ---comment ---@param map table ---@return any[] the keys in an array @@ -51,6 +93,17 @@ M.split_words = function(str) return words end +---comment +---@param input string +---@return string[] +M.split_to_chars = function(input) + local chars = {} + for i = 1, #input do + chars[i] = input:sub(i, i) + end + return chars +end + ---comment ---@param strings string[] ---@return string[] @@ -173,8 +226,8 @@ end -- Define the decisions and their weights -- Function to select a decision based on weights ---comment ----@param actions Action[] ----@return Action? +---@param actions EntityAction[] +---@return EntityAction? ---@return number? M.weighted_random = function(actions) local totalWeight = 0 @@ -200,4 +253,49 @@ M.sleep = function(a) end end +---comment +---@param array any +---@return table +M.unique = function(array) + local seen = {} + local unique_array = {} + for _, item in ipairs(array) do + if not seen[item] then + table.insert(unique_array, item) + seen[item] = true + end + end + return unique_array +end + +--- restrict to boundary +---@param value number +---@param min number +---@param max number +---@return number +M.bounded = function(value, min, max) + return math.max(math.min(value, max), min) +end + +---@param list any[] +---@param item any +M.contains = function(list, item) + for _, v in ipairs(list) do + if v == item then + return true + end + end + return false +end + +M.file_exists = function(name) + local f = io.open(name, "r") + if f ~= nil then + io.close(f) + return true + else + return false + end +end + return M diff --git a/lua/neohack/view_buffer.lua b/lua/neohack/view_buffer.lua deleted file mode 100644 index 9c3907f..0000000 --- a/lua/neohack/view_buffer.lua +++ /dev/null @@ -1,223 +0,0 @@ -local constant_defs = require("neohack.constant_defs") ---- all the interactions with the actual neovim buffer that is seen, ie the view ---- - -local M = { - AutoCmdGroup = "NeoHackGameTick", -} - -local state = require("neohack.state") - -local namespace = vim.api.nvim_create_namespace("NeoHack") -local group = vim.api.nvim_create_augroup(M.AutoCmdGroup, { clear = true }) - ---- Copy the current buffer and return the new buffer number ----@return integer bufnr -M.copy_buffer = function() - local current_bufnr = vim.api.nvim_get_current_buf() - local lines = vim.api.nvim_buf_get_lines(current_bufnr, 0, -1, false) - return M.new_buffer(lines) -end - ----comment ----@param lines string[] ----@return integer -M.new_buffer = function(lines) - local new_bufnr = vim.api.nvim_create_buf(true, false) - vim.api.nvim_buf_set_lines(new_bufnr, 0, -1, false, lines) - vim.api.nvim_set_current_buf(new_bufnr) - return new_bufnr -end - -M.setup_buffer = function(bufnr, first_floor) - vim.api.nvim_set_current_buf(bufnr) - if first_floor then - M.set_cursor(first_floor.row, first_floor.col) - end -end - ---- Remove handlers for a specific buffer ----@param bufnr integer -M.remove_handlers = function(bufnr) - vim.api.nvim_clear_autocmds({ group = M.AutoCmdGroup, buffer = bufnr }) -end - -M.add_handler = function(buffer, event, func) - vim.api.nvim_create_autocmd(event, { - group = group, - buffer = buffer.bufnr, - callback = func, - }) -end - ---- Read buffer content as a grid of chars ----@param bufnr integer ----@return string[][] -M.read_buf_chars = function(bufnr) - local new_cells = {} -- prepare a new frame - local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) - for row, value in ipairs(lines) do - local result = {} - for col = 1, #value do - local char = value:sub(col, col) - result[col] = char - end - new_cells[row] = result - end - return new_cells -end - ---- Write the buffer content based on the cells for a specific buffer ----comment ----@param bufnr integer ----@param cells Entity[][] -M.write_buf = function(bufnr, cells) - local lines = {} - for row_index, row in ipairs(cells) do - local value = {} - for _, entity in ipairs(row) do - if entity.movement.visible then - if not state.player.vision:can_see(entity) then - if state.debug then - table.insert(value, "^") - else - table.insert(value, " ") - end - else - table.insert(value, entity.movement.char) - end - elseif entity.movement.seen then - table.insert(value, entity.movement.char) - else - table.insert(value, constant_defs.not_visible) - end - end - lines[row_index] = table.concat(value) - end - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) -end - ---- Set highlight for a specific position in a specific buffer ----@param row integer ----@param col integer ----@param highlight string? -M.set_highlight = function(row, col, highlight) - local bufnr = state.current_bufnr - if not bufnr then - error("no bufnr") - end - if highlight then - vim.defer_fn(function() - vim.api.nvim_buf_add_highlight(bufnr, namespace, highlight, row - 1, col - 1, col) - end, 1) - end -end ---- Highlight hit at a specific position in a specific buffer ----@param row integer ----@param col integer ----@param highlight string ----@param timeout integer -M.highlight_hit = function(row, col, highlight, timeout) - local bufnr = state.current_bufnr - if not bufnr then - error("no bufnr") - end - M.set_highlight(row, col, highlight) - --TODO: fix clearing too many highlights, keeping hurt highlight is better - -- vim.defer_fn(function() - -- vim.api.nvim_buf_clear_namespace(bufnr, namespace, row - 1, row) - -- end, timeout) -end - ----comment ----@param row integer ----@param col integer -M.set_cursor = function(row, col) - vim.api.nvim_win_set_cursor(0, { row, col - 1 }) -end - ----comment -M.move_prev_cursor = function() - -- TODO: show the bouncing effect using smear-cursor - -- vim.defer_fn(function() - M.set_cursor(state.prev_cursor.row, state.prev_cursor.col) - -- end, 10) -end - -M.show_char_at_pos = function(row, col, char) - vim.api.nvim_buf_set_text(state.current_bufnr, row - 1, col - 1, row - 1, col, { char }) -end - --- TODO: do we need to save/restore buffer settings anymore with a copy of the buffer? ----comment ----@param bufnr integer ----@param buffer Buffer ----@return integer bufnr -M.setup_game_buffer = function(bufnr, buffer) - local winnr = 0 - if not bufnr then - error("No buffer found") - end - buffer.buf_settings = { - spell = vim.wo[winnr].spell, - hlsearch = vim.api.nvim_get_option("hlsearch"), - wrap = vim.wo[winnr].wrap, - listchars = vim.o.listchars, - cursorline_hl = vim.api.nvim_get_hl(0, { name = "CursorLine", link = false }), - cursorcolumn_hl = vim.api.nvim_get_hl(0, { name = "CursorColumn", link = false }), - statusline = vim.o.statusline, - laststatus = vim.o.laststatus, - } - - vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1) - - vim.api.nvim_buf_set_option(bufnr, "cursorline", true) - vim.api.nvim_buf_set_option(bufnr, "cursorcolumn", true) - - vim.api.nvim_buf_set_option(bufnr, "spell", false) - vim.cmd.nohlsearch() - vim.cmd("set nowrap") - vim.o.listchars = "" - - -- set the statusline to show in game info - vim.o.statusline = "%!v:lua.require'neohack'.status_line()" - vim.o.laststatus = 3 - - -- TODO: disable code complete - - return bufnr -end - ----comment ----comment ----@param bufnr integer ----@param buffer Buffer -M.restore_original_settings = function(bufnr, buffer) - if not bufnr then - error("No buffer found") - end - vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) - - if buffer.buf_settings then - ---@diagnostic disable-next-line: undefined-field - vim.api.nvim_buf_set_option(bufnr, "spell", buffer.buf_settings.spell) - ---@diagnostic disable-next-line: undefined-field - if buffer.buf_settings.spell then - vim.cmd("set spell") - end - ---@diagnostic disable-next-line: undefined-field - if buffer.buf_settings.hlsearch then - vim.cmd("set hlsearch") - end - ---@diagnostic disable-next-line: undefined-field - if buffer.buf_settings.wrap then - vim.cmd("set wrap") - end - ---@diagnostic disable-next-line: undefined-field - vim.o.listchars = buffer.buf_settings.listchars - vim.o.statusline = buffer.buf_settings.status_line - vim.o.laststatus = buffer.buf_settings.laststatus - end -end - -return M diff --git a/lua/neohack/vim/async.lua b/lua/neohack/vim/async.lua new file mode 100644 index 0000000..be58b1d --- /dev/null +++ b/lua/neohack/vim/async.lua @@ -0,0 +1,25 @@ +local M = {} + +M.get_time = function() + return vim.loop.now() +end + +M.later = function(callback) + vim.schedule(callback) +end + +M.defer = function(callback, timeout) + vim.defer_fn(callback, timeout) +end + +local timer = vim.loop.new_timer() +M.loop = function(callback, timeout) + timer:start(0, timeout, vim.schedule_wrap(callback)) +end + +M.loop_stop = function() + timer:stop() + timer:close() +end + +return M diff --git a/lua/neohack/vim/cursor.lua b/lua/neohack/vim/cursor.lua new file mode 100644 index 0000000..e20fcd0 --- /dev/null +++ b/lua/neohack/vim/cursor.lua @@ -0,0 +1,38 @@ +local M = {} + +local cursorline_hl = vim.api.nvim_get_hl(0, { name = "CursorLine", link = false }) +local cursorcolumn_hl = vim.api.nvim_get_hl(0, { name = "CursorColumn", link = false }) + +M.get = function() + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + col = col + 1 + return row, col +end + +M.set = function(row, col) + vim.api.nvim_win_set_cursor(0, { row, col - 1 }) +end + +M.animate = function(row, col) + vim.api.nvim_win_set_cursor(0, { row, col - 1 }) +end + +--- Highlight cursor line and column with specific settings +---@param highlight Highlight +M.highlight_cursor = function(highlight) + local vim_hl = { + link = highlight, + } + vim.api.nvim_set_hl(0, "CursorLine", vim_hl) + vim.api.nvim_set_hl(0, "CursorColumn", vim_hl) + + vim.schedule(function() + vim.api.nvim_set_hl(0, "CursorLine", cursorline_hl) + vim.api.nvim_set_hl(0, "CursorColumn", cursorcolumn_hl) + end) +end + +M.write_cursor = function() end + +---@type Cursor +return M diff --git a/lua/neohack/edits.lua b/lua/neohack/vim/edits.lua similarity index 96% rename from lua/neohack/edits.lua rename to lua/neohack/vim/edits.lua index 882a0a5..f5ca298 100644 --- a/lua/neohack/edits.lua +++ b/lua/neohack/vim/edits.lua @@ -2,16 +2,17 @@ local M = {} local Def = require("neohack.def") local defs = require("neohack.defs") -local buffer = require("neohack.buffer") local message = require("neohack.message") local map = require("neohack.map") local state = require("neohack.state") local utils = require("neohack.utils") local chance = require("neohack.chance") local actions = require("neohack.actions") +local api = require("neohack.api") M.handle_insert_enter = function() - state.insert_enter_start = vim.api.nvim_win_get_cursor(0) + local row, col = api.cursor.get() + state.insert_enter_start = { row, col } -- message.notify("handle_insert_enter " .. vim.inspect(state.insert_enter_start)) end @@ -27,14 +28,14 @@ M.handle_insert = function() -- --TODO: this gets confused if arrow keys are used -- with TextChangedI - local cursor_row, cursor_col = unpack(vim.api.nvim_win_get_cursor(0)) + local cursor_row, cursor_col = api.cursor.get() local lines = vim.api.nvim_buf_get_text( - state.current_bufnr, + 0, state.insert_enter_start[1] - 1, - state.insert_enter_start[2], + state.insert_enter_start[2] - 1, cursor_row - 1, - cursor_col, + cursor_col - 1, {} ) state.inserted = lines diff --git a/lua/neohack/vim/keymaps.lua b/lua/neohack/vim/keymaps.lua new file mode 100644 index 0000000..5db0725 --- /dev/null +++ b/lua/neohack/vim/keymaps.lua @@ -0,0 +1,241 @@ +local state = require("neohack.state") +local utils = require("neohack.utils") +local constant_defs = require("neohack.constant_defs") +local actions = require("neohack.actions") + +local M = {} + +---comment +---@param prompt string +---@return string +M.prompt = function(prompt) + local result + vim.ui.input({ prompt = prompt .. " " }, function(input) + result = input + end) + return result +end + +---create buffer local keymaps for every possible action, including containing inventory +---The name of the keymap should be a valid command to insert +M.dynamic_keymaps = function() + local bufnr = 0 + M._remove_keymaps(bufnr) + + local wk = require("which-key") + -- require("which-key").setup({ + -- triggers = { + -- { "", mode = { "n", "v" } }, + -- }, + -- }) + + local letters = utils.split_to_chars(constant_defs.letters) + local add_group = function(key, desc) + wk.add({ mode = { "n", "v" }, { actions.leader_key .. key, desc = desc } }) + end + local hide_group = function(key, desc) + wk.add({ mode = { "n", "v" }, { actions.leader_key .. key, desc = desc, hidden = true } }) + end + local function map(key, func, desc) + vim.keymap.set({ "n", "v" }, actions.leader_key .. key, func, { buffer = bufnr, desc = desc }) + end + + local function direction_keymaps(key, func, name) + add_group(key, name) + for _, map_info in ipairs(constant_defs.directions) do + map(key .. map_info.key, function() + func({ object = map_info.key }) + end, name .. " " .. map_info.name) + end + end + + local inventory = state.player.inventory.items + local function inv_maps(key, func, name) + if #inventory == 0 then + hide_group(key, name) + return + end + add_group(key, name) + for index, item in ipairs(inventory) do + map(key .. letters[index], function() + func({ object = index }) + end, name .. " " .. item.name) + end + end + + local function wear_maps(key, func, name) + add_group(key, name) + + for index, item in ipairs(inventory) do + for slot_name, slot_key in pairs(state.slot_types) do + add_group(key .. letters[index], name .. " " .. item.name) + map(key .. letters[index] .. slot_key, function() + func({ object = index, target = slot_key }) + end, name .. " " .. item.name .. " " .. slot_name) + -- print("add keymap", key .. letters[index] .. slot_key, name .. " " .. item.name .. " " .. slot_name) + end + end + + for slot_name, slot_key in pairs(state.slot_types) do + local nothing_key = "0" + add_group(key .. nothing_key, name .. " nothing") + map(key .. nothing_key .. slot_key, function() + func({ object = nothing_key, target = slot_key }) + end, name .. " nothing " .. slot_name) + end + end + + local function inv_inv_maps(key, func, name) + if #inventory < 2 then + hide_group(key, name) + return + end + add_group(key, name) + for index1, item1 in ipairs(inventory) do + add_group(key .. letters[index1], name .. " " .. item1.name) + for index2, item2 in ipairs(inventory) do + if index1 ~= index2 then + map(key .. letters[index1] .. letters[index2], function() + func({ object = index1, target = index2 }) + end, name .. " " .. item1.name .. " " .. item2.name) + end + end + end + end + + map("?", actions.actions.help, "help") + map("q", function() + vim.cmd(":qa!") + end, "quit") + -- map("?", function() actions.insert_action("help") end, "help") + map("i", actions.actions.inventory, "inventory") + -- map("i", function() actions.insert_action("inventory") end, "inventory") + + -- map("w", actions.prompt_wear, "wear") + wear_maps("w", actions.actions.wear, "wear") + + -- map("k", actions.prompt_kick, "kick") + direction_keymaps("k", actions.actions.kick, "kick") + + -- map("f", actions.prompt_fuse, "fuse") + inv_inv_maps("f", actions.actions.fuse, "fuse") + + -- map("r", actions.prompt_rename, "rename") + inv_maps("R", actions.prompt_rename_to, "Rename") + + -- map("l", actions.prompt_look, "look") + map("l0", function() + actions.actions.look({ object = "0" }) + end, "look self") + inv_maps("l", actions.actions.look, "look") + add_group("l", "look") + + -- map("p", actions.prompt_pilfer, "pilfer") + inv_maps("p", actions.actions.pilfer, "pilfer") + + direction_keymaps("P", actions.actions.pick, "Pick") + + -- map("e", actions.prompt_eat, "eat") + inv_maps("e", actions.actions.eat, "eat") + + -- map("d", actions.prompt_drop, "drop") + inv_maps("d", actions.actions.drop, "drop") + + --TODO: could limit which spells you can say to the ones you've read? + -- map("s", actions.prompt_say, "say") + local player = state.player + local spells = player.inventory:get_spell_items() + if #spells == 0 then + hide_group("s", "say") + else + add_group("s", "say") + for index, spell_item in ipairs(spells) do + local word = spell_item.speak.spell.inscription + add_group("s" .. letters[index], "say " .. word) + local enemies, items = + state.scan_area(state.current_level, player.movement.row, player.movement.col, player.vision:view_distance()) + local names = {} + for _, enemy in ipairs(enemies) do + table.insert(names, enemy.name) + end + for _, item in ipairs(items) do + table.insert(names, item.name) + end + + for i, name in ipairs(utils.unique(names)) do + map("s" .. letters[index] .. letters[i], function() + actions.actions.say({ object = word, target = name }) + end, "say " .. word .. " " .. name) + end + end + end + + add_group("r", "rest") + for i = 1, 9, 1 do + map("r" .. i, function() + actions.actions.rest({ action = "rest", object = tostring(i) }) + end, "rest " .. tostring(i)) + end + + -- refresh to get new keymaps + -- wk.setup({ + -- triggers = { + -- { "", mode = { "n", "v" } }, + -- }, + -- }) +end + +---remove all existing buffer keymaps +---@param bufnr number +M._remove_keymaps = function(bufnr) + -- local modes = { "n", "i", "v", "x", "s", "o" } -- common modes + local wk = require("which-key") + local modes = { "n", "v" } + for _, mode in ipairs(modes) do + local keymaps = vim.api.nvim_buf_get_keymap(bufnr, mode) + for _, keymap in ipairs(keymaps) do + vim.api.nvim_buf_del_keymap(bufnr, mode, keymap.lhs) + -- print("removed keymap", keymap.lhs) + end + end + + -- special case for wear, otherwise the old keymaps stay around + for _, letter in ipairs(utils.split_to_chars(constant_defs.letters)) do + wk.add({ mode = { "n", "v" }, { "w" .. letter, hidden = true } }) + wk.add({ mode = { "n", "v" }, { "s" .. letter, hidden = true } }) + end +end + +---comment +---@return boolean +M.in_sneak_mode = function() + local mode = vim.api.nvim_get_mode().mode + return mode == "v" +end + +M.in_aim_mode = function() + local mode = vim.api.nvim_get_mode().mode + return mode == "V" +end + +M.in_other_mode = function() + local mode = vim.api.nvim_get_mode().mode + local block_visual = "\22" + return mode == block_visual +end + +local game_mode = { + n = "walking", + i = "action", + v = "sneaking", + V = "aiming", + ["\22"] = "other", -- CTRL-V +} + +---@return string +M.get_mode = function() + local mode = vim.fn.mode() + return game_mode[mode] +end + +return M diff --git a/lua/neohack/vim/render.lua b/lua/neohack/vim/render.lua new file mode 100644 index 0000000..fecc937 --- /dev/null +++ b/lua/neohack/vim/render.lua @@ -0,0 +1,143 @@ +local view_buffer = require("neohack.vim.view_buffer") +local M = { + ---@type table level to bufnr + levels = {}, + ---@type table> row to col to highlight group + highlights = {}, +} + +local message = require("neohack.vim.vim_message") + +--- Open the UI elements +M.open_ui = function() + vim.cmd("silent! only") + message.open() + vim.fn.setreg('"', "") +end + +local namespace = vim.api.nvim_create_namespace("NeoHack") + +local function write_highlight(row, col, highlight) + local bufnr = 0 + if highlight then + vim.schedule(function() + vim.api.nvim_buf_add_highlight(bufnr, namespace, highlight, row - 1, col - 1, col) + end) + end +end + +--- write the full set of lines to the buffer +---@param lines string[] +M.write_buf = function(lines) + local current_bufnr = 0 + vim.api.nvim_buf_set_lines(current_bufnr, 0, -1, false, lines) + + for row_index, row in ipairs(lines) do + for col_index = 1, #row do + local highlight_row = M.highlights[row_index] + if highlight_row then + local highlight = highlight_row[col_index] + if highlight then + write_highlight(row_index, col_index, highlight) + end + end + end + end + M.highlights = {} -- clear color for next frame +end + +--- Write a single character to the buffer at the specified position +---@param row integer +---@param col integer +---@param char string a single char +M.write_pos = function(row, col, char) + vim.api.nvim_buf_set_text(0, row - 1, col - 1, row - 1, col, { char }) + local highlight_row = M.highlights[row] + if highlight_row then + write_highlight(row, col, highlight_row[col]) + end +end + +---comment +---@param level integer +M.new_view = function(level) + local new_bufnr = vim.api.nvim_create_buf(true, false) + vim.api.nvim_set_current_buf(new_bufnr) + M.levels[level] = new_bufnr +end + +--- Set up the buffer with the given handlers +---@param handlers table|nil +M.setup_view = function(handlers) + vim.api.nvim_set_current_buf(0) + + view_buffer._remove_handlers(0) + if handlers ~= nil then + view_buffer._add_handlers(0, handlers) + end + view_buffer._set_buffer_settings() +end + +--- Unset the buffer and restore original settings +M.unsetup_view = function() + view_buffer._remove_handlers(0) + -- M._restore_original_settings(buffer) +end + +M.rgb_to_hex = function(rgb) + return string.format("#%02X%02X%02X", rgb.red, rgb.green, rgb.blue) +end + +local function create_highlight(name, rgb, fgbg) + -- Convert RGB to hex + local color = M.rgb_to_hex(rgb) + local highlight_group = name:gsub("[^%w]", "_") .. color:gsub("#", "_") + -- message.debug("highlight_group", highlight_group, color) + --TODO: do we really want totally unique highlight group for every entity? + --TODO: memoize + vim.api.nvim_command("highlight " .. highlight_group .. fgbg .. color) + return highlight_group +end + +--- Create a color +--- @param name string name to base the color on +--- @param rgb table +--- @return string highlight group for the color +M.create_color = function(name, rgb) + return create_highlight(name, rgb, " guifg=") +end + +--- Create a background color +--- @param name string name to base the color on +--- @param rgb table +--- @return string highlight group for the color +M.create_background_color = function(name, rgb) + return create_highlight(name, rgb, " guibg=") +end + +--- Set highlight for a specific position in a specific buffer +---@param row integer +---@param col integer +---@param highlight string? highlight group +M.set_highlight = function(row, col, highlight) + local highlight_row = M.highlights[row] + if not highlight_row then + M.highlights[row] = {} + end + M.highlights[row][col] = highlight +end + +-- --- Highlight hit at a specific position in a specific buffer +-- ---@param row integer +-- ---@param col integer +-- ---@param highlight string +-- ---@param timeout integer +-- M.highlight_hit = function(row, col, highlight, timeout) +-- M.set_highlight(row, col, highlight) +-- --TODO: fix clearing too many highlights, keeping hurt highlight is better +-- -- api.async.defer(function() +-- -- vim.api.nvim_buf_clear_namespace(bufnr, namespace, row - 1, row) +-- -- end, timeout) +-- end + +return M diff --git a/lua/neohack/vim/view_buffer.lua b/lua/neohack/vim/view_buffer.lua new file mode 100644 index 0000000..e6cd83b --- /dev/null +++ b/lua/neohack/vim/view_buffer.lua @@ -0,0 +1,133 @@ +--- all the interactions with the actual neovim buffer that is seen, ie the view +--- + +local M = { + AutoCmdGroup = "NeoHackGameTick", +} + +local group = vim.api.nvim_create_augroup(M.AutoCmdGroup, { clear = true }) + +-- --- Copy the current buffer and return the new buffer number +-- ---@return integer bufnr +-- M.copy_buffer = function() +-- local current_bufnr = vim.api.nvim_get_current_buf() +-- local lines = vim.api.nvim_buf_get_lines(current_bufnr, 0, -1, false) +-- return M.new_buffer(lines) +-- end + +--- Add handlers for a specific buffer +---@param bufnr integer +M._add_handlers = function(bufnr, handlers) + if + handlers.handle_moved + and handlers.handle_insert + and handlers.handle_insert_enter + and handlers.handle_changed + and handlers.handle_yanked + then + M._add_handler(bufnr, "CursorMoved", handlers.handle_moved) + M._add_handler(bufnr, "TextChangedI", handlers.handle_insert) + M._add_handler(bufnr, "InsertEnter", handlers.handle_insert_enter) + -- changed is part of CursorMoved now + -- M.add_handler(bufnr, "TextChanged", handlers.handle_changed) + M._add_handler(bufnr, "TextYankPost", handlers.handle_yanked) + else + error("missing handlers") + end +end + +--- Remove handlers for a specific buffer +---@param bufnr integer +M._remove_handlers = function(bufnr) + vim.api.nvim_clear_autocmds({ group = M.AutoCmdGroup, buffer = bufnr }) +end + +M._add_handler = function(bufnr, event, func) + vim.api.nvim_create_autocmd(event, { + group = group, + buffer = bufnr, + callback = func, + }) +end + +--- Read buffer content as a grid of chars +---@return string[][] +M.read_buf_chars = function() + local bufnr = 0 + local new_cells = {} -- prepare a new frame + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + for row, value in ipairs(lines) do + local result = {} + for col = 1, #value do + local char = value:sub(col, col) + result[col] = char + end + new_cells[row] = result + end + return new_cells +end + +--- customize the per buffer settings +---@return integer bufnr +M._set_buffer_settings = function() + -- TODO: do we need to save/restore buffer settings anymore with a copy of the buffer? + -- local winnr = 0 + -- buffer.buf_settings = { + -- spell = vim.wo[winnr].spell, + -- hlsearch = vim.api.nvim_get_option("hlsearch"), + -- wrap = vim.wo[winnr].wrap, + -- listchars = vim.o.listchars, + -- statusline = vim.o.statusline, + -- laststatus = vim.o.laststatus, + -- } + + local bufnr = 0 + vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1) + + vim.api.nvim_buf_set_option(bufnr, "cursorline", true) + vim.api.nvim_buf_set_option(bufnr, "cursorcolumn", true) + + vim.api.nvim_buf_set_option(bufnr, "spell", false) + vim.cmd.nohlsearch() + vim.cmd("set nowrap") + vim.o.listchars = "" + + -- set the statusline to show in game info + vim.o.statusline = "%!v:lua.require'neohack'.status_line()" + vim.o.laststatus = 3 + + -- TODO: disable code complete + + return bufnr +end + +-- ---comment +-- ---comment +-- ---@param buffer Buffer +-- M._restore_original_settings = function(buffer) +-- local bufnr = 0 +-- vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) +-- +-- if buffer.buf_settings then +-- ---@diagnostic disable-next-line: undefined-field +-- vim.api.nvim_buf_set_option(bufnr, "spell", buffer.buf_settings.spell) +-- ---@diagnostic disable-next-line: undefined-field +-- if buffer.buf_settings.spell then +-- vim.cmd("set spell") +-- end +-- ---@diagnostic disable-next-line: undefined-field +-- if buffer.buf_settings.hlsearch then +-- vim.cmd("set hlsearch") +-- end +-- ---@diagnostic disable-next-line: undefined-field +-- if buffer.buf_settings.wrap then +-- vim.cmd("set wrap") +-- end +-- ---@diagnostic disable-next-line: undefined-field +-- vim.o.listchars = buffer.buf_settings.listchars +-- vim.o.statusline = buffer.buf_settings.status_line +-- vim.o.laststatus = buffer.buf_settings.laststatus +-- end +-- end +-- +return M diff --git a/lua/neohack/vim/vim_message.lua b/lua/neohack/vim/vim_message.lua new file mode 100644 index 0000000..c72c4c7 --- /dev/null +++ b/lua/neohack/vim/vim_message.lua @@ -0,0 +1,44 @@ +local M = { + message_buf = nil, +} + +---comment +M.open = function() + vim.cmd("20 split") + vim.cmd("enew") + M.message_buf = vim.api.nvim_get_current_buf() + vim.api.nvim_command("wincmd p") +end + +---comment +---@param s string +---@return string[] +local function split_string_into_lines(s) + local lines = {} + for line in s:gmatch("([^\n]*)\n?") do + if line ~= "\n" and #line > 0 then + table.insert(lines, line) + end + end + return lines +end + +---comment +---comment +---@param ... any +M.send_to_message_buf = function(...) + local values = { ... } + vim.schedule(function() + for _, value in ipairs(values) do + vim.api.nvim_buf_set_lines(M.message_buf, -1, -1, false, split_string_into_lines(tostring(value))) + end + local win_ids = vim.fn.win_findbuf(M.message_buf) + if #win_ids > 0 then + local win_id = win_ids[1] + local line_count = vim.api.nvim_buf_line_count(M.message_buf) + vim.api.nvim_win_set_cursor(win_id, { line_count, 0 }) + end + end) +end + +return M diff --git a/lua/profile.lua b/lua/profile.lua new file mode 100644 index 0000000..c459407 --- /dev/null +++ b/lua/profile.lua @@ -0,0 +1,193 @@ +local clock = os.clock + +--- The "profile" module controls when to start or stop collecting data and can be used to generate reports. +-- @module profile +-- @alias profile +local profile = {} + +-- function labels +local _labeled = {} +-- function definitions +local _defined = {} +-- time of last call +local _tcalled = {} +-- total execution time +local _telapsed = {} +-- number of calls +local _ncalls = {} +-- list of internal profiler functions +local _internal = {} + +--- This is an internal function. +-- @tparam string event Event type +-- @tparam number line Line number +-- @tparam[opt] table info Debug info table +function profile.hooker(event, line, info) + info = info or debug.getinfo(2, 'fnS') + local f = info.func + -- ignore the profiler itself + if _internal[f] or info.what ~= "Lua" then + return + end + -- get the function name if available + if info.name then + _labeled[f] = info.name + end + -- find the line definition + if not _defined[f] then + _defined[f] = info.short_src..":"..info.linedefined + _ncalls[f] = 0 + _telapsed[f] = 0 + end + if _tcalled[f] then + local dt = clock() - _tcalled[f] + _telapsed[f] = _telapsed[f] + dt + _tcalled[f] = nil + end + if event == "tail call" then + local prev = debug.getinfo(3, 'fnS') + profile.hooker("return", line, prev) + profile.hooker("call", line, info) + elseif event == 'call' then + _tcalled[f] = clock() + else + _ncalls[f] = _ncalls[f] + 1 + end +end + +--- Sets a clock function to be used by the profiler. +-- @tparam function func Clock function that returns a number +function profile.setclock(f) + assert(type(f) == "function", "clock must be a function") + clock = f +end + +--- Starts collecting data. +function profile.start() + if rawget(_G, 'jit') then + jit.off() + jit.flush() + end + debug.sethook(profile.hooker, "cr") +end + +--- Stops collecting data. +function profile.stop() + debug.sethook() + for f in pairs(_tcalled) do + local dt = clock() - _tcalled[f] + _telapsed[f] = _telapsed[f] + dt + _tcalled[f] = nil + end + -- merge closures + local lookup = {} + for f, d in pairs(_defined) do + local id = (_labeled[f] or '?')..d + local f2 = lookup[id] + if f2 then + _ncalls[f2] = _ncalls[f2] + (_ncalls[f] or 0) + _telapsed[f2] = _telapsed[f2] + (_telapsed[f] or 0) + _defined[f], _labeled[f] = nil, nil + _ncalls[f], _telapsed[f] = nil, nil + else + lookup[id] = f + end + end + collectgarbage('collect') +end + +--- Resets all collected data. +function profile.reset() + for f in pairs(_ncalls) do + _ncalls[f] = 0 + end + for f in pairs(_telapsed) do + _telapsed[f] = 0 + end + for f in pairs(_tcalled) do + _tcalled[f] = nil + end + collectgarbage('collect') +end + +--- This is an internal function. +-- @tparam function a First function +-- @tparam function b Second function +-- @treturn boolean True if "a" should rank higher than "b" +function profile.comp(a, b) + local dt = _telapsed[b] - _telapsed[a] + if dt == 0 then + return _ncalls[b] < _ncalls[a] + end + return dt < 0 +end + +--- Generates a report of functions that have been called since the profile was started. +-- Returns the report as a numeric table of rows containing the rank, function label, number of calls, total execution time and source code line number. +-- @tparam[opt] number limit Maximum number of rows +-- @treturn table Table of rows +function profile.query(limit) + local t = {} + for f, n in pairs(_ncalls) do + if n > 0 then + t[#t + 1] = f + end + end + table.sort(t, profile.comp) + if limit then + while #t > limit do + table.remove(t) + end + end + for i, f in ipairs(t) do + local dt = 0 + if _tcalled[f] then + dt = clock() - _tcalled[f] + end + t[i] = { i, _labeled[f] or '?', _ncalls[f], _telapsed[f] + dt, _defined[f] } + end + return t +end + +local cols = { 3, 29, 11, 24, 32 } + +--- Generates a text report of functions that have been called since the profile was started. +-- Returns the report as a string that can be printed to the console. +-- @tparam[opt] number limit Maximum number of rows +-- @treturn string Text-based profiling report +function profile.report(n) + local out = {} + local report = profile.query(n) + for i, row in ipairs(report) do + for j = 1, 5 do + local s = row[j] + local l2 = cols[j] + s = tostring(s) + local l1 = s:len() + if l1 < l2 then + s = s..(' '):rep(l2-l1) + elseif l1 > l2 then + s = s:sub(l1 - l2 + 1, l1) + end + row[j] = s + end + out[i] = table.concat(row, ' | ') + end + + local row = " +-----+-------------------------------+-------------+--------------------------+----------------------------------+ \n" + local col = " | # | Function | Calls | Time | Code | \n" + local sz = row..col..row + if #out > 0 then + sz = sz..' | '..table.concat(out, ' | \n | ')..' | \n' + end + return '\n'..sz..row +end + +-- store all internal profiler functions +for _, v in pairs(profile) do + if type(v) == "function" then + _internal[v] = true + end +end + +return profile \ No newline at end of file diff --git a/tests/action_spec.lua b/tests/action_spec.lua index 20a023d..42bda93 100644 --- a/tests/action_spec.lua +++ b/tests/action_spec.lua @@ -1,15 +1,13 @@ local actions = require("neohack.actions") -local match = require("luassert.match") -local message = require("neohack.message") - ----@diagnostic disable-next-line: undefined-field +local api = require("neohack.api") +local assert = require("luassert") local eq = assert.is.equal actions.leader_key = " " actions.tick = function() print("tick called") end -message.notify_func = print +api.notify = print describe("parse_action", function() it("handles nil", function() @@ -34,13 +32,13 @@ describe("parse_action", function() it("handles help", function() local action, request = actions.parse_action("help") eq(actions.actions.help, action) - match.equal({ action = "help" }, request) + assert.are.same({ action = "help" }, request) end) it("handles help spaces", function() local action, request = actions.parse_action(" help ") eq(actions.actions.help, action) - match.equal({ action = "help" }, request) + assert.are.same({ action = "help" }, request) end) it("handles help synonyms", function() diff --git a/tests/buffer_spec.lua b/tests/buffer_spec.lua index c01dfd2..2329ce9 100644 --- a/tests/buffer_spec.lua +++ b/tests/buffer_spec.lua @@ -1,34 +1,15 @@ -- lsp action, mark describe as defined global local buffer = require("neohack.buffer") local state = require("neohack.state") -local Entity = require("neohack.entity") -local message = require("neohack.message") -local Player = require("neohack.player") local helpers = require("tests.helpers") - -local match = require("luassert.match") ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal -local not_nil = match.is_not_nil - -local function newE(name) - ---@diagnostic disable-next-line: missing-fields - return Entity.new({ char = name, name = "entity " .. name }, 1, 1) -end - -local function set_buffer(cells) - state.current_bufnr = 1 - ---@diagnostic disable-next-line: missing-fields - buffer.buffers[1] = { - cells = cells, - } -end helpers.setup_game() describe("get_entity_at_pos", function() it("handles nil", function() - set_buffer({ + helpers.set_buffer({ cells = { { nil }, }, @@ -39,30 +20,30 @@ describe("get_entity_at_pos", function() end) it("returns the cell", function() - set_buffer({ - { newE("a"), newE("b"), newE("c") }, - { newE("d"), newE("e"), newE("f") }, - { newE("g"), newE("h"), newE("i") }, + helpers.set_buffer({ + { "a", "b", "c" }, + { "d", "e", "f" }, + { "g", "h", "i" }, }) local result = buffer.get_entity_at_pos(2, 2) - not_nil(result) + assert.is_not.equal(nil, result) ---@diagnostic disable-next-line: need-check-nil eq("entity e", result.name) end) it("returns the player", function() - set_buffer({ - { newE("a"), newE("b"), newE("c") }, - { newE("d"), newE("e"), newE("f") }, - { newE("g"), newE("h"), newE("i") }, + helpers.set_buffer({ + { "a", "b", "c" }, + { "d", "e", "f" }, + { "g", "h", "i" }, }) state.player.movement.row = 2 state.player.movement.col = 2 local result = buffer.get_entity_at_pos(2, 2) - not_nil(result) + assert.is_not.equal(nil, result) ---@diagnostic disable-next-line: need-check-nil eq("player", result.name) @@ -70,3 +51,20 @@ describe("get_entity_at_pos", function() eq("entity e", result2.name) end) end) + +describe("cells_to_render", function() + it("gets the chars", function() + helpers.set_buffer({ + { "a", "b", "c" }, + { "d", "e", "f" }, + { "g", "h", "i" }, + }) + + local result = buffer.cells_to_render(buffer.buffers[1].cells) + assert.are.same({ + "abc", + "def", + "ghi", + }, result) + end) +end) diff --git a/tests/chat_spec.lua b/tests/chat_spec.lua index 9b6aafc..46ee53a 100644 --- a/tests/chat_spec.lua +++ b/tests/chat_spec.lua @@ -1,40 +1,42 @@ -local match = require("luassert.match") -local message = require("neohack.message") +local assert = require("luassert") local chat = require("neohack.chat") - -message.notify_func = print +local api = require("neohack.api") +api.notify = print describe("parse_request", function() it("handles nil", function() ---@diagnostic disable-next-line: param-type-mismatch - match.equal(nil, chat.parse_request(nil)) + assert.are.same(nil, chat.parse_request(nil)) end) it("handles empty", function() - match.equal(nil, chat.parse_request("")) + assert.are.same(nil, chat.parse_request("")) end) it("handles spaces", function() - match.equal(nil, chat.parse_request(" ")) + assert.are.same(nil, chat.parse_request(" ")) end) it("handles one word", function() - match.equal({ action = "a" }, chat.parse_request("a")) - match.equal({ action = "a" }, chat.parse_request(" a ")) - match.equal({ action = "a" }, chat.parse_request("a ")) - match.equal({ action = "a" }, chat.parse_request(" a")) - match.equal({ action = "action" }, chat.parse_request(" action ")) + assert.are.same({ action = "a" }, chat.parse_request("a")) + assert.are.same({ action = "a" }, chat.parse_request(" a ")) + assert.are.same({ action = "a" }, chat.parse_request("a ")) + assert.are.same({ action = "a" }, chat.parse_request(" a")) + assert.are.same({ action = "action" }, chat.parse_request(" action ")) end) it("handles two words", function() - match.equal({ action = "a", object = "o" }, chat.parse_request("a o")) - match.equal({ action = "action", object = "object" }, chat.parse_request("action object")) + assert.are.same({ action = "a", object = "o" }, chat.parse_request("a o")) + assert.are.same({ action = "action", object = "object" }, chat.parse_request("action object")) end) it("handles three words", function() - match.equal({ action = "a", object = "o", target = "t" }, chat.parse_request("a o t")) - match.equal({ action = "action", object = "object", target = "target" }, chat.parse_request("action object target")) - match.equal( + assert.are.same({ action = "a", object = "o", target = "t" }, chat.parse_request("a o t")) + assert.are.same( + { action = "action", object = "object", target = "target" }, + chat.parse_request("action object target") + ) + assert.are.same( { action = "action", object = "object", target = "target" }, chat.parse_request(" action object target ") ) diff --git a/tests/components/combat_spec.lua b/tests/components/combat_spec.lua index dfc3e24..d68216a 100644 --- a/tests/components/combat_spec.lua +++ b/tests/components/combat_spec.lua @@ -1,7 +1,6 @@ local Combat = require("neohack.components.combat") local helpers = require("tests.helpers") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("combat", function() diff --git a/tests/components/decision_spec.lua b/tests/components/decision_spec.lua index 8184ff4..93bf2ea 100644 --- a/tests/components/decision_spec.lua +++ b/tests/components/decision_spec.lua @@ -1,8 +1,7 @@ local helpers = require("tests.helpers") local Decision = require("neohack.components.decision") local Def = require("neohack.def") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("decision", function() diff --git a/tests/components/eat_spec.lua b/tests/components/eat_spec.lua index e578434..f08224f 100644 --- a/tests/components/eat_spec.lua +++ b/tests/components/eat_spec.lua @@ -1,8 +1,7 @@ local helpers = require("tests.helpers") local Eat = require("neohack.components.eat") local defs = require("neohack.defs") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("sneak", function() diff --git a/tests/components/fuse_spec.lua b/tests/components/fuse_spec.lua index f9c2e32..bf5f258 100644 --- a/tests/components/fuse_spec.lua +++ b/tests/components/fuse_spec.lua @@ -1,8 +1,7 @@ local helpers = require("tests.helpers") local Fuse = require("neohack.components.fuse") local defs = require("neohack.defs") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("fuse", function() diff --git a/tests/components/health_spec.lua b/tests/components/health_spec.lua index b69b621..1c0d7a7 100644 --- a/tests/components/health_spec.lua +++ b/tests/components/health_spec.lua @@ -1,7 +1,7 @@ local Health = require("neohack.components.health") local helpers = require("tests.helpers") ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("health", function() diff --git a/tests/components/inventory_spec.lua b/tests/components/inventory_spec.lua index 9a138ec..31496fe 100644 --- a/tests/components/inventory_spec.lua +++ b/tests/components/inventory_spec.lua @@ -6,9 +6,7 @@ local generated_defs = require("neohack.generated_defs") local attributes = require("neohack.attribute_getters") local helpers = require("tests.helpers") local Player = require("neohack.player") - -local match = require("luassert.match") ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal helpers.setup_game() @@ -31,19 +29,19 @@ describe("pickup", function() local item = defs.new_entity_from_char("!", 1, 1) inventory:pickup(item) eq(item, inventory:get_items()[1]) - eq("1:long_sword\n", inventory:get_inventory_item_with_index()) + assert.is.same({ "1:long_sword" }, inventory:get_inventory_item_with_index()) end) it("get_spell_items", function() local inventory = Inventory.new(helpers.new_entity("one")) - state.current_floor = 1 + state.current_level = 1 generated_defs.init() local item = Entity.new(defs.tome, 1, 1) item.speak.spell = generated_defs.generate_spell() inventory:pickup(item) eq(item, inventory:get_items()[1]) - match.equal({}, inventory:get_spell_items()) + assert.are.same({ item }, inventory:get_spell_items()) end) it("retrieve_item_char", function() @@ -51,7 +49,7 @@ describe("pickup", function() local item = defs.new_entity_from_char("!", 1, 1) inventory:pickup(item) eq(item, inventory:get_items()[1]) - match.equal(item, inventory:retrieve_item_char("!")) + assert.are.same(item, inventory:retrieve_item_char("!")) end) it("retrieve_items", function() @@ -59,7 +57,7 @@ describe("pickup", function() local item = defs.new_entity_from_char("!", 1, 1) inventory:pickup(item) eq(item, inventory:get_items()[1]) - match.equal(item, inventory:retrieve_items({ "long_sword" })[1]) + assert.are.same(item, inventory:retrieve_items({ "long_sword" })[1]) end) end) @@ -124,7 +122,7 @@ describe("pilfer", function() it("pilfer", function() local player = Player.new() - local one = defs.new_entity_from_char("$", 1, 1) + local one = defs.new_entity_from_char("x", 1, 1) local two = defs.new_entity_from_char("!", 1, 1) local three = defs.new_entity_from_char("l", 1, 1) @@ -138,11 +136,11 @@ describe("pilfer", function() player.inventory:pickup(one) eq(1, #player.inventory.items) - player.inventory:pilfer("coin") + player.inventory:pilfer("unknown_corpse") eq(3, #player.inventory.items) - eq("coin", player.inventory.items[1].name) - eq("wooden_leg", player.inventory.items[2].name) - eq("long_sword", player.inventory.items[3].name) + eq("wooden_leg", player.inventory.items[1].name) + eq("long_sword", player.inventory.items[2].name) + eq("unknown_corpse", player.inventory.items[3].name) end) end) diff --git a/tests/components/movement_spec.lua b/tests/components/movement_spec.lua index 626b5f7..7d0d2de 100644 --- a/tests/components/movement_spec.lua +++ b/tests/components/movement_spec.lua @@ -1,8 +1,7 @@ local helpers = require("tests.helpers") local Movement = require("neohack.components.movement") local moves = require("neohack.moves") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("movement", function() @@ -10,7 +9,7 @@ describe("movement", function() describe("attributes", function() it("has defaults", function() - local one = Movement.new(helpers.new_entity("one"), "o", moves.four, 1, 2, false) + local one = Movement.new(helpers.new_entity("one"), "o", moves.four, 1, 2, false, 0.5) eq("o", one.char) eq(moves.four, one.moves) eq(1, one.row) @@ -19,6 +18,7 @@ describe("movement", function() eq(0.01, one.parent.attributes.scared) eq(false, one.seen) eq(false, one.parent.attributes.block_vision) + eq(0.5, one.parent.attributes.speed) end) end) end) diff --git a/tests/components/sneak_spec.lua b/tests/components/sneak_spec.lua index 0e76dd1..164fb01 100644 --- a/tests/components/sneak_spec.lua +++ b/tests/components/sneak_spec.lua @@ -1,7 +1,6 @@ local helpers = require("tests.helpers") local Sneak = require("neohack.components.sneak") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("sneak", function() diff --git a/tests/components/speak_spec.lua b/tests/components/speak_spec.lua index eebfade..975bde1 100644 --- a/tests/components/speak_spec.lua +++ b/tests/components/speak_spec.lua @@ -3,8 +3,7 @@ local Speak = require("neohack.components.speak") local state = require("neohack.state") local Def = require("neohack.def") local Spell = require("neohack.spells") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("speak", function() @@ -61,7 +60,7 @@ describe("speak", function() } spell.type = Def.DefType.item one.parent.inventory:pickup(spell) - eq("1:spell\n", one.parent.inventory:get_inventory_item_with_index()) + assert.is.same({ "1:spell" }, one.parent.inventory:get_inventory_item_with_index()) local casting, effects = one:spells_spoken("magic_word") eq(1, #casting) diff --git a/tests/components/vision_spec.lua b/tests/components/vision_spec.lua index bda957c..6d386a2 100644 --- a/tests/components/vision_spec.lua +++ b/tests/components/vision_spec.lua @@ -1,8 +1,7 @@ local helpers = require("tests.helpers") local Vision = require("neohack.components.vision") local defs = require("neohack.defs") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("vision", function() diff --git a/tests/entity_spec.lua b/tests/entity_spec.lua index bc1543a..c0bb7ce 100644 --- a/tests/entity_spec.lua +++ b/tests/entity_spec.lua @@ -1,7 +1,6 @@ local helpers = require("tests.helpers") local utils = require("neohack.utils") - ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal describe("entity", function() @@ -9,7 +8,7 @@ describe("entity", function() it("inspect", function() eq( - "name=one char=o row=1 col=1 health=10 durability=10 damage=1 weapon_damage=1 hit_rate=0.5 dodge_skill=0.1 weapon_skill=0.1 deflect_skill=0.01 sneak_skill=0.5 fuse_skill=0.5 eat_skill=0.1 view_distance=10 search_skill=0.5 vision=0.5 inscription= randomness=0.1 wearing: \nitems:\n ", + "name=one char=o row=1 col=1 health=10 durability=10 damage=1 weapon_damage=1 hit_rate=0.5 dodge_skill=0.1 weapon_skill=0.1 deflect_skill=0.01 sneak_skill=0.5 fuse_skill=0.5 eat_skill=0.1 view_distance=10 search_skill=0.5 vision=0.5 inscription= randomness=0.1 {wearing: ,items:,} ", helpers.new_entity("one"):inspect() ) end) @@ -18,6 +17,6 @@ describe("entity", function() local attrs = helpers.new_entity("one"):get_attributes() eq("number", type(attrs["health"])) eq(10, attrs["health"]) - eq(19, #utils.keys(attrs)) + eq(20, #utils.keys(attrs)) end) end) diff --git a/tests/fuser_spec.lua b/tests/fuser_spec.lua index f906fe8..e9b540e 100644 --- a/tests/fuser_spec.lua +++ b/tests/fuser_spec.lua @@ -1,15 +1,12 @@ -local match = require("luassert.match") -local message = require("neohack.message") local fuse = require("neohack.fuser") local defs = require("neohack.defs") - ----@diagnostic disable-next-line: undefined-field +local helpers = require("tests.helpers") +local assert = require("luassert") local eq = assert.is.equal -local not_nil = match.is_not_nil - -message.notify_func = print describe("fuse", function() + helpers.setup_game() + it("fails", function() local one = defs.new_entity_from_char("!", 1, 1) local two = defs.new_entity_from_char("l", 2, 2) @@ -21,7 +18,7 @@ describe("fuse", function() local one = defs.new_entity_from_char("!", 1, 1) local two = defs.new_entity_from_char("l", 2, 2) local result = fuse.fuse_entities(one, two, 0.01, 0, 0) - not_nil(result) + assert.is_not.equal(nil, result) if result then eq("F", result.movement.char) eq(3, result.attributes.damage) @@ -32,7 +29,7 @@ describe("fuse", function() local one = defs.new_entity_from_char("!", 1, 1) local two = defs.new_entity_from_char("l", 2, 2) local result = fuse.fuse_entities(one, two, 100, 0.001, 0.1) - not_nil(result) + assert.is_not.equal(nil, result) if result then eq("F", result.movement.char) eq(5, result.attributes.damage) diff --git a/tests/generated_defs_spec.lua b/tests/generated_defs_spec.lua index bb7146d..75bbdab 100644 --- a/tests/generated_defs_spec.lua +++ b/tests/generated_defs_spec.lua @@ -1,23 +1,21 @@ -local match = require("luassert.match") +local assert = require("luassert") local generated_defs = require("neohack.generated_defs") local helpers = require("tests.helpers") local utils = require("neohack.utils") ----@diagnostic disable-next-line: undefined-field local eq = assert.is.equal -local not_nil = match.is_not_nil describe("attributes", function() helpers.setup_game() it("example_entity", function() local attrs = generated_defs.example_entity():get_attributes() - eq(20, #utils.keys(attrs)) + eq(21, #utils.keys(attrs)) eq("number", type(attrs.damage)) end) it("numeric_attributes", function() local attrs = generated_defs.numeric_attributes() - eq(15, #utils.keys(attrs)) + eq(16, #utils.keys(attrs)) end) end) @@ -48,12 +46,13 @@ describe("allocate_portions", function() it("portions", function() local portions = generated_defs.allocate_portions(10) - not_nil(portions.health) - not_nil(portions.damage) - not_nil(portions.durability) - not_nil(portions.hit_rate) - not_nil(portions.randomness) - not_nil(portions.vision) + assert.is_not.equal(nil, portions.health) + assert.is_not.equal(nil, portions.damage) + assert.is_not.equal(nil, portions.durability) + assert.is_not.equal(nil, portions.hit_rate) + assert.is_not.equal(nil, portions.randomness) + assert.is_not.equal(nil, portions.vision) + assert.is_not.equal(nil, portions.speed) end) end) @@ -64,13 +63,24 @@ describe("generate_effect", function() it("cast effect attribute", function() ---@type Effect local effect = generated_defs.generate_effect() - match.is_not_nil(effect.name) + assert.is_not.equal(nil, effect.name) local entity = helpers.new_entity("one") local attrs = entity:get_attributes() - match.equal(attrs, entity:get_attributes()) + assert.are.same(attrs, entity:get_attributes()) effect.cast(entity, 1) -- some attribute was affected by the spell - match.not_equal(attrs, entity:get_attributes()) + assert.are_not.same(attrs, entity:get_attributes()) + end) +end) + +describe("generate_speed", function() + math.randomseed(12345) + helpers.setup_game() + + it("speed attribute", function() + local attrs = generated_defs.example_entity():get_attributes() + eq("number", type(attrs.speed)) + assert.is_not.equal(0, attrs.speed) end) end) diff --git a/tests/helpers.lua b/tests/helpers.lua index 5511f0d..bdf3644 100644 --- a/tests/helpers.lua +++ b/tests/helpers.lua @@ -1,18 +1,20 @@ -local message = require("neohack.message") local state = require("neohack.state") +local Def = require("neohack.def") local buffer = require("neohack.buffer") -local Health = require("neohack.components.health") -local Inventory = require("neohack.components.inventory") -local Combat = require("neohack.components.combat") -local Sneak = require("neohack.components.sneak") -local Fuse = require("neohack.components.fuse") -local Vision = require("neohack.components.vision") -local Eat = require("neohack.components.eat") -local Movement = require("neohack.components.movement") -local moves = require("neohack.moves") +-- local Health = require("neohack.components.health") +-- local Inventory = require("neohack.components.inventory") +-- local Combat = require("neohack.components.combat") +-- local Sneak = require("neohack.components.sneak") +-- local Fuse = require("neohack.components.fuse") +-- local Vision = require("neohack.components.vision") +-- local Eat = require("neohack.components.eat") +-- local Movement = require("neohack.components.movement") +-- local moves = require("neohack.moves") local generated_defs = require("neohack.generated_defs") local Entity = require("neohack.entity") local Player = require("neohack.player") +local api = require("neohack.api") +local vim_render = require("neohack.vim.render") local M = {} @@ -46,9 +48,10 @@ M.word_list = { } M.setup_game = function() - message.notify_func = print - state.current_floor = 1 - state.current_bufnr = 1 + api.notify = print + api.debug = print + api.render = vim_render + state.current_level = 1 ---@diagnostic disable-next-line: missing-fields buffer.buffers[1] = { cells = { { @@ -119,4 +122,57 @@ M.new_entity = function(name) return Entity.new(def, 1, 1) end +M.level = 1 + +---comment +---@param chars string[][] +M.set_buffer = function(chars) + state.current_level = M.level + ---@diagnostic disable-next-line: missing-fields + buffer.buffers[M.level] = { + cells = M.toEntities(chars), + } +end + +---comment +---@param chars string[][] +---@return Entity[][] +M.toEntities = function(chars) + local entities = {} + for i, row in ipairs(chars) do + local entity_row = {} + for j, char in ipairs(row) do + table.insert(entity_row, M.newE(char, i, j)) + end + table.insert(entities, entity_row) + end + return entities +end + +M.enemy = "E" +M.friend = "F" + +---comment +---@param name string +---@return Entity +M.newE = function(name, row, col) + local type = Def.DefType.item + if name == M.enemy or name == "x" then + type = Def.DefType.enemy + elseif name == M.friend then + type = Def.DefType.friend + elseif name == " " then + type = Def.DefType.floor + elseif name == "P" then + type = Def.DefType.player + end + ---@diagnostic disable-next-line: missing-fields + return Entity.new({ + char = name, + name = "entity " .. name, + type = type, + block_vision = true, + }, row, col) +end + return M diff --git a/tests/highlight_spec.lua b/tests/highlight_spec.lua new file mode 100644 index 0000000..1c46bf9 --- /dev/null +++ b/tests/highlight_spec.lua @@ -0,0 +1,35 @@ +local helpers = require("tests.helpers") +local highlight = require("neohack.highlight") +local assert = require("luassert") +local Def = require("neohack.def") + +---@diagnostic disable-next-line: undefined-field + +describe("highlight", function() + helpers.setup_game() + + describe("generate_rgb", function() + it("generates 0,255", function() + local entity = helpers.new_entity("one") + + entity.type = Def.DefType.item + local rgb = highlight.generate_rgb(entity) + assert.are.same({ red = 65, green = 255, blue = 78 }, rgb) + + entity.type = Def.DefType.terrain + rgb = highlight.generate_rgb(entity) + assert.are.same(nil, rgb) + + entity.type = Def.DefType.enemy + rgb = highlight.generate_rgb(entity) + assert.are.same({ red = 155, green = 255, blue = 78 }, rgb) + + entity.attributes.health = 0 + entity.attributes.durability = 0 + entity.attributes.durability = 0 + + rgb = highlight.generate_rgb(entity) + assert.are.same({ red = 155, green = 0, blue = 78 }, rgb) + end) + end) +end) diff --git a/tests/love2d/vikeys_spec.lua b/tests/love2d/vikeys_spec.lua new file mode 100644 index 0000000..34c78ea --- /dev/null +++ b/tests/love2d/vikeys_spec.lua @@ -0,0 +1,79 @@ +local assert = require("luassert") +local vikeys = require("neohack.love2d.vikeys") +local helpers = require("tests.helpers") + +helpers.setup_game() + +describe("vikeys", function() + it("word", function() + local cells = { + { "E", " ", "E" }, + { " ", "E", " " }, + { "E", "E", "E" }, + } + helpers.set_buffer(cells) + assert.are.same({ 1, 3 }, { vikeys.word(1, 1) }) + assert.are.same({ 1, 3 }, { vikeys.word(1, 2) }) + assert.are.same({ 3, 1 }, { vikeys.word(2, 2) }) + assert.are.same({ 2, 2 }, { vikeys.word(1, 3) }) + assert.are.same({ nil, nil }, { vikeys.word(3, 3) }) + end) + + it("back_word", function() + local cells = { + { "E", " ", "E" }, + { " ", "E", " " }, + { "E", "E", "E" }, + } + helpers.set_buffer(cells) + assert.are.same({ nil, nil }, { vikeys.back_word(1, 1) }) + assert.are.same({ 1, 1 }, { vikeys.back_word(1, 2) }) + assert.are.same({ 1, 1 }, { vikeys.back_word(1, 3) }) + assert.are.same({ 1, 3 }, { vikeys.back_word(2, 2) }) + assert.are.same({ 2, 2 }, { vikeys.back_word(3, 3) }) + end) + + it("find_pattern", function() + local cells = { + { "a", "b", "c", " " }, + { " ", "a", "b" }, + { "b", "a", "a", "b" }, + } + helpers.set_buffer(cells) + assert.are.same({ nil, nil }, { vikeys.find_pattern(1, 1, "d") }) + assert.are.same({ 1, 2 }, { vikeys.find_pattern(1, 1, "b") }) + assert.are.same({ nil, nil }, { vikeys.find_pattern(1, 2, "abc") }) + assert.are.same({ 2, 2 }, { vikeys.find_pattern(1, 3, "ab") }) + assert.are.same({ 3, 3 }, { vikeys.find_pattern(2, 2, "ab") }) + assert.are.same({ 3, 3 }, { vikeys.find_pattern(3, 1, "ab") }) + assert.are.same({ nil, nil }, { vikeys.find_pattern(3, 3, "ab") }) + + assert.are.same({ 2, 1 }, { vikeys.find_pattern(1, 1, ".ab") }) + assert.are.same({ 1, 2 }, { vikeys.find_pattern(1, 1, "a*b") }) + assert.are.same({ 3, 2 }, { vikeys.find_pattern(3, 1, "a*b") }) + end) + + it("back_find_pattern", function() + local cells = { + { "a", "b", "c", " " }, + { " ", "a", "b" }, + { "b", "a", "a", "b" }, + } + helpers.set_buffer(cells) + assert.are.same({ nil, nil }, { vikeys.back_find_pattern(3, 4, "d") }) + assert.are.same({ 3, 1 }, { vikeys.back_find_pattern(3, 4, "b") }) + assert.are.same({ 1, 2 }, { vikeys.back_find_pattern(1, 4, "b") }) + assert.are.same({ 2, 3 }, { vikeys.back_find_pattern(3, 1, "b") }) + assert.are.same({ nil, nil }, { vikeys.back_find_pattern(1, 1, "abc") }) + assert.are.same({ 2, 2 }, { vikeys.back_find_pattern(3, 3, "ab") }) + -- TODO: vim will include cursor here, not sure how to replicate + -- assert.are.same({ 3, 3 }, { vikeys.back_find_pattern(3, 4, "ab") }) + assert.are.same({ 2, 2 }, { vikeys.back_find_pattern(3, 4, "ab") }) + + assert.are.same({ 2, 1 }, { vikeys.back_find_pattern(3, 1, ".ab") }) + assert.are.same({ 1, 2 }, { vikeys.back_find_pattern(2, 1, "a*b") }) + -- TODO; same here + -- assert.are.same({ 3, 2 }, { vikeys.back_find_pattern(3, 4, "a*b") }) + assert.are.same({ 3, 1 }, { vikeys.back_find_pattern(3, 4, "a*b") }) + end) +end) diff --git a/tests/map_spec.lua b/tests/map_spec.lua index 147b497..2095135 100644 --- a/tests/map_spec.lua +++ b/tests/map_spec.lua @@ -1,86 +1,29 @@ -- lsp action, mark describe as defined global local map = require("neohack.map") -local Entity = require("neohack.entity") -local message = require("neohack.message") local buffer = require("neohack.buffer") local state = require("neohack.state") -local Def = require("neohack.def") +local helpers = require("tests.helpers") -local match = require("luassert.match") ----@diagnostic disable-next-line: undefined-field +local assert = require("luassert") local eq = assert.is.equal -local not_nil = match.is_not_nil - -message.notify_func = print - -local enemy = "E" -local friend = "F" - ----comment ----@param name string ----@return Entity -local function newE(name, row, col) - local type = Def.DefType.item - if name == enemy or name == "x" then - type = Def.DefType.enemy - elseif name == friend then - type = Def.DefType.friend - elseif name == " " then - type = Def.DefType.floor - elseif name == "P" then - type = Def.DefType.player - end - ---@diagnostic disable-next-line: missing-fields - return Entity.new({ - char = name, - name = "entity " .. name, - type = type, - block_vision = true, - }, row, col) -end - ----comment ----@param chars string[][] ----@return Entity[][] -local function toEntities(chars) - local entities = {} - for i, row in ipairs(chars) do - local entity_row = {} - for j, char in ipairs(row) do - table.insert(entity_row, newE(char, i, j)) - end - table.insert(entities, entity_row) - end - return entities -end ---comment ---@param expected table[] ---@param found table[]? local function assertFound(expected, found) - not_nil(found) + assert.is_not.equal(nil, found) if found then eq(#expected, #found) for i, e in ipairs(expected) do local row = found[i].movement.row local col = found[i].movement.col - eq(e[1], row, "wrong row " .. row .. " expected " .. e[1] .. "," .. e[2]) - eq(e[2], col, "wrong col " .. col .. " expected " .. e[1] .. "," .. e[2]) + assert.are.same({ row, col }, { e[1], e[2] }) + -- eq(e[1], row, "wrong row " .. row .. " expected " .. e[1] .. "," .. e[2]) + -- eq(e[2], col, "wrong col " .. col .. " expected " .. e[1] .. "," .. e[2]) end end end -local bufnr = 1 ----comment ----@param chars string[][] -local function set_buffer(chars) - state.current_bufnr = bufnr - ---@diagnostic disable-next-line: missing-fields - buffer.buffers[bufnr] = { - cells = toEntities(chars), - } -end - -- describe("find_deleted_positions", function() -- it("finds deleted", function() -- local old = { @@ -139,6 +82,8 @@ local function as_chars(result) end describe("find_all_matches", function() + helpers.setup_game() + it("finds single char", function() local cells = { { "a", "b", "c" }, @@ -148,14 +93,14 @@ describe("find_all_matches", function() { "m", "n", "o" }, { "p", "q", "p" }, } - eq("a\n", as_chars(map.find_all_matches(toEntities(cells), { "a" }))) + eq("a\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "a" }))) - eq("b\n", as_chars(map.find_all_matches(toEntities(cells), { "b" }))) + eq("b\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "b" }))) - eq("c\n", as_chars(map.find_all_matches(toEntities(cells), { "c" }))) + eq("c\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "c" }))) - eq("p\np\n", as_chars(map.find_all_matches(toEntities(cells), { "p" }))) - eq("", as_chars(map.find_all_matches(toEntities(cells), { "z" }))) + eq("p\np\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "p" }))) + eq("", as_chars(map.find_all_matches(helpers.toEntities(cells), { "z" }))) end) it("finds multi char", function() @@ -167,11 +112,11 @@ describe("find_all_matches", function() { "m", "a", "a" }, { "a", "q", "a" }, } - eq("a\na\na\na\na\na\na\na\na\na\na\n", as_chars(map.find_all_matches(toEntities(cells), { "a" }))) - eq("aa\naa\naa\naa\naa\n", as_chars(map.find_all_matches(toEntities(cells), { "aa" }))) - eq("aaa\n", as_chars(map.find_all_matches(toEntities(cells), { "aaa" }))) - eq("", as_chars(map.find_all_matches(toEntities(cells), { "aaaa" }))) - eq("", as_chars(map.find_all_matches(toEntities(cells), { "cd" }))) + eq("a\na\na\na\na\na\na\na\na\na\na\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "a" }))) + eq("aa\naa\naa\naa\naa\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "aa" }))) + eq("aaa\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "aaa" }))) + eq("", as_chars(map.find_all_matches(helpers.toEntities(cells), { "aaaa" }))) + eq("", as_chars(map.find_all_matches(helpers.toEntities(cells), { "cd" }))) end) it("finds multi line", function() @@ -183,9 +128,9 @@ describe("find_all_matches", function() { "m", "a", "a" }, { "a", "q", "a" }, } - eq("aa\naa\naa\naa\n", as_chars(map.find_all_matches(toEntities(cells), { "a", "a" }))) + eq("aa\naa\naa\naa\n", as_chars(map.find_all_matches(helpers.toEntities(cells), { "a", "a" }))) - local found_1 = map.find_all_matches(toEntities(cells), { "aa", "a" }) + local found_1 = map.find_all_matches(helpers.toEntities(cells), { "aa", "a" }) eq("aaa\naaa\n", as_chars(found_1)) eq(2, #found_1) assertFound({ @@ -199,16 +144,16 @@ describe("find_all_matches", function() { 6, 1 }, }, found_1[2]) - local found_2 = map.find_all_matches(toEntities(cells), { "l", "m" }) + local found_2 = map.find_all_matches(helpers.toEntities(cells), { "l", "m" }) eq("lm\n", as_chars(found_2)) eq(1, #found_2) assertFound({ { 4, 3 }, { 5, 1 } }, found_2[1]) -- no line split, so doesn't match - local found_3 = map.find_all_matches(toEntities(cells), { "lm" }) + local found_3 = map.find_all_matches(helpers.toEntities(cells), { "lm" }) eq("", as_chars(found_3)) eq(0, #found_3) - assertFound({}, found_3[1]) + eq(nil, found_3[1]) end) it("finds over line single", function() @@ -216,7 +161,7 @@ describe("find_all_matches", function() { "a" }, { "b" }, } - local found_1 = map.find_all_matches(toEntities(cells), { "a", "b" }) + local found_1 = map.find_all_matches(helpers.toEntities(cells), { "a", "b" }) eq("ab\n", as_chars(found_1)) eq(1, #found_1) assertFound({ @@ -230,7 +175,7 @@ describe("find_all_matches", function() { "a", "b" }, { "c", "d" }, } - local found_1 = map.find_all_matches(toEntities(cells), { "ab", "cd" }) + local found_1 = map.find_all_matches(helpers.toEntities(cells), { "ab", "cd" }) eq("abcd\n", as_chars(found_1)) eq(1, #found_1) assertFound({ @@ -246,7 +191,7 @@ describe("find_all_matches", function() { "a", "b", "c" }, { "d", "e", "f" }, } - local found_1 = map.find_all_matches(toEntities(cells), { "abc", "def" }) + local found_1 = map.find_all_matches(helpers.toEntities(cells), { "abc", "def" }) eq("abcdef\n", as_chars(found_1)) eq(1, #found_1) assertFound({ @@ -264,7 +209,7 @@ describe("find_all_matches", function() { "a", "b", "c" }, { "d", "e", "f" }, } - local found_1 = map.find_all_matches(toEntities(cells), { "bc", "de" }) + local found_1 = map.find_all_matches(helpers.toEntities(cells), { "bc", "de" }) eq("bcde\n", as_chars(found_1)) eq(1, #found_1) assertFound({ @@ -286,15 +231,15 @@ describe("find_closest_match", function() { "m", "n", "o" }, { "p", "q", "p" }, } - set_buffer(cells) + helpers.set_buffer(cells) local row = 1 local col = 1 - local found = map.find_closest_match(bufnr, { "a" }, row, col) + local found = map.find_closest_match(helpers.level, { "a" }, row, col) assertFound({ { 1, 1 } }, found) - local found_last = map.find_closest_match(bufnr, { "p" }, row, col) + local found_last = map.find_closest_match(helpers.level, { "p" }, row, col) assertFound({ { 6, 1 } }, found_last) end) @@ -308,12 +253,12 @@ describe("find_closest_match", function() { "a", "q", "a" }, } - set_buffer(cells) + helpers.set_buffer(cells) - local found = map.find_closest_match(bufnr, { "aa" }, 1, 1) + local found = map.find_closest_match(helpers.level, { "aa" }, 1, 1) assertFound({ { 1, 1 }, { 1, 2 } }, found) - local found_last = map.find_closest_match(bufnr, { "aa" }, 6, 3) + local found_last = map.find_closest_match(helpers.level, { "aa" }, 6, 3) assertFound({ { 5, 2 }, { 5, 3 } }, found_last) end) @@ -327,16 +272,16 @@ describe("find_closest_match", function() { "a", "q", "a" }, } - set_buffer(cells) + helpers.set_buffer(cells) - local found = map.find_closest_match(bufnr, { "aa", "a" }, 1, 1) + local found = map.find_closest_match(helpers.level, { "aa", "a" }, 1, 1) assertFound({ { 2, 2 }, { 2, 3 }, { 3, 1 }, }, found) - local found_last = map.find_closest_match(bufnr, { "aa", "a" }, 6, 3) + local found_last = map.find_closest_match(helpers.level, { "aa", "a" }, 6, 3) assertFound({ { 5, 2 }, { 5, 3 }, @@ -354,10 +299,10 @@ describe("scan_buf", function() { " ", "E", " ", "F", " " }, { "F", " ", " ", "x", "F" }, } - set_buffer(cells) + helpers.set_buffer(cells) - local enemies, friends, first_floor = map.scan_buf(bufnr) - match.equal({ row = 1, col = 3 }, first_floor) + local enemies, friends, first_floor = map.scan_buf(helpers.level) + assert.are.same({ row = 1, col = 3 }, first_floor) assertFound({ { 1, 5 }, { 2, 2 }, { 4, 2 } }, enemies) assertFound({ { 1, 1 }, { 2, 4 }, { 3, 3 }, { 4, 4 }, { 5, 1 }, { 5, 5 } }, friends) end) @@ -372,20 +317,47 @@ describe("scan_area", function() { " ", "E", " ", "I", " " }, { "I", " ", " ", " ", "I" }, } - set_buffer(cells) + helpers.set_buffer(cells) local row = 3 local col = 3 - local enemies, items = map.scan_area(bufnr, row, col, 1) + local enemies, items = map.scan_area(helpers.level, row, col, 1) assertFound({ { 2, 2 }, { 4, 2 } }, enemies) assertFound({ { 2, 4 }, { 3, 3 }, { 4, 4 } }, items) end) end) +-- describe("get_visible_entities", function() +-- it("finds things with manhattan_distance", function() +-- state.player = helpers.newE("P", 2, 3) +-- local cells = { +-- { "I", " ", " ", "x", "E" }, +-- { " ", "E", " ", "I", "x" }, +-- { "x", " ", "I", " ", " " }, +-- { " ", "E", " ", "I", " " }, +-- { "I", " ", " ", " ", "I" }, +-- } +-- helpers.set_buffer(cells) +-- local entity = buffer.buffers[1].cells[3][3] +-- +-- local found1 = map.get_visible_entities(helpers.level, entity, 1) +-- eq(1, #found1) +-- assertFound({ { 2, 3 } }, found1) +-- +-- local found2 = map.get_visible_entities(helpers.level, entity, 2) +-- eq(6, #found2) +-- assertFound({ { 2, 2 }, { 2, 3 }, { 2, 4 }, { 3, 1 }, { 4, 2 }, { 4, 4 } }, found2) +-- +-- local found3 = map.get_visible_entities(helpers.level, entity, 3) +-- eq(8, #found3) +-- assertFound({ { 1, 4 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 3, 1 }, { 4, 2 }, { 4, 4 } }, found3) +-- end) +-- end) + describe("get_visible_entities", function() it("finds things", function() - state.player = newE("P", 2, 3) + state.player = helpers.newE("P", 2, 3) local cells = { { "I", " ", " ", "x", "E" }, { " ", "E", " ", "I", "x" }, @@ -393,19 +365,19 @@ describe("get_visible_entities", function() { " ", "E", " ", "I", " " }, { "I", " ", " ", " ", "I" }, } - set_buffer(cells) + helpers.set_buffer(cells) local entity = buffer.buffers[1].cells[3][3] - local found1 = map.get_visible_entities(bufnr, entity, 1) - eq(1, #found1) - assertFound({ { 2, 3 } }, found1) + local found0 = map.get_visible_entities(helpers.level, entity, 0) + assertFound({}, found0) + + local found1 = map.get_visible_entities(helpers.level, entity, 1) + assertFound({ { 2, 2 }, { 2, 3 }, { 2, 4 }, { 4, 2 }, { 4, 4 } }, found1) - local found2 = map.get_visible_entities(bufnr, entity, 2) - eq(6, #found2) - assertFound({ { 2, 2 }, { 2, 3 }, { 2, 4 }, { 3, 1 }, { 4, 2 }, { 4, 4 } }, found2) + local found2 = map.get_visible_entities(helpers.level, entity, 2) + assertFound({ { 1, 4 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 3, 1 }, { 4, 2 }, { 4, 4 } }, found2) - local found3 = map.get_visible_entities(bufnr, entity, 3) - eq(8, #found3) + local found3 = map.get_visible_entities(helpers.level, entity, 3) assertFound({ { 1, 4 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 3, 1 }, { 4, 2 }, { 4, 4 } }, found3) end) end) diff --git a/tests/player_spec.lua b/tests/player_spec.lua index e8110e9..7e92e02 100644 --- a/tests/player_spec.lua +++ b/tests/player_spec.lua @@ -58,7 +58,7 @@ end) describe("inventory", function() local player = Player.new() it("get_inventory", function() - eq("wearing: \nitems:\n", player.inventory:get_inventory()) + assert.is.same({ "wearing: ", "items:" }, player.inventory:get_inventory()) end) it("get_weapon", function() @@ -79,7 +79,7 @@ describe("inspect", function() eq(0.1, player.eat:eat_skill()) eq(0.05, player.combat:deflect_skill()) eq( - "name=player char= row=0 col=0 health=10 durability=20 damage=1 weapon_damage=1 hit_rate=0.5 dodge_skill=0.5 weapon_skill=0.5 deflect_skill=0.05 sneak_skill=0.5 fuse_skill=0.5 eat_skill=0.1 view_distance=10 search_skill=0.5 vision=0.3 inscription= randomness=0.01 wearing: \nitems:\n turns:0", + "name=player char= row=0 col=0 health=10 durability=20 damage=1 weapon_damage=1 hit_rate=0.5 dodge_skill=0.5 weapon_skill=0.5 deflect_skill=0.05 sneak_skill=0.5 fuse_skill=0.5 eat_skill=0.1 view_distance=10 search_skill=0.5 vision=0.3 inscription= randomness=0.01 {wearing: ,items:,} turns:0", player:inspect() ) end)