ansi-terminal-game 0.2.1.0 → 1.9.4.0
raw patch · 44 files changed
Files
- AUTHORS +3/−0
- COPYING +674/−0
- LICENSE +0/−674
- NEWS +441/−0
- README +52/−0
- ansi-terminal-game.cabal +152/−42
- changes.txt +0/−20
- example/Alone.hs +62/−42
- example/Balls.hs +188/−0
- example/MainAlone.hs +12/−0
- example/MainBalls.hs +22/−0
- example/MainHotReload.hs +87/−0
- example/MainPlayback.hs +30/−0
- platform-dep/non-win/Terminal/Game/Input.hs +0/−9
- platform-dep/non-win/Terminal/Game/Utils.hs +14/−0
- platform-dep/windows/Terminal/Game/Input.hs +0/−22
- platform-dep/windows/Terminal/Game/Utils.hs +30/−0
- src/Terminal/Game.hs +232/−56
- src/Terminal/Game/ANSI.hs +0/−96
- src/Terminal/Game/Animation.hs +15/−35
- src/Terminal/Game/Character.hs +43/−0
- src/Terminal/Game/Draw.hs +203/−21
- src/Terminal/Game/GameLoop.hs +0/−193
- src/Terminal/Game/Layer/Imperative.hs +298/−0
- src/Terminal/Game/Layer/Object.hs +30/−0
- src/Terminal/Game/Layer/Object/GameIO.hs +21/−0
- src/Terminal/Game/Layer/Object/IO.hs +317/−0
- src/Terminal/Game/Layer/Object/Interface.hs +60/−0
- src/Terminal/Game/Layer/Object/Narrate.hs +28/−0
- src/Terminal/Game/Layer/Object/Primitive.hs +139/−0
- src/Terminal/Game/Layer/Object/Record.hs +54/−0
- src/Terminal/Game/Layer/Object/Test.hs +77/−0
- src/Terminal/Game/Plane.hs +184/−90
- src/Terminal/Game/Random.hs +20/−0
- src/Terminal/Game/Utils.hs +0/−11
- test/Terminal/Game/DrawSpec.hs +68/−0
- test/Terminal/Game/Layer/ImperativeSpec.hs +72/−0
- test/Terminal/Game/Layer/Object/TestSpec.hs +19/−0
- test/Terminal/Game/PlaneSpec.hs +57/−0
- test/Terminal/Game/RandomSpec.hs +20/−0
- test/records/alone-record-left.gr binary
- test/records/alone-record-test.gr binary
- test/records/balls-dims.gr binary
- test/records/balls-slow.gr binary
+ AUTHORS view
@@ -0,0 +1,3 @@+Francesco Ariis+Simon Michael+José Rafael Vieira
+ COPYING view
@@ -0,0 +1,674 @@+ GNU GENERAL PUBLIC LICENSE+ Version 3, 29 June 2007++ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>+ Everyone is permitted to copy and distribute verbatim copies+ of this license document, but changing it is not allowed.++ Preamble++ The GNU General Public License is a free, copyleft license for+software and other kinds of works.++ The licenses for most software and other practical works are designed+to take away your freedom to share and change the works. By contrast,+the GNU General Public License is intended to guarantee your freedom to+share and change all versions of a program--to make sure it remains free+software for all its users. We, the Free Software Foundation, use the+GNU General Public License for most of our software; it applies also to+any other work released this way by its authors. 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+them 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 prevent others from denying you+these rights or asking you to surrender the rights. Therefore, you have+certain responsibilities if you distribute copies of the software, or if+you modify it: responsibilities to respect the freedom of others.++ For example, if you distribute copies of such a program, whether+gratis or for a fee, you must pass on to the recipients the same+freedoms that you received. 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.++ Developers that use the GNU GPL protect your rights with two steps:+(1) assert copyright on the software, and (2) offer you this License+giving you legal permission to copy, distribute and/or modify it.++ For the developers' and authors' protection, the GPL clearly explains+that there is no warranty for this free software. For both users' and+authors' sake, the GPL requires that modified versions be marked as+changed, so that their problems will not be attributed erroneously to+authors of previous versions.++ Some devices are designed to deny users access to install or run+modified versions of the software inside them, although the manufacturer+can do so. This is fundamentally incompatible with the aim of+protecting users' freedom to change the software. The systematic+pattern of such abuse occurs in the area of products for individuals to+use, which is precisely where it is most unacceptable. Therefore, we+have designed this version of the GPL to prohibit the practice for those+products. If such problems arise substantially in other domains, we+stand ready to extend this provision to those domains in future versions+of the GPL, as needed to protect the freedom of users.++ Finally, every program is threatened constantly by software patents.+States should not allow patents to restrict development and use of+software on general-purpose computers, but in those that do, we wish to+avoid the special danger that patents applied to a free program could+make it effectively proprietary. To prevent this, the GPL assures that+patents cannot be used to render the program non-free.++ The precise terms and conditions for copying, distribution and+modification follow.++ TERMS AND CONDITIONS++ 0. Definitions.++ "This License" refers to version 3 of the GNU General Public License.++ "Copyright" also means copyright-like laws that apply to other kinds of+works, such as semiconductor masks.++ "The Program" refers to any copyrightable work licensed under this+License. Each licensee is addressed as "you". "Licensees" and+"recipients" may be individuals or organizations.++ To "modify" a work means to copy from or adapt all or part of the work+in a fashion requiring copyright permission, other than the making of an+exact copy. The resulting work is called a "modified version" of the+earlier work or a work "based on" the earlier work.++ A "covered work" means either the unmodified Program or a work based+on the Program.++ To "propagate" a work means to do anything with it that, without+permission, would make you directly or secondarily liable for+infringement under applicable copyright law, except executing it on a+computer or modifying a private copy. Propagation includes copying,+distribution (with or without modification), making available to the+public, and in some countries other activities as well.++ To "convey" a work means any kind of propagation that enables other+parties to make or receive copies. Mere interaction with a user through+a computer network, with no transfer of a copy, is not conveying.++ An interactive user interface displays "Appropriate Legal Notices"+to the extent that it includes a convenient and prominently visible+feature that (1) displays an appropriate copyright notice, and (2)+tells the user that there is no warranty for the work (except to the+extent that warranties are provided), that licensees may convey the+work under this License, and how to view a copy of this License. If+the interface presents a list of user commands or options, such as a+menu, a prominent item in the list meets this criterion.++ 1. Source Code.++ The "source code" for a work means the preferred form of the work+for making modifications to it. "Object code" means any non-source+form of a work.++ A "Standard Interface" means an interface that either is an official+standard defined by a recognized standards body, or, in the case of+interfaces specified for a particular programming language, one that+is widely used among developers working in that language.++ The "System Libraries" of an executable work include anything, other+than the work as a whole, that (a) is included in the normal form of+packaging a Major Component, but which is not part of that Major+Component, and (b) serves only to enable use of the work with that+Major Component, or to implement a Standard Interface for which an+implementation is available to the public in source code form. A+"Major Component", in this context, means a major essential component+(kernel, window system, and so on) of the specific operating system+(if any) on which the executable work runs, or a compiler used to+produce the work, or an object code interpreter used to run it.++ The "Corresponding Source" for a work in object code form means all+the source code needed to generate, install, and (for an executable+work) run the object code and to modify the work, including scripts to+control those activities. However, it does not include the work's+System Libraries, or general-purpose tools or generally available free+programs which are used unmodified in performing those activities but+which are not part of the work. For example, Corresponding Source+includes interface definition files associated with source files for+the work, and the source code for shared libraries and dynamically+linked subprograms that the work is specifically designed to require,+such as by intimate data communication or control flow between those+subprograms and other parts of the work.++ The Corresponding Source need not include anything that users+can regenerate automatically from other parts of the Corresponding+Source.++ The Corresponding Source for a work in source code form is that+same work.++ 2. Basic Permissions.++ All rights granted under this License are granted for the term of+copyright on the Program, and are irrevocable provided the stated+conditions are met. This License explicitly affirms your unlimited+permission to run the unmodified Program. The output from running a+covered work is covered by this License only if the output, given its+content, constitutes a covered work. This License acknowledges your+rights of fair use or other equivalent, as provided by copyright law.++ You may make, run and propagate covered works that you do not+convey, without conditions so long as your license otherwise remains+in force. You may convey covered works to others for the sole purpose+of having them make modifications exclusively for you, or provide you+with facilities for running those works, provided that you comply with+the terms of this License in conveying all material for which you do+not control copyright. Those thus making or running the covered works+for you must do so exclusively on your behalf, under your direction+and control, on terms that prohibit them from making any copies of+your copyrighted material outside their relationship with you.++ Conveying under any other circumstances is permitted solely under+the conditions stated below. Sublicensing is not allowed; section 10+makes it unnecessary.++ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.++ No covered work shall be deemed part of an effective technological+measure under any applicable law fulfilling obligations under article+11 of the WIPO copyright treaty adopted on 20 December 1996, or+similar laws prohibiting or restricting circumvention of such+measures.++ When you convey a covered work, you waive any legal power to forbid+circumvention of technological measures to the extent such circumvention+is effected by exercising rights under this License with respect to+the covered work, and you disclaim any intention to limit operation or+modification of the work as a means of enforcing, against the work's+users, your or third parties' legal rights to forbid circumvention of+technological measures.++ 4. Conveying Verbatim Copies.++ You may convey 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;+keep intact all notices stating that this License and any+non-permissive terms added in accord with section 7 apply to the code;+keep intact all notices of the absence of any warranty; and give all+recipients a copy of this License along with the Program.++ You may charge any price or no price for each copy that you convey,+and you may offer support or warranty protection for a fee.++ 5. Conveying Modified Source Versions.++ You may convey a work based on the Program, or the modifications to+produce it from the Program, in the form of source code under the+terms of section 4, provided that you also meet all of these conditions:++ a) The work must carry prominent notices stating that you modified+ it, and giving a relevant date.++ b) The work must carry prominent notices stating that it is+ released under this License and any conditions added under section+ 7. This requirement modifies the requirement in section 4 to+ "keep intact all notices".++ c) You must license the entire work, as a whole, under this+ License to anyone who comes into possession of a copy. This+ License will therefore apply, along with any applicable section 7+ additional terms, to the whole of the work, and all its parts,+ regardless of how they are packaged. This License gives no+ permission to license the work in any other way, but it does not+ invalidate such permission if you have separately received it.++ d) If the work has interactive user interfaces, each must display+ Appropriate Legal Notices; however, if the Program has interactive+ interfaces that do not display Appropriate Legal Notices, your+ work need not make them do so.++ A compilation of a covered work with other separate and independent+works, which are not by their nature extensions of the covered work,+and which are not combined with it such as to form a larger program,+in or on a volume of a storage or distribution medium, is called an+"aggregate" if the compilation and its resulting copyright are not+used to limit the access or legal rights of the compilation's users+beyond what the individual works permit. Inclusion of a covered work+in an aggregate does not cause this License to apply to the other+parts of the aggregate.++ 6. Conveying Non-Source Forms.++ You may convey a covered work in object code form under the terms+of sections 4 and 5, provided that you also convey the+machine-readable Corresponding Source under the terms of this License,+in one of these ways:++ a) Convey the object code in, or embodied in, a physical product+ (including a physical distribution medium), accompanied by the+ Corresponding Source fixed on a durable physical medium+ customarily used for software interchange.++ b) Convey the object code in, or embodied in, a physical product+ (including a physical distribution medium), accompanied by a+ written offer, valid for at least three years and valid for as+ long as you offer spare parts or customer support for that product+ model, to give anyone who possesses the object code either (1) a+ copy of the Corresponding Source for all the software in the+ product that is covered by this License, on a durable physical+ medium customarily used for software interchange, for a price no+ more than your reasonable cost of physically performing this+ conveying of source, or (2) access to copy the+ Corresponding Source from a network server at no charge.++ c) Convey individual copies of the object code with a copy of the+ written offer to provide the Corresponding Source. This+ alternative is allowed only occasionally and noncommercially, and+ only if you received the object code with such an offer, in accord+ with subsection 6b.++ d) Convey the object code by offering access from a designated+ place (gratis or for a charge), and offer equivalent access to the+ Corresponding Source in the same way through the same place at no+ further charge. You need not require recipients to copy the+ Corresponding Source along with the object code. If the place to+ copy the object code is a network server, the Corresponding Source+ may be on a different server (operated by you or a third party)+ that supports equivalent copying facilities, provided you maintain+ clear directions next to the object code saying where to find the+ Corresponding Source. Regardless of what server hosts the+ Corresponding Source, you remain obligated to ensure that it is+ available for as long as needed to satisfy these requirements.++ e) Convey the object code using peer-to-peer transmission, provided+ you inform other peers where the object code and Corresponding+ Source of the work are being offered to the general public at no+ charge under subsection 6d.++ A separable portion of the object code, whose source code is excluded+from the Corresponding Source as a System Library, need not be+included in conveying the object code work.++ A "User Product" is either (1) a "consumer product", which means any+tangible personal property which is normally used for personal, family,+or household purposes, or (2) anything designed or sold for incorporation+into a dwelling. In determining whether a product is a consumer product,+doubtful cases shall be resolved in favor of coverage. For a particular+product received by a particular user, "normally used" refers to a+typical or common use of that class of product, regardless of the status+of the particular user or of the way in which the particular user+actually uses, or expects or is expected to use, the product. A product+is a consumer product regardless of whether the product has substantial+commercial, industrial or non-consumer uses, unless such uses represent+the only significant mode of use of the product.++ "Installation Information" for a User Product means any methods,+procedures, authorization keys, or other information required to install+and execute modified versions of a covered work in that User Product from+a modified version of its Corresponding Source. The information must+suffice to ensure that the continued functioning of the modified object+code is in no case prevented or interfered with solely because+modification has been made.++ If you convey an object code work under this section in, or with, or+specifically for use in, a User Product, and the conveying occurs as+part of a transaction in which the right of possession and use of the+User Product is transferred to the recipient in perpetuity or for a+fixed term (regardless of how the transaction is characterized), the+Corresponding Source conveyed under this section must be accompanied+by the Installation Information. But this requirement does not apply+if neither you nor any third party retains the ability to install+modified object code on the User Product (for example, the work has+been installed in ROM).++ The requirement to provide Installation Information does not include a+requirement to continue to provide support service, warranty, or updates+for a work that has been modified or installed by the recipient, or for+the User Product in which it has been modified or installed. Access to a+network may be denied when the modification itself materially and+adversely affects the operation of the network or violates the rules and+protocols for communication across the network.++ Corresponding Source conveyed, and Installation Information provided,+in accord with this section must be in a format that is publicly+documented (and with an implementation available to the public in+source code form), and must require no special password or key for+unpacking, reading or copying.++ 7. Additional Terms.++ "Additional permissions" are terms that supplement the terms of this+License by making exceptions from one or more of its conditions.+Additional permissions that are applicable to the entire Program shall+be treated as though they were included in this License, to the extent+that they are valid under applicable law. If additional permissions+apply only to part of the Program, that part may be used separately+under those permissions, but the entire Program remains governed by+this License without regard to the additional permissions.++ When you convey a copy of a covered work, you may at your option+remove any additional permissions from that copy, or from any part of+it. (Additional permissions may be written to require their own+removal in certain cases when you modify the work.) You may place+additional permissions on material, added by you to a covered work,+for which you have or can give appropriate copyright permission.++ Notwithstanding any other provision of this License, for material you+add to a covered work, you may (if authorized by the copyright holders of+that material) supplement the terms of this License with terms:++ a) Disclaiming warranty or limiting liability differently from the+ terms of sections 15 and 16 of this License; or++ b) Requiring preservation of specified reasonable legal notices or+ author attributions in that material or in the Appropriate Legal+ Notices displayed by works containing it; or++ c) Prohibiting misrepresentation of the origin of that material, or+ requiring that modified versions of such material be marked in+ reasonable ways as different from the original version; or++ d) Limiting the use for publicity purposes of names of licensors or+ authors of the material; or++ e) Declining to grant rights under trademark law for use of some+ trade names, trademarks, or service marks; or++ f) Requiring indemnification of licensors and authors of that+ material by anyone who conveys the material (or modified versions of+ it) with contractual assumptions of liability to the recipient, for+ any liability that these contractual assumptions directly impose on+ those licensors and authors.++ All other non-permissive additional terms are considered "further+restrictions" within the meaning of section 10. If the Program as you+received it, or any part of it, contains a notice stating that it is+governed by this License along with a term that is a further+restriction, you may remove that term. If a license document contains+a further restriction but permits relicensing or conveying under this+License, you may add to a covered work material governed by the terms+of that license document, provided that the further restriction does+not survive such relicensing or conveying.++ If you add terms to a covered work in accord with this section, you+must place, in the relevant source files, a statement of the+additional terms that apply to those files, or a notice indicating+where to find the applicable terms.++ Additional terms, permissive or non-permissive, may be stated in the+form of a separately written license, or stated as exceptions;+the above requirements apply either way.++ 8. Termination.++ You may not propagate or modify a covered work except as expressly+provided under this License. Any attempt otherwise to propagate or+modify it is void, and will automatically terminate your rights under+this License (including any patent licenses granted under the third+paragraph of section 11).++ However, if you cease all violation of this License, then your+license from a particular copyright holder is reinstated (a)+provisionally, unless and until the copyright holder explicitly and+finally terminates your license, and (b) permanently, if the copyright+holder fails to notify you of the violation by some reasonable means+prior to 60 days after the cessation.++ Moreover, your license from a particular copyright holder is+reinstated permanently if the copyright holder notifies you of the+violation by some reasonable means, this is the first time you have+received notice of violation of this License (for any work) from that+copyright holder, and you cure the violation prior to 30 days after+your receipt of the notice.++ Termination of your rights under this section does not terminate the+licenses of parties who have received copies or rights from you under+this License. If your rights have been terminated and not permanently+reinstated, you do not qualify to receive new licenses for the same+material under section 10.++ 9. Acceptance Not Required for Having Copies.++ You are not required to accept this License in order to receive or+run a copy of the Program. Ancillary propagation of a covered work+occurring solely as a consequence of using peer-to-peer transmission+to receive a copy likewise does not require acceptance. However,+nothing other than this License grants you permission to propagate or+modify any covered work. These actions infringe copyright if you do+not accept this License. Therefore, by modifying or propagating a+covered work, you indicate your acceptance of this License to do so.++ 10. Automatic Licensing of Downstream Recipients.++ Each time you convey a covered work, the recipient automatically+receives a license from the original licensors, to run, modify and+propagate that work, subject to this License. You are not responsible+for enforcing compliance by third parties with this License.++ An "entity transaction" is a transaction transferring control of an+organization, or substantially all assets of one, or subdividing an+organization, or merging organizations. If propagation of a covered+work results from an entity transaction, each party to that+transaction who receives a copy of the work also receives whatever+licenses to the work the party's predecessor in interest had or could+give under the previous paragraph, plus a right to possession of the+Corresponding Source of the work from the predecessor in interest, if+the predecessor has it or can get it with reasonable efforts.++ You may not impose any further restrictions on the exercise of the+rights granted or affirmed under this License. For example, you may+not impose a license fee, royalty, or other charge for exercise of+rights granted under this License, and you may not initiate litigation+(including a cross-claim or counterclaim in a lawsuit) alleging that+any patent claim is infringed by making, using, selling, offering for+sale, or importing the Program or any portion of it.++ 11. Patents.++ A "contributor" is a copyright holder who authorizes use under this+License of the Program or a work on which the Program is based. The+work thus licensed is called the contributor's "contributor version".++ A contributor's "essential patent claims" are all patent claims+owned or controlled by the contributor, whether already acquired or+hereafter acquired, that would be infringed by some manner, permitted+by this License, of making, using, or selling its contributor version,+but do not include claims that would be infringed only as a+consequence of further modification of the contributor version. For+purposes of this definition, "control" includes the right to grant+patent sublicenses in a manner consistent with the requirements of+this License.++ Each contributor grants you a non-exclusive, worldwide, royalty-free+patent license under the contributor's essential patent claims, to+make, use, sell, offer for sale, import and otherwise run, modify and+propagate the contents of its contributor version.++ In the following three paragraphs, a "patent license" is any express+agreement or commitment, however denominated, not to enforce a patent+(such as an express permission to practice a patent or covenant not to+sue for patent infringement). To "grant" such a patent license to a+party means to make such an agreement or commitment not to enforce a+patent against the party.++ If you convey a covered work, knowingly relying on a patent license,+and the Corresponding Source of the work is not available for anyone+to copy, free of charge and under the terms of this License, through a+publicly available network server or other readily accessible means,+then you must either (1) cause the Corresponding Source to be so+available, or (2) arrange to deprive yourself of the benefit of the+patent license for this particular work, or (3) arrange, in a manner+consistent with the requirements of this License, to extend the patent+license to downstream recipients. "Knowingly relying" means you have+actual knowledge that, but for the patent license, your conveying the+covered work in a country, or your recipient's use of the covered work+in a country, would infringe one or more identifiable patents in that+country that you have reason to believe are valid.++ If, pursuant to or in connection with a single transaction or+arrangement, you convey, or propagate by procuring conveyance of, a+covered work, and grant a patent license to some of the parties+receiving the covered work authorizing them to use, propagate, modify+or convey a specific copy of the covered work, then the patent license+you grant is automatically extended to all recipients of the covered+work and works based on it.++ A patent license is "discriminatory" if it does not include within+the scope of its coverage, prohibits the exercise of, or is+conditioned on the non-exercise of one or more of the rights that are+specifically granted under this License. You may not convey a covered+work if you are a party to an arrangement with a third party that is+in the business of distributing software, under which you make payment+to the third party based on the extent of your activity of conveying+the work, and under which the third party grants, to any of the+parties who would receive the covered work from you, a discriminatory+patent license (a) in connection with copies of the covered work+conveyed by you (or copies made from those copies), or (b) primarily+for and in connection with specific products or compilations that+contain the covered work, unless you entered into that arrangement,+or that patent license was granted, prior to 28 March 2007.++ Nothing in this License shall be construed as excluding or limiting+any implied license or other defenses to infringement that may+otherwise be available to you under applicable patent law.++ 12. No Surrender of Others' Freedom.++ If 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 convey a+covered work so as to satisfy simultaneously your obligations under this+License and any other pertinent obligations, then as a consequence you may+not convey it at all. For example, if you agree to terms that obligate you+to collect a royalty for further conveying from those to whom you convey+the Program, the only way you could satisfy both those terms and this+License would be to refrain entirely from conveying the Program.++ 13. Use with the GNU Affero General Public License.++ Notwithstanding any other provision of this License, you have+permission to link or combine any covered work with a work licensed+under version 3 of the GNU Affero General Public License into a single+combined work, and to convey the resulting work. The terms of this+License will continue to apply to the part which is the covered work,+but the special requirements of the GNU Affero General Public License,+section 13, concerning interaction through a network will apply to the+combination as such.++ 14. Revised Versions of this License.++ The Free Software Foundation may publish revised and/or new versions of+the GNU 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 that a certain numbered version of the GNU General+Public License "or any later version" applies to it, you have the+option of following the terms and conditions either of that numbered+version or of any later version published by the Free Software+Foundation. If the Program does not specify a version number of the+GNU General Public License, you may choose any version ever published+by the Free Software Foundation.++ If the Program specifies that a proxy can decide which future+versions of the GNU General Public License can be used, that proxy's+public statement of acceptance of a version permanently authorizes you+to choose that version for the Program.++ Later license versions may give you additional or different+permissions. However, no additional obligations are imposed on any+author or copyright holder as a result of your choosing to follow a+later version.++ 15. Disclaimer of Warranty.++ 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.++ 16. Limitation of Liability.++ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS+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.++ 17. Interpretation of Sections 15 and 16.++ If the disclaimer of warranty and limitation of liability provided+above cannot be given local legal effect according to their terms,+reviewing courts shall apply local law that most closely approximates+an absolute waiver of all civil liability in connection with the+Program, unless a warranty or assumption of liability accompanies a+copy of the Program in return for a fee.++ 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+state the exclusion of warranty; and each file should have at least+the "copyright" line and a pointer to where the full notice is found.++ <one line to give the program's name and a brief idea of what it does.>+ Copyright (C) <year> <name of author>++ 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 3 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, see <http://www.gnu.org/licenses/>.++Also add information on how to contact you by electronic and paper mail.++ If the program does terminal interaction, make it output a short+notice like this when it starts in an interactive mode:++ <program> Copyright (C) <year> <name of author>+ This program 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, your program's commands+might be different; for a GUI interface, you would use an "about box".++ You should also get your employer (if you work as a programmer) or school,+if any, to sign a "copyright disclaimer" for the program, if necessary.+For more information on this, and how to apply and follow the GNU GPL, see+<http://www.gnu.org/licenses/>.++ The GNU 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. But first, please read+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
− LICENSE
@@ -1,674 +0,0 @@- GNU GENERAL PUBLIC LICENSE- Version 3, 29 June 2007-- Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>- Everyone is permitted to copy and distribute verbatim copies- of this license document, but changing it is not allowed.-- Preamble-- The GNU General Public License is a free, copyleft license for-software and other kinds of works.-- The licenses for most software and other practical works are designed-to take away your freedom to share and change the works. By contrast,-the GNU General Public License is intended to guarantee your freedom to-share and change all versions of a program--to make sure it remains free-software for all its users. We, the Free Software Foundation, use the-GNU General Public License for most of our software; it applies also to-any other work released this way by its authors. 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-them 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 prevent others from denying you-these rights or asking you to surrender the rights. Therefore, you have-certain responsibilities if you distribute copies of the software, or if-you modify it: responsibilities to respect the freedom of others.-- For example, if you distribute copies of such a program, whether-gratis or for a fee, you must pass on to the recipients the same-freedoms that you received. 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.-- Developers that use the GNU GPL protect your rights with two steps:-(1) assert copyright on the software, and (2) offer you this License-giving you legal permission to copy, distribute and/or modify it.-- For the developers' and authors' protection, the GPL clearly explains-that there is no warranty for this free software. For both users' and-authors' sake, the GPL requires that modified versions be marked as-changed, so that their problems will not be attributed erroneously to-authors of previous versions.-- Some devices are designed to deny users access to install or run-modified versions of the software inside them, although the manufacturer-can do so. This is fundamentally incompatible with the aim of-protecting users' freedom to change the software. The systematic-pattern of such abuse occurs in the area of products for individuals to-use, which is precisely where it is most unacceptable. Therefore, we-have designed this version of the GPL to prohibit the practice for those-products. If such problems arise substantially in other domains, we-stand ready to extend this provision to those domains in future versions-of the GPL, as needed to protect the freedom of users.-- Finally, every program is threatened constantly by software patents.-States should not allow patents to restrict development and use of-software on general-purpose computers, but in those that do, we wish to-avoid the special danger that patents applied to a free program could-make it effectively proprietary. To prevent this, the GPL assures that-patents cannot be used to render the program non-free.-- The precise terms and conditions for copying, distribution and-modification follow.-- TERMS AND CONDITIONS-- 0. Definitions.-- "This License" refers to version 3 of the GNU General Public License.-- "Copyright" also means copyright-like laws that apply to other kinds of-works, such as semiconductor masks.-- "The Program" refers to any copyrightable work licensed under this-License. Each licensee is addressed as "you". "Licensees" and-"recipients" may be individuals or organizations.-- To "modify" a work means to copy from or adapt all or part of the work-in a fashion requiring copyright permission, other than the making of an-exact copy. The resulting work is called a "modified version" of the-earlier work or a work "based on" the earlier work.-- A "covered work" means either the unmodified Program or a work based-on the Program.-- To "propagate" a work means to do anything with it that, without-permission, would make you directly or secondarily liable for-infringement under applicable copyright law, except executing it on a-computer or modifying a private copy. Propagation includes copying,-distribution (with or without modification), making available to the-public, and in some countries other activities as well.-- To "convey" a work means any kind of propagation that enables other-parties to make or receive copies. Mere interaction with a user through-a computer network, with no transfer of a copy, is not conveying.-- An interactive user interface displays "Appropriate Legal Notices"-to the extent that it includes a convenient and prominently visible-feature that (1) displays an appropriate copyright notice, and (2)-tells the user that there is no warranty for the work (except to the-extent that warranties are provided), that licensees may convey the-work under this License, and how to view a copy of this License. If-the interface presents a list of user commands or options, such as a-menu, a prominent item in the list meets this criterion.-- 1. Source Code.-- The "source code" for a work means the preferred form of the work-for making modifications to it. "Object code" means any non-source-form of a work.-- A "Standard Interface" means an interface that either is an official-standard defined by a recognized standards body, or, in the case of-interfaces specified for a particular programming language, one that-is widely used among developers working in that language.-- The "System Libraries" of an executable work include anything, other-than the work as a whole, that (a) is included in the normal form of-packaging a Major Component, but which is not part of that Major-Component, and (b) serves only to enable use of the work with that-Major Component, or to implement a Standard Interface for which an-implementation is available to the public in source code form. A-"Major Component", in this context, means a major essential component-(kernel, window system, and so on) of the specific operating system-(if any) on which the executable work runs, or a compiler used to-produce the work, or an object code interpreter used to run it.-- The "Corresponding Source" for a work in object code form means all-the source code needed to generate, install, and (for an executable-work) run the object code and to modify the work, including scripts to-control those activities. However, it does not include the work's-System Libraries, or general-purpose tools or generally available free-programs which are used unmodified in performing those activities but-which are not part of the work. For example, Corresponding Source-includes interface definition files associated with source files for-the work, and the source code for shared libraries and dynamically-linked subprograms that the work is specifically designed to require,-such as by intimate data communication or control flow between those-subprograms and other parts of the work.-- The Corresponding Source need not include anything that users-can regenerate automatically from other parts of the Corresponding-Source.-- The Corresponding Source for a work in source code form is that-same work.-- 2. Basic Permissions.-- All rights granted under this License are granted for the term of-copyright on the Program, and are irrevocable provided the stated-conditions are met. This License explicitly affirms your unlimited-permission to run the unmodified Program. The output from running a-covered work is covered by this License only if the output, given its-content, constitutes a covered work. This License acknowledges your-rights of fair use or other equivalent, as provided by copyright law.-- You may make, run and propagate covered works that you do not-convey, without conditions so long as your license otherwise remains-in force. You may convey covered works to others for the sole purpose-of having them make modifications exclusively for you, or provide you-with facilities for running those works, provided that you comply with-the terms of this License in conveying all material for which you do-not control copyright. Those thus making or running the covered works-for you must do so exclusively on your behalf, under your direction-and control, on terms that prohibit them from making any copies of-your copyrighted material outside their relationship with you.-- Conveying under any other circumstances is permitted solely under-the conditions stated below. Sublicensing is not allowed; section 10-makes it unnecessary.-- 3. Protecting Users' Legal Rights From Anti-Circumvention Law.-- No covered work shall be deemed part of an effective technological-measure under any applicable law fulfilling obligations under article-11 of the WIPO copyright treaty adopted on 20 December 1996, or-similar laws prohibiting or restricting circumvention of such-measures.-- When you convey a covered work, you waive any legal power to forbid-circumvention of technological measures to the extent such circumvention-is effected by exercising rights under this License with respect to-the covered work, and you disclaim any intention to limit operation or-modification of the work as a means of enforcing, against the work's-users, your or third parties' legal rights to forbid circumvention of-technological measures.-- 4. Conveying Verbatim Copies.-- You may convey 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;-keep intact all notices stating that this License and any-non-permissive terms added in accord with section 7 apply to the code;-keep intact all notices of the absence of any warranty; and give all-recipients a copy of this License along with the Program.-- You may charge any price or no price for each copy that you convey,-and you may offer support or warranty protection for a fee.-- 5. Conveying Modified Source Versions.-- You may convey a work based on the Program, or the modifications to-produce it from the Program, in the form of source code under the-terms of section 4, provided that you also meet all of these conditions:-- a) The work must carry prominent notices stating that you modified- it, and giving a relevant date.-- b) The work must carry prominent notices stating that it is- released under this License and any conditions added under section- 7. This requirement modifies the requirement in section 4 to- "keep intact all notices".-- c) You must license the entire work, as a whole, under this- License to anyone who comes into possession of a copy. This- License will therefore apply, along with any applicable section 7- additional terms, to the whole of the work, and all its parts,- regardless of how they are packaged. This License gives no- permission to license the work in any other way, but it does not- invalidate such permission if you have separately received it.-- d) If the work has interactive user interfaces, each must display- Appropriate Legal Notices; however, if the Program has interactive- interfaces that do not display Appropriate Legal Notices, your- work need not make them do so.-- A compilation of a covered work with other separate and independent-works, which are not by their nature extensions of the covered work,-and which are not combined with it such as to form a larger program,-in or on a volume of a storage or distribution medium, is called an-"aggregate" if the compilation and its resulting copyright are not-used to limit the access or legal rights of the compilation's users-beyond what the individual works permit. Inclusion of a covered work-in an aggregate does not cause this License to apply to the other-parts of the aggregate.-- 6. Conveying Non-Source Forms.-- You may convey a covered work in object code form under the terms-of sections 4 and 5, provided that you also convey the-machine-readable Corresponding Source under the terms of this License,-in one of these ways:-- a) Convey the object code in, or embodied in, a physical product- (including a physical distribution medium), accompanied by the- Corresponding Source fixed on a durable physical medium- customarily used for software interchange.-- b) Convey the object code in, or embodied in, a physical product- (including a physical distribution medium), accompanied by a- written offer, valid for at least three years and valid for as- long as you offer spare parts or customer support for that product- model, to give anyone who possesses the object code either (1) a- copy of the Corresponding Source for all the software in the- product that is covered by this License, on a durable physical- medium customarily used for software interchange, for a price no- more than your reasonable cost of physically performing this- conveying of source, or (2) access to copy the- Corresponding Source from a network server at no charge.-- c) Convey individual copies of the object code with a copy of the- written offer to provide the Corresponding Source. This- alternative is allowed only occasionally and noncommercially, and- only if you received the object code with such an offer, in accord- with subsection 6b.-- d) Convey the object code by offering access from a designated- place (gratis or for a charge), and offer equivalent access to the- Corresponding Source in the same way through the same place at no- further charge. You need not require recipients to copy the- Corresponding Source along with the object code. If the place to- copy the object code is a network server, the Corresponding Source- may be on a different server (operated by you or a third party)- that supports equivalent copying facilities, provided you maintain- clear directions next to the object code saying where to find the- Corresponding Source. Regardless of what server hosts the- Corresponding Source, you remain obligated to ensure that it is- available for as long as needed to satisfy these requirements.-- e) Convey the object code using peer-to-peer transmission, provided- you inform other peers where the object code and Corresponding- Source of the work are being offered to the general public at no- charge under subsection 6d.-- A separable portion of the object code, whose source code is excluded-from the Corresponding Source as a System Library, need not be-included in conveying the object code work.-- A "User Product" is either (1) a "consumer product", which means any-tangible personal property which is normally used for personal, family,-or household purposes, or (2) anything designed or sold for incorporation-into a dwelling. In determining whether a product is a consumer product,-doubtful cases shall be resolved in favor of coverage. For a particular-product received by a particular user, "normally used" refers to a-typical or common use of that class of product, regardless of the status-of the particular user or of the way in which the particular user-actually uses, or expects or is expected to use, the product. A product-is a consumer product regardless of whether the product has substantial-commercial, industrial or non-consumer uses, unless such uses represent-the only significant mode of use of the product.-- "Installation Information" for a User Product means any methods,-procedures, authorization keys, or other information required to install-and execute modified versions of a covered work in that User Product from-a modified version of its Corresponding Source. The information must-suffice to ensure that the continued functioning of the modified object-code is in no case prevented or interfered with solely because-modification has been made.-- If you convey an object code work under this section in, or with, or-specifically for use in, a User Product, and the conveying occurs as-part of a transaction in which the right of possession and use of the-User Product is transferred to the recipient in perpetuity or for a-fixed term (regardless of how the transaction is characterized), the-Corresponding Source conveyed under this section must be accompanied-by the Installation Information. But this requirement does not apply-if neither you nor any third party retains the ability to install-modified object code on the User Product (for example, the work has-been installed in ROM).-- The requirement to provide Installation Information does not include a-requirement to continue to provide support service, warranty, or updates-for a work that has been modified or installed by the recipient, or for-the User Product in which it has been modified or installed. Access to a-network may be denied when the modification itself materially and-adversely affects the operation of the network or violates the rules and-protocols for communication across the network.-- Corresponding Source conveyed, and Installation Information provided,-in accord with this section must be in a format that is publicly-documented (and with an implementation available to the public in-source code form), and must require no special password or key for-unpacking, reading or copying.-- 7. Additional Terms.-- "Additional permissions" are terms that supplement the terms of this-License by making exceptions from one or more of its conditions.-Additional permissions that are applicable to the entire Program shall-be treated as though they were included in this License, to the extent-that they are valid under applicable law. If additional permissions-apply only to part of the Program, that part may be used separately-under those permissions, but the entire Program remains governed by-this License without regard to the additional permissions.-- When you convey a copy of a covered work, you may at your option-remove any additional permissions from that copy, or from any part of-it. (Additional permissions may be written to require their own-removal in certain cases when you modify the work.) You may place-additional permissions on material, added by you to a covered work,-for which you have or can give appropriate copyright permission.-- Notwithstanding any other provision of this License, for material you-add to a covered work, you may (if authorized by the copyright holders of-that material) supplement the terms of this License with terms:-- a) Disclaiming warranty or limiting liability differently from the- terms of sections 15 and 16 of this License; or-- b) Requiring preservation of specified reasonable legal notices or- author attributions in that material or in the Appropriate Legal- Notices displayed by works containing it; or-- c) Prohibiting misrepresentation of the origin of that material, or- requiring that modified versions of such material be marked in- reasonable ways as different from the original version; or-- d) Limiting the use for publicity purposes of names of licensors or- authors of the material; or-- e) Declining to grant rights under trademark law for use of some- trade names, trademarks, or service marks; or-- f) Requiring indemnification of licensors and authors of that- material by anyone who conveys the material (or modified versions of- it) with contractual assumptions of liability to the recipient, for- any liability that these contractual assumptions directly impose on- those licensors and authors.-- All other non-permissive additional terms are considered "further-restrictions" within the meaning of section 10. If the Program as you-received it, or any part of it, contains a notice stating that it is-governed by this License along with a term that is a further-restriction, you may remove that term. If a license document contains-a further restriction but permits relicensing or conveying under this-License, you may add to a covered work material governed by the terms-of that license document, provided that the further restriction does-not survive such relicensing or conveying.-- If you add terms to a covered work in accord with this section, you-must place, in the relevant source files, a statement of the-additional terms that apply to those files, or a notice indicating-where to find the applicable terms.-- Additional terms, permissive or non-permissive, may be stated in the-form of a separately written license, or stated as exceptions;-the above requirements apply either way.-- 8. Termination.-- You may not propagate or modify a covered work except as expressly-provided under this License. Any attempt otherwise to propagate or-modify it is void, and will automatically terminate your rights under-this License (including any patent licenses granted under the third-paragraph of section 11).-- However, if you cease all violation of this License, then your-license from a particular copyright holder is reinstated (a)-provisionally, unless and until the copyright holder explicitly and-finally terminates your license, and (b) permanently, if the copyright-holder fails to notify you of the violation by some reasonable means-prior to 60 days after the cessation.-- Moreover, your license from a particular copyright holder is-reinstated permanently if the copyright holder notifies you of the-violation by some reasonable means, this is the first time you have-received notice of violation of this License (for any work) from that-copyright holder, and you cure the violation prior to 30 days after-your receipt of the notice.-- Termination of your rights under this section does not terminate the-licenses of parties who have received copies or rights from you under-this License. If your rights have been terminated and not permanently-reinstated, you do not qualify to receive new licenses for the same-material under section 10.-- 9. Acceptance Not Required for Having Copies.-- You are not required to accept this License in order to receive or-run a copy of the Program. Ancillary propagation of a covered work-occurring solely as a consequence of using peer-to-peer transmission-to receive a copy likewise does not require acceptance. However,-nothing other than this License grants you permission to propagate or-modify any covered work. These actions infringe copyright if you do-not accept this License. Therefore, by modifying or propagating a-covered work, you indicate your acceptance of this License to do so.-- 10. Automatic Licensing of Downstream Recipients.-- Each time you convey a covered work, the recipient automatically-receives a license from the original licensors, to run, modify and-propagate that work, subject to this License. You are not responsible-for enforcing compliance by third parties with this License.-- An "entity transaction" is a transaction transferring control of an-organization, or substantially all assets of one, or subdividing an-organization, or merging organizations. If propagation of a covered-work results from an entity transaction, each party to that-transaction who receives a copy of the work also receives whatever-licenses to the work the party's predecessor in interest had or could-give under the previous paragraph, plus a right to possession of the-Corresponding Source of the work from the predecessor in interest, if-the predecessor has it or can get it with reasonable efforts.-- You may not impose any further restrictions on the exercise of the-rights granted or affirmed under this License. For example, you may-not impose a license fee, royalty, or other charge for exercise of-rights granted under this License, and you may not initiate litigation-(including a cross-claim or counterclaim in a lawsuit) alleging that-any patent claim is infringed by making, using, selling, offering for-sale, or importing the Program or any portion of it.-- 11. Patents.-- A "contributor" is a copyright holder who authorizes use under this-License of the Program or a work on which the Program is based. The-work thus licensed is called the contributor's "contributor version".-- A contributor's "essential patent claims" are all patent claims-owned or controlled by the contributor, whether already acquired or-hereafter acquired, that would be infringed by some manner, permitted-by this License, of making, using, or selling its contributor version,-but do not include claims that would be infringed only as a-consequence of further modification of the contributor version. For-purposes of this definition, "control" includes the right to grant-patent sublicenses in a manner consistent with the requirements of-this License.-- Each contributor grants you a non-exclusive, worldwide, royalty-free-patent license under the contributor's essential patent claims, to-make, use, sell, offer for sale, import and otherwise run, modify and-propagate the contents of its contributor version.-- In the following three paragraphs, a "patent license" is any express-agreement or commitment, however denominated, not to enforce a patent-(such as an express permission to practice a patent or covenant not to-sue for patent infringement). To "grant" such a patent license to a-party means to make such an agreement or commitment not to enforce a-patent against the party.-- If you convey a covered work, knowingly relying on a patent license,-and the Corresponding Source of the work is not available for anyone-to copy, free of charge and under the terms of this License, through a-publicly available network server or other readily accessible means,-then you must either (1) cause the Corresponding Source to be so-available, or (2) arrange to deprive yourself of the benefit of the-patent license for this particular work, or (3) arrange, in a manner-consistent with the requirements of this License, to extend the patent-license to downstream recipients. "Knowingly relying" means you have-actual knowledge that, but for the patent license, your conveying the-covered work in a country, or your recipient's use of the covered work-in a country, would infringe one or more identifiable patents in that-country that you have reason to believe are valid.-- If, pursuant to or in connection with a single transaction or-arrangement, you convey, or propagate by procuring conveyance of, a-covered work, and grant a patent license to some of the parties-receiving the covered work authorizing them to use, propagate, modify-or convey a specific copy of the covered work, then the patent license-you grant is automatically extended to all recipients of the covered-work and works based on it.-- A patent license is "discriminatory" if it does not include within-the scope of its coverage, prohibits the exercise of, or is-conditioned on the non-exercise of one or more of the rights that are-specifically granted under this License. You may not convey a covered-work if you are a party to an arrangement with a third party that is-in the business of distributing software, under which you make payment-to the third party based on the extent of your activity of conveying-the work, and under which the third party grants, to any of the-parties who would receive the covered work from you, a discriminatory-patent license (a) in connection with copies of the covered work-conveyed by you (or copies made from those copies), or (b) primarily-for and in connection with specific products or compilations that-contain the covered work, unless you entered into that arrangement,-or that patent license was granted, prior to 28 March 2007.-- Nothing in this License shall be construed as excluding or limiting-any implied license or other defenses to infringement that may-otherwise be available to you under applicable patent law.-- 12. No Surrender of Others' Freedom.-- If 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 convey a-covered work so as to satisfy simultaneously your obligations under this-License and any other pertinent obligations, then as a consequence you may-not convey it at all. For example, if you agree to terms that obligate you-to collect a royalty for further conveying from those to whom you convey-the Program, the only way you could satisfy both those terms and this-License would be to refrain entirely from conveying the Program.-- 13. Use with the GNU Affero General Public License.-- Notwithstanding any other provision of this License, you have-permission to link or combine any covered work with a work licensed-under version 3 of the GNU Affero General Public License into a single-combined work, and to convey the resulting work. The terms of this-License will continue to apply to the part which is the covered work,-but the special requirements of the GNU Affero General Public License,-section 13, concerning interaction through a network will apply to the-combination as such.-- 14. Revised Versions of this License.-- The Free Software Foundation may publish revised and/or new versions of-the GNU 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 that a certain numbered version of the GNU General-Public License "or any later version" applies to it, you have the-option of following the terms and conditions either of that numbered-version or of any later version published by the Free Software-Foundation. If the Program does not specify a version number of the-GNU General Public License, you may choose any version ever published-by the Free Software Foundation.-- If the Program specifies that a proxy can decide which future-versions of the GNU General Public License can be used, that proxy's-public statement of acceptance of a version permanently authorizes you-to choose that version for the Program.-- Later license versions may give you additional or different-permissions. However, no additional obligations are imposed on any-author or copyright holder as a result of your choosing to follow a-later version.-- 15. Disclaimer of Warranty.-- 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.-- 16. Limitation of Liability.-- IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS-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.-- 17. Interpretation of Sections 15 and 16.-- If the disclaimer of warranty and limitation of liability provided-above cannot be given local legal effect according to their terms,-reviewing courts shall apply local law that most closely approximates-an absolute waiver of all civil liability in connection with the-Program, unless a warranty or assumption of liability accompanies a-copy of the Program in return for a fee.-- 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-state the exclusion of warranty; and each file should have at least-the "copyright" line and a pointer to where the full notice is found.-- <one line to give the program's name and a brief idea of what it does.>- Copyright (C) <year> <name of author>-- 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 3 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, see <http://www.gnu.org/licenses/>.--Also add information on how to contact you by electronic and paper mail.-- If the program does terminal interaction, make it output a short-notice like this when it starts in an interactive mode:-- <program> Copyright (C) <year> <name of author>- This program 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, your program's commands-might be different; for a GUI interface, you would use an "about box".-- You should also get your employer (if you work as a programmer) or school,-if any, to sign a "copyright disclaimer" for the program, if necessary.-For more information on this, and how to apply and follow the GNU GPL, see-<http://www.gnu.org/licenses/>.-- The GNU 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. But first, please read-<http://www.gnu.org/philosophy/why-not-lgpl.html>.
+ NEWS view
@@ -0,0 +1,441 @@+1.9.4.0+-------++- Thanks to José Rafael Vieira, ansi-terminal-game now sports an API for+ efficient dense-grid drawing. `Cell` is exported, along with functions+ to create Cell`s++ creaCell, colorCell, rgbColorCell, paletteColorCell,+ boldCell, reverseCell++ The most important function is++ cellsPlane :: Width -> Height -> [(Coords, Cell)] -> Plane++ which merges the cells into a `Plane` in a single pass.+ Many thanks to José!+++1.9.3.0+-------++- Bump ansi-terminal 1.1. Due to the introduction of hNowSupportsANSI,+ older ansi-terminal support has been dropped. If you rely on them,+ contact me.+- Released on mer 7 feb 2024, 17:44:31++1.9.2.0+-------++- This version of a-t-g introduces convenience file-embedding functions+ `embedFile` and `embedDir`.+ An example on how this can be useful: suppose you are working on an ASCII+ map using `tiled` map editor. You could save the map in a `data/map` and+ then with `embedFile`++ {-# LANGUAGE TemplateHaskell #-}++ mapBS :: ByteString+ mapBS = $(embedFile "data/map/gamma-labs.map")++ gammaLabs :: GameMap+ gammaLabs = parseMap mapBS++ having it as a pure value in your program, and being able to ship a+ single binary instead of a zipped archive.+ `unpack :: ByteString -> String` and the `ByteString` type itself are+ also re-exported as helpers.++1.9.1.3+-------++- Bump hsped+- dom 14 mag 2023, 14:58:31++1.9.1.2+-------++- Bump ansi-terminal, fix hspec bounds.+- Minor documentation fixes.+- Released dom 14 mag 2023, 12:13:07.+++1.9.1.1+-------++- New git repository home, browse it at+ http://www.ariis.it/static/repos/stagit/ansi-terminal-game++1.9.1.0+-------++- This version of ansi-terminal-game introduces new ways to describe+ colours: RGB and xterm colours. You can find a description of the+ API in the “Non-standard colors” section of Haddock.+ The new functions and types are are+ data Colour a+ rgbColor, paletteColor, sRGB24, sRGB, sRGB24read -- RGB+ xterm6LevelRGB, xterm24LevelGray, xtermSystem, -- xterm+ Enticing as they are, they are supported only by a minority of+ terminals/multiplexers, so use them only when you are sure of the+ terminal you are targetting.+ This change was proposed /and/ implemented by José Rafael Vieira,+ whom I thank.+- Fixed a bug on `balls` example (thanks Andread Abel).+- The haskell tiny game competition is over+ https://github.com/haskell-game/tiny-games-hs+ a number of games were made with ansi-terminal-game.+- Released mer 1 mar 2023, 06:34:52++1.9.0.0+-------++tl;dr and migration guide:+- This version of ansi-terminal-games has a new signature for logic+ function:+ gLogicFunction :: GEnv -> s -> Event -> Either r s+- Notice the `Either r s`: `Left` means “game is over”; `Right`means+ “game continues”.+- To migrate a project to 1.9.0.0 you should:+ - Adjust logic function to incorporate those changes.+ - Get rid of your `quitFunction` in your `Game`.+ - Modify every `Game s` to `Game s ()`.++Breaking changes:+- This version changes the logic function from+ gLogicFunction :: GEnv -> s -> Event -> s+ to+ gLogicFunction :: GEnv -> s -> Event -> Either r s+ `Either r s` is a way to explicitly state whether the game is over+ not not. If you return `Left $ …` then the game will stop, if you+ return `Right $ …` your game will continue.+- the `r` stands for `result` and is present in the type constructor+ too:+ Game s r -- A game with state `s` which will,+ -- upon exit, return a result `r`.+- Usually r is () (as simple games do not care about end results,+ they just quit to terminal). But there are cases (games embedded+ in a larger program, a set of minigames, high scores) where you+ want to return something and this is the way to do it.+- Many other functions have had a slight change of signature to+ accomodate this change+ playGame :: Game s r -> IO r+ narrateGame :: Game s r -> GRec -> IO ()+ testGame :: Game s -> GRec -> Either s r+ I will spend some second on the test function. A game tested in+ a pure environment will end in two ways: a) by reaching Left+ (proper end game) b) by exhausting the input stream.+ In case b) we cannot return a result `r`, but just a half-baked+ ingame state. This is very useful for testing purposes.+ A trick that I do is this: record events with `recordGame` and+ then press Ctrl-C midgame. This way the stream is cut and I will+ get a `Right` state when running `testGame`. I can then analyse+ the resulting state.+- Functions have been deleted too+ playGame :: Game s -> IO s+ is no more+- And new functions were introduced:+ playGame_ :: Game s r -> IO () -- discard result+- The change was suggested by Gergő Érdi, whom I thank.+ The rationale was to improve ergonomics for the game-makes, I+ welcome feedback from you.+- Released mar 28 feb 2023, 20:23:02++Other changes:+- Clarified KeyPress and Tick behaviour. tl;dr: *all* keypresses are+ recorded and fed to your game-logic function. If your played manages+ to type the Divine Comedy in the space of a Tick, all those characters+ are recorded, not just one.+ If your game is running faster when keys are pressed, that probably+ means you are updating some world variables on `KeyPress` events too,+ while you should do that only on `Tick` events.++1.8.1.0+-------++- Fixed hcat, vcat, stringPlane, stringPlane documentation to match+ behaviour (i.e. they do not error on empty strong/list).+- Now `subPlane` too does not throw an exception when called with+ inconsistent coordinates, but returns a transparent 1×1 plane.+- Introduced `MalformedGRec` exception for when `readRecord` fails.+- Provided instructions for hot-reloading a game running in normal+ (interactive) mode with various means (tmux, urxvt, etc.).+ Check `example/MainHotReload.hs`.+- Introduced RGB and term colours (patch by José Rafael Vieira).+- Added AUTHORS.++1.8.0.0+-------++- Fixed testing facilities `recordGame`, `testGame`, `narrateGame` and+ similar functions. `testGame` in particular is able to precisely+ emulate recorded environment (so if your game has a bug only at a+ specific size, `testGame` will now catch it).+ Check `cabal run -f alone-playback examples ` to see a replay in action+ and `test/Terminal/Game/Layer/ImperativeSpec.hs` for pure test ideas.+- Added information on how to have an hot-reload mode, albeit only for+ non-interactive game replays. Check `example/MainHotReload.hs` if+ interested.+- Added a new exception, `DisplayTooSmall`, which expands gracefully to+ a “please resize your terminal” message to the player if uncaught.+ Nothing changes if you do not already use `asserTermDims`.+- `assertTermDims` is now curries (`Width -> Height -> IO ()` instead+ of `Dimensions -> IO ()`) to better fit the rest of the API.+- Modified behaviour of functions `vcat`, `hcat`, `stringPlane`,+ `stringPlaneTrans`. They will not error on empty list, rather return+ a transparent, 1×1 plane.+- Changed licence and changes files to COPYING and NEWS.++1.7.0.0+-------++- After some feedback from library users, I decided to eliminate+ `simpleGame` from the API.+ To reiterate hte migration guide, if your type was:++ Game 80 24 13 initState logicFun drawFun quitFun+ -- or+ -- simpleGame (80, 24) 13 initState logicFun drawFun quitFun++ You just need to modify it like this:++ Game 13 initState+ (const logicFun)+ (\e s -> centerFull e $ drawFun s)+ quitFun+ -- notice how we lost `80 24`. You can still have a screen size+ -- check with `assertTermDims`, as described below.+- Added `blankPlaneFull` and `centerFull` convenience functions (to work+ with GEnv terminal dimensions).+- Added assertTermDims, a quick way to check your user terminal is big+ enough at the start of the game.+- minimal blitting optimisation (you should be able to see a 1–2+ FPS improvement).+- improved documentation on various functions.++1.6.0.2+-------++- lun 15 nov 2021, 02:21:08+- more doc tweaking++1.6.0.1+-------++- released lun 15 nov 2021, 00:35:41+- minor documentation / spelling fixes++1.6.0.0+-------++Summary and tl;dr migration guide:+- This version introduces a breaking changes in the main way to make+ a `Game`. I will detail the changes below, but first a three-lines+ migration guide:+ the only thing you should have to do is to replace your `Game`+ data constructor with `simpleGame` smart constructor, and substitute+ the first to `c` `r` arguments (col/row) with a `(c, r)` tuple.+ And of course, if you are interested in displaying FPS and adapt to+ screen size modifications at game-time (“liquid” layout), read along!++Changes:+- This version introduces GEnv, a structure that exposes current frame+ rate (in FPS) and current terminal size (in Width, Height).+- `GEnv` is added as a parameter to logic and draw functions, which+ now have these signatures:+ gLogicFunction :: GEnv -> s -> Event -> slightly+ gDrawFunction :: GEnv -> s -> plane+- If you do not want to dabble with GEnv, you can still use `simpleGame`+ smart constructor, which mimicks the old `Game`. `simpleGame` has some+ nice defaults:+ - if the terminal is too small it will ask the player to resize it+ (even in the middle of the game), blocking any input;+ - if the terminal is bigger, it will paste `Plane` in the middle+ of the screen.+- For this reason, `DisplayTooSmall` exception exists no more.+- the new `Game` does not have those defaults, but allows you to get+ creative with screen resizes, e.g. accomodating as much gameworld+ as possible etc. Check `cabal run -f examples balls` and resize the+ screen to see it in action.+- Minor change: I have introduced a `Dimensions` alias for+ `(Width, Height)`.++Future work:+- these changes lay the path for an even more general `Game` type,+ adding effects like reading form a game configuration, writing to it+ etc.+ I would like to have these wrapped in a pure interface (maybe à la+ Response/Request? Maybe callbacks?) and for sure want them to be+ composable with current test scaffolding (testGame,+ narrateGame, etc.). It will not be easy to design; if are reading+ this and have any suggestion, please write to me.++Released dom 14 nov 2021, 20:25:19++1.5.0.0+-------++- `timers-tick` has released a new version: all timers function (creaTimer,+ creaBoolTimer, creaTimerLoop, creaBoolTimerLoop, creaAnimation,+ creaLoopAnimation, ticks) are slightly more robust now (will `error`+ on nonsenical arguments, e.g. frame duration <1).+ This should not impact any of your current projects, it just makes+ catching bugs easier.+- Removed `getFrames` from Animation interface.+- Updated `Random` interface to fit the new `random`. This is a breaking+ change but it should be easy to fix by updating your `Random` constraints+ to `UniformRange`.+ Be mindful that `recordGame` could play slightly differently, as the+ update function for the StdGen in `random` has changed.+- Removed `getRandomList` from Random interface.+- Added `pickRandom` to Random interface.+- Removed unuseful `creaStaticAnimation` from Animation interface.+- Released mar 9 nov 2021, 15:56:14.++1.4.0.0+-------++- Fixed an annoying bug that made a game run slower than expected on+ low TPS. Now if you select 5 ticks per second, you can rest assured+ that after 50 ticks, 5 seconds have elapsed.+- Renamed `FPS` to `TPS` (ticks per second); highlight logic speed is+ constant timewise on all machines, while FPS might be different on+ differently efficient terminals.+ This will allow in future releases to provide a function to easily+ calculate actual FPS of the game.+- Added alternative origin combinators `%^>`, `%.<`, `%.>`; they are+ useful when you want to — e.g. — «paste a plane one row from+ bottom-right corner».++1.3.0.0+-------++- `displaySize` and `playGame`/`playGameS` now throw an exception+ (of type `ATGException`) instead of `error`ing. These exeptions are+ `CannotGetDisplaySize` and `DisplayTooSmall`; they are synchronous,+ for easier catching. (requested by sm)+- Released sab 16 ott 2021, 21:09:22++1.2.1.0+-------++- Fixed textBox, textBoxHyphen bug (boxes were not transparent, contrary+ to what stated in docs) (reported by sm).+- Released lun 11 ott 2021, 22:29:40++1.2.0.0+-------++- Added textBoxHyphen and textBoxHyphenLiquid and a handful of `Hypenator`s.+ This will allow you to have autohyphenation in textboxes. Compare:+ (normal textbox) (hyphenated textbox)+ Rimasi un po’ a meditare nel buio Rimasi un po’ a meditare nel buio+ velato appena dal barlume azzurrino velato appena dal barlume azzurrino+ del fornello a gas, su cui del fornello a gas, su cui sobbol-+ sobbollliva quieta la pentola. liva quieta la pentola.+- Switched `Width`, `Height`, `Row`, `Col` from `Integer` to `Int`.+ This is unfortunate, but will make playing with `base` simpler. I will+ switch it back once `Prelude` handles both integers appropriately+ or exports the relevant function. (request by sm)+- Changed signature for `box`, `textBox` and `textBoxLiquid`. Now+ width/height parameters come *before* the character/string. E.g.:+ textBoxLiquid :: String -> Width -> Plane -- this was before+ textBoxLiquid :: Width -> String -> Plane -- this is now+ This felt more ergonomic while writing games.+- `paperPlane` is now `planePaper` (to respect SVO order)++1.1.1.0+-------++- Added (***) (centre blit) (request by sm)+- Released gio 30 set 2021, 12:29:22++1.1.0.0+-------++- Added Plane justapoxition functions (===, |||, vcat, hcat).+- Added `word` and and `textBoxLiquid` drawing functions.+- Added `subPlane`, `displaySize` Plane functions.+- Removed unused `trimPlane`.+- Sanitized non-ASCII chars on Win32 console.+- Wed 03 Feb 2021 18:41:20 CET++1.0.0.0+-------++- Milestone release.+- Beefed up documentation.+- Released Sun 08 Dec 2019 04:19:33 CET++0.7.2.0+-------++- Fixed 0.7.1.0 unbumped dependency.+- Released Fri 22 Nov 2019 16:51:25 CET++0.7.1.0+-------++- Fixed 0.7.0.0 (deprecated) interface.+- Released Fri 22 Nov 2019 14:51:40 CET++0.7.0.0+-------++- Simplified Animation interface (breaking changes).+- Added `creaLoopAnimation` and `creaStaticAnimation`.+- Released Fri 22 Nov 2019 14:40:44 CET++0.6.1.0+-------++- Reworked Timers/Animations interface and documentation.+- Added `lapse` (for Timers/Animations).+- Released Fri 22 Nov 2019 01:03:37 CET++0.6.0.1+-------++- Add public repo (requested by sm).+- Released Tue 19 Nov 2019 22:38:34 CET++0.6.0.0+-------++- Add random generation functions.+- Released Sun 10 Nov 2019 13:44:32 CET++0.5.0.0+-------++- Add `setupGame` to setup games before playtesting (skip menus, etc.).+- Fixed screen corruption on Windows.+- Released Fri 08 Nov 2019 13:52:39 CET++0.4.0.0+-------++- Exposed new functions in API.+- Greatly improved haddock documentation.+- Released Tue 25 Jun 2019 16:08:53 CEST++0.2.1.0+-------++- Improved haddock documentation a bit.+- Cleanup runs regardless of exception.+- Released on Sun 18 Mar 2018 03:04:07 CET.++0.2.0.0+-------++- Added dependencies constraints.+- Removed internal module.+- Fixed changelog.+- Released on Fri 16 Mar 2018 00:42:41 CET.++0.1.0.0+-------++- Initial release.+- Released on Fri 16 Mar 2018 00:33:18 CET.
+ README view
@@ -0,0 +1,52 @@+==================+ansi-terminal-game+==================++`ansi-terminal-game` is a library for creating games in a terminal setting.++Goals+-----++- be cross platform (linux/win/mac). If you plan to have your executable+ unix only, I invite you to check brick [1] or other, more expressive+ libraries.+- be simple: no curses/ncurses/pdcurses/etc. dependencies, all+ functionality built on a standard input / ANSI terminal base.++[1] http://hackage.haskell.org/package/brick++Learn+-----++- run the basic example with `cabal new-run -f examples alone`;+- check the source in `examples/Alone.hs`;+- open the 'Terminal.Game' haddock documentation (start reading from+ `data Game`).++A full game can be found at:++ http://www.ariis.it/static/articles/venzone/page.html++Other games made with a-t-g+---------------------------++- caverunner:+ https://github.com/simonmichael/games/blob/main/caverunner/caverunner.hs+- pigafetta: http://www.ariis.it/link/repos/pigafetta/+- avoidance: https://sabadev.xyz/avoidance_game++If you want yours to be added to this list, write to me.++Contact+-------++For any feedback or report, contact me at:++ http://ariis.it/static/articles/mail/page.html++Contributing+------------++browse repo: http://www.ariis.it/static/repos/stagit/ansi-terminal-game/+Upload your modifications somewhere and send me an email (<fa-ml@ariis.it>).+
ansi-terminal-game.cabal view
@@ -1,55 +1,80 @@ name: ansi-terminal-game-version: 0.2.1.0-synopsis: sdl-like functions for terminal applications, based on- ansi-terminal+version: 1.9.4.0+synopsis: cross-platform library for terminal games description: Library which aims to replicate standard 2d game functions (blit, ticks, timers, etc.) in a terminal- setting.+ setting; features double buffering to optimise+ performance. Aims to be cross compatible (based on "ansi-terminal", no unix-only dependencies), practical.- This is a proof of concept release, used to implement- @http://www.ariis.it/static/articles/animascii/page.html@- . See example folder for some minimal programs.-homepage: none-yet+ See @examples@ folder for some minimal programs. A+ full game: <http://www.ariis.it/static/articles/venzone/page.html venzone>.+homepage: http://www.ariis.it/static/articles/libraries/page.html#ansi-terminal-game license: GPL-3-license-file: LICENSE-author: Francesco Ariis+license-file: COPYING+author: Francesco Ariis et al. (see AUTHORS) maintainer: fa-ml@ariis.it-copyright: © 2017-2018 Francesco Ariis+copyright: © 2017-2023 Francesco Ariis et al. category: Game build-type: Simple-extra-source-files: changes.txt-cabal-version: >=1.10+extra-source-files: README,+ NEWS,+ AUTHORS,+ test/records/alone-record-test.gr,+ test/records/alone-record-left.gr,+ test/records/balls-dims.gr,+ test/records/balls-slow.gr+cabal-version: >= 1.10 -flag example+flag examples description: builds examples default: False +source-repository head+ type: git+ location: http://www.ariis.it/static/repos/stagit/ansi-terminal-game/+ library exposed-modules: Terminal.Game- other-modules: Terminal.Game.ANSI,- Terminal.Game.Plane,+ other-modules: Terminal.Game.Animation,+ Terminal.Game.Character, Terminal.Game.Draw,- Terminal.Game.Animation,- Terminal.Game.Input,+ Terminal.Game.Layer.Imperative,+ Terminal.Game.Layer.Object,+ Terminal.Game.Layer.Object.GameIO,+ Terminal.Game.Layer.Object.Interface,+ Terminal.Game.Layer.Object.IO,+ Terminal.Game.Layer.Object.Narrate,+ Terminal.Game.Layer.Object.Primitive,+ Terminal.Game.Layer.Object.Record,+ Terminal.Game.Layer.Object.Test,+ Terminal.Game.Plane,+ Terminal.Game.Random, Terminal.Game.Timer,- Terminal.Game.GameLoop Terminal.Game.Utils build-depends: base == 4.*,- ansi-terminal == 0.8.*,+ ansi-terminal >= 1.0 && < 1.2, array == 0.5.*,- bytestring == 0.10.*,+ bytestring >= 0.10 && < 0.13, cereal == 0.5.*,- clock == 0.7.*,- linebreak == 1.0.*,+ clock >= 0.7 && < 0.9,+ containers >= 0.6 && < 0.9,+ exceptions == 0.10.*,+ file-embed >= 0.0.15 && < 0.1,+ linebreak == 1.1.*,+ mintty == 0.1.*,+ mtl >= 2.2 && < 2.4,+ QuickCheck >= 2.13 && < 2.18,+ random >= 1.2 && < 1.4, split == 0.2.*, terminal-size == 0.3.*,- timers-tick == 0.4.*+ unidecode >= 0.1.0 && < 0.2,+ timers-tick > 0.5 && < 0.6,+ colour >= 2.3.6 && < 2.4 hs-source-dirs: src default-language: Haskell2010+ ghc-options: -Wall - -- horrible horrible horrible hack to make unbuffered input- -- work on Windows if os(windows) hs-source-dirs: platform-dep/windows if !os(windows)@@ -57,29 +82,114 @@ test-suite test default-language: Haskell2010- ghc-options: -Wall- HS-Source-Dirs: test, src+ hs-Source-Dirs: test, src, example main-is: Test.hs- build-depends: base==4.*,- array,- linebreak+ other-modules: Alone,+ Balls,+ Terminal.Game,+ Terminal.Game.Animation,+ Terminal.Game.Character,+ Terminal.Game.Draw,+ Terminal.Game.DrawSpec,+ Terminal.Game.Layer.Imperative,+ Terminal.Game.Layer.ImperativeSpec,+ Terminal.Game.Layer.Object,+ Terminal.Game.Layer.Object.GameIO,+ Terminal.Game.Layer.Object.Interface,+ Terminal.Game.Layer.Object.IO,+ Terminal.Game.Layer.Object.Narrate,+ Terminal.Game.Layer.Object.Primitive,+ Terminal.Game.Layer.Object.Record,+ Terminal.Game.Layer.Object.Test,+ Terminal.Game.Layer.Object.TestSpec,+ Terminal.Game.Utils,+ Terminal.Game.Plane,+ Terminal.Game.PlaneSpec+ Terminal.Game.Random,+ Terminal.Game.RandomSpec+ build-depends: base == 4.*,+ ansi-terminal >= 1.0 && < 1.2,+ array == 0.5.*,+ bytestring >= 0.10 && < 0.13,+ cereal == 0.5.*,+ clock >= 0.7 && < 0.9,+ containers >= 0.6 && < 0.9,+ exceptions == 0.10.*,+ file-embed >= 0.0.15 && < 0.1,+ linebreak == 1.1.*,+ mintty == 0.1.*,+ mtl >= 2.2 && < 2.4,+ QuickCheck >= 2.13 && < 2.18,+ random >= 1.2 && < 1.4,+ split == 0.2.*,+ terminal-size == 0.3.*,+ unidecode >= 0.1.0 && < 0.2,+ timers-tick > 0.5 && < 0.6,+ colour >= 2.3.6 && < 2.4 -- the above plus hspec- , hspec+ , hspec >= 2.10.1 && < 2.12+ build-tool-depends: hspec-discover:hspec-discover type: exitcode-stdio-1.0+ ghc-options: -Wall -executable alone-in-a-room- if flag(example)- build-depends: base == 4.*,- ansi-terminal-game+ if os(windows)+ hs-source-dirs: platform-dep/windows+ if !os(windows)+ hs-source-dirs: platform-dep/non-win++executable alone+ if flag(examples)+ build-depends: base == 4.*,+ ansi-terminal-game else- buildable: False+ buildable: False hs-source-dirs: example- main-is: Alone.hs+ main-is: MainAlone.hs+ other-modules: Alone default-language: Haskell2010- -- With the -threaded option, only foreign calls with the unsafe- -- attribute will block all other threads.- -- TODO [breaking] [severe] [u:3] senza threaded non compila sin windows- -- required on win before ghc 8.4? ghc-options: -threaded+ -Wall +executable alone-playback+ if flag(examples)+ build-depends: base == 4.*,+ ansi-terminal-game,+ temporary == 1.3.*+ else+ buildable: False++ hs-source-dirs: example+ main-is: MainPlayback.hs+ other-modules: Alone+ default-language: Haskell2010+ ghc-options: -threaded+ -Wall++executable balls+ if flag(examples)+ build-depends: base == 4.*,+ ansi-terminal-game+ else+ buildable: False++ hs-source-dirs: example+ main-is: MainBalls.hs+ other-modules: Balls+ default-language: Haskell2010+ ghc-options: -threaded+ -Wall++executable hot-reload+ if flag(examples)+ build-depends: base == 4.*,+ ansi-terminal-game+ else+ buildable: False++ hs-source-dirs: example+ main-is: MainHotReload.hs+ other-modules: Alone+ default-language: Haskell2010+ ghc-options: -threaded+ -Wall
− changes.txt
@@ -1,20 +0,0 @@-0.2.1.0----------- Improved haddock documentation a bit.-- Cleanup runs regardless of exception.-- Released on Sun 18 Mar 2018 03:04:07 CET.--0.2.0.0----------- Added dependencies constraints.-- Removed internal module.-- Fixed changelog.-- Released on Fri 16 Mar 2018 00:42:41 CET.--0.1.0.0----------- Initial release.-- Released on Fri 16 Mar 2018 00:33:18 CET.
example/Alone.hs view
@@ -1,40 +1,46 @@-module Main where+module Alone where --- alone in a room: a scary game--- vai a dormire--- scary room--- escape--- blink exit, blink enemies--- pit--- randomly generated+-- Alone in a room, game definition (logic & draw)+-- run with: cabal new-run -f examples alone import Terminal.Game -main :: IO ()-main = gameLoop "Alone in a room"- (GameState (10, 10) Stop False)- logicFun- drawFun- (\gs -> gsQuit gs)- 5+import qualified Data.Tuple as T --- STATE --+-- game specification+aloneInARoom :: Game MyState ()+aloneInARoom = Game 13 -- ticks per second+ (MyState (10, 10) Stop) -- init state+ (\_ s e -> logicFun s e) -- logic function+ (\r s -> centerFull r $+ drawFun s) -- draw function -data GameState = GameState { gsCoord :: (Integer, Integer),- gsMove :: Move,- gsQuit :: Bool }+sizeCheck :: IO ()+sizeCheck = let (w, h) = T.swap . snd $ boundaries+ in assertTermDims w h +-------------------------------------------------------------------------------+-- Types++data MyState = MyState { gsCoord :: Coords,+ gsMove :: Move }+ deriving (Show, Eq)+ data Move = N | S | E | W | Stop deriving (Show, Eq) -logicFun :: GameState -> Maybe Char -> IO GameState-logicFun gs (Just 'q') = return $ gs { gsQuit = True }-logicFun (GameState cs m b) Nothing =- return $ GameState (pos m cs) m b -- xxx duplicated code-logicFun (GameState cs m b) (Just c) =- let m' = move m c- in return $ GameState (pos m' cs) m' b+boundaries :: (Coords, Coords)+boundaries = ((1, 1), (24, 80)) +-------------------------------------------------------------------------------+-- Logic++logicFun :: MyState -> Event -> Either () MyState+logicFun _ (KeyPress 'q') = Left ()+logicFun gs Tick = Right $ gs { gsCoord = pos (gsMove gs)+ (gsCoord gs) }+logicFun gs (KeyPress c) = Right $ gs { gsMove = move (gsMove gs) c }+ -- SCI movement move :: Move -> Char -> Move move N 'w' = Stop@@ -47,22 +53,36 @@ move _ 'd' = E move m _ = m --- todo add boundaries-pos :: Move -> (Integer, Integer) -> (Integer, Integer)-pos Stop cs = cs-pos N (r, c) = (r-1, c )-pos S (r, c) = (r+1, c )-pos E (r, c) = (r , c+1)-pos W (r, c) = (r , c-1)+pos :: Move -> (Width, Height) -> (Width, Height)+pos m oldcs | oob newcs = oldcs+ | otherwise = newcs+ where+ newcs = new m oldcs + new Stop cs = cs+ new N (r, c) = (r-1, c )+ new S (r, c) = (r+1, c )+ new E (r, c) = (r , c+1)+ new W (r, c) = (r , c-1) --- DRAW --+ ((lr, lc), (hr, hc)) = boundaries+ oob (r, c) = r <= lr || c <= lc ||+ r >= hr || c >= hc -drawFun :: GameState -> Plane-drawFun (GameState (r, c) _ _) =- blankPlane 80 25 &- (1, 1) % box '_' 80 25 &- (2, 2) % box ' ' 78 23 &- (15, 20) % textBox "tap WASD to move, tap again to stop"- 10 4 &- (r, c) % cell '@'+-------------------------------------------------------------------------------+-- Draw++drawFun :: MyState -> Plane+drawFun (MyState (r, c) _) =+ blankPlane mw mh &+ (1, 1) % box mw mh '.' &+ (2, 2) % box (mw-2) (mh-2) ' ' &+ (15, 20) % textBox 10 4+ "Tap WASD to move, tap again to stop." &+ (20, 60) % textBox 8 10+ "Press Q to quit." # color Blue Vivid &+ (r, c) % cell '@' # invert+ where+ mh :: Height+ mw :: Width+ (mh, mw) = snd boundaries
+ example/Balls.hs view
@@ -0,0 +1,188 @@+module Balls where++-- library module for `balls`++import Terminal.Game++import qualified Data.Bool as B+import qualified Data.Ix as I+import qualified Data.Maybe as M+import qualified Data.Tuple as T++{-+ There are three things I will showcase in this example:++ 1. ** How you can display current FPS. **+ This is done using information passed via `GEnv` (eFPS).++ 2. ** How your game can gracefully handle screen resize. **+ Notice how if you resize the terminal, balls will still+ fill the entire screen. This is again possible using `Game`+ and the information passed via GEnv (in this case, terminal+ dimensions).++ 3. ** That — while FPS can change — game speed does not. **+ Check the timer: even when screen is crowded and frames are+ dropped, it is not slowed down.+++ This game runs at 60 FPS, you will almost surely never need such+ a high TPS! 15–20 is more than enough in most cases.+-}++-------------------------------------------------------------------------------+-- Ball++data Ball = Ball { pChar :: Plane,+ pSpeed :: Timed Bool,+ pDir :: Coords,+ pPos :: Coords }++-- change direction is necessary, then and move+modPar :: Dimensions -> Ball -> Maybe Ball+modPar ds b@(Ball _ _ d _) =+ -- tick the ball and check it is time to move+ let b' = tickBall b in+ if not (fetchFrame . pSpeed $ b')+ then Just b' -- no time to move for you+ else++ -- check all popssible directions+ let pd = [d, togR d, togC d, togB d]+ bs = map (\ld -> b' { pDir = ld }) pd+ bs' = filter (isIn ds) $ map modPos bs in++ -- returns a moved ball nor nothing to mark it “to eliminate”+ case bs' of+ [] -> Nothing+ (cp:_) -> Just cp+ where+ togR (wr, wc) = (-wr, wc)+ togC (wr, wc) = ( wr, -wc)+ togB (wr, wc) = (-wr, -wc)++tickBall :: Ball -> Ball+tickBall b = b { pSpeed = tick (pSpeed b) }++modPos :: Ball -> Ball+modPos (Ball p t d@(dr, dc) (r, c)) = Ball p t d (r+dr, c+dc)++isIn :: Dimensions -> Ball -> Bool+isIn (w, h) (Ball p _ _ (pr, pc)) =+ let (pw, ph) = planeSize p+ in pr >= 1 &&+ pr+ph-1 <= h &&+ pc >= 1 &&+ pc+pw-1 <= w++dpart :: Ball -> (Coords, Plane)+dpart (Ball p _ _ cs) = (cs, p)++genBall :: StdGen -> Dimensions -> (Ball, StdGen)+genBall g ds =+ let (c, g1) = pickRandom [minBound..] g+ (s, g2) = getRandom (1, 3) g1+ (v, g3) = pickRandom dirs g2+ (p, g4) = ranIx ((1,1), T.swap ds) g3+ b = Ball (cell 'o' # color c Vivid)+ (creaBoolTimerLoop s) v p+ in (b, g4)+ where+ dirs = [(1, 1), (1, -1), (-1, 1), (-1, -1)]++ -- tuples instances are yet to be added to `random`+ -- as nov 21; this will do meanwhile.+ ranIx :: I.Ix a => (a, a) -> StdGen -> (a, StdGen)+ ranIx r wg = pickRandom (I.range r) wg++-------------------------------------------------------------------------------+-- Timer++type Timer = (Timed Bool, Integer)++ctimer :: TPS -> Timer+ctimer tps = (creaBoolTimerLoop tps, 0)++ltimer :: Timer -> Timer+ltimer (t, i) = let t' = tick t+ k = B.bool 0 1 (fetchFrame t')+ in (t', i+k)++dtimer :: Timer -> Plane+dtimer (_, i) = word . show $ i++-------------------------------------------------------------------------------+-- Game++data GState = GState { gen :: StdGen,+ timer :: Timer,+ balls :: [Ball],+ bslow :: Bool }+ -- pSlow is not used in game, it is there just+ -- for the test suite++fireworks :: StdGen -> Game GState Int+fireworks g = Game tps istate lfun dfun+ where+ tps = 60++ istate :: GState+ istate = GState g (ctimer tps) [] False++-------------------------------------------------------------------------------+-- Logic++-- The `Int` in `Either Int Gstate` is: number of balls+-- on screen at the end of the game.+lfun :: GEnv -> GState -> Event -> Either Int GState+lfun e s (KeyPress 's') =+ let g = gen s+ ds = eTermDims e+ (b, g1) = genBall g ds+ s' = s { gen = g1,+ balls = b : balls s }+ in Right s'+lfun _ s (KeyPress 'q') = Left $ length (balls s)+lfun _ s (KeyPress _) = Right s+lfun r s Tick =+ let ds = eTermDims r++ ps = balls s+ ps' = M.mapMaybe (modPar ds) ps++ bs = eFPS r < 30+ s' = s { timer = ltimer (timer s),+ balls = filter (isIn ds) ps',+ bslow = bs }+ in Right s'++-------------------------------------------------------------------------------+-- Draw++dfun :: GEnv -> GState -> Plane+dfun r s = mergePlanes+ (uncurry blankPlane ds)+ (map dpart $ balls s) &+ (1, 2) %^> tui # trans &+ (1, 2) %.< inst # trans # bold+ where+ ds = eTermDims r+ tm = timer s++ tui :: Plane+ tui = let fps = eFPS r+ np = length $ balls s++ l1 = word "FPS: " ||| word (show fps)+ l2 = word "Timer: " ||| dtimer tm+ l3 = word ("Balls: " ++ show np)+ l4 = word ("Term. dims.: " ++ show ds)+ in vcat [l1, l2, l3, l4]++ inst :: Plane+ inst = word "Press (s) to spawn" ===+ word "Press (q) to quit" ===+ word "Resize terminal to pop balls"++ trans :: Draw+ trans = makeTransparent ' '
+ example/MainAlone.hs view
@@ -0,0 +1,12 @@+module Main where+++import Alone ( aloneInARoom, sizeCheck )++import Terminal.Game++-- run with: cabal new-run -f examples alone++main :: IO ()+main = do sizeCheck+ errorPress $ playGame aloneInARoom
+ example/MainBalls.hs view
@@ -0,0 +1,22 @@+module Main where++import Balls++import Terminal.Game++-- Balls Main module. The meat of the game is in `examples/Balls.hs`++main :: IO ()+main = do+ g <- getStdGen+ r <- playGame (fireworks g)+ -- We use game result `r` (how many balls were on+ -- screen) and feed it to another function.+ -- This could be useful to upload high scores to+ -- a site, or for a game embedded in a larger pro-+ -- gram, etc.+ putStrLn (bye r)+ where+ bye wi = "See you later!\nYou left the game with " +++ show wi ++ " balls on screen."+
+ example/MainHotReload.hs view
@@ -0,0 +1,87 @@+module Main where++import Alone ( aloneInARoom, sizeCheck )++import Terminal.Game++-- Hot reloading is a handy feature while writing a game. Here I will+-- show you various ways to do that with ansi-terminal-game.+--+-- Hot reloading makes use of `entr` (install it from your repos) and+-- some additional scaffolding, provided by `tmux` or your plain terminal.+-- Read below to see two ideas in action.+++{- === HOT RELOAD WITH ENTR AND TMUX ===++ 1. Install `entr` and `tmux`.++ 2. open a tmux window, in the bottom-right plane launch the game+ in an infinite loop, e.g.++ while true; do cabal run -f examples alone; done++ Remember, the pane has to be the bottom-right one, like this:++ +----------------------------------------------++ | | |+ | | |+ | | |+ | | |+ | | |+ | | |+ | |---------------------|+ | | |+ | | |+ | | |+ | | G A M E |+ | | |+ | | |+ | | |+ +----------------------------------------------+++ 3. in another pane launch `entr` in this fashion:++ find src example/ -name *.hs | entr tmux send-keys -t {bottom-right} q++ 4. Now whenever you modify a source file in example/ , the game+ will be reloaded! -}+++{- === HOT RELOAD WITH ENTR AND PLAIN TERMINAL ===++ 1. If your terminal has a `command` option, it is even easier.+ We will use `urxvt` for this example++ find src example/*.hs | entr -r urxvt -e cabal run -f examples alone++ 2. Every time you save a source file, a terminal will be spawn with+ your game running in it. -}+++{- === HOT RELOAD REPLAYS WITH ENTR ===++ `entr` by itself *cannot* autoreload a game in the same window, as it+ cannot handle interactive programs. But if you are just displaying a+ replay, this can come handy++ find example/*.hs | entr -cr cabal run -f examples hot-reload++ This is very useful to incrementally build NPCs’ behaviour,+ iron out mechanics bugs etc.++ Remember that you can use `recordGame` to record a session. -}+++-- If you you need something fancier for your game (e.g. hot reloading user+-- maps), `venzone` [1] (module Watcher) has a builtin /watch mode/ you can+-- take inspiration from.+--+-- [1] https://hackage.haskell.org/package/venzone++main :: IO ()+main = do+ sizeCheck+ gr <- readRecord "test/records/alone-record-test.gr"+ -- check `readRecord+ () <$ narrateGame aloneInARoom gr
+ example/MainPlayback.hs view
@@ -0,0 +1,30 @@+module Main where++import Alone ( aloneInARoom, sizeCheck )++import Terminal.Game++import System.IO.Temp ( emptySystemTempFile )++-- plays the game and, once you quit, shows a replay of the session+-- run with: cabal new-run -f examples alone-playback++main :: IO ()+main = do+ sizeCheck+ tf <- emptySystemTempFile "alone-record.gr"+ playback tf++playback :: FilePath -> IO ()+playback f = do+ prompt "Press <Enter> to play the game."+ recordGame aloneInARoom f+ prompt "Press <Enter> to watch playback."+ es <- readRecord f+ _ <- narrateGame aloneInARoom es+ prompt "Playback over! Press <Enter> to quit."+ where+ prompt :: String -> IO ()+ prompt s = putStrLn s >> () <$ getLine++
− platform-dep/non-win/Terminal/Game/Input.hs
@@ -1,9 +0,0 @@------------------------------------------------------------------------------------ Nonbuffering getChar--- 2017 Francesco Ariis GPLv3 80cols-----------------------------------------------------------------------------------module Terminal.Game.Input where--inputCharTerminal :: IO Char-inputCharTerminal = getChar
+ platform-dep/non-win/Terminal/Game/Utils.hs view
@@ -0,0 +1,14 @@+--------------------------------------------------------------------------------+-- Nonbuffering getChar+-- 2017 Francesco Ariis GPLv3 80cols+--------------------------------------------------------------------------------++module Terminal.Game.Utils ( inputCharTerminal,+ isWin32Console )+ where++inputCharTerminal :: IO Char+inputCharTerminal = getChar++isWin32Console :: IO Bool+isWin32Console = return False
− platform-dep/windows/Terminal/Game/Input.hs
@@ -1,22 +0,0 @@------------------------------------------------------------------------------------ Nonbuffering getChar--- 2017 Francesco Ariis GPLv3 80cols----------------------------------------------------------------------------------{-# LANGUAGE ForeignFunctionInterface #-}---- see .cabal conditionals for more info--module Terminal.Game.Input where--import qualified Foreign.C.Types as FT-import qualified Data.Char as C--inputCharTerminal :: IO Char-inputCharTerminal = getCharWindows---- no idea but unsafe breaks it-getCharWindows :: IO Char-getCharWindows = fmap (C.chr . fromEnum) c_getch-foreign import ccall safe "conio.h getch"- c_getch :: IO FT.CInt-
+ platform-dep/windows/Terminal/Game/Utils.hs view
@@ -0,0 +1,30 @@+--------------------------------------------------------------------------------+-- Nonbuffering getChar et al+-- 2017 Francesco Ariis GPLv3 80cols+--------------------------------------------------------------------------------+{-# LANGUAGE ForeignFunctionInterface #-}++-- horrible horrible horrible hack to make unbuffered input+-- work on Windows (and win32console check)++module Terminal.Game.Utils (inputCharTerminal,+ isWin32Console )+ where++import qualified Data.Char as C+import qualified Foreign.C.Types as FT+import qualified System.Console.MinTTY as M++inputCharTerminal :: IO Char+inputCharTerminal = getCharWindows++-- no idea why, but unsafe breaks it+getCharWindows :: IO Char+getCharWindows = fmap (C.chr . fromEnum) c_getch+foreign import ccall safe "conio.h getch"+ c_getch :: IO FT.CInt++-- TODO elimina isWin32Console [win]+-- not perfect, but it is what it is (on win, non minTTY)+isWin32Console :: IO Bool+isWin32Console = not <$> M.isMinTTY
src/Terminal/Game.hs view
@@ -1,8 +1,8 @@---------------------------------------------------------------------------------+------------------------------------------------------------------------------- -- | -- Module : Terminal.Game--- Copyright : © 2017-2018 Francesco Ariis--- License : GPLv3 (see LICENSE file)+-- Copyright : © 2017-2023 Francesco Ariis et al.+-- License : GPLv3 (see COPYING) -- -- Maintainer : Francesco Ariis <fa-ml@ariis.it> -- Stability : provisional@@ -10,70 +10,246 @@ -- -- Machinery and utilities for 2D terminal games. -----------------------------------------------------------------------------------+-- New? Start from 'Game'.+--+------------------------------------------------------------------------------- -- Basic col-on-black ASCII terminal, operations. -- Only module to be imported. --- todo color [release]--- todo resize screen corruption [grave]--- add docs [release]+-- todo you can eliminate unidecode!+-- todo you probably can eliminate part of platform dep too (ffi)+-- (anche check what is linked -- see lib.so.8 error)+-- todo you should check again on win the «press exit to continue» test+-- todo write that ascii/latin-1/greek letters should be ok on win -module Terminal.Game ( -- * Game Loop- gameLoop,- -- * Plane- Plane, -- types- Coords, Width, Height,- stringPlane, -- crea- blankPlane, addVitrum,- copyPlane, pastePlane, -- slice- planeSize, paperPlane, -- info- -- * Draw- (%), (&), cell, -- draw- box, textBox,- -- * Animations- Animation, Loop(..), -- types- creaAni,- tick, reset, -- operate- -- xxx metti fetchframe in- -- documentation- -- e anche isexpired- getFrames,- encodeAni, -- serialise- decodeAni,- -- * Timers- Timed, -- types- ExpBehaviour(..),- creaTimer, -- crea- creaBoolTimer,- fetchFrame, isExpired, -- operate- -- * Utils- screenSize+-- todo rewrite test w/ an internal library method++module Terminal.Game ( -- * Running+ TPS,+ FPS,+ Event(..),+ GEnv(..),+ Game(..),+ playGame,+ ATGException(..),++ -- ** Helpers+ playGame_,+ Terminal.Game.displaySize,+ assertTermDims,+ errorPress,+ blankPlaneFull,+ centerFull,++ -- * Game logic+ -- | Some convenient function dealing with+ -- Timers ('Timed') and 'Animation's.+ --+ -- Usage of these is not mandatory: 'Game' is+ -- parametrised over any state @s@, you are free+ -- to implement game logic as you prefer.++ -- ** Timers/Animations++ -- *** Timers+ Timed,+ creaTimer, creaBoolTimer,+ creaTimerLoop, creaBoolTimerLoop,++ -- *** Animations+ Animation,+ creaAnimation,+ creaLoopAnimation,++ -- *** T/A interface+ tick, ticks, reset, lapse,+ fetchFrame, isExpired,++ -- ** Random numbers+ StdGen,+ getStdGen, mkStdGen,+ getRandom, pickRandom,+ UniformRange,++ -- * Drawing+ -- | To get to the gist of drawing, check the+ -- documentation for '%'.+ --+ -- Blitting on screen is double-buffered and diff'd+ -- (at each frame, only cells with changed character+ -- will be redrawn).++ -- ** Plane+ Plane,+ Dimensions,+ Coords,+ Row, Column,+ Width, Height,+ blankPlane,+ stringPlane,+ stringPlaneTrans,+ makeTransparent,+ makeOpaque,+ planePaper,+ planeSize,++ -- *** Performance blitting+ -- $performance++ Cell,+ creaCell,+ colorCell,+ rgbColorCell,+ paletteColorCell,+ boldCell,+ reverseCell,+ cellsPlane,++ -- ** Draw+ Draw,+ (%), (&), (#),+ subPlane,+ mergePlanes,+ cell, word, box,+ Color(..), ColorIntensity(..),+ color, bold, invert,++ -- *** Alternative origins+ -- $origins+ (%^>), (%.<), (%.>),++ -- *** Text boxes+ textBox, textBoxLiquid,+ textBoxHyphen, textBoxHyphenLiquid,+ Hyphenator,+ -- | Eurocentric convenience reexports. Check+ -- "Text.Hyphenation.Language" for more languages.+ english_GB, english_US, esperanto,+ french, german_1996, italian, spanish,++ -- *** Declarative drawing+ (|||), (===), (***), hcat, vcat,++ -- *** Non-standard colors+ -- | Non-standard RGB and xterm colors. These are+ -- prettier, but work on a minority of terminal+ -- emulators/multiplexers.+ -- Use them only on your machine or when you are sure+ -- of the terminal you are targetting.+ S.Colour, rgbColor, paletteColor,+ S.sRGB24, S.sRGBBounded, S.sRGB, S.sRGB24read,+ xterm6LevelRGB, xterm24LevelGray, xtermSystem,++ -- * Testing+ GRec,+ recordGame,+ readRecord,+ testGame,+ setupGame,+ narrateGame,++ -- | A quick and dirty way to have /hot reload/+ -- (autorestarting your game when source files change)+ -- is illustrated in @example/MainHotReload.hs@.++ -- * Embedding files++ -- | Embedding files is convenient when working on+ -- assets separately and still wanting to ship a+ -- single binary. Remember to add this pragma to+ -- the top of your module:+ --+ -- > {-# LANGUAGE TemplateHaskell #-}+ embedFile,+ embedDir,+ BC.ByteString,+ BC.unpack,++ -- * Cross platform+ -- $xcompat ) where -import Terminal.Game.GameLoop-import Terminal.Game.Plane-import Terminal.Game.Draw--- xxx rivedi gli export di animation, encapsula tutto il possibile+import Data.FileEmbed+import System.Console.ANSI import Terminal.Game.Animation-import Terminal.Game.Timer-import Terminal.Game.Utils+import Terminal.Game.Draw+import Terminal.Game.Layer.Imperative+import Terminal.Game.Layer.Object as O+import Terminal.Game.Plane+import Terminal.Game.Random+import Text.LineBreak --- todo text deve essere gestito da una cosa smart, pensa a pp leijin+import qualified Control.Monad as CM+import qualified Data.ByteString.Char8 as BC+import qualified Data.Colour.SRGB as S --- todo [post-first-release] setTitle hSupportsANSI bold? italics? intensity+-- $origins+-- Placing a plane is sometimes more convenient if the coordinates origin+-- is a corner other than top-left (e.g. “Paste this plane one row from+-- bottom-left corner”). These combinators — meant to be used instead of '%'+-- — allow you to do so. Example:+--+-- @+-- prova :: Plane+-- prova = let rect = box 6 3 \'.\'+-- letters = word "ab"+-- in rect &+-- (1, 1) %.> letters -- start from bottom-right+--+-- -- λ> putStr (planePaper prova)+-- -- ......+-- -- ......+-- -- ....ab+-- @ --- todo geekosaur threaded shit bug+-- $xcompat+-- Good practices for cross-compatibility:+--+-- * Choose game dimensions of no more than __24 rows__ and __80 columns__.+-- This ensures compatibility with the trickiest terminals (i.e. Win32+-- console).+--+-- * Use __ASCII characters__ only. Again this is for Win32 console+-- compatibility, until+-- [this GHC bug](https://gitlab.haskell.org/ghc/ghc/issues/7593) gets+-- fixed.+--+-- * Employ colour sparingly: as some users will play your game in a+-- light-background terminal and some in a dark one, choose only colours+-- that go well with either (blue, red, etc.).+--+-- * Some terminals/multiplexers (i.e. tmux) do not make a distinction+-- between vivid/dull, others do not display bold; do not base your game+-- mechanics on that difference.+--+-- * If you use WASD for movement, you can readily gain compatibility with+-- AZERTY keyboard layout by mapping “up” to both @W@ and @Z@ and “left”+-- to @A@ and @Q@. Users from France, Belgium, and Québec will thank you. --- geekosaur f-a, a. heh. so the thoguht I had turned out to--- be correct:--- Control.Concurent.rtsSupportsBoundThreads (because--- green threads can't be usefully bound)--- x hiratara--- [~hiratara@240f:7:4708:1:47a:b8c4:d18a:d063]--- has left Ping timeout: 276 seconds [#haskell]--- geekosaur so if that produces True, it's using the--- threaded runtime; if False, you can say--- something useful instead of hanging+-- $performance+-- Using 'Plane' for graphics is convenient due to the many primitives+-- for drawing. If you need extra performance, @ansi-terminal-game@+-- exposes 'Cell' functions too, for efficient dense-grid drawing.+--+-- Remember that most likely the bottleneck for your app will still be+-- the terminal your users run! +-- | /Usable/ terminal display size (on Win32 console the last line is+-- set aside for input). Throws 'CannotGetDisplaySize' on error.+displaySize :: IO Dimensions+displaySize = O.displaySizeErr++-- | Check if terminal can accomodate 'Dimensions', otherwise throws+-- 'DisplayTooSmall' with a helpful message for the player.+assertTermDims :: Width -> Height -> IO ()+assertTermDims dw dh =+ clearScreen >>+ setCursorPosition 0 0 >>+ displaySizeErr >>= \ads ->+ CM.when (isSmaller ads)+ (throwExc $ DisplayTooSmall (dw, dh) ads)+ where+ isSmaller :: Dimensions -> Bool+ isSmaller (ww, wh) = ww < dw || wh < dh
− src/Terminal/Game/ANSI.hs
@@ -1,96 +0,0 @@----------------------------------------------------------------------------------- ANSI terminal display--- (C) 2017 Francesco Ariis (GPL v3)------------------------------------------------------------------------------------ Plane to ANSI terminal display--module Terminal.Game.ANSI where--import Terminal.Game.Draw-import Terminal.Game.Plane--import qualified System.Console.ANSI as CA-import qualified Data.List.Split as LS-import qualified Control.Monad as CM-import qualified Data.Array as A---- xxx elmina 80 cols---- th tw: terminal width and height--- pn: new plane, po: old plane--- fps sono gli fps attuali, puoi stamparli come preferisci (o non stamparli)--- wo, ho: dimensions of the terminal. If they change, reinit double buffering-blitPlane :: Width -> Height -> Maybe Plane -> Plane -> Integer -> IO ()-blitPlane tw th mpo pn cFps =-- -- old plane- let- (pw, ph) = planeSize pn- bp = blankPlane pw ph- po = pastePlane (maybe bp id mpo) bp (1, 1)- in-- -- new plane- let pn' = pastePlane pn bp (1, 1)- pn'' = pastePlane (textBox (show cFps) 100 100) pn' (1, 2)- in-- -- reset formatting and print everything- -- CA.setSGR [CA.Reset, CA.SetColor CA.Background CA.Dull CA.Black] >>- CA.setSGR [CA.Reset] >>- blitMap po pn' tw th----------------------- ANCILLARIES ------------------------ plane + term w/h-blitMap :: Plane -> Plane -> Width -> Height -> IO ()-blitMap po pn tw th = CM.when (planeSize po /= planeSize pn)- (error "blitMap: different plane sizes") >>- CA.setCursorPosition (fi cr) (fi cc) >>- blitToTerminal cc (orderedCells po) (orderedCells pn)- where- (pw, ph) = planeSize pn-- cr = div (th - ph) 2- cc = div (tw - pw) 2-- fi = fromIntegral--orderedCells :: Plane -> [[Cell]]-orderedCells p = LS.chunksOf (fromIntegral w) cells- where- -- todo altra funzione invece che un map 2nd?- cells = map snd $ assocsPlane p- (w, _) = planeSize p----- ordered sequence of cells, both old and new, like they were a String to--- print to screen-blitToTerminal :: Column -> [[Cell]] -> [[Cell]] -> IO ()-blitToTerminal rc ocs ncs = mapM_ blitLine oldNew- where- oldNew :: [[(Cell, Cell)]]- oldNew = zipWith zip ocs ncs-- blitLine :: [(Cell, Cell)] -> IO ()- blitLine ccs = CM.foldM blitChar 0 ccs >>- CA.cursorDown 1 >>- CA.setCursorColumn (fromIntegral rc)-- -- k is "spaces to skip"- blitChar :: Int -> (Cell, Cell) -> IO Int- blitChar k (clo, cln)- | cln == clo = return (k+1)- | otherwise = moveIf k >>= \k' ->- putChar (cellChar cln) >>- return k'-- moveIf :: Int -> IO Int- moveIf k | k == 0 = return k- | otherwise = CA.cursorForward k >>- return 0-
src/Terminal/Game/Animation.hs view
@@ -1,12 +1,13 @@-{-# LANGUAGE DeriveGeneric #-}-{-# LANGUAGE DefaultSignatures #-}-{-# LANGUAGE StandaloneDeriving #-}-{-# LANGUAGE FlexibleInstances #-} ------------------------------------------------------------------------------- -- Animation -- 2018 Francesco Ariis GPLv3 ------------------------------------------------------------------------------- +-- {-# LANGUAGE DeriveGeneric #-}+-- {-# LANGUAGE DefaultSignatures #-}+-- {-# LANGUAGE StandaloneDeriving #-}+-- {-# LANGUAGE FlexibleInstances #-}+ module Terminal.Game.Animation (module Terminal.Game.Animation, module T ) where@@ -15,39 +16,18 @@ import Control.Timer.Tick as T -import Data.Serialize-import GHC.Generics--import qualified Data.ByteString as BS-import qualified Data.Bifunctor as BF+-- import Data.Serialize --- todo missing: creaani, creaframe, fetchcurrframe, etc.+-- import qualified Data.ByteString as BS+-- import qualified Data.Bifunctor as BF +-- | An @Animation@ is a series of timed time-separated 'Plane's. type Animation = T.Timed Plane -creaAni :: Loop -> [(Integer, Plane)] -> Animation-creaAni = creaTimedRes------------------------ SERIALISATION -------------------------- deriving instance Generic loc => Generic (Frame loc)--- instance (Generic loc, Serialize loc) => Serialize (Frame loc Integer)--instance Serialize ExpBehaviour-instance Serialize Loop-instance Serialize Cell-instance Serialize Plane-instance Serialize Animation--encodeAni :: FilePath -> Animation -> IO ()-encodeAni fp fs = BS.writeFile fp (encode fs)--decodeAni :: FilePath -> IO (Either String Animation)-decodeAni fp = fmap decode (BS.readFile fp) >>=- return . BF.bimap err id- where- err se = fp ++ ": " ++ se-+-- | Creates an 'Animation'.+creaAnimation :: [(Integer, Plane)] -> Animation+creaAnimation ips = creaTimedRes (Times 1 Elapse) ips +-- | Creates a looped 'Animation'.+creaLoopAnimation :: [(Integer, Plane)] -> Animation+creaLoopAnimation ips = creaTimedRes AlwaysLoop ips
+ src/Terminal/Game/Character.hs view
@@ -0,0 +1,43 @@+module Terminal.Game.Character where++import Data.Char as C+import Text.Unidecode as D+import System.IO.Unsafe as U+++import Terminal.Game.Utils++-- Non ASCII character still cause crashes on Win32 console (see this+-- report: https://gitlab.haskell.org/ghc/ghc/issues/7593 ).+-- We provide a function to substitute them when playing on Win32+-- console, with another appropriate chatacter.++win32SafeChar :: Char -> Char+win32SafeChar c | areWeWin32 = toASCII c+ | otherwise = c+ where+ areWeWin32 :: Bool+ areWeWin32 = unsafePerformIO isWin32Console++-- ANCILLARIES --++toASCII :: Char -> Char+toASCII c | C.isAscii c = c+ | Just cm <- lu = cm -- hand-made substitution+ | [cu] <- unidecode c = cu -- unidecode+ | otherwise = '?' -- all else failing+ where+ lu = lookup c subDictionary++subDictionary :: [(Char, Char)]+subDictionary = [ -- various open/close quotes+ ('«', '<'),+ ('»', '>'),+ ('“', '\''),+ ('”', '\''),+ ('‘', '\''),+ ('’', '\''),++ -- typographical marks+ ('—', '-') ]+
src/Terminal/Game/Draw.hs view
@@ -7,67 +7,244 @@ -- assumed to be opaque module Terminal.Game.Draw (module Terminal.Game.Draw,- module DF) where+ (F.&)+ ) where import Terminal.Game.Plane import Text.LineBreak -import Data.Function as DF ( (&) )+import qualified Data.Colour.RGBSpace as S+import qualified Data.Function as F ( (&) )+import qualified Data.List as L+import qualified Data.Word as W+import qualified System.Console.ANSI as CA + ----------- -- TYPES -- ----------- +-- | A drawing function, usually executed with the help of '%'. type Draw = Plane -> Plane ++-----------------+-- COMBINATORS --+-----------------++-- | Pastes one 'Plane' onto another. To be used along with 'F.&'+-- like this:+--+-- @+-- d :: Plane+-- d = blankPlane 100 100 &+-- (3, 4) % box '_' 3 5 &+-- (a, b) % cell \'A\' '#' bold+-- @ (%) :: Coords -> Plane -> Draw cds % p1 = \p2 -> pastePlane p1 p2 cds+infixl 4 % - -- most of the drawing is done with % and &, e.g.- --- -- let d :: Plane- -- d = blankPlane (100, 100) &- -- (3, 4) % box '_' Yellow (3, 5) &- -- (a, b) % butCell '@' Red- --+-- | Apply style to plane, e.g.+--+-- > cell 'w' # bold+(#) :: Plane -> Draw -> Plane+p # sf = sf p+infixl 8 # +-- | Shorthand for sequencing 'Plane's, e.g.+--+-- @+-- firstPlane &+-- (3, 4) '%' secondPlane &+-- (1, 9) '%' thirdPlane+-- @+--+-- is equal to+--+-- @+-- mergePlanes firstPlane [((3,4), secondPlane),+-- ((1,9), thirdPlane)]+-- @+mergePlanes :: Plane -> [(Coords, Plane)] -> Plane+mergePlanes p cps = L.foldl' addPlane p cps+ where+ addPlane :: Plane -> (Coords, Plane) -> Plane+ addPlane bp (cs, tp) = bp F.& cs % tp++-- | Place two 'Plane's side-by-side, horizontally.+(|||) :: Plane -> Plane -> Plane+(|||) a b = let (wa, ha) = planeSize a+ (wb, hb) = planeSize b+ in mergePlanes (blankPlane (wa + wb) (max ha hb))+ [((1,1), a),+ ((1,wa+1), b)]++-- | Place two 'Plane's side-by-side, vertically.+(===) :: Plane -> Plane -> Plane+(===) a b = let (wa, ha) = planeSize a+ (wb, hb) = planeSize b+ in mergePlanes (blankPlane (max wa wb) (ha + hb))+ [((1,1), a),+ ((ha+1,1), b)]++-- | @a *** b@ blits @b@ in the centre of @a@.+(***) :: Plane -> Plane -> Plane+(***) a b = let (aw, ah) = planeSize a+ (bw, bh) = planeSize b+ r = quot (ah - bh) 2 + 1+ c = quot (aw - bw) 2 + 1+ in a F.&+ (r, c) % b+++-- | Place a list of 'Plane's side-by-side, horizontally. Returns a 1×1+-- transparent plane on empty list.+hcat :: [Plane] -> Plane+hcat [] = blankPlane 1 1 # makeTransparent ' '+hcat ps = L.foldl1' (|||) ps++-- | Place a list of 'Plane's side-by-side, vertically. Returns a 1×1+-- transparent plane on empty list.+vcat :: [Plane] -> Plane+vcat [] = blankPlane 1 1 # makeTransparent ' '+vcat ps = L.foldl1' (===) ps++infixl 6 |||, ===, ***+++------------+-- STYLES --+------------++-- | Set foreground color.+color :: CA.Color -> CA.ColorIntensity -> Plane -> Plane+color c i p = mapPlane (colorCell c i) p++-- | Apply bold style to 'Plane'.+bold :: Plane -> Plane+bold p = mapPlane boldCell p++-- | Swap foreground and background colours of 'Plane'.+invert :: Plane -> Plane+invert p = mapPlane reverseCell p++-- | Set RGB color+rgbColor :: S.Colour Float -> Plane -> Plane+rgbColor k p = mapPlane (rgbColorCell k) p++-- | Set Palette color+paletteColor :: W.Word8 -> Plane -> Plane+paletteColor k p = mapPlane (paletteColorCell k) p++ ------------- -- DRAWING -- ------------- -box :: Char -> Width -> Height -> Plane-box chr w h = seqCellsDim w h cells+-- | A box of dimensions @w h@.+box :: Width -> Height -> Char -> Plane+box w h chr = seqCellsDim w h cells where cells = [((r, c), chr) | r <- [1..h], c <- [1..w]] +-- | A 1×1 @Plane@. cell :: Char -> Plane-cell ch = box ch 1 1+cell ch = box 1 1 ch +-- | @1xn@ 'Plane' with a word in it. If you need to import multiline+-- ASCII art, check 'stringPlane' and 'stringPlaneTrans'.+word :: String -> Plane+word w = seqCellsDim (L.genericLength w) 1 cells+ where+ cells = zip (zip (repeat 1) [1..]) w+ -- opaque :: Plane -> Plane -- opaque p = pastePlane p (box ' ' White w h) (1, 1) -- where -- (w, h) = pSize p --- assumes ' ' are transparent-textBox :: String -> Width -> Height -> Plane-textBox cs w h = transparent+-- | A text-box. Assumes @' '@s are transparent.+textBox :: Width -> Height -> String -> Plane+textBox w h cs = frameTrans w h (textBoxLiquid w cs)++-- | Like 'textBox', but tall enough to fit @String@.+textBoxLiquid :: Width -> String -> Plane+textBoxLiquid w cs = textBoxGeneralLiquid Nothing w cs++-- | As 'textBox', but hypenated. Example:+--+-- @+-- (normal textbox) (hyphenated textbox)+-- Rimasi un po’ a meditare nel buio Rimasi un po’ a meditare nel buio+-- velato appena dal barlume azzurrino velato appena dal barlume azzurrino+-- del fornello a gas, su cui del fornello a gas, su cui sobbol-+-- sobbolliva quieta la pentola. liva quieta la pentola.+-- @+--+-- Notice how in the right box /sobbolliva/ is broken in two. This+-- can be useful and aesthetically pleasing when textboxes are narrow.+textBoxHyphen :: Hyphenator -> Width -> Height -> String -> Plane+textBoxHyphen hp w h cs = frameTrans w h (textBoxHyphenLiquid hp w cs)++-- | As 'textBoxLiquid', but hypenated.+textBoxHyphenLiquid :: Hyphenator -> Width -> String -> Plane+textBoxHyphenLiquid h w cs = textBoxGeneralLiquid (Just h) w cs++textBoxGeneralLiquid :: Maybe Hyphenator -> Width -> String -> Plane+textBoxGeneralLiquid mh w cs = transparent where -- hypenathion- hyp = Nothing -- Just english_GB- bf = BreakFormat (fromIntegral w) 4 '-' hyp- hcs = breakStringLn bf (take (fromIntegral $ w*h) cs)- hl = fromIntegral $ length hcs+ bf = BreakFormat (fromIntegral w) 4 '-' mh+ hcs = breakStringLn bf cs+ h = L.genericLength hcs f :: [String] -> [(Coords, Char)] f css = concatMap (uncurry rf) (zip [1..] css)- where rf :: Integer -> String -> [(Coords, Char)]+ where rf :: Int -> String -> [(Coords, Char)] rf cr ln = zip (zip (repeat cr) [1..]) ln out = seqCellsDim w h (f hcs)- transparent = addVitrum ' ' out+ transparent = makeTransparent ' ' out +----------------------------+-- ADDITIONAL COMBINATORS --+----------------------------++-- Coords as if origin were @ bottom-right+recipCoords :: Coords -> Plane -> Plane -> Coords+recipCoords (r, c) p p1 =+ let (pw, ph) = planeSize p+ (p1w, p1h) = planeSize p1+ r' = ph-p1h-r+2+ c' = pw-p1w-c+2+ in (r', c')++-- | Pastes a plane onto another (origin: top-right).+(%^>) :: Coords -> Plane -> Draw+(r, c) %^> p1 = \p ->+ let (_, c') = recipCoords (r, c) p p1+ in p F.& (r, c') % p1++-- | Pastes a plane onto another (origin: bottom-left).+(%.<) :: Coords -> Plane -> Draw+(r, c) %.< p1 = \p ->+ let (r', _) = recipCoords (r, c) p p1+ in p F.& (r', c) % p1++-- | Pastes a plane onto another (origin: bottom-right).+(%.>) :: Coords -> Plane -> Draw+cs %.> p1 = \p ->+ let (r', c') = recipCoords cs p p1+ in p F.& (r', c') % p1++infixl 4 %^>+infixl 4 %.<+infixl 4 %.>++ ----------------- -- ANCILLARIES -- -----------------@@ -79,4 +256,9 @@ seqCells p cells = updatePlane p (map f cells) where f (cds, chr) = (cds, creaCell chr)++-- paste plane on a blank one, and make ' ' transparent+frameTrans :: Width -> Height -> Plane -> Plane+frameTrans w h p = let bt = makeTransparent ' ' (blankPlane w h)+ in bt F.& (1, 1) % p
− src/Terminal/Game/GameLoop.hs
@@ -1,193 +0,0 @@----------------------------------------------------------------------------------- Input/compute/output loop--- 2017 Francesco Ariis GPLv3----------------------------------------------------------------------------------{-# OPTIONS_GHC -fno-warn-type-defaults #-}--module Terminal.Game.GameLoop where--import Terminal.Game.Plane-import Terminal.Game.Input-import Terminal.Game.ANSI-import Terminal.Game.Utils-import Control.Concurrent--import qualified System.IO as SI-import qualified Control.Monad as CM-import qualified System.Console.ANSI as CA-import qualified System.Console.Terminal.Size as TS-import qualified System.Clock as SC-import qualified Control.Exception as E---- todo [release] [study] no full IO for s, but a--- jailed IO (provided by a datatype), both for I--- and for O---- todo elimina fps ora che li puoi fare on IO---- | Entry point for the game, should be called in @main@. The two--- most important functions are the one dealing with logic and the--- blitting one. Check @alone-in-a-room@ (you can compiler it with--- @cabal new-build -f examples@) to see a simple game in action.-gameLoop :: String -- ^Terminal title.- -> s -- ^Initial state of the game.- -> (s -> Maybe Char -> IO s) -- ^Logic function.- -> (s -> Plane) -- ^Draw function.- -> (s -> Bool) -- ^\"Should I quit?" function.- -> Integer -- ^Framerate (in fps).- -> IO ()-gameLoop t s lf df qf fps =-- E.finally (initPart >> game)- cleanAndExit -- this will be run regardless- -- of exception- where- initPart :: IO ()- initPart = -- init- SI.hSetBuffering SI.stdout SI.NoBuffering >>- SI.hSetBuffering SI.stdin SI.NoBuffering >>- SI.hSetEcho SI.stdin False >>-- -- title and initial setup/checks- CA.setTitle t >>- CA.hideCursor >>- blackScreen-- game :: IO ()- game = -- mvars & fork- newMVar 1 >>= \frameCounter ->- newMVar Nothing >>= \inputChar ->- forkIO (inputAction inputChar) >>- forkIO (incTimer frameCounter fps) >>-- logicDraw inputChar frameCounter- s lf df qf Nothing- (initFPSCounter 20) (0,0)---------------------- CONCURRENT ----------------------- get action char-inputAction :: MVar (Maybe Char) -> IO ()-inputAction mc = -- vedi platform-dep/- inputCharTerminal >>= \c ->- swapMVar mc (Just c) >>- inputAction mc---- modifica il timer-incTimer :: MVar Integer -> Integer -> IO ()-incTimer mi fps = modifyMVar_ mi (return . succ) >>- threadDelay delayAmount >>- incTimer mi fps- where- delayAmount :: Int- delayAmount = fromIntegral $ div (10^6) fps----- from http://www.loomsoft.net/resources/alltut/alltut_lesson5.htm-logicDraw :: MVar (Maybe Char) -> MVar Integer -> s -> -- input, ticks, state- (s -> Maybe Char -> IO s) -> -- logic function- (s -> Plane) -> -- draw function- (s -> Bool) -> -- quit? function- Maybe Plane -> -- last blitted screen- FPSCounter -> -- FPS counter- (Width, Height) -> -- Term Dimensions- IO ()-logicDraw mc mi s lf df qf opln fc td =-- -- not to hog CPU cycles- -- todo come mai 300 così alto? come influenza i timer?- threadDelay 300 >>-- -- quit?- if qf s- then return ()- else-- -- no tick from timer yet?- readMVar mi >>= \k ->- if k <= 0- then logicDraw mc mi s lf df qf opln fc td- else-- -- do logic- readMVarNothing mc >>= \c ->- modifyMVar mi (\a -> return (a-1, a-1)) >>= \k' ->- lf s c >>= \s' ->-- -- not enough logic done? Skip blitting- if k' > 0- then logicDraw mc mi s' lf df qf opln fc td- else-- -- clear screen if resolution change- screenSize >>= \td'@(tw, th) ->- let resc = td /= td' in- CM.when resc blackScreen >>-- let opln' | resc = Nothing -- res changed? restart double buffering- | otherwise = opln- npln = df s'- cFps = getCurrFPS fc in- blitPlane tw th opln' npln cFps >>- tickCounter fc >>= \fc' ->- logicDraw mc mi s' lf df qf (Just npln) fc' td'----------------------- FPS COUNTER ------------------------ poll fps every x frames, current fps, stored time, current fps-data FPSCounter = FPSCounter Integer Integer SC.TimeSpec Integer---- poll utctime every x ticks-initFPSCounter :: Integer -> FPSCounter-initFPSCounter x = FPSCounter x 0 0 0--tickCounter :: FPSCounter -> IO FPSCounter-tickCounter (FPSCounter g e t1 cf)- | g > e = return (FPSCounter g (e+1) t1 cf)- | g == e = SC.getTime SC.Monotonic >>= \t2 ->- let dtn = SC.toNanoSecs $ SC.diffTimeSpec t2 t1- fr = fi dtn / fi (g+1)- fps = round $ fi (10^9) / fr in- --- xxx no div- return (FPSCounter g 0 t2 fps)- | otherwise = error "tickCounter: g < e"- where- fi = fromIntegral--getCurrFPS :: FPSCounter -> Integer-getCurrFPS (FPSCounter _ _ _ cFps) = cFps---------------------- ANCILLARIES ------------------------ todo [release] catch any exception in IO and execute this-cleanAndExit :: IO ()-cleanAndExit = CA.setSGR [CA.Reset] >>- CA.clearScreen >>- CA.setCursorPosition 0 0 >>- CA.showCursor--readMVarNothing :: MVar (Maybe a) -> IO (Maybe a)-readMVarNothing mvar = readMVar mvar >>= \ma ->- CM.unless (null ma)- (() <$ swapMVar mvar Nothing) >>- return ma---- turn screen into black-blackScreen :: IO ()-blackScreen = CA.setCursorPosition 0 0 >>- -- CA.setSGR [CA.Reset,- -- CA.SetColor CA.Foreground CA.Dull CA.White,- -- CA.SetColor CA.Background CA.Dull CA.Black] >>- -- è dull black che voglio?- screenSize >>= \(w, h) ->- CM.replicateM_ (fromIntegral $ w*h) (putChar ' ')-
+ src/Terminal/Game/Layer/Imperative.hs view
@@ -0,0 +1,298 @@+-------------------------------------------------------------------------------+-- Layer 1 (imperative), as per+-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html+-- 2019 Francesco Ariis GPLv3+-------------------------------------------------------------------------------++{-# Language ScopedTypeVariables #-}++module Terminal.Game.Layer.Imperative where++import Terminal.Game.Draw+import Terminal.Game.Layer.Object++import qualified Control.Concurrent as CC+import qualified Control.Exception as E+import qualified Control.Monad as CM+import qualified Data.Bool as B+import qualified Data.Either as ET+import qualified Data.List as D+import qualified System.IO as SI++import Terminal.Game.Plane++-- | Game definition datatype, parametrised on:+--+-- * your gamestate @s@; and+-- * a result when the game is finished @r@. Simple games do not need this,+-- just fill @r@ with @()@.+--+-- The two most important elements are the function dealing with logic and+-- the drawing one. Check @alone@ demo (@cabal run -f examples alone@) to+-- see a basic game in action.+data Game s r = Game {+ gTPS :: TPS,+ -- ^ Game speed in ticks per second. You do not+ -- need high values, since the 2D canvas is coarse+ -- (e.g. 13 TPS is enough for action games).+ gInitState :: s, -- ^ Initial state of the game.+ gLogicFunction :: GEnv -> s -> Event -> Either r s,+ -- ^ Logic function. If `gLogicFunction` returns @Right s@+ -- the game will continue with state @s@; if it returns @Left@+ -- the game is over (quit condition).+ --+ -- Curious to see how @r@ can be useful? Check+ -- @cabal run -f examples balls@ and+ -- @example/MainBalls.hs@.+ gDrawFunction :: GEnv -> s -> Plane+ -- ^ Draw function. Just want to blit your game+ -- in the middle? Check 'centerFull'.+ }++-- | A blank plane as big as the terminal.+blankPlaneFull :: GEnv -> Plane+blankPlaneFull e = uncurry blankPlane (eTermDims e)++-- | Blits plane in the middle of terminal.+--+-- @+-- draw :: GEnv -> MyState -> Plane+-- draw ev s =+-- centerFull ev $+-- ⁝+-- @+centerFull :: GEnv -> Plane -> Plane+centerFull e p = blankPlaneFull e *** p++-- | Entry point for the game execution, should be called in @main@.+--+-- You __must__ compile your programs with @-threaded@; if you do not do+-- this the game will crash, at start-up. Just add:+--+-- @+-- ghc-options: -threaded+-- @+--+-- in your @.cabal@ file and you will be fine!+playGame :: Game s r -> IO r+playGame g = either id (error "`Right` in playGame") <$>+ runGIO (runGameGeneral g)++-- | As 'playGame', but ignore the result @r@.+playGame_ :: Game s r -> IO ()+playGame_ g = () <$ playGame g++-- | Tests a game in a /pure/ environment. Aims to accurately emulate 'GEnv'+-- changes (screen size, FPS) too. Returns a result @r@ or a state @s@ in+-- case the Event stream is exhausted before the game exits.+--+-- A useful trick is to call 'recordGame' and press /Ctrl-C/ while playing+-- (instead of quitting properly). This way @testGame@ will return+-- @Left s@, a state that you can then inspect.+testGame :: Game s r -> GRec -> Either r s+testGame g ts =+ case runTest (runGameGeneral g) ts of+ (Nothing, l) -> error $ "testGame, exception called: " +++ show l+ -- it is fine to use error here since in the end+ -- hspec can deal with it gracefully and we give+ -- more infos on a failed test+ (Just s, _) -> s++-- | As 'testGame', but returns 'Game' instead of result/state.+-- Useful to fast-forward (e.g.: skip menus) before invoking 'playGame'.+setupGame :: Game s r -> GRec -> Game s r+setupGame g ts = let s' = testGame g ts+ in case s' of+ -- If the game is already over, return a mock logic+ -- function which simply ends the game.+ Left r -> g { gLogicFunction = \_ _ _ -> Left r }+ Right s -> g { gInitState = s }++-- | Similar to 'testGame', runs the game given a 'GRec'. Unlike+-- 'testGame', the playthrough will be displayed on screen. Useful when a+-- test fails and you want to see how.+--+-- See this in action with @cabal run -f examples alone-playback@.+--+-- Notice that 'GEnv' will be provided at /run-time/, and not+-- record-time; this can make emulation slightly inaccurate if — e.g. —+-- you replay the game on a smaller terminal than the one you recorded+-- the session on.+narrateGame :: Game s r -> GRec -> IO ()+narrateGame g e = () <$ runReplay (runGameGeneral g) e++-- | Play as in 'playGame' and write the session (input stream, etc.) to+-- @file@. Then you can use this with 'testGame' and 'narrateGame'. Session+-- will be recorded even if an exception happens while playing.+recordGame :: Game s r -> FilePath -> IO ()+recordGame g fp =+ E.bracket+ (CC.newMVar igrec)+ (\ve -> writeRec fp ve)+ (\ve -> () <$ runRecord (runGameGeneral g) ve)++data Config = Config { cMEvents :: CC.MVar [Event],+ cTPS :: TPS }++runGameGeneral :: forall s r m. MonadGameIO m =>+ Game s r -> m (Either r s)+runGameGeneral (Game tps s lf df) =+ -- init+ setupDisplay >>+ startEvents tps >>= \(InputHandle ve ts) ->+ displaySizeErr >>= \ds ->++ -- do it!+ let c = Config ve tps in+ cleanUpErr (game c ds)+ -- this under will be run regardless+ (stopEvents ts >>+ shutdownDisplay )+ where+ game :: MonadGameIO m => Config -> Dimensions -> m (Either r s)+ game c wds = gameLoop c (Right s) lf df+ Nothing wds+ (creaFPSCalc tps)++-- | Wraps an @IO@ computation so that any 'ATGException' or 'error' gets+-- displayed along with a @\<press any key to quit\>@ prompt.+-- Some terminals shut-down immediately upon program end; adding+-- @errorPress@ to 'playGame' makes it easier to beta-test games on those+-- terminals.+errorPress :: IO a -> IO a+errorPress m = E.catches m [E.Handler errorDisplay,+ E.Handler atgDisplay]+ where+ errorDisplay :: E.ErrorCall -> IO a+ errorDisplay (E.ErrorCallWithLocation cs l) = report $+ putStrLn (cs ++ "\n\n") >>+ putStrLn "Stack trace info:\n" >>+ putStrLn l++ atgDisplay :: ATGException -> IO a+ atgDisplay e = report $ print e++ report :: IO () -> IO a+ report wm =+ putStrLn "ERROR REPORT\n" >>+ wm >>+ putStrLn "\n\n <Press any key to quit>" >>+ SI.hSetBuffering SI.stdin SI.NoBuffering >>+ getChar >>+ errorWithoutStackTrace "errorPress"+++-----------+-- LOGIC --+-----------++-- from http://www.loomsoft.net/resources/alltut/alltut_lesson6.htm+gameLoop :: MonadGameIO m =>+ Config -> -- event source+ Either r s -> -- state+ (GEnv ->+ s -> Event ->+ Either r s) -> -- logic function+ (GEnv ->+ s -> Plane) -> -- draw function+ Maybe Plane -> -- last blitted screen+ Dimensions -> -- Term dimensions+ FPSCalc -> -- calculate fps+ m (Either r s)+gameLoop c s lf df opln td fps =++ -- Quit?+ areEventsOver >>= \qb ->+ -- We will quit in case input stream (events) is exhausted.+ -- This might happen during test/narrate.+ if ET.isLeft s || qb+ then return s+ else++ -- Fetch events (if any).+ -- This is safe as we checked for `areEventsOver` above.+ pollEvents (cMEvents c) >>= \es ->++ -- no events? skip everything+ if null es+ then sleepABit (cTPS c) >>+ gameLoop c s lf df opln td fps+ else++ displaySizeErr >>= \td' ->++ -- logic+ let ge = GEnv td' (calcFPS fps)+ (i, s') = stepsLogic s (lf ge) es in++ -- no `Tick` events? You do not need to blit, just update state+ if i == 0+ then gameLoop c s' lf df opln td fps+ else++ -- FPS calc+ let fps' = addFPS i fps in++ -- clear screen if resolution changed+ let resc = td /= td' in+ CM.when resc clearDisplay >>++ -- draw+ let+ opln' | resc = Nothing -- res changed? restart double buffering+ | otherwise = opln+ npln = case s' of+ (Right rs) -> df ge rs+ (Left _) -> uncurry blankPlane td'+ -- In case the logic function came to an end+ -- (Left), just print a blank plane.+ in++ blitPlane opln' npln >>++ gameLoop c s' lf df (Just npln) td' fps'++-- Int = number of `Tick` events+stepsLogic :: Either r s -> (s -> Event -> Either r s) -> [Event] ->+ (Integer, Either r s)+stepsLogic s lf es = let ies = D.genericLength . filter isTick $ es+ in (ies, logicFold lf s es)+ where+ isTick Tick = True+ isTick _ = False++ logicFold :: (s -> Event -> Either r s) ->+ Either r s -> [Event] -> Either r s+ logicFold _ (Left r) _ = Left r+ logicFold wlf (Right ws) wes = CM.foldM wlf ws wes+++-------------------------------------------------------------------------------+-- Frame per Seconds++data FPSCalc = FPSCalc [Integer] TPS+ -- list with number of `Ticks` processed at each blit and expected+ -- FPS (i.e. TPS)++-- the size of moving average will be TPS (that simplifies calculations)+creaFPSCalc :: TPS -> FPSCalc+creaFPSCalc tps = FPSCalc (D.genericReplicate tps {- (tps*2) -} 1) tps+ -- tps*1: size of thw window in **blit actions** (not tick actions!)+ -- so keeping it small should be responsive and non flickery+ -- at the same time!++-- add ticks+addFPS :: Integer -> FPSCalc -> FPSCalc+addFPS nt (FPSCalc (_:fps) tps) = FPSCalc (fps ++ [nt]) tps+addFPS _ (FPSCalc [] _) = error "addFPS: empty list."++calcFPS :: FPSCalc -> Integer+calcFPS (FPSCalc fps tps) =+ let ts = sum fps+ ds = D.genericLength fps+ in roundQuot (tps * ds) ts+ where+ roundQuot :: Integer -> Integer -> Integer+ roundQuot a b = let (q, r) = quotRem a b+ in q + B.bool 0 1 (r > div b 2)
+ src/Terminal/Game/Layer/Object.hs view
@@ -0,0 +1,30 @@+-------------------------------------------------------------------------------+-- Layer 2 (mockable IO), as per+-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html+-- 2019 Francesco Ariis GPLv3+-------------------------------------------------------------------------------++module Terminal.Game.Layer.Object ( module Export ) where++import Terminal.Game.Layer.Object.Interface as Export+import Terminal.Game.Layer.Object.GameIO as Export+import Terminal.Game.Layer.Object.Narrate as Export+import Terminal.Game.Layer.Object.Primitive as Export+import Terminal.Game.Layer.Object.Record as Export+import Terminal.Game.Layer.Object.Test as Export++-- DESIGN NOTES --++-- The classes are described in 'Interface'.++-- The implemented monads are four:+-- - GameIO (via MonadIO): playing the game+-- - Test: testing the game in a pure manner+-- - Record: playing the game and record the Events in a file+-- - Replay: replay a game using a set of Events.+--+-- The last two monads (Record/Replay) take advantage of "overlapping+-- instances". Instead of reimplementing most of what happens in MonadIO,+-- they'll just touch the classes from interface which behaviour they+-- will modify; being more specific, they will be chosen instead of plain+-- IO.
+ src/Terminal/Game/Layer/Object/GameIO.hs view
@@ -0,0 +1,21 @@+-------------------------------------------------------------------------------+-- Layer 2 (mockable IO), as per+-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html+-- 2019 Francesco Ariis GPLv3+-------------------------------------------------------------------------------++{-# LANGUAGE GeneralizedNewtypeDeriving #-}+++module Terminal.Game.Layer.Object.GameIO where++import qualified Control.Monad.Catch as MC+import qualified Control.Monad.Trans as T+++newtype GameIO a = GameIO { runGIO :: IO a }+ deriving (Functor, Applicative, Monad,+ T.MonadIO,+ MC.MonadThrow, MC.MonadCatch, MC.MonadMask)++
+ src/Terminal/Game/Layer/Object/IO.hs view
@@ -0,0 +1,317 @@+-------------------------------------------------------------------------------+-- Layer 2 (mockable IO), as per+-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html+-- 2019 Francesco Ariis GPLv3+-------------------------------------------------------------------------------++{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}+++module Terminal.Game.Layer.Object.IO where++import Terminal.Game.Utils++import Terminal.Game.Layer.Object.Interface+import Terminal.Game.Layer.Object.Primitive+import Terminal.Game.Plane++import qualified Control.Concurrent as CC+import qualified Control.Monad as CM+import qualified Control.Monad.Catch as MC+import qualified Control.Monad.Trans as T+import qualified GHC.IO.StdHandles as GH+import qualified Data.List.Split as LS+import qualified System.Clock as SC+import qualified System.Console.ANSI as CA+import qualified System.Console.Terminal.Size as TS+import qualified System.IO as SI++-- Most General MonadIO operations.++----------------+-- Game input --+----------------++instance {-# OVERLAPS #-} (Monad m, T.MonadIO m) => MonadInput m where+ startEvents tps = T.liftIO $ startIOInput tps+ pollEvents ve = T.liftIO $ CC.swapMVar ve []+ stopEvents ts = T.liftIO $ stopEventsIO ts+ areEventsOver = return False+ -- IO monad is the actual game, we never quit bar if+ -- the logic function returns `Right`.+++-- filepath = logging+startIOInput :: TPS -> IO InputHandle+startIOInput tps =+ SI.hSetBuffering SI.stdin SI.NoBuffering >>+ SI.hSetBuffering SI.stdout (SI.BlockBuffering Nothing) >>+ SI.hSetEcho SI.stdin False >>+ -- all the buffering settings has to happen+ -- at the top of startIOInput. If i move+ -- them to display, you need to press enter+ -- before playing the game on some machines.++ -- event and log variables+ CC.newMVar [] >>= \ve ->++ getTimeTick tps >>= \it ->+ CC.forkIO (addTick ve tps it) >>= \te ->+ CC.forkIO (addKeypress ve) >>= \tk ->+ return (InputHandle ve [te, tk])++-- a precise timer, not based on `threadDelay`+type Elapsed = Integer -- in `Ticks`++-- elapsed from Epoch in ticks+getTimeTick :: TPS -> IO Elapsed+getTimeTick tps =+ getTime >>= \tm ->+ let ns = 10 ^ (9 :: Integer)+ t1 = quot ns tps in+ return (quot tm t1)++-- mr: maybe recording+addTick :: CC.MVar [Event] -> TPS -> Elapsed -> IO ()+addTick ve tps el =+ -- precise timing. With `treadDelay`, on finer TPS,+ -- ticks take too much (check threadDelay doc).+ getTimeTick tps >>= \t ->+ CM.replicateM_ (fromIntegral $ t-el)+ (addEvent ve Tick) >>++ -- sleep some+ sleepABit tps >>+ addTick ve tps t++-- get action char+-- mr: maybe recording+addKeypress :: CC.MVar [Event] -> IO ()+addKeypress ve = -- vedi platform-dep/+ inputCharTerminal >>= \c ->+ addEvent ve (KeyPress c) >>+ addKeypress ve++-- mr: maybe recording+addEvent :: CC.MVar [Event] -> Event -> IO ()+addEvent ve e = vf ve+ where+ vf d = CC.modifyMVar_ d (return . (++[e]))++stopEventsIO :: [CC.ThreadId] -> IO ()+stopEventsIO ts = do+ mapM_ CC.killThread ts+ SI.hSetBuffering SI.stdout SI.NoBuffering+ -- We need this to make `alone-playback` work, see (thnks jrvieira):+ -- https://stackoverflow.com/questions/27324354/haskell-compiled-io-actionorder-and-flushing++-----------------+-- Game timing --+-----------------++instance {-# OVERLAPS #-} (Monad m, T.MonadIO m) => MonadTimer m where+ getTime = T.liftIO $ SC.toNanoSecs <$> SC.getTime SC.Monotonic+ sleepABit tps = T.liftIO $+ CC.threadDelay (fromIntegral $ quot oneTickSec (tps*10))++--------------------+-- Error handling --+--------------------++instance {-# OVERLAPS #-}+ (Monad m, T.MonadIO m, MC.MonadMask m, MC.MonadThrow m) =>+ MonadException m where+ cleanUpErr m c = MC.finally m c+ throwExc t = MC.throwM t++-------------+-- Display --+-------------++instance {-# OVERLAPS #-} (Monad m, T.MonadIO m) => MonadDisplay m where+ setupDisplay = T.liftIO initPart+ clearDisplay = T.liftIO clearScreen+ displaySize = T.liftIO displaySizeIO+ blitPlane mp p = T.liftIO (blitPlaneIO mp p)+ shutdownDisplay = T.liftIO cleanAndExit++displaySizeIO :: IO (Maybe Dimensions)+displaySizeIO =+ TS.size >>= \ts ->+ -- cannot use ansi-terminal, on Windows you get+ -- "ConsoleException 87" (too much scrolling)+ -- and it does not work for mintty and it is+ -- inefficient as it gets (attempts to scroll past+ -- bottom right)+ isWin32Console >>= \bw ->+ -- cmd.exe is present on Win10 `C:\Windows\system32\cmd.exe`+ -- — and default — too. So this is needed for the foreseeable+ -- future.++ return (fmap (f bw) ts)+ where+ f :: Bool -> TS.Window Int -> Dimensions+ f wbw (TS.Window h w) =+ let h' | wbw = h - 1+ | otherwise = h+ in (w, h')++-- pn: new plane, po: old plane+-- wo, ho: dimensions of the terminal. If they change, reinit double buffering+blitPlaneIO :: Maybe Plane -> Plane -> IO ()+blitPlaneIO mpo pn =++ -- remember that Nothing will be passed:+ -- - at the beginning of the game (first blit)+ -- - when resolution changes (see gameLoop)+ -- so do not duplicate hasResChanged checks here!++ -- old plane+ let+ (pw, ph) = planeSize pn+ bp = blankPlane pw ph+ po = pastePlane (maybe bp id mpo) bp (1, 1)+ in++ -- new plane+ let pn' = pastePlane pn bp (1, 1)+ in++ -- trimming is foundamental, as blitMap could otherwise print+ -- outside terminal boundaries and scroll to its death+ -- (error 87 on Win32 console).++ CA.setSGR [CA.Reset] >>+ blitMap po pn'+++-----------------+-- ANCILLARIES --+-----------------++-- represent current terminal SGR State+-- so we can keep track of it between blits+data Style = Style { sBold :: Bool+ , sReversed :: Bool+ , sColor :: Maybe ColorInfo+ }+ deriving (Eq)++resetStyle :: Style+resetStyle = Style False False Nothing++cellToStyle :: Cell -> Style+cellToStyle c = Style (isBold c) (isReversed c) (cellColor c)++initPart :: IO ()+initPart = -- check thread support+ CM.unless CC.rtsSupportsBoundThreads+ (error errMes) >>++ -- initial setup/checks+ CA.hideCursor >>+ CA.hNowSupportsANSI GH.stdout >>+ -- On Windows, tries to turn ANSI control+ -- characters support on.++ -- text encoding+ SI.mkTextEncoding "UTF-8//TRANSLIT" >>= \te ->+ SI.hSetEncoding SI.stdout te >>++ clearScreen+ where+ errMes = unlines+ ["\nError: you *must* compile this program with -threaded!",+ "Just add",+ "",+ " ghc-options: -threaded",+ "",+ "in your .cabal file (executable section) and you will be fine!"]++-- clears screen+clearScreen :: IO ()+clearScreen = CA.setCursorPosition 0 0 >>+ CA.setSGR [CA.Reset] >>+ displaySizeErr >>= \(w, h) ->+ CM.replicateM_ (fromIntegral $ w*h) (putChar ' ')++cleanAndExit :: IO ()+cleanAndExit = CA.setSGR [CA.Reset] >>+ CA.clearScreen >>+ CA.setCursorPosition 0 0 >>+ CA.showCursor++-- plane+blitMap :: Plane -> Plane -> IO ()+blitMap po pn =+ CM.when (planeSize po /= planeSize pn)+ (error "blitMap: different plane sizes") >>+ CA.setCursorPosition 0 0 >>+ -- setCursorPosition is *zero* based!+ blitToTerminal (0, 0) (orderedCells po) (orderedCells pn) >>+ SI.hFlush SI.stdout++orderedCells :: Plane -> [[Cell]]+orderedCells p = LS.chunksOf (fromIntegral w) cells+ where+ cells = map snd $ assocsPlane p+ (w, _) = planeSize p++-- ordered sequence of cells, both old and new, like they were a String to+-- print to screen.+-- Coords: initial blitting position+-- Remember that this Column is *zero* based+blitToTerminal :: Coords -> [[Cell]] -> [[Cell]] -> IO ()+blitToTerminal (rr, rc) ocs ncs = CM.foldM_ blitLine (rr, resetStyle) oldNew+ where+ oldNew :: [[(Cell, Cell)]]+ oldNew = zipWith zip ocs ncs++ -- row = previous row, st = current terminal style+ blitLine :: (Row, Style) -> [(Cell, Cell)] -> IO (Row, Style)+ blitLine (pr, st) ccs =+ CM.foldM blitCell (0, st) ccs >>= \(_, st') ->+ let wr = pr + 1 in+ -- have to use setCursorPosition (instead of nextrow) b/c+ -- on win there is an auto "go-to-next-line" when reaching+ -- column end and on win it does not do so+ CA.setCursorPosition (fromIntegral wr)+ (fromIntegral rc) >>+ return (wr, st')++ -- k is "spaces to skip"+ blitCell :: (Int, Style) -> (Cell, Cell) -> IO (Int, Style)+ blitCell (k, st) (clo, cln)+ | cln == clo = return (k+1, st)+ | otherwise = moveIf k >>= \k' ->+ putCellStyle st cln >>= \st' ->+ return (k', st')++ moveIf :: Int -> IO Int+ moveIf k | k == 0 = return k+ | otherwise = CA.cursorForward k >>+ return 0++putCellStyle :: Style -> Cell -> IO Style+putCellStyle st c =+ CM.when (st /= cst) (CA.setSGR ([CA.Reset] ++ sgrb ++ sgrr ++ sgrc)) >>+ putChar (cellChar c) >>+ return cst+ where+ cst = cellToStyle c++ sgrb | isBold c = [CA.SetConsoleIntensity CA.BoldIntensity]+ | otherwise = []++ sgrr | isReversed c = [CA.SetSwapForegroundBackground True]+ | otherwise = []++ sgrc | Just (ANSIColorInfo (k, i)) <- cellColor c = [CA.SetColor CA.Foreground i k]+ | Just (RGBColorInfo k) <- cellColor c = [CA.SetRGBColor CA.Foreground k]+ | Just (PaletteColorInfo k) <- cellColor c = [CA.SetPaletteColor CA.Foreground k]+ | otherwise = []++oneTickSec :: Integer+oneTickSec = 10 ^ (6 :: Integer)
+ src/Terminal/Game/Layer/Object/Interface.hs view
@@ -0,0 +1,60 @@+-------------------------------------------------------------------------------+-- Layer 2 (mockable IO), as per+-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html+-- 2019 Francesco Ariis GPLv3+-------------------------------------------------------------------------------++{-# LANGUAGE ConstraintKinds #-}+{-# LANGUAGE LambdaCase #-}++module Terminal.Game.Layer.Object.Interface where++import Terminal.Game.Plane+import Terminal.Game.Layer.Object.Primitive++import qualified Control.Concurrent as CC++-------------------------------------------------------------------------------+-- mtl interface for game++type MonadGameIO m = (MonadInput m, MonadTimer m,+ MonadException m, MonadDisplay m)++data InputHandle = InputHandle+ { ihKeyMVar :: CC.MVar [Event],+ ihOpenThreads :: [CC.ThreadId] }++class Monad m => MonadInput m where+ startEvents :: TPS -> m InputHandle+ pollEvents :: CC.MVar [Event] -> m [Event]+ stopEvents :: [CC.ThreadId] -> m ()+ areEventsOver :: m Bool+ -- Why do we need this? For test/narrate purposes. When+ -- we play a game events are never over, but when we+ -- test/narrate, it might be than the stream of [Event]+ -- is exhausted before the state function returns Right.+ -- We do not want to be stuck in an endless loop in that+ -- case.++class Monad m => MonadTimer m where+ getTime :: m Integer -- to nanoseconds+ sleepABit :: TPS -> m () -- Given TPS, sleep a fracion of a single+ -- Tick.++-- if a fails, do b (useful for cleaning up)+class Monad m => MonadException m where+ cleanUpErr :: m a -> m b -> m a+ throwExc :: ATGException -> m a++class Monad m => MonadDisplay m where+ setupDisplay :: m ()+ clearDisplay :: m ()+ displaySize :: m (Maybe Dimensions)+ blitPlane :: Maybe Plane -> Plane -> m ()+ shutdownDisplay :: m ()++displaySizeErr :: (MonadDisplay m, MonadException m) => m Dimensions+displaySizeErr = displaySize >>= \case+ Nothing -> throwExc CannotGetDisplaySize+ Just d -> return d+
+ src/Terminal/Game/Layer/Object/Narrate.hs view
@@ -0,0 +1,28 @@+{-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Terminal.Game.Layer.Object.Narrate where++-- Narrate Monad, replay on screen from a GRec++import Terminal.Game.Layer.Object.Interface+import Terminal.Game.Layer.Object.Primitive+import Terminal.Game.Layer.Object.IO () -- MonadIo++import qualified Control.Monad.Catch as MC+import qualified Control.Monad.State as S+import qualified Control.Monad.Trans as T+++newtype Narrate a = Narrate (S.StateT GRec IO a)+ deriving (Functor, Applicative, Monad,+ T.MonadIO, S.MonadState GRec,+ MC.MonadThrow, MC.MonadCatch, MC.MonadMask)++instance MonadInput Narrate where+ startEvents fps = T.liftIO $ startEvents fps+ pollEvents _ = S.state getPolled+ stopEvents ts = T.liftIO $ stopEvents ts+ areEventsOver = S.gets isOver++runReplay :: Narrate a -> GRec -> IO a+runReplay (Narrate s) k = S.evalStateT s k
+ src/Terminal/Game/Layer/Object/Primitive.hs view
@@ -0,0 +1,139 @@+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE LambdaCase #-}++module Terminal.Game.Layer.Object.Primitive where++import Terminal.Game.Plane++import qualified Control.Monad.Catch as MC+import qualified GHC.Generics as G+import qualified Data.ByteString as BS+import qualified Data.Serialize as Z+import qualified Data.Sequence as S+import qualified Test.QuickCheck as Q++-------------------------------------------------------------------------------+-- Assorted API types++-- | The number of 'Tick's fed each second to the logic function;+-- constant on every machine. /Frames/ per second might be lower+-- (depending on drawing function onerousness, terminal refresh rate,+-- etc.).+type TPS = Integer++-- | The number of frames blit to terminal per second. Frames might be+-- dropped, but game speed will remain constant. Check @balls@+-- (@cabal run -f examples balls@) to see how to display FPS.+-- For obvious reasons (blits would be wasted) @max FPS = TPS@.+type FPS = Integer++-- | An @Event@ is a 'Tick' (time passes) or a 'KeyPress'.+--+-- Note that all @Keypress@es are recorded and fed to your game-logic+-- function. This means you will not lose a single character, no matter+-- how fast your player is at typing or how low you set 'FPS' to be.+--+-- Example: in a game where you are controlling a hot-air baloon and have+-- @direction@ and @position@ variables, you most likely want @direction@+-- to change at every @KeyPress@, while having @position@ only change at+-- @Tick@s.+data Event = Tick+ | KeyPress Char+ -- ↑↓→← do not work on Windows (are handled by the app,+ -- not passed to the program) both on cmd.exe and+ -- PowerShell.+ deriving (Show, Eq, G.Generic)+instance Z.Serialize Event where++instance Q.Arbitrary Event where+ arbitrary = Q.oneof [ pure Tick,+ KeyPress <$> Q.arbitrary ]++-- | Game environment with current terminal dimensions and current display+-- rate.+data GEnv = GEnv { eTermDims :: Dimensions,+ -- ^ Current terminal dimensions.+ eFPS :: FPS+ -- ^ Current blitting rate.+ }+ deriving (Show, Eq)++-------------------------------------------------------------------------------+-- GRec record/replay game typs++-- | Opaque data type with recorded game input, for testing purposes.+data GRec = GRec { aPolled :: S.Seq [Event],+ -- Seq. of polled events+ aTermSize :: S.Seq (Maybe Dimensions) }+ -- Seq. of polled termdims+ deriving (Show, Eq, G.Generic)+instance Z.Serialize GRec where++igrec :: GRec+igrec = GRec S.Empty S.Empty++addDims :: Maybe Dimensions -> GRec -> GRec+addDims mds (GRec p s) = GRec p (mds S.<| s)++getDims :: GRec -> (Maybe Dimensions, GRec)+getDims (GRec p (ds S.:|> d)) = (d, GRec p ds)+getDims _ = error "getDims: empty Seq"+ -- Have to use _ or “non exhaustive patterns” warning++addPolled :: [Event] -> GRec -> GRec+addPolled es (GRec p s) = GRec (es S.<| p) s++getPolled :: GRec -> ([Event], GRec)+getPolled (GRec (ps S.:|> p) d) = (p, GRec ps d)+getPolled _ = error "getPolled: empty Seq"++isOver :: GRec -> Bool+isOver (GRec S.Empty _) = True+isOver _ = False++-- | Reads a file containing a recorded session. Throws+-- 'MalformedGRec' on failure.+readRecord :: FilePath -> IO GRec+readRecord fp = Z.decode <$> BS.readFile fp >>= \case+ Left e -> MC.throwM (MalformedGRec e)+ Right r -> return r++-- | Convenience function to create a 'GRec' from screen size (constant) plus a list of events. Useful with 'setupGame'.+createGRec :: Dimensions -> [Event] -> GRec+createGRec ds es = let l = length es * 2 in+ GRec (S.fromList [es])+ (S.fromList . replicate l $ Just ds)++-------------------------------------------------------------------------------+-- Exceptions++-- | @ATGException@s are thrown synchronously for easier catching.+data ATGException = CannotGetDisplaySize+ | DisplayTooSmall Dimensions Dimensions+ -- ^ Required and actual dimensions.+ | MalformedGRec String+ deriving (Eq)++instance Show ATGException where+ show CannotGetDisplaySize = "CannotGetDisplaySize"+ show (DisplayTooSmall (sw, sh) tds) =+ let colS ww = ww < sw+ rowS wh = wh < sh++ smallMsg :: Dimensions -> String+ smallMsg (ww, wh) =+ let cm = show ww ++ " columns"+ rm = show wh ++ " rows"+ em | colS ww && rowS wh = cm ++ " and " ++ rm+ | colS ww = cm+ | rowS wh = rm+ | otherwise = "smallMsg: passed correct term size!"+ in+ "This games requires a display of " ++ show sw +++ " columns and " ++ show sh ++ " rows.\n" +++ "Yours only has " ++ em ++ "!\n\n" +++ "Please resize your terminal and restart the game.\n"+ in "DisplayTooSmall.\n" ++ smallMsg tds+ show (MalformedGRec e) = "MalformedGRec: " ++ e++instance MC.Exception ATGException where
+ src/Terminal/Game/Layer/Object/Record.hs view
@@ -0,0 +1,54 @@+{-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Terminal.Game.Layer.Object.Record where++-- Record Monad, for when I need to play the game and record Events+-- (keypresses, ticks, screen size, FPS) to a file.++import Terminal.Game.Layer.Object.Interface+import Terminal.Game.Layer.Object.Primitive+import Terminal.Game.Layer.Object.IO ()++import qualified Control.Concurrent as CC+import qualified Control.Monad.Catch as MC+import qualified Control.Monad.Reader as R+import qualified Control.Monad.Trans as T -- MonadIO+import qualified Data.ByteString as BS+import qualified Data.Serialize as S++-- record the key pressed in a game session++newtype Record a = Record (R.ReaderT (CC.MVar GRec) IO a)+ deriving (Functor, Applicative, Monad,+ T.MonadIO, R.MonadReader (CC.MVar GRec),+ MC.MonadThrow, MC.MonadCatch, MC.MonadMask)++-- Lifts IO interface, records where necessary+instance MonadInput Record where+ startEvents tps = T.liftIO (startEvents tps)+ pollEvents ve = T.liftIO (pollEvents ve) >>= \es ->+ modMRec addPolled es+ stopEvents ts = T.liftIO (stopEvents ts)+ areEventsOver = T.liftIO areEventsOver++instance MonadDisplay Record where+ setupDisplay = T.liftIO setupDisplay+ clearDisplay = T.liftIO clearDisplay+ displaySize = T.liftIO displaySize >>= \ds ->+ modMRec addDims ds+ blitPlane mp p = T.liftIO (blitPlane mp p)+ shutdownDisplay = T.liftIO shutdownDisplay++-- logs and passes the value on+modMRec :: (a -> GRec -> GRec) -> a -> Record a+modMRec f a = R.ask >>= \mv ->+ let fmv = CC.modifyMVar_ mv (return . f a) in+ T.liftIO fmv >>+ return a++runRecord :: Record a -> CC.MVar GRec -> IO a+runRecord (Record r) me = R.runReaderT r me++writeRec :: FilePath -> CC.MVar GRec -> IO ()+writeRec fp vr = CC.readMVar vr >>= \k ->+ BS.writeFile fp (S.encode k)
+ src/Terminal/Game/Layer/Object/Test.hs view
@@ -0,0 +1,77 @@+-------------------------------------------------------------------------------+-- Layer 2 (mockable IO), as per+-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html+-- 2019 Francesco Ariis GPLv3+-------------------------------------------------------------------------------++{-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Terminal.Game.Layer.Object.Test where++-- Test (pure) MonadGame* typeclass implementation for testing purposes.++import Terminal.Game.Layer.Object.Interface+import Terminal.Game.Layer.Object.Primitive++import qualified Control.Monad.Except as E+import qualified Control.Monad.RWS as S+import qualified Data.Bifunctor as B+++-----------+-- TYPES --+-----------++data TestEvent = TCleanUpError+ | TException ATGException+ | TQuitGame+ | TSetupDisplay+ | TShutdownDisplay+ | TStartGame+ | TStartEvents+ | TStopEvents+ deriving (Eq, Show)++-- e: ()+-- r: ()+-- w: [TestEvents]+-- s: [GTest]+newtype Test a = Test (E.ExceptT () (S.RWS () [TestEvent] GRec) a)+ deriving (Functor, Applicative, Monad,+ E.MonadError (), S.MonadState GRec,+ S.MonadWriter [TestEvent])++runTest :: Test a -> GRec -> (Maybe a, [TestEvent])+runTest (Test em) es = let m = E.runExceptT em+ t = S.evalRWS m () es in+ B.first (either (const Nothing) Just) t++-------------------------------------------------------------------------------+-- Class++mockHandle :: InputHandle+mockHandle = InputHandle (error "mock handle keyMvar")+ (error "mock handle threads")++instance MonadInput Test where+ startEvents _ = S.tell [TStartEvents] >>+ return mockHandle+ pollEvents _ = S.state getPolled+ stopEvents _ = S.tell [TStopEvents]+ areEventsOver = S.gets isOver++instance MonadTimer Test where+ getTime = return 1+ sleepABit _ = return ()++instance MonadException Test where+ cleanUpErr a _ = S.tell [TCleanUpError] >> a+ throwExc e = S.tell [TException e] >>+ E.throwError ()++instance MonadDisplay Test where+ setupDisplay = () <$ S.tell [TSetupDisplay]+ clearDisplay = return ()+ displaySize = Test $ S.state getDims+ blitPlane _ _ = return ()+ shutdownDisplay = () <$ S.tell [TShutdownDisplay]
src/Terminal/Game/Plane.hs view
@@ -9,156 +9,250 @@ module Terminal.Game.Plane where -import qualified GHC.Generics as G +import Terminal.Game.Character + import qualified Data.Array as A -import qualified Data.Char as C -import qualified Data.List as L +import qualified Data.Bifunctor as B +import qualified Data.Colour.RGBSpace as S import qualified Data.List.Split as LS import qualified Data.Tuple as T +import qualified Data.Word as W +import qualified GHC.Generics as G +import qualified System.Console.ANSI as CA ---------------- -- DATA TYPES -- ---------------- -type Width = Integer -type Height = Integer -type Row = Integer -type Column = Integer -type Coords = (Row, Column) -- row, column, from TL (TL = 1, 1) +-- | 'Row's and 'Column's are 1-based (top-left position is @1 1@). +type Coords = (Row, Column) +type Row = Int +type Column = Int +-- | Size of a surface in 'Row's and 'Column's. +type Dimensions = (Width, Height) + +-- | Expressed in 'Column's. +type Width = Int +-- | Expressed in 'Row's. +type Height = Int + +type Bold = Bool +type Reversed = Bool + +data ColorInfo = ANSIColorInfo (CA.Color, CA.ColorIntensity) + | RGBColorInfo (S.Colour Float) + | PaletteColorInfo W.Word8 + deriving (Show, Eq) + -- can be an ASCIIChar or a special, transparent character -data Cell = CellChar Char +data Cell = CellChar Char Bold Reversed (Maybe ColorInfo) | Transparent - deriving (Show, Eq, Ord, G.Generic) + deriving (Show, Eq, G.Generic) + -- I found no meaningful speed improvements by making this + -- only w/ 1 constructor. --- A place where to blit stuff. Coordinates starts from top left --- corner (1, 1) -newtype Plane = Plane { fromPlane :: (A.Array Coords Cell) } +-- | A two-dimensional surface (Row, Column) where to blit stuff. +newtype Plane = Plane { fromPlane :: A.Array Coords Cell } deriving (Show, Eq, G.Generic) + -- Could this be made into an UArray? Nope, since UArray is + -- only instanced on Words, Int, Chars, etc. +------------------------------------------------------------------------------- +-- Plane interface (abstracting Array) + +listPlane :: Coords -> [Cell] -> Plane +listPlane (r, c) cs = Plane $ A.listArray ((1,1), (r, c)) cs + +-- | Dimensions or a plane. +planeSize :: Plane -> Dimensions +planeSize p = T.swap . snd $ A.bounds (fromPlane p) + +assocsPlane :: Plane -> [(Coords, Cell)] +assocsPlane p = A.assocs (fromPlane p) + +elemsPlane :: Plane -> [Cell] +elemsPlane p = A.elems (fromPlane p) + +-- Array.// +updatePlane :: Plane -> [(Coords, Cell)] -> Plane +updatePlane (Plane a) kcs = Plane $ a A.// kcs + +-- faux map +mapPlane :: (Cell -> Cell) -> Plane -> Plane +mapPlane f (Plane a) = Plane $ fmap f a + + ---------- -- CREA -- ---------- creaCell :: Char -> Cell -creaCell ch = CellChar ch - --- creates plane from a string, good to import ascii art/diagrams/etc. --- Char indicates transparency, integer = pic width -stringPlane :: Maybe Char -> Integer -> String -> Plane -stringPlane mc w t = vitrous +creaCell ch = CellChar chm False False Nothing where - lined = lines t + chm = win32SafeChar ch - h :: Integer - h = L.genericLength lined +colorCell :: CA.Color -> CA.ColorIntensity -> Cell -> Cell +colorCell k i (CellChar c b r _) = CellChar c b r (Just $ ANSIColorInfo (k, i)) +colorCell _ _ Transparent = Transparent - pad :: Integer -> String -> String - pad mw t = take (fromIntegral mw) (t ++ repeat ' ') +rgbColorCell :: S.Colour Float -> Cell -> Cell +rgbColorCell k (CellChar c b r _) = CellChar c b r (Just $ RGBColorInfo k) +rgbColorCell _ Transparent = Transparent - padded :: [String] - padded = map (pad w) lined +paletteColorCell :: W.Word8 -> Cell -> Cell +paletteColorCell k (CellChar c b r _) = CellChar c b r (Just $ PaletteColorInfo k) +paletteColorCell _ Transparent = Transparent - celled :: [Cell] - celled = map creaCell . concat $ padded +boldCell :: Cell -> Cell +boldCell (CellChar c _ r k) = CellChar c True r k +boldCell Transparent = Transparent - plane :: Plane - plane = Plane $ A.listArray ((1,1), (h, w)) celled +reverseCell :: Cell -> Cell +reverseCell (CellChar c b _ k) = CellChar c b True k +reverseCell Transparent = Transparent - vitrous :: Plane - vitrous = case mc of - Just c -> addVitrum c plane - Nothing -> plane +-- | Creates a 'Plane' from a list of individually styled cells. +-- More efficient than folding ('&') when drawing many cells +-- as it performs a single array update instead of one per cell. +-- Cells outside the plane bounds are silently ignored. +cellsPlane :: Width -> Height -> [(Coords, Cell)] -> Plane +cellsPlane w h cells = updatePlane (blankPlane w h) (filter inside cells) + where + inside ((r, c), _) = r >= 1 && r <= h && c >= 1 && c <= w +-- | Creates 'Plane' from 'String', good way to import ASCII +-- art/diagrams. Returns a 1×1 transparent plane on empty string. +stringPlane :: String -> Plane +stringPlane t = stringPlaneGeneric Nothing t --- creates an empty, opaque Plane (limits: TL, BR) +-- | Same as 'stringPlane', but with transparent 'Char'. +-- Returns a 1×1 transparent plane on empty string. +stringPlaneTrans :: Char -> String -> Plane +stringPlaneTrans c t = stringPlaneGeneric (Just c) t + +-- | Creates an empty, opaque 'Plane'. blankPlane :: Width -> Height -> Plane -blankPlane w h = Plane $ A.listArray ((1,1), (h, w)) (repeat $ creaCell ' ') +blankPlane w h = listPlane (h, w) (repeat $ creaCell ' ') --- add transparency to a plane, matching a given character -addVitrum :: Char -> Plane -> Plane -addVitrum tc p = mapPlane f p +-- | Adds transparency to a plane, matching a given character +makeTransparent :: Char -> Plane -> Plane +makeTransparent tc p = mapPlane f p where f cl | cellChar cl == tc = Transparent | otherwise = cl +-- | Changes every transparent cell in the 'Plane' to an opaque @' '@ +-- character. +makeOpaque :: Plane -> Plane +makeOpaque p = let (w, h) = planeSize p + in pastePlane p (blankPlane w h) (1, 1) + + ----------- -- SLICE -- ----------- --- copies a slice of the plane (bl, tr) -copyPlane :: Plane -> Coords -> Coords -> Plane -copyPlane p (r1, c1) (r2, c2) = - Plane $ A.listArray ((1, 1), (w', h')) (map snd section) - where - inside ((r, c), _) | r >= r1 && r <= r2 && - c >= c1 && c <= c2 = True - | otherwise = False - - (w, h) = planeSize p - - w' = min w c2 - max c1 1 + 1 - h' = min h r2 - max r1 1 + 1 - - section = filter inside (assocsPlane p) - --- paste one plane over the other at a certain position (p1 gets over p2). --- Remember that coordinates start from bottom left! --- Maybe char = possible transparency +-- | Paste one plane over the other at a certain position (p1 gets over p2). pastePlane :: Plane -> Plane -> Coords -> Plane -pastePlane p1 p2 (r, c) = updatePlane p2 filtered +pastePlane p1 p2 (r, c) + | r > h2 || c > w2 = p2 + | otherwise = + let ks = assocsPlane p1 + fs = filter (\x -> solid x && inside x) ks + ts = fmap (B.first trasl) fs + in updatePlane p2 ts where - cs = assocsPlane p1 - (w2, h2) = planeSize p2 - traslated = fmap (\((r1, c1), cl) -> ((r1 + r - 1, c1 + c -1), cl)) - cs - filtered = filter (\x -> inside x && solid x) traslated + trasl :: Coords -> Coords + trasl (wr, wc) = (wr + r - 1, wc + c - 1) - inside ((r1, c1), _) | r1 >= 1 && r1 <= h2 && - c1 >= 1 && c1 <= w2 = True - | otherwise = False + -- inside new position, cheaper than first mapping and then + -- filtering. + inside (wcs, _) = + let (r1', c1') = trasl wcs + in r1' >= 1 && r1' <= h2 && + c1' >= 1 && c1' <= w2 solid (_, Transparent) = False - solid (_, otherwise) = True + solid _ = True + (w2, h2) = planeSize p2 +-- | Cut out a plane by top-left and bottom-right coordinates. +-- Returns a 1×1 transparent plane when @r1>r2@ or @c1>c2@. +subPlane :: Plane -> Coords -> Coords -> Plane +subPlane p (r1, c1) (r2, c2) + | r1 > r2 || c1 > c2 = makeTransparent ' ' (blankPlane 1 1) + | otherwise = + let cs = assocsPlane p + fs = filter f cs + (pw, ph) = planeSize p + (w, h) = (min pw (c2-c1+1), min ph (r2-r1+1)) + in listPlane (h, w) (map snd fs) + where + f ((rw, cw), _) = rw >= r1 && rw <= r2 && + cw >= c1 && cw <= c2 + ------------- -- INQUIRE -- ------------- -planeSize :: Plane -> (Width, Height) -planeSize p = T.swap . snd $ A.bounds (fromPlane p) - cellChar :: Cell -> Char -cellChar (CellChar ch) = ch -cellChar Transparent = ' ' +cellChar (CellChar c _ _ _) = c +cellChar Transparent = ' ' -assocsPlane :: Plane -> [(Coords, Cell)] -assocsPlane p = A.assocs (fromPlane p) +cellColor :: Cell -> Maybe ColorInfo +cellColor (CellChar _ _ _ k) = k +cellColor Transparent = Nothing --- an '\n' divided (and ended) String ready to be written on file -paperPlane :: Plane -> String -paperPlane p = unlines . LS.chunksOf w . - map c2c . A.elems $ fromPlane p +isBold :: Cell -> Bool +isBold (CellChar _ b _ _) = b +isBold _ = False + +isReversed :: Cell -> Bool +isReversed (CellChar _ _ r _) = r +isReversed _ = False + +-- | A String (@\\n@ divided and ended) representing the 'Plane'. Useful +-- for debugging/testing purposes. +planePaper :: Plane -> String +planePaper p = unlines . LS.chunksOf w . map cellChar $ elemsPlane p where w :: Int w = fromIntegral . fst . planeSize $ p - c2c :: Cell -> Char - c2c Transparent = ' ' - c2c (CellChar c) = c - - ----------------- -- ANCILLARIES -- ----------------- --- faux map -mapPlane :: (Cell -> Cell) -> Plane -> Plane -mapPlane f (Plane a) = Plane $ fmap f a +stringPlaneGeneric :: Maybe Char -> String -> Plane +stringPlaneGeneric _ "" = makeTransparent ' ' (blankPlane 1 1) +stringPlaneGeneric mc t = vitrous + where + lined = lines t --- Array.// -updatePlane :: Plane -> [(Coords, Cell)] -> Plane -updatePlane (Plane a) kcs = Plane $ a A.// kcs + h :: Int + h = length lined + + w :: Int + w = maximum (map length lined) + + pad :: Int -> String -> String + pad mw tl = take mw (tl ++ repeat ' ') + + padded :: [String] + padded = map (pad w) lined + + celled :: [Cell] + celled = map creaCell . concat $ padded + + plane :: Plane + plane = listPlane (h, w) celled + + vitrous :: Plane + vitrous = case mc of + Just c -> makeTransparent c plane + Nothing -> plane +
+ src/Terminal/Game/Random.hs view
@@ -0,0 +1,20 @@+module Terminal.Game.Random ( R.StdGen,+ R.UniformRange,+ R.getStdGen,+ R.mkStdGen,+ getRandom,+ pickRandom )+ where++import System.Random as R+++-- | Simple, pure pseudo-random generator.+getRandom :: UniformRange a => (a, a) -> StdGen -> (a, StdGen)+getRandom bs sg = uniformR bs sg++-- | Picks at random from list.+pickRandom :: [a] -> StdGen -> (a, StdGen)+pickRandom as sg = let l = length as+ (a, sg') = getRandom (0, l-1) sg+ in (as !! a, sg')
− src/Terminal/Game/Utils.hs
@@ -1,11 +0,0 @@-module Terminal.Game.Utils where--import Terminal.Game.Plane--import qualified System.Console.Terminal.Size as TS--screenSize :: IO (Width, Height)-screenSize =- TS.size >>= \ts ->- let (TS.Window h w) = maybe (error "cannot get TERM size") id ts- in return (w, h)
+ test/Terminal/Game/DrawSpec.hs view
@@ -0,0 +1,68 @@+module Terminal.Game.DrawSpec where++import Test.Hspec+import Terminal.Game.Plane+import Terminal.Game.Draw+import Terminal.Game -- language hyphenators+++spec :: Spec+spec = do++ describe "mergePlanes" $ do+ it "piles multiple planes together" $+ mergePlanes (stringPlane "aa")+ [((1,2), cell 'b')] `shouldBe` stringPlane "ab"+ it "works in the middle too" $+ mergePlanes (stringPlane "aaa\naaa\naaa")+ [((2,2), cell 'b')] `shouldBe`+ stringPlane "aaa\naba\naaa"++ describe "textBox/textBoxLiquid" $ do+ let s = "las rana in Spa"+ w = 6+ ps = textBox w 2 s+ pl = textBoxLiquid w s+ it "textBox follows specific size" $+ planeSize ps `shouldBe` (6, 2)+ it "textBoxLiquid fits the whole string" $+ planeSize pl `shouldBe` (6, 3)+ it "textBox should make a transparent plane" $+ let p1 = textBox 6 1 "a c e "+ p2 = textBox 6 1 " b d f"+ pc = p1 & (1, 1) % p2+ in planePaper pc `shouldBe` "abcdef\n"++ describe "textBoxHypen" $ do+ let tbh = textBoxHyphen spanish 8 2 "Con pianito"+ it "hyphens long words" $+ planePaper tbh `shouldSatisfy` elem '-'++ describe "***" $ do+ let a = stringPlane ".\n.\n.\n"+ b = stringPlane "*"+ c = stringPlane ".\n*\n.\n"+ it "blits b in the centre of a" $+ a *** b `shouldBe` c++ -- combinators++ let sp = stringPlane "ab"+ bp = blankPlane 4 3++ describe "%^>" $ do+ it "blits in the top right corner" $+ planePaper (bp & (1,1) %^> sp) `shouldBe` " ab\n \n \n"++ describe "%_<" $ do+ it "blits in the bottom left corner" $+ planePaper (bp & (2,1) %.< sp) `shouldBe` " \nab \n \n"++ describe "%_<" $ do+ it "blits in the bottom left corner" $+ planePaper (bp & (2,3) %.> sp) `shouldBe` " \nab \n \n"++ describe "%" $ do+ it "mixes with alternative combinators" $+ planePaper (bp & (1,2) % sp & (2,3) %.> sp) `shouldBe`+ " ab \nab \n \n"
+ test/Terminal/Game/Layer/ImperativeSpec.hs view
@@ -0,0 +1,72 @@+module Terminal.Game.Layer.ImperativeSpec where++import Terminal.Game.Layer.Imperative+import Terminal.Game.Layer.Object+import Terminal.Game.Random+import Alone+import Balls++import Test.Hspec+import Test.Hspec.QuickCheck++import qualified Control.Exception as E+import qualified Test.QuickCheck as Q+import qualified GHC.Exts as X++-- Test for state.+stateTest :: Show r => Game s r -> GRec -> s+stateTest g r = either em id (testGame g r)+ where+ em wr = error $ "stateTest: " ++ show wr++spec :: Spec+spec = do++ describe "runGame" $ do+ let nd = error "<not-defined>"+ s :: (Integer, Bool, Integer)+ s = (0, False, 0)+ lf (t, True, i) Tick = Right (t+1, True, i+1)+ lf (t, b, i) Tick = Right (t+1, b, i )+ lf (t, _, i) (KeyPress _) = Right (t, True, i )+ es = [Tick, KeyPress 'c', KeyPress 'c', Tick, Tick]+ g :: Game (Integer, Bool, Integer) ()+ g = Game nd s (const lf) nd+ it "does not confuse input and logic" $+ stateTest g (createGRec (80, 24) es) `shouldBe` (3, True, 2)++ describe "testGame" $ do+ it "tests a game" $ do+ r <- readRecord "test/records/alone-record-test.gr"+ stateTest aloneInARoom r `shouldBe` MyState (20, 66) Stop+ it "tests a game exiting correctly" $ do+ r <- readRecord "test/records/alone-record-left.gr"+ testGame aloneInARoom r `shouldBe` Left ()+ it "picks up screen resize events" $ do+ r <- readRecord "test/records/balls-dims.gr"+ let g = fireworks (mkStdGen 1)+ t = stateTest g r+ length (balls t) `shouldBe` 1+ it "picks FPS too" $ do+ r <- readRecord "test/records/balls-slow.gr"+ let g = fireworks (mkStdGen 1)+ t = stateTest g r+ bslow t `shouldBe` True+ it "does not hang on empty/unclosed input" $+ let w = createGRec (80, 24) [Tick] in+ stateTest aloneInARoom w `shouldBe` MyState (10, 10) Stop+ modifyMaxSize (const 1000) $+ it "does not crash/hang on random input" $ Q.property $+ let genEvs = Q.listOf1 Q.arbitrary+ in Q.forAll genEvs $+ \es -> let w = createGRec (80, 24) es+ a = testGame aloneInARoom w+ in a == a+ it "fails with an informative message" $ do+ r <- readRecord "test/records/alone-record-test.gr"+ let r' = r { aTermSize = X.fromList (replicate 1000 Nothing) }+ t = testGame aloneInARoom r'+ e = "testGame, exception called: [TSetupDisplay,TStartEvents,\+ \TException CannotGetDisplaySize]"+ E.evaluate t `shouldThrow` errorCall e+
+ test/Terminal/Game/Layer/Object/TestSpec.hs view
@@ -0,0 +1,19 @@+module Terminal.Game.Layer.Object.TestSpec where++import Terminal.Game.Layer.Imperative+import Terminal.Game.Layer.Object+import Alone++import Test.Hspec++import qualified GHC.Exts as E++spec :: Spec+spec = do++ describe "runTest" $ do+ it "logs exceptions without failing" $ do+ r <- readRecord "test/records/alone-record-test.gr"+ let r' = r { aTermSize = E.fromList (replicate 1000 Nothing) }+ t = runTest (runGameGeneral aloneInARoom) r'+ last (snd t) `shouldBe` TException CannotGetDisplaySize
+ test/Terminal/Game/PlaneSpec.hs view
@@ -0,0 +1,57 @@+module Terminal.Game.PlaneSpec where++import Test.Hspec+import Terminal.Game.Plane+import Terminal.Game.Draw+++spec :: Spec+spec = do++ let testPlane = blankPlane 2 2 &+ (1,1) % box 2 2 '.' &+ (1,2) % cell ' '++ describe "listPlane" $ do+ it "creates a plane from string" $+ listPlane (2,2) (map creaCell ". ..") `shouldBe` testPlane+ it "ignores extra characters" $+ listPlane (2,2) (map creaCell ". ..abc") `shouldBe` testPlane++ describe "pastePlane" $ do+ it "pastes a simple plane onto another" $+ pastePlane (cell 'a') (cell 'b') (1,1) `shouldBe` cell 'a'++ describe "stringPlane" $ do+ it "creates plane from spec" $+ stringPlane ".\n.." `shouldBe` testPlane++ describe "stringPlaneTrans" $ do+ it "allows transparency" $+ stringPlaneTrans '.' ".\n.." `shouldBe` makeTransparent '.' testPlane++ describe "updatePlane" $ do+ let ma = listPlane (2,1) (map creaCell "ab")+ mb = listPlane (2,1) (map creaCell "xb")+ it "updates a Plane" $+ updatePlane ma [((1,1), creaCell 'x')] `shouldBe` mb++ describe "subPlane" $ do+ let pa = word "prova" === word "fol"+ it "cuts out a plane" $+ planePaper (subPlane pa (1, 1) (2, 1)) `shouldBe` "p\nf\n"+ it "does not crash on OOB" $+ planeSize (subPlane pa (1, 1) (10, 10)) `shouldBe` (5, 2)+ it "does not err on inconsistent coords" $+ subPlane pa (2, 3) (1, 1) `shouldBe`+ (blankPlane 1 1 # makeTransparent ' ')+ it "but not on a single cell" $+ subPlane pa (2, 3) (2, 3) `shouldBe` cell 'l'++ describe "hcat/vcat" $ do+ let pa = blankPlane 2 1+ pb = blankPlane 3 4+ it "concats planes horizontally with hcat" $+ planeSize (hcat [pa, pb]) `shouldBe` (5, 4)+ it "concats planes horizontally with vcat" $+ planeSize (vcat [pa, pb]) `shouldBe` (3, 5)
+ test/Terminal/Game/RandomSpec.hs view
@@ -0,0 +1,20 @@+module Terminal.Game.RandomSpec where++import Test.Hspec+import Test.Hspec.QuickCheck+import Terminal.Game.Random+++spec :: Spec+spec = do++ describe "pickRandom" $ do+ prop "picks items at random from a list" $+ \i -> let g = mkStdGen i+ in fst (pickRandom ['a', 'b'] g) /= 'c'+ prop "does not exclude any item" $+ \i -> let g = mkStdGen i+ rf tg = pickRandom [1,2] tg+ rs = iterate (\(_, lg') -> rf lg') (rf g)+ ts = take 100 rs+ in sum (map fst ts) /= length ts
+ test/records/alone-record-left.gr view
binary file changed (absent → 2960 bytes)
+ test/records/alone-record-test.gr view
binary file changed (absent → 9317 bytes)
+ test/records/balls-dims.gr view
binary file changed (absent → 6460 bytes)
+ test/records/balls-slow.gr view
binary file changed (absent → 2418 bytes)