├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── apple-strdupa.h ├── makesamples ├── puzzlebox.c └── webform.cgi /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *~ 3 | *.swp 4 | puzzlebox 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | puzzlebox: puzzlebox.c 2 | ifeq ($(shell uname),Darwin) 3 | /usr/local/opt/gcc/bin/gcc-8 -L/usr/local/lib -I/usr/local/include -O -o $@ $< -lpopt -lm -g -D_GNU_SOURCE 4 | else 5 | cc -O -o $@ $< -lpopt -lm -g -D_GNU_SOURCE 6 | endif 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Puzzle Box 2 | Makes OpenSCAD file for a puzzle box 3 | 4 | Has been tested on linux, you probably need libpopt-dev 5 | 6 | As you will see from the Makefile, you will need popt development library. 7 | You also need gcc supporting _GNU_SOURCE for strdupa() and asprintf() 8 | 9 | And, for the avoidance of doubt, obviously, this is normally built using "make" 10 | 11 | When built, use --help for options, or see https://www.me.uk/puzzlebox 12 | 13 | On macOS, this requires gcc, so "brew install gcc", then should make with no problem. 14 | 15 | (c) Copyright 2019 Adrian Kennard. See LICENSE file (GPL) 16 | -------------------------------------------------------------------------------- /apple-strdupa.h: -------------------------------------------------------------------------------- 1 | #ifdef __APPLE__ 2 | 3 | # define strdupa(s) \ 4 | (__extension__ \ 5 | ({ \ 6 | const char *__old = (s); \ 7 | size_t __len = strlen (__old) + 1; \ 8 | char *__new = (char *) __builtin_alloca (__len); \ 9 | (char *) memcpy (__new, __old, __len); \ 10 | })) 11 | 12 | #endif // __APPLE__ 13 | -------------------------------------------------------------------------------- /makesamples: -------------------------------------------------------------------------------- 1 | #!/bin/csh 2 | rm -f /tmp/*.scad /tmp/*.stl 3 | # Version box 4 | setenv F version 5 | ./puzzlebox --parts=2 --core-height=100 --core-diameter=12 --outer-sides=7 --text-outset --text-slow --text-depth=0.8 --text-end='AK' --text-side=" Test box $*\www.me.uk\@TheRealRevK\Build version date :-\`date --utc +%FT%TZ`\GitHub revk/PuzzleBox\Thingiverse 2410748" --text-font=FiveByNineJTD --text-font-end=FiveByNineJTD --logo --text-side-scale=0.85 --maze-complexity=8 $* > /tmp/$F.scad 6 | # Other boxes 7 | foreach h (0 1 2 3) 8 | foreach w (0 1 2) 9 | setenv F test-helix-$h-part-$w-of-2 10 | if(! $w) setenv F all-test-helix-$h 11 | ./puzzlebox --base-height=5 --parts=2 --core-height=15 --core-diameter=10 --part=$w --text-end="$h\$h" --helix=$h --logo $* > /tmp/$F.scad 12 | end 13 | end 14 | foreach w (0 1 2 3) 15 | setenv F test-hard-part-$w-of-3 16 | if(! $w) setenv F all-test-hard 17 | ./puzzlebox --parts=3 --core-height=30 --core-diameter=10 --outer-sides=5 --part=$w --inside --logo $* > /tmp/$F.scad 18 | end 19 | foreach w (0 1 2 3) 20 | setenv F test-flip-part-$w-of-3 21 | if(! $w) setenv F all-test-flip 22 | ./puzzlebox --parts=3 --base-height=5 --core-height=20 --core-diameter=10 --outer-sides=5 --part=$w --flip --inside --logo $* > /tmp/$F.scad 23 | end 24 | foreach w (0 1 2) 25 | setenv F lottery-ticket-hard-part-$w-of-2 26 | if(! $w) setenv F all-lottery-ticket-hard 27 | ./puzzlebox --parts=2 --core-height=85 --core-gap=20 --core-diameter=10 --inside --outer-sides=4 --part=$w --text-slow --text-depth=1 --text-end=' £ ' --text-side="Good luck\\Prepare to\be amazed!" --text-font="Mountains of Christmas" --logo $* > /tmp/$F.scad 28 | end 29 | foreach w (0 1 2) 30 | setenv F lottery-ticket-easy-part-$w-of-2 31 | if(! $w) setenv F all-lottery-ticket-easy 32 | ./puzzlebox --parts=2 --core-height=85 --core-gap=20 --core-diameter=10 --outer-sides=4 --part=$w --text-slow --text-depth=1 --text-end=' £ ' --text-side="Good luck\\Prepare to\be amazed!" --text-font="Mountains of Christmas" --logo $* > /tmp/$F.scad 33 | end 34 | foreach w (0 1 2 3) 35 | setenv F five-pound-coins-part-$w-of-3 36 | if(! $w) setenv F all-five-pound-coin 37 | ./puzzlebox --parts=3 --inside --flip --core-solid --core-gap=10 --core-height=14 --core-diameter=24 --outer-sides=12 --part=$w --text-slow --text-depth=1 --text-end=' £ ' --logo $* > /tmp/$F.scad 38 | end 39 | foreach w (0 1 2 3) 40 | setenv F ten-pound-coins-part-$w-of-3 41 | if(! $w) setenv F all-ten-pound-coin 42 | ./puzzlebox --parts=3 --inside --flip --core-solid --core-gap=10 --core-height=28 --core-diameter=24 --outer-sides=12 --part=$w --text-slow --text-depth=1 --text-end=' £ ' --logo $* > /tmp/$F.scad 43 | end 44 | foreach s (30/30 50/20 70/10) 45 | foreach w (0 1 2 3 4) 46 | setenv F easy-${s:t}x${s:h}mm-part-$w-of-4 47 | if(! $w) setenv F all-easy-${s:t}x${s:h}mm 48 | ./puzzlebox --parts=4 --core-gap=10 --core-diameter=$s:t --core-height=$s:h --part=$w --text-slow --text-depth=1 --text-end="☺" --logo $* > /tmp/$F.scad 49 | end 50 | foreach w (0 1 2 3 4) 51 | setenv F hard-${s:t}x${s:h}mm-part-$w-of-4 52 | if(! $w) setenv F all-hard-${s:t}x${s:h}mm 53 | ./puzzlebox --parts=4 --core-gap=10 --core-diameter=$s:t --core-height=$s:h --inside --part=$w --text-slow --text-depth=1 --text-end="☺" --logo $* > /tmp/$F.scad 54 | end 55 | end 56 | 57 | setenv F all-test-six-outside 58 | ./puzzlebox --parts=6 --core-gap=10 --core-diameter=12 --core-height=50 --logo $* > /tmp/$F.scad 59 | setenv F all-test-six-inside 60 | ./puzzlebox --parts=6 --core-gap=10 --core-diameter=12 --core-height=50 --logo --inside $* > /tmp/$F.scad 61 | setenv F all-test-six-outside-flip 62 | ./puzzlebox --parts=6 --core-gap=10 --core-diameter=12 --core-height=50 --logo --flip $* > /tmp/$F.scad 63 | setenv F all-test-six-inside-flip 64 | ./puzzlebox --parts=6 --core-gap=10 --core-diameter=12 --core-height=50 --logo --flip --inside $* > /tmp/$F.scad 65 | setenv F all-ten 66 | ./puzzlebox --parts=10 --core-gap=10 --core-diameter=20 --core-height=50 --logo --text-slow --text-depth=1 --text-end='10\9\8\7\6\5\4\3\2\1' $* > /tmp/$F.scad 67 | 68 | parallel --bar openscad -o "{.}.stl" "{}" ::: /tmp/*.scad 69 | -------------------------------------------------------------------------------- /puzzlebox.c: -------------------------------------------------------------------------------- 1 | // Puzzle box maker 2 | // (c) 2018 Adrian Kennard www.me.uk @TheRealRevK 3 | // This includes a distinctive "A" in the design at the final park point, otherwise there are no loops in the maze 4 | // Please leave the "A" in the design as a distinctive feature 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include "apple-strdupa.h" 19 | 20 | // Flags for maze array 21 | #define FLAGL 0x01 // Left 22 | #define FLAGR 0x02 // Right 23 | #define FLAGU 0x04 // Up 24 | #define FLAGD 0x08 // Down 25 | #define FLAGA 0x0F // All directions 26 | #define FLAGI 0x80 // Invalid 27 | 28 | #define BIASL 2 // Direction bias for random maze choices 29 | #define BIASR 1 30 | #define BIASU 1 31 | #define BIASD 4 32 | 33 | #define SCALE 1000LL // Scales used for some aspects of output 34 | #define SCALEI "0.001" 35 | #define scaled(x) ((long long)round((x)*SCALE)) 36 | 37 | int 38 | main (int argc, const char *argv[]) 39 | { 40 | double basethickness = 1.6; 41 | double basegap = 0.4; 42 | double baseheight = 10; 43 | double corediameter = 10; 44 | double coreheight = 50; 45 | double wallthickness = 1.2; 46 | double mazethickness = 2; 47 | double mazestep = 3; 48 | double clearance = 0.4; // General X/Y clearance for parts 49 | double nubrclearance = 0.1; // Extra radius clearance for nub, should be less than clearance, can be -ve 50 | double nubzclearance = 0.2; // Extra Z clearance (per /4 maze step) 51 | double parkthickness = 0.7; 52 | double coregap = 0; 53 | double outerround = 2; 54 | double mazemargin = 1; 55 | double textdepth = 0.5; 56 | double logodepth = 0.6; 57 | double gripdepth = 2; 58 | double textsidescale = 1; 59 | char *textinside = NULL; 60 | char *textend = NULL; 61 | char *textsides = NULL; 62 | char *textfont = NULL; 63 | char *textfontend = NULL; 64 | int parts = 4; 65 | int part = 0; 66 | int inside = 0; 67 | int flip = 0; 68 | int outersides = 7; 69 | int testmaze = 0; 70 | int helix = 3; 71 | int nubs = helix; 72 | int logo = 0; 73 | int textslow = 0; 74 | int textoutset = 0; 75 | int symmectriccut = 0; 76 | int coresolid = 0; 77 | int mime = (getenv ("HTTP_HOST") ? 1 : 0); 78 | int webform = 0; 79 | int parkvertical = 0; 80 | int mazecomplexity = 5; 81 | int mirrorinside = 0; // Clockwise lock on inside - may be unwise as more likely to come undone with outer. 82 | int noa = 0; 83 | int basewide = 0; 84 | 85 | int f = open ("/dev/urandom", O_RDONLY); 86 | if (f < 0) 87 | err (1, "Open /dev/random"); 88 | 89 | char pathsep = 0; 90 | char *path = getenv ("PATH_INFO"); 91 | if (path) 92 | pathsep = '/'; 93 | else if ((path = getenv ("QUERY_STRING"))) 94 | pathsep = '&'; 95 | 96 | const struct poptOption optionsTable[] = { 97 | {"parts", 'm', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &parts, 0, "Total parts", "N"}, 98 | {"part", 'n', POPT_ARG_INT, &part, 0, "Part to make", "N (0 for all)"}, 99 | {"inside", 'i', POPT_ARG_NONE, &inside, 0, "Maze on inside (hard)"}, 100 | {"flip", 'f', POPT_ARG_NONE, &flip, 0, "Alternating inside/outside maze"}, 101 | {"nubs", 'N', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &nubs, 0, "Nubs", "N"}, 102 | {"helix", 'H', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &helix, 0, "Helix", "N (0 for non helical)"}, 103 | {"base-height", 'b', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &baseheight, 0, "Base height", "mm"}, 104 | {"core-diameter", 'c', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &corediameter, 0, "Core diameter for content", "mm"}, 105 | {"core-height", 'h', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &coreheight, 0, "Core height for content", "mm"}, 106 | {"core-gap", 'C', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &coregap, 0, "Core gap to allow content to be removed", "mm"}, 107 | {"core-solid", 'q', POPT_ARG_NONE, &coresolid, 0, "Core solid (content is in part 2)"}, 108 | {"base-thickness", 'B', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &basethickness, 0, "Base thickness", "mm"}, 109 | {"base-gap", 'G', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &basegap, 0, "Base gap (Z clearance)", "mm"}, 110 | {"base-wide", 'W', POPT_ARG_NONE, &basewide, 0, "Inside base full width"}, 111 | {"part-thickness", 'w', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &wallthickness, 0, "Wall thickness", "mm"}, 112 | {"maze-thickness", 't', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &mazethickness, 0, "Maze thickness", "mm"}, 113 | {"maze-step", 'z', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &mazestep, 0, "Maze spacing", "mm"}, 114 | {"maze-margin", 'M', POPT_ARG_DOUBLE | (mazemargin ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &mazemargin, 0, "Maze top margin", "mm"}, 115 | {"maze-complexity", 'X', POPT_ARG_INT | (mazecomplexity ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &mazecomplexity, 0, 116 | "Maze complexity", "-10 to 10"}, 117 | {"park-thickness", 'p', POPT_ARG_DOUBLE | (parkthickness ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &parkthickness, 0, 118 | "Thickness of park ridge to click closed", "mm"}, 119 | {"park-vertical", 'v', POPT_ARG_NONE, &parkvertical, 0, "Park vertically"}, 120 | {"clearance", 'g', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &clearance, 0, "General X/Y clearance", "mm"}, 121 | {"outer-sides", 's', POPT_ARG_INT | (outersides ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &outersides, 0, "Number of outer sides", 122 | "N (0=round)"}, 123 | {"outer-round", 'r', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &outerround, 0, "Outer rounding on ends", "mm"}, 124 | {"grip-depth", 'R', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &gripdepth, 0, "Grip depth", "mm"}, 125 | {"text-depth", 'D', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &textdepth, 0, "Text depth", "mm"}, 126 | {"text-end", 'E', POPT_ARG_STRING | (textend ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &textend, 0, "Text (initials) on end", 127 | "X{\\X...}"}, 128 | {"text-side", 'S', POPT_ARG_STRING | (textsides ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &textsides, 0, "Text on sides", 129 | "Text{\\Text...}"}, 130 | {"text-font", 'F', POPT_ARG_STRING | (textfont ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &textfont, 0, "Text font (optional)", 131 | "Font"}, 132 | {"text-font-end", 'e', POPT_ARG_STRING | (textfontend ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &textfontend, 0, 133 | "Text font for end (optional)", "Font"}, 134 | {"text-slow", 'd', POPT_ARG_NONE, &textslow, 0, "Text has diagonal edges (very slow)"}, 135 | {"text-side-scale", 'T', POPT_ARG_DOUBLE, &textsidescale, 0, "Scale side text (i.e. if too long)", "N"}, 136 | {"text-outset", 'O', POPT_ARG_NONE, &textoutset, 0, "Text on sides is outset not embossed"}, 137 | {"text-inside", 'I', POPT_ARG_STRING | (textinside ? POPT_ARGFLAG_SHOW_DEFAULT : 0), &textinside, 0, 138 | "Text (initials) inside end", "X{\\X...}"}, 139 | {"logo-depth", 'L', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &logodepth, 0, "Logo (and inside text) cut depth", "mm"}, 140 | {"symmetric-cut", 'V', POPT_ARG_NONE, &symmectriccut, 0, "Symmetric maze cut"}, 141 | {"nub-r-clearance", 'y', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &nubrclearance, 0, "Extra clearance on radius for nub", 142 | "mm"}, 143 | {"nub-z-clearance", 'Z', POPT_ARG_DOUBLE | POPT_ARGFLAG_SHOW_DEFAULT, &nubzclearance, 0, "Extra clearance on height of nub", 144 | "mm"}, 145 | {"logo", 'A', POPT_ARG_NONE, &logo, 0, "Include A&A logo in last lid"}, 146 | {"test", 'Q', POPT_ARG_NONE, &testmaze, 0, "Test pattern instead of maze"}, 147 | {"mime", 0, POPT_ARG_NONE | (mime ? POPT_ARGFLAG_DOC_HIDDEN : 0), &mime, 0, "MIME Header"}, 148 | {"no-a", 0, POPT_ARG_NONE | (noa ? POPT_ARGFLAG_DOC_HIDDEN : 0), &noa, 0, "No A"}, 149 | {"web-form", 0, POPT_ARG_NONE, &webform, 0, "Web form"}, 150 | POPT_AUTOHELP {} 151 | }; 152 | 153 | { // POPT 154 | poptContext optCon; // context for parsing command-line options 155 | 156 | optCon = poptGetContext (NULL, argc, argv, optionsTable, 0); 157 | //poptSetOtherOptionHelp (optCon, ""); 158 | 159 | int c; 160 | if ((c = poptGetNextOpt (optCon)) < -1) 161 | errx (1, "%s: %s\n", poptBadOption (optCon, POPT_BADOPTION_NOALIAS), poptStrerror (c)); 162 | 163 | if (poptPeekArg (optCon)) 164 | { 165 | poptPrintUsage (optCon, stderr, 0); 166 | return -1; 167 | } 168 | poptFreeContext (optCon); 169 | } 170 | 171 | char *error = NULL; 172 | if (path) 173 | { // Settings from PATH_INFO 174 | path = strdupa (path); 175 | while (*path) 176 | { 177 | if (*path == pathsep) 178 | { 179 | *path++ = 0; 180 | continue; 181 | } 182 | if (!isalpha (*path)) 183 | { 184 | if (asprintf (&error, "Path error [%s]\n", path) < 0) 185 | errx (1, "malloc"); 186 | break; 187 | } 188 | char arg = *path++; 189 | int o; 190 | for (o = 0; optionsTable[o].longName && optionsTable[o].shortName != arg; o++); 191 | if (!optionsTable[o].shortName) 192 | { 193 | if (asprintf (&error, "Unknown arg [%c]", arg) < 0) 194 | errx (1, "malloc"); 195 | break; 196 | } 197 | if (optionsTable[o].arg) 198 | switch (optionsTable[o].argInfo & POPT_ARG_MASK) 199 | { 200 | case POPT_ARG_INT: 201 | if (*path != '=') 202 | { 203 | if (asprintf (&error, "Missing value [%c=]", arg) < 0) 204 | errx (1, "malloc"); 205 | break; 206 | } 207 | if (path[1]) 208 | *(int *) optionsTable[o].arg = strtod (path + 1, &path); 209 | break; 210 | case POPT_ARG_DOUBLE: 211 | if (*path != '=') 212 | { 213 | if (asprintf (&error, "Missing value [%c=]", arg) < 0) 214 | errx (1, "malloc"); 215 | break; 216 | } 217 | if (path[1]) 218 | *(double *) optionsTable[o].arg = strtod (path + 1, &path); 219 | break; 220 | case POPT_ARG_NONE: 221 | *(int *) optionsTable[o].arg = 1; 222 | if (*path == '=') 223 | { // Skip =on, etc. 224 | path++; 225 | while (*path && *path != pathsep) 226 | path++; 227 | } 228 | break; 229 | case POPT_ARG_STRING: 230 | if (*path != '=') 231 | { 232 | if (asprintf (&error, "Missing value [%c=]", arg) < 0) 233 | errx (1, "malloc"); 234 | break; 235 | } 236 | path++; 237 | *(char **) optionsTable[o].arg = path; 238 | char *o = path; 239 | while (*path && *path != pathsep) 240 | { 241 | if (pathsep == '&' && *path == '+') 242 | *o++ = ' '; 243 | else if (pathsep == '&' && *path == '%' && isxdigit (path[1]) && isxdigit (path[2])) 244 | { 245 | *o++ = 246 | (((isalpha (path[1]) ? 9 : 0) + (path[1] & 0xF)) << 4) + ((isalpha (path[2]) ? 9 : 0) + (path[2] & 0xF)); 247 | path += 2; 248 | } else 249 | *o++ = *path; 250 | path++; 251 | } 252 | if (o < path) 253 | *o = 0; 254 | break; 255 | } 256 | } 257 | } 258 | 259 | if (webform) 260 | { 261 | int o; 262 | for (o = 0; optionsTable[o].longName; o++) 263 | if (optionsTable[o].shortName && optionsTable[o].arg) 264 | { 265 | printf (""); 266 | printf ("", optionsTable[o].shortName, optionsTable[o].shortName, 267 | (optionsTable[o].argInfo & POPT_ARG_MASK) == POPT_ARG_NONE ? "" : "="); 268 | printf (""); 269 | switch (optionsTable[o].argInfo & POPT_ARG_MASK) 270 | { 271 | case POPT_ARG_NONE: // Checkbox 272 | printf ("", optionsTable[o].shortName, optionsTable[o].shortName); 273 | break; 274 | case POPT_ARG_INT: // Select 275 | { 276 | int l = 0, 277 | h = 3, 278 | v = *(int *) optionsTable[o].arg; 279 | if (optionsTable[o].shortName == 'N') 280 | l = 1; // Nubs 281 | if (optionsTable[o].shortName == 'm') 282 | l = 2; // Walls 283 | if (optionsTable[o].shortName == 'n' || optionsTable[o].shortName == 'm') 284 | h = 6; // Walls or part 285 | if (optionsTable[o].shortName == 's') 286 | h = 20; // Sides 287 | if (optionsTable[o].shortName == 'X') 288 | { // Complexity 289 | l = -10; 290 | h = 10; 291 | } 292 | printf (""); 296 | } 297 | break; 298 | case POPT_ARG_DOUBLE: // Double 299 | { 300 | double v = *(double *) optionsTable[o].arg; 301 | printf (" temp && p[-1] == '0'; p--); 308 | if (p > temp && p[-1] == '.') 309 | p--; 310 | *p = 0; 311 | printf (" value='%s'", temp); 312 | } 313 | printf ("/>"); 314 | } 315 | break; 316 | case POPT_ARG_STRING: // String 317 | { 318 | char *v = *(char **) optionsTable[o].arg; 319 | printf (""); 325 | } 326 | break; 327 | } 328 | if (optionsTable[o].argDescrip) 329 | printf ("%s", optionsTable[o].argDescrip); 330 | printf (""); 331 | printf ("", optionsTable[o].shortName, optionsTable[o].descrip); 332 | printf ("\n"); 333 | } 334 | return 0; 335 | } 336 | 337 | // Sanity checks and adjustments 338 | char *normalise (char *t) 339 | { // Simple text normalise 340 | if (!t || !*t) 341 | return NULL; 342 | char *text = t; 343 | while (*t) 344 | { 345 | if (*t == '"') 346 | *t == '\''; 347 | t++; 348 | } 349 | return text; 350 | } 351 | textend = normalise (textend); 352 | textsides = normalise (textsides); 353 | textinside = normalise (textinside); 354 | if (!outersides) 355 | textsides = NULL; 356 | if (textfont && !*textfont) 357 | textfont = NULL; 358 | if (textfont && !textfontend) 359 | textfontend = textfont; 360 | if (textend && !*textend) 361 | textend = NULL; 362 | if (textinside && !*textinside) 363 | textinside = NULL; 364 | if (textsides && !*textsides) 365 | { 366 | textsidescale = 0; 367 | textsides = NULL; 368 | } 369 | if (helix && nubs > 1 && nubs < helix) 370 | { 371 | if (!(helix % 2) && nubs <= helix / 2) 372 | nubs = helix / 2; 373 | else 374 | nubs = helix; 375 | } 376 | if (helix && nubs > helix) 377 | nubs = helix; 378 | if (gripdepth > (baseheight - outerround) / 6) 379 | gripdepth = (baseheight - outerround) / 6; 380 | if (gripdepth > mazethickness) 381 | gripdepth = mazethickness; 382 | if (!logo && !textinside) 383 | logodepth = 0; 384 | if (!textsides && !textend && !textinside) 385 | textdepth = 0; 386 | if (coresolid && coregap < mazestep * 2) 387 | coregap = mazestep * 2; 388 | 389 | int markpos0 = (outersides && outersides / nubs * nubs != outersides); // Mark on position zero for alignment 390 | double nubskew = (symmectriccut ? 0 : mazestep / 8); // Skew the shape of the cut 391 | 392 | // MIME header 393 | if (mime) 394 | { 395 | printf ("Content-Type: application/scad\r\nContent-Disposition: Attachment; filename=puzzlebox"); 396 | int o; 397 | for (o = 0; optionsTable[o].longName; o++) 398 | if (optionsTable[o].shortName && optionsTable[o].arg) 399 | switch (optionsTable[o].argInfo & POPT_ARG_MASK) 400 | { 401 | case POPT_ARG_NONE: 402 | if (!*(int *) optionsTable[o].arg) 403 | break; 404 | printf ("-%c", optionsTable[o].shortName); 405 | break; 406 | case POPT_ARG_INT: 407 | if (!*(int *) optionsTable[o].arg) 408 | break; 409 | printf ("-%d%c", *(int *) optionsTable[o].arg, optionsTable[o].shortName); 410 | break; 411 | case POPT_ARG_DOUBLE: 412 | if (!*(double *) optionsTable[o].arg) 413 | break; 414 | char temp[50], 415 | *p; 416 | sprintf (temp, "%f", *(double *) optionsTable[o].arg); 417 | p = temp + strlen (temp) - 1; 418 | while (*p == '0') 419 | *p-- = 0; 420 | p = strchr (temp, '.'); 421 | if (*p) 422 | *p++ = 0; 423 | printf ("-%s%c%s", temp, optionsTable[o].shortName, p); 424 | break; 425 | case POPT_ARG_STRING: 426 | if (!*(char **) optionsTable[o].arg) 427 | break; 428 | { 429 | char *p = strdupa (*(char * *) optionsTable[o].arg), 430 | *q; 431 | for (q = p; *q; q++) 432 | if (*q <= ' ' || *q == '/' || *q == '\\' || *q == '"' || *q == '\'' || *q == ':' || *q == ';') 433 | *q = '_'; 434 | printf ("-%c%s", optionsTable[o].shortName, p); 435 | } 436 | break; 437 | } 438 | printf (".scad\r\n\r\n"); // Used from apache 439 | } 440 | 441 | printf ("// Puzzlebox by RevK, @TheRealRevK www.me.uk\n"); 442 | printf ("// Thingiverse examples and instructions https://www.thingiverse.com/thing:2410748\n"); 443 | printf ("// GitHub source https://github.com/revk/PuzzleBox\n"); 444 | printf ("// Get new random custom maze gift boxes from https://www.me.uk/puzzlebox\n"); 445 | { // Document args 446 | time_t now = time (0); 447 | struct tm t; 448 | gmtime_r (&now, &t); 449 | printf ("// Created %04d-%02d-%02dT%02d:%02d:%02dZ %s\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, 450 | t.tm_sec, getenv ("REMOTE_ADDR") ? : ""); 451 | int o; 452 | for (o = 0; optionsTable[o].longName; o++) 453 | if (optionsTable[o].shortName && optionsTable[o].arg) 454 | switch (optionsTable[o].argInfo & POPT_ARG_MASK) 455 | { 456 | case POPT_ARG_NONE: 457 | if (*(int *) optionsTable[o].arg) 458 | printf ("// %s: %c\n", optionsTable[o].descrip, optionsTable[o].shortName); 459 | break; 460 | case POPT_ARG_INT: 461 | { 462 | int v = *(int *) optionsTable[o].arg; 463 | if (v) 464 | printf ("// %s: %c=%d\n", optionsTable[o].descrip, optionsTable[o].shortName, v); 465 | } 466 | break; 467 | case POPT_ARG_DOUBLE: 468 | { 469 | double v = *(double *) optionsTable[o].arg; 470 | if (v) 471 | { 472 | char temp[50], 473 | *p; 474 | sprintf (temp, "%f", v); 475 | for (p = temp + strlen (temp); p > temp && p[-1] == '0'; p--); 476 | if (p > temp && p[-1] == '.') 477 | p--; 478 | *p = 0; 479 | printf ("// %s: %c=%s\n", optionsTable[o].descrip, optionsTable[o].shortName, temp); 480 | } 481 | } 482 | break; 483 | case POPT_ARG_STRING: 484 | { 485 | char *v = *(char * *) optionsTable[o].arg; 486 | if (v && *v) 487 | printf ("// %s: %c=%s\n", optionsTable[o].descrip, optionsTable[o].shortName, v); 488 | } 489 | break; 490 | } 491 | } 492 | if (error) 493 | { // Problem 494 | printf ("// ** %s **\n", error); 495 | return 1; 496 | } 497 | 498 | // Other adjustments 499 | basethickness += logodepth; 500 | 501 | { // Modules 502 | if (textslow) 503 | printf 504 | ("module cuttext(){translate([0,0,-%d])minkowski(){rotate([0,0,22.5])cylinder(h=%lld,d1=%lld,d2=0,$fn=8);linear_extrude(height=%d,convexity=10)mirror([1,0,0])children();}}\n", 505 | SCALE, scaled (textdepth), scaled (textdepth), SCALE); 506 | else 507 | printf ("module cuttext(){linear_extrude(height=%lld,convexity=10,center=true)mirror([1,0,0])children();}\n", 508 | scaled (textdepth)); 509 | // You can use the A&A logo on your maze print providing it is tasteful and not in any way derogatory to A&A or any staff/officers. 510 | if (logo) 511 | printf 512 | ("module aa(w=100,white=0,$fn=100){scale(w/100){if(!white)difference(){circle(d=100.5);circle(d=99.5);}difference(){if(white)circle(d=100);difference(){circle(d=92);for(m=[0,1])mirror([m,0,0]){difference(){translate([24,0,0])circle(r=22.5);translate([24,0,0])circle(r=15);}polygon([[1.5,22],[9,22],[9,-18.5],[1.5,-22]]);}}}}} // A&A Logo is copyright (c) 2013 and trademark Andrews & Arnold Ltd\n"); 513 | } 514 | void cuttext (double s, char *t, char *f, int outset) 515 | { 516 | if (outset) 517 | printf ("mirror([0,0,1])"); 518 | printf ("cuttext()"); 519 | printf ("scale(%lld)", scaled (1)); 520 | printf ("text(\"%s\"", t); 521 | printf (",halign=\"center\""); 522 | printf (",valign=\"center\""); 523 | printf (",size=%lf", s); 524 | if (*t & 0x80) 525 | printf (",font=\"Noto Emoji\""); // Assume emoji - not clean - TODO needs fontconfig stuff really 526 | else if (f) 527 | printf (",font=\"%s\"", f); 528 | printf (");\n"); 529 | } 530 | // The base 531 | printf ("module outer(h,r){e=%lld;minkowski(){cylinder(r1=0,r2=e,h=e,$fn=24);cylinder(h=h-e,r=r,$fn=%d);}}\n", 532 | scaled (outerround), outersides ? : 100); 533 | // Start 534 | double x = 0, 535 | y = 0; 536 | int sq = sqrt (parts) + 0.5, 537 | n = sq * sq - parts; 538 | int box (int part) 539 | { // Make the box - part 1 in inside 540 | int N, 541 | X, 542 | Y, 543 | Z, 544 | S; 545 | double entrya = 0; // Entry angle 546 | int mazeinside = inside; // This part has maze inside 547 | int mazeoutside = !inside; // This part has maze outside 548 | int nextinside = inside; // Next part has maze inside 549 | int nextoutside = !inside; // Next part has maze outside 550 | if (flip) 551 | { 552 | if (part & 1) 553 | { 554 | mazeinside = 1 - mazeinside; 555 | nextoutside = 1 - nextoutside; 556 | } else 557 | { 558 | mazeoutside = 1 - mazeoutside; 559 | nextinside = 1 - nextinside; 560 | } 561 | } 562 | if (part == 1) 563 | mazeinside = 0; 564 | if (part == parts) 565 | mazeoutside = 0; 566 | if (part + 1 >= parts) 567 | nextoutside = 0; 568 | if (part == parts) 569 | nextinside = 0; 570 | // Dimensions 571 | // r0 is inside of part+maze 572 | // r1 is outside of part+maze 573 | // r2 is outside of base before "sides" adjust 574 | // r3 is outside of base with "sides" adjust 575 | double r1 = corediameter / 2 + wallthickness + (part - 1) * (wallthickness + mazethickness + clearance); // Outer 576 | if (coresolid) 577 | r1 -= wallthickness + mazethickness + clearance - (inside ? mazethickness : 0); // Adjust to make part 2 the core diameter 578 | int W = ((int) (r1 * 2 * M_PI / mazestep)) / nubs * nubs; // Default value 579 | double r0 = r1 - wallthickness; // Inner 580 | if (mazeinside && part > 1) 581 | r0 -= mazethickness; // Maze on inside 582 | if (mazeoutside && part < parts) 583 | r1 += mazethickness; // Maze on outside 584 | double r2 = r1; // Base outer 585 | if (part < parts) 586 | r2 += clearance; 587 | if (part + 1 >= parts && textsides && !textoutset) 588 | r2 += textdepth; 589 | if (nextinside) 590 | r2 += mazethickness; 591 | if (nextoutside || part + 1 == parts) 592 | r2 += wallthickness; 593 | if (basewide && part + 1 < parts) 594 | r2 += nextoutside ? mazethickness : wallthickness; 595 | double r3 = r2; 596 | if (outersides && part + 1 >= parts) 597 | r3 /= cos ((double) M_PI / outersides); // Bigger because of number of sides 598 | printf ("// Part %d (%.2fmm to %.2fmm and %.2fmm/%.2fmm base)\n", part, r0, r1, r2, r3); 599 | double height = (coresolid ? coregap + baseheight : 0) + coreheight + basethickness + (basethickness + basegap) * (part - 1); 600 | if (part == 1) 601 | height -= (coresolid ? coreheight : coregap); 602 | if (part > 1) 603 | height -= baseheight; // base from previous unit is added to this 604 | // Output 605 | void makemaze (double r, int inside) 606 | { // Make the maze 607 | W = ((int) ((r + (inside ? mazethickness : -mazethickness)) * 2 * M_PI / mazestep)) / nubs * nubs; // Update W for actual maze 608 | double base = (inside ? basethickness : baseheight); 609 | if (inside && part > 2) 610 | base += baseheight; // Nubs don't go all the way to the end 611 | if (inside && part == 2) 612 | base += (coresolid ? coreheight : coregap); // First one is short... 613 | if (inside) 614 | base += basegap; 615 | double h = height - base - mazemargin - (parkvertical ? mazestep / 4 : 0) - mazestep / 8; 616 | int H = (int) (h / mazestep); 617 | printf ("// Maze %s %d/%d\n", inside ? "inside" : "outside", W, H); 618 | double y0 = base + mazestep / 2 - mazestep * (helix + 1) + mazestep / 8; 619 | H += 2 + helix; // Allow one above, one below and helix below 620 | if (W < 3 || H < 1) 621 | errx (1, "Too small"); 622 | double a = 0, 623 | dy = 0; 624 | if (helix) 625 | { 626 | a = atan (mazestep * helix / r / 2 / M_PI) * 180 / M_PI; 627 | dy = mazestep * helix / W; 628 | } 629 | unsigned char maze[W][H]; 630 | memset (maze, 0, sizeof (unsigned char) * W * H); 631 | int test (int x, int y) 632 | { // Test if in use... 633 | while (x < 0) 634 | { 635 | x += W; 636 | y -= helix; 637 | } 638 | while (x >= W) 639 | { 640 | x -= W; 641 | y += helix; 642 | } 643 | int n = nubs; 644 | unsigned char v = 0; 645 | while (n--) 646 | { 647 | if (y < 0 || y >= H) 648 | v |= FLAGI; 649 | else 650 | v |= maze[x][y]; 651 | if (!n) 652 | break; 653 | x += W / nubs; 654 | while (x >= W) 655 | { 656 | x -= W; 657 | y += helix; 658 | } 659 | if (helix == nubs) 660 | y--; 661 | } 662 | return v; 663 | } 664 | { // Maze 665 | double margin = mazemargin; 666 | // Make maze 667 | // Clear too high/low 668 | for (Y = 0; Y < H; Y++) 669 | for (X = 0; X < W; X++) 670 | if (mazestep * Y + y0 + dy * X < base + mazestep / 2 + mazestep / 8 671 | || mazestep * Y + y0 + dy * X > height - mazestep / 2 - margin - mazestep / 8) 672 | maze[X][Y] |= FLAGI; // To high or low 673 | // Final park point 674 | if (parkvertical) 675 | { 676 | for (N = 0; N < helix + 2; N++) // Down to final 677 | { 678 | maze[0][N] |= FLAGU + FLAGD; 679 | maze[X = 0][Y = N + 1] |= FLAGD; 680 | } 681 | if (!inside && !noa && W / nubs > 2 && H > helix + 4) 682 | { // An "A" at finish 683 | maze[X][Y] |= FLAGD | FLAGU | FLAGR; 684 | maze[X][Y + 1] |= FLAGD | FLAGR; 685 | maze[X + 1][Y] |= FLAGD | FLAGU | FLAGL; 686 | maze[X + 1][Y + 1] |= FLAGD | FLAGL; 687 | maze[X + 1][Y - 1] |= FLAGU; 688 | X++; 689 | Y--; 690 | } 691 | } else // Left to final 692 | { 693 | maze[0][helix + 1] |= FLAGR; 694 | maze[X = 1][Y = helix + 1] |= FLAGL; 695 | if (!inside && !noa && W / nubs > 3 && H > helix + 3) 696 | { // An "A" at finish 697 | maze[X][Y] |= FLAGL | FLAGR | FLAGU; 698 | maze[X + 1][Y] |= FLAGL | FLAGU; 699 | maze[X + 1][Y + 1] |= FLAGL | FLAGD; 700 | maze[X][Y + 1] |= FLAGL | FLAGR | FLAGD; 701 | maze[X - 1][Y + 1] |= FLAGR; 702 | X--; 703 | Y++; 704 | } 705 | } 706 | // Make maze 707 | int maxx = 0; 708 | if (testmaze) 709 | { // Simple test pattern 710 | for (Y = 0; Y < H; Y++) 711 | for (X = 0; X < W; X++) 712 | if (!(test (X, Y) & FLAGI) && !(test (X + 1, Y) & FLAGI)) 713 | { 714 | maze[X][Y] |= FLAGR; 715 | int x = X + 1, 716 | y = Y; 717 | if (x >= W) 718 | { 719 | x -= W; 720 | y += helix; 721 | } 722 | maze[x][y] |= FLAGL; 723 | } 724 | if (!flip || inside) 725 | while (maxx + 1 < W && !(test (maxx + 1, H - 2) & FLAGI)) 726 | maxx++; 727 | } else 728 | { // Actual maze 729 | int max = 0; 730 | typedef struct pos_s pos_t; 731 | struct pos_s 732 | { 733 | pos_t *next; 734 | int x, 735 | y, 736 | n; 737 | }; 738 | pos_t *pos = malloc (sizeof (*pos)), 739 | *last = NULL; 740 | pos->x = X; 741 | pos->y = Y; 742 | pos->n = 0; 743 | pos->next = NULL; 744 | last = pos; 745 | while (pos) 746 | { 747 | pos_t *p = pos; 748 | pos = p->next; 749 | p->next = NULL; 750 | if (!pos) 751 | last = NULL; 752 | // Where we are 753 | X = p->x; 754 | Y = p->y; 755 | int v, 756 | n = 0; 757 | // Which way can we go 758 | // Some bias for direction 759 | if (!test (X + 1, Y)) 760 | n += BIASR; // Right 761 | if (!test (X - 1, Y)) 762 | n += BIASL; // Left 763 | if (!test (X, Y - 1)) 764 | n += BIASD; // Down 765 | if (!test (X, Y + 1)) 766 | n += BIASU; // Up 767 | if (!n) 768 | { // No way forward 769 | free (p); 770 | continue; 771 | } 772 | // Pick one of the ways randomly 773 | if (read (f, &v, sizeof (v)) != sizeof (v)) 774 | err (1, "Read /dev/random"); 775 | v %= n; 776 | // Move forward 777 | if (!test (X + 1, Y) && (v -= BIASR) < 0) 778 | { // Right 779 | maze[X][Y] |= FLAGR; 780 | X++; 781 | if (X >= W) 782 | { 783 | X -= W; 784 | Y += helix; 785 | } 786 | maze[X][Y] |= FLAGL; 787 | } else if (!test (X - 1, Y) && (v -= BIASL) < 0) 788 | { // Left 789 | maze[X][Y] |= FLAGL; 790 | X--; 791 | if (X < 0) 792 | { 793 | X += W; 794 | Y -= helix; 795 | } 796 | maze[X][Y] |= FLAGR; 797 | } else if (!test (X, Y - 1) && (v -= BIASD) < 0) 798 | { // Down 799 | maze[X][Y] |= FLAGD; 800 | Y--; 801 | maze[X][Y] |= FLAGU; 802 | } else if (!test (X, Y + 1) && (v -= BIASU) < 0) 803 | { // Up 804 | maze[X][Y] |= FLAGU; 805 | Y++; 806 | maze[X][Y] |= FLAGD; 807 | } else 808 | errx (1, "WTF"); // We should have picked a way we can go 809 | // Entry 810 | if (p->n > max && (test (X, Y + 1) & FLAGI) // 811 | && (!flip || inside || !(X % (W / nubs)))) 812 | { // Longest path that reaches top 813 | max = p->n; 814 | maxx = X; 815 | } 816 | // Next point to consider 817 | pos_t *next = malloc (sizeof (*next)); 818 | next->x = X; 819 | next->y = Y; 820 | next->n = p->n + 1; 821 | next->next = NULL; 822 | // How to add points to queue... start or end 823 | if (read (f, &v, sizeof (v)) != sizeof (v)) 824 | err (1, "Read /dev/random"); 825 | v %= 10; 826 | if (v < (mazecomplexity < 0 ? -mazecomplexity : mazecomplexity)) 827 | { // add next point at start - makes for longer path 828 | if (!pos) 829 | last = next; 830 | next->next = pos; 831 | pos = next; 832 | } else 833 | { // add next point at end - makes for multiple paths, which can mean very simple solution 834 | if (last) 835 | last->next = next; 836 | else 837 | pos = next; 838 | last = next; 839 | } 840 | if (mazecomplexity <= 0 && v < -mazecomplexity) 841 | { // current point to start 842 | if (!pos) 843 | last = p; 844 | p->next = pos; 845 | pos = p; 846 | } else 847 | { 848 | if (last) 849 | last->next = p; 850 | else 851 | pos = p; 852 | last = p; 853 | } 854 | } 855 | printf ("// Path length %d\n", max); 856 | } 857 | entrya = (double) 360 *maxx / W; 858 | // Entry point for maze 859 | for (X = maxx % (W / nubs); X < W; X += W / nubs) 860 | { 861 | Y = H - 1; 862 | while (Y && (maze[X][Y] & FLAGI)) 863 | maze[X][Y--] |= FLAGU + FLAGD; 864 | maze[X][Y] += FLAGU; 865 | } 866 | 867 | int MAXY = height / (mazestep / 4) + 10; 868 | struct 869 | { // Data for each slive 870 | // Pre calculated x/y for left side 0=back, 1=recess, 2=front - used to create points 871 | double x[3], 872 | y[3]; 873 | // The last points as we work up slice (-ve for recess, 0 for not set yet) 874 | int l, 875 | r; 876 | // Points from bottom up on this slice in order - used to ensure manifold buy using points that would be skipped 877 | int n; // Points added to p 878 | int p[MAXY]; 879 | } s[W * 4]; 880 | memset (&s, 0, sizeof (*s) * W * 4); 881 | int p[W][H]; // The point start for each usable maze location (0 for not set) - 16 points 882 | memset (*p, 0, sizeof (int) * W * H); 883 | // Work out pre-sets 884 | for (S = 0; S < W * 4; S++) 885 | { 886 | double a = M_PI * 2 * (S - 1.5) / W / 4; 887 | if (!inside) 888 | a = M_PI * 2 - a; 889 | double sa = sin (a), 890 | ca = cos (a); 891 | if (inside) 892 | { 893 | s[S].x[0] = (r + mazethickness + (part < parts ? wallthickness : clearance + 0.01)) * sa; 894 | s[S].y[0] = (r + mazethickness + (part < parts ? wallthickness : clearance + 0.01)) * ca; 895 | s[S].x[1] = (r + mazethickness) * sa; 896 | s[S].y[1] = (r + mazethickness) * ca; 897 | s[S].x[2] = r * sa; 898 | s[S].y[2] = r * ca; 899 | } else 900 | { 901 | s[S].x[0] = (r - mazethickness - wallthickness) * sa; 902 | s[S].y[0] = (r - mazethickness - wallthickness) * ca; 903 | s[S].x[1] = (r - mazethickness) * sa; 904 | s[S].y[1] = (r - mazethickness) * ca; 905 | s[S].x[2] = r * sa; 906 | s[S].y[2] = r * ca; 907 | } 908 | } 909 | if (inside && mirrorinside) 910 | printf ("mirror([1,0,0])"); 911 | printf ("polyhedron("); 912 | // Make points 913 | printf ("points=["); 914 | int P = 0; 915 | void addpoint (int S, double x, double y, double z) 916 | { 917 | printf ("[%lld,%lld,%lld],", scaled (x), scaled (y), scaled (z)); 918 | if (s[S].n >= MAXY) 919 | errx (1, "WTF points %d", S); 920 | s[S].p[s[S].n++] = P++; 921 | } 922 | void addpointr (int S, double x, double y, double z) 923 | { 924 | printf ("[%lld,%lld,%lld],", scaled (x), scaled (y), scaled (z)); 925 | if (s[S].n >= MAXY) 926 | errx (1, "WTF points %d", S); 927 | s[S].p[s[S].n++] = -(P++); 928 | } 929 | int bottom = P; 930 | // Base points 931 | for (S = 0; S < W * 4; S++) 932 | addpoint (S, s[S].x[0], s[S].y[0], basethickness - clearance); 933 | for (S = 0; S < W * 4; S++) 934 | addpointr (S, s[S].x[1], s[S].y[1], basethickness - clearance); 935 | for (S = 0; S < W * 4; S++) 936 | addpoint (S, s[S].x[2], s[S].y[2], basethickness - clearance); 937 | { // Points for each maze location 938 | double dy = mazestep * helix / W / 4; // Step per S 939 | double my = mazestep / 8; // Vertical steps 940 | double y = y0 - dy * 1.5; // Y vertical centre for S=0 941 | for (Y = 0; Y < H; Y++) 942 | for (X = 0; X < W; X++) 943 | { 944 | unsigned char v = test (X, Y); 945 | if (!(v & FLAGA) || (v & FLAGI)) 946 | continue; 947 | p[X][Y] = P; 948 | for (S = X * 4; S < X * 4 + 4; S++) 949 | addpoint (S, s[S].x[2], s[S].y[2], y + Y * mazestep + dy * S - my * 3); 950 | for (S = X * 4; S < X * 4 + 4; S++) 951 | addpointr (S, s[S].x[1], s[S].y[1], y + Y * mazestep + dy * S - my - nubskew); 952 | for (S = X * 4; S < X * 4 + 4; S++) 953 | addpointr (S, s[S].x[1], s[S].y[1], y + Y * mazestep + dy * S + my - nubskew); 954 | for (S = X * 4; S < X * 4 + 4; S++) 955 | addpoint (S, s[S].x[2], s[S].y[2], y + Y * mazestep + dy * S + my * 3); 956 | } 957 | } 958 | int top = P; 959 | for (S = 0; S < W * 4; S++) 960 | addpoint (S, s[S].x[2], s[S].y[2], height - (basewide && !inside && part > 1 ? 0 : margin)); // lower 961 | for (S = 0; S < W * 4; S++) 962 | addpoint (S, s[S].x[1], s[S].y[1], height); 963 | for (S = 0; S < W * 4; S++) 964 | addpoint (S, s[S].x[0], s[S].y[0], height); 965 | for (S = 0; S < W * 4; S++) 966 | { // Wrap back to start 967 | if (s[S].n >= MAXY) 968 | errx (1, "WTF points"); 969 | s[S].p[s[S].n++] = S; 970 | } 971 | printf ("]"); 972 | // Make faces 973 | void slice (int S, int l, int r) 974 | { // Advance slice S to new L and R (-ve for recess) 975 | inline int abs (int x) 976 | { 977 | if (x < 0) 978 | return -x; 979 | return x; 980 | } 981 | inline int sgn (int x) 982 | { 983 | if (x < 0) 984 | return -1; 985 | if (x > 0) 986 | return 1; 987 | return 0; 988 | } 989 | if (S >= W * 4) 990 | errx (1, "Bad render %d", S); 991 | char start = 0; 992 | if (!s[S].l) 993 | { // New - draw to bottom 994 | s[S].l = (l < 0 ? -1 : 1) * (bottom + S + W * 4 + (l < 0 ? 0 : W * 4)); 995 | s[S].r = (r < 0 ? -1 : 1) * (bottom + (S + 1) % (W * 4) + W * 4 + (r < 0 ? 0 : W * 4)); 996 | printf ("[%d,%d,%d,%d],", abs (s[S].l), abs (s[S].r), (S + 1) % (W * 4), S); 997 | } 998 | // Advance 999 | if (l == s[S].l && r == s[S].r) 1000 | return; 1001 | int SR = (S + 1) % (W * 4); 1002 | printf ("["); 1003 | int p = 0; 1004 | int n1, 1005 | n2; 1006 | for (n1 = 0; n1 < s[S].n && abs (s[S].p[n1]) != abs (s[S].l); n1++); 1007 | for (n2 = n1; n2 < s[S].n && abs (s[S].p[n2]) != abs (l); n2++); 1008 | if (n1 == s[S].n || n2 == s[S].n) 1009 | errx (1, "Bad render %d->%d", s[S].l, l); 1010 | while (n1 < n2) 1011 | { 1012 | if (sgn (s[S].p[n1]) == sgn (s[S].l)) 1013 | { 1014 | printf ("%d,", abs (s[S].p[n1])); 1015 | p++; 1016 | } 1017 | n1++; 1018 | } 1019 | printf ("%d,", abs (l)); 1020 | if (p) 1021 | printf ("%d],", abs (r)); // Triangles 1022 | for (n1 = 0; n1 < s[SR].n && abs (s[SR].p[n1]) != abs (s[S].r); n1++); 1023 | for (n2 = n1; n2 < s[SR].n && abs (s[SR].p[n2]) != abs (r); n2++); 1024 | if (n1 == s[SR].n || n2 == s[SR].n) 1025 | errx (1, "Bad render %d->%d", r, s[S].r); 1026 | if (!p || n1 < n2) 1027 | { 1028 | n2--; 1029 | if (p) 1030 | printf ("["); 1031 | printf ("%d", abs (r)); 1032 | while (n1 <= n2) 1033 | { 1034 | if (sgn (s[SR].p[n2]) == sgn (s[S].r)) 1035 | printf (",%d", abs (s[SR].p[n2])); 1036 | n2--; 1037 | } 1038 | if (p) 1039 | printf (",%d", abs (s[S].l)); 1040 | printf ("],"); 1041 | } 1042 | s[S].l = l; 1043 | s[S].r = r; 1044 | } 1045 | printf (",\nfaces=["); 1046 | // Maze 1047 | for (Y = 0; Y < H; Y++) 1048 | for (X = 0; X < W; X++) 1049 | { 1050 | unsigned char v = test (X, Y); 1051 | if (!(v & FLAGA) || (v & FLAGI)) 1052 | continue; 1053 | S = X * 4; 1054 | int P = p[X][Y]; 1055 | // Left 1056 | if (!(v & FLAGD)) 1057 | slice (S + 0, P + 0, P + 1); 1058 | slice (S + 0, P + 0, -(P + 5)); 1059 | if (v & FLAGL) 1060 | { 1061 | slice (S + 0, -(P + 4), -(P + 5)); 1062 | slice (S + 0, -(P + 8), -(P + 9)); 1063 | } 1064 | slice (S + 0, P + 12, -(P + 9)); 1065 | if (!(v & FLAGU)) 1066 | slice (S + 0, P + 12, P + 13); 1067 | // Middle 1068 | if (!(v & FLAGD)) 1069 | slice (S + 1, P + 1, P + 2); 1070 | slice (S + 1, -(P + 5), -(P + 6)); 1071 | slice (S + 1, -(P + 9), -(P + 10)); 1072 | if (!(v & FLAGU)) 1073 | slice (S + 1, P + 13, P + 14); 1074 | // Right 1075 | if (!(v & FLAGD)) 1076 | slice (S + 2, P + 2, P + 3); 1077 | slice (S + 2, -(P + 6), P + 3); 1078 | if (v & FLAGR) 1079 | { 1080 | slice (S + 2, -(P + 6), -(P + 7)); 1081 | slice (S + 2, -(P + 10), -(P + 11)); 1082 | } 1083 | slice (S + 2, -(P + 10), P + 15); 1084 | if (!(v & FLAGU)) 1085 | slice (S + 2, P + 14, P + 15); 1086 | { // Joining to right 1087 | int x = X + 1, 1088 | y = Y; 1089 | if (x >= W) 1090 | { 1091 | x -= W; 1092 | y += helix; 1093 | } 1094 | if (y >= 0 && y < H) 1095 | { 1096 | int PR = p[x][y]; 1097 | if (PR) 1098 | { 1099 | slice (S + 3, P + 3, PR + 0); 1100 | if (v & FLAGR) 1101 | { 1102 | slice (S + 3, -(P + 7), -(PR + 4)); 1103 | slice (S + 3, -(P + 11), -(PR + 8)); 1104 | } 1105 | slice (S + 3, P + 15, PR + 12); 1106 | } 1107 | } 1108 | } 1109 | } 1110 | // Top 1111 | for (S = 0; S < W * 4; S++) 1112 | { 1113 | //slice (S, (s[S].l < 0 ? -1 : 1) * (top + S + (s[S].l < 0 ? W * 4 : 0)), (s[S].r < 0 ? -1 : 1) * (top + ((S + 1) % (W * 4)) + (s[S].r < 0 ? W * 4 : 0))); 1114 | slice (S, top + S + (s[S].l < 0 ? W * 4 : 0), top + ((S + 1) % (W * 4)) + (s[S].r < 0 ? W * 4 : 0)); 1115 | slice (S, top + S + W * 4, top + ((S + 1) % (W * 4)) + W * 4); 1116 | slice (S, top + S + 2 * W * 4, top + ((S + 1) % (W * 4)) + 2 * W * 4); 1117 | slice (S, bottom + S, bottom + (S + 1) % (W * 4)); 1118 | } 1119 | printf ("]"); 1120 | printf (",convexity=10"); 1121 | // Done 1122 | printf (");\n"); 1123 | if (parkthickness) 1124 | { // Park ridge 1125 | if (inside && mirrorinside) 1126 | printf ("mirror([1,0,0])"); 1127 | printf ("polyhedron(points=["); 1128 | for (N = 0; N < W; N += W / nubs) 1129 | for (Y = 0; Y < 4; Y++) 1130 | for (X = 0; X < 4; X++) 1131 | { 1132 | int S = N * 4 + X + (parkvertical ? 0 : 2); 1133 | double z = 1134 | y0 - dy * 1.5 / 4 + (helix + 1) * mazestep + Y * mazestep / 4 + dy * X / 4 + 1135 | (parkvertical ? mazestep / 8 : dy / 2 - mazestep * 3 / 8); 1136 | double x = s[S].x[1]; 1137 | double y = s[S].y[1]; 1138 | if (parkvertical ? Y == 1 || Y == 2 : X == 1 || X == 2) 1139 | { // ridge height instead or surface 1140 | x = (s[S].x[1] * (mazethickness - parkthickness) + s[S].x[2] * parkthickness) / mazethickness; 1141 | y = (s[S].y[1] * (mazethickness - parkthickness) + s[S].y[2] * parkthickness) / mazethickness; 1142 | } else if (parkvertical) 1143 | z -= nubskew; 1144 | printf ("[%lld,%lld,%lld],", scaled (s[S].x[0]), scaled (s[S].y[0]), scaled (z)); 1145 | printf ("[%lld,%lld,%lld],", scaled (x), scaled (y), scaled (z)); 1146 | } 1147 | printf ("],faces=["); 1148 | for (N = 0; N < nubs; N++) 1149 | { 1150 | int P = N * 32; 1151 | inline void add (int a, int b, int c, int d) 1152 | { 1153 | printf ("[%d,%d,%d],[%d,%d,%d],", P + a, P + b, P + c, P + a, P + c, P + d); 1154 | } 1155 | for (X = 0; X < 6; X += 2) 1156 | { 1157 | add (X + 0, X + 1, X + 3, X + 2); 1158 | for (Y = 0; Y < 24; Y += 8) 1159 | { 1160 | add (X + 0 + Y, X + 2 + Y, X + 10 + Y, X + 8 + Y); 1161 | add (X + 1 + Y, X + 9 + Y, X + 11 + Y, X + 3 + Y); 1162 | } 1163 | add (X + 25, X + 24, X + 26, X + 27); 1164 | } 1165 | for (Y = 0; Y < 24; Y += 8) 1166 | { 1167 | add (Y + 0, Y + 8, Y + 9, Y + 1); 1168 | add (Y + 6, Y + 7, Y + 15, Y + 14); 1169 | } 1170 | } 1171 | printf ("],convexity=10);\n"); 1172 | } 1173 | } 1174 | } 1175 | printf ("translate([%lld,%lld,0])\n", scaled (x + (outersides & 1 ? r3 : r2)), scaled (y + (outersides & 1 ? r3 : r2))); 1176 | if (outersides) 1177 | printf ("rotate([0,0,%f])", (double) 180 / outersides + (part + 1 == parts ? 180 : 0)); 1178 | printf ("{\n"); 1179 | void mark (void) 1180 | { // Marking position 0 1181 | if (!markpos0 || part + 1 < parts) 1182 | return; 1183 | double a = 0, 1184 | r = r0 + wallthickness / 2, 1185 | t = wallthickness * 2; 1186 | if (mazeinside) 1187 | r = r0 + mazethickness + wallthickness / 2; 1188 | else if (mazeoutside) 1189 | r = r1 - mazethickness - wallthickness / 2; 1190 | if (!mazeoutside) 1191 | { // Try not to cut outside of box 1192 | r -= wallthickness / 2; 1193 | t = wallthickness * 3 / 2; 1194 | } 1195 | if (part == parts && mazeinside) 1196 | a = (mirrorinside ? 1 : -1) * entrya; 1197 | if (part + 1 == parts && mazeoutside) 1198 | a = entrya; 1199 | printf ("rotate([0,0,%f])translate([0,%lld,%lld])cylinder(d=%lld,h=%lld,center=true,$fn=4);\n", a, scaled (r), 1200 | scaled (height), scaled (t), scaled (mazestep / 2)); 1201 | } 1202 | // Maze 1203 | printf ("difference(){union(){"); 1204 | if (mazeinside) 1205 | makemaze (r0, 1); 1206 | if (mazeoutside) 1207 | makemaze (r1, 0); 1208 | if (!mazeinside && !mazeoutside && part < parts) 1209 | { 1210 | printf ("difference(){\n"); 1211 | printf ("translate([0,0,%lld])cylinder(r=%lld,h=%lld,$fn=%d);translate([0,0,%lld])cylinder(r=%lld,h=%lld,$fn=%d);\n", scaled (basethickness / 2 - clearance), scaled (r1), scaled (height - basethickness / 2 + clearance), W * 4, scaled (basethickness), scaled (r0), scaled (height), W * 4); // Non maze 1212 | printf ("}\n"); 1213 | } 1214 | // Base 1215 | printf ("difference(){\n"); 1216 | if (part == parts) 1217 | printf ("outer(%lld,%lld);\n", scaled (height), scaled ((r2 - outerround) / cos ((double) M_PI / (outersides ? : 100)))); 1218 | else if (part + 1 >= parts) 1219 | printf ("mirror([1,0,0])outer(%lld,%lld);\n", scaled (baseheight), 1220 | scaled ((r2 - outerround) / cos ((double) M_PI / (outersides ? : 100)))); 1221 | else 1222 | printf ("hull(){cylinder(r=%lld,h=%lld,$fn=%d);translate([0,0,%lld])cylinder(r=%lld,h=%lld,$fn=%d);}\n", 1223 | scaled (r2 - mazethickness), scaled (baseheight), W * 4, scaled (mazemargin), scaled (r2), 1224 | scaled (baseheight - mazemargin), W * 4); 1225 | printf ("translate([0,0,%lld])cylinder(r=%lld,h=%lld,$fn=%d);\n", scaled (basethickness), scaled (r0 + (part > 1 && mazeinside ? mazethickness + clearance : 0) + (!mazeinside && part < parts ? clearance : 0)), scaled (height), W * 4); // Hole 1226 | printf ("}\n"); 1227 | printf ("}\n"); 1228 | // Cut outs 1229 | if (gripdepth && part + 1 < parts) 1230 | printf 1231 | ("rotate([0,0,%f])translate([0,0,%lld])rotate_extrude(convexity=10,$fn=%d)translate([%lld,0,0])circle(r=%lld,$fn=9);\n", 1232 | (double) 360 / W / 4 / 2, scaled (mazemargin + (baseheight - mazemargin) / 2), W * 4, scaled (r2 + gripdepth), 1233 | scaled (gripdepth * 2)); 1234 | else if (gripdepth && part + 1 == parts) 1235 | printf ("translate([0,0,%lld])rotate_extrude(convexity=10,$fn=%d)translate([%lld,0,0])circle(r=%lld,$fn=9);\n", 1236 | scaled (outerround + (baseheight - outerround) / 2), outersides ? : 100, scaled (r3 + gripdepth), 1237 | scaled (gripdepth * 2)); 1238 | if (basewide && nextoutside && part + 1 < parts) // Connect endpoints over base 1239 | { 1240 | int W = ((int) ((r2 - mazethickness) * 2 * M_PI / mazestep)) / nubs * nubs; 1241 | double wi = 2 * (r2 - mazethickness) * 2 * M_PI / W / 4; 1242 | double wo = 2 * r2 * 2 * M_PI * 3 / W / 4; 1243 | printf 1244 | ("for(a=[0:%f:359])rotate([0,0,a])translate([0,%lld,0])hull(){cube([%lld,%lld,%lld],center=true);cube([%lld,0.01,%lld],center=true);}\n", 1245 | (double) 360 / nubs, scaled (r2), scaled (wi), scaled (mazethickness * 2), scaled (baseheight * 2 + clearance), 1246 | scaled (wo), scaled (baseheight * 2 + clearance)); 1247 | } 1248 | if (textend) 1249 | { 1250 | int n = 0; 1251 | char *p = strdupa (textend); 1252 | while (p) 1253 | { 1254 | char *q = strchr (p, '\\'); 1255 | if (q) 1256 | *q++ = 0; 1257 | if (*p && n == (parts - part)) 1258 | { 1259 | printf ("rotate([0,0,%f])", (part == parts ? 1 : -1) * (90 + (double) 180 / (outersides ? : 100))); 1260 | cuttext (r2 - outerround, p, textfontend, 0); 1261 | } 1262 | p = q; 1263 | n++; 1264 | } 1265 | } 1266 | void textside (int outset) 1267 | { 1268 | double a = 90 + (double) 180 / outersides; 1269 | double h = r3 * sin (M_PI / outersides) * textsidescale; 1270 | char *p = strdupa (textsides); 1271 | while (p) 1272 | { 1273 | char *q = strchr (p, '\\'); 1274 | if (q) 1275 | *q++ = 0; 1276 | if (*p) 1277 | { 1278 | printf ("rotate([0,0,%f])translate([0,-%lld,%lld])rotate([-90,-90,0])", a, scaled (r2), 1279 | scaled (outerround + (height - outerround) / 2)); 1280 | cuttext (h, p, textfont, outset); 1281 | } 1282 | a -= 360 / outersides; 1283 | p = q; 1284 | } 1285 | } 1286 | if (textsides && part == parts && outersides && !textoutset) 1287 | textside (0); 1288 | if (logo && part == parts) 1289 | printf ("translate([0,0,%lld])linear_extrude(height=%lld,convexity=10)aa(%lld,white=true);\n", 1290 | scaled (basethickness - logodepth), scaled (logodepth * 2), scaled (r0 * 1.8)); 1291 | else if (textinside) 1292 | printf 1293 | ("translate([0,0,%lld])linear_extrude(height=%lld,convexity=10)text(\"%s\",font=\"%s\",size=%lld,halign=\"center\",valign=\"center\");\n", 1294 | scaled (basethickness - logodepth), scaled (logodepth * 2), textinside, textfontend, scaled (r0)); 1295 | if (markpos0 && part + 1 >= parts) 1296 | mark (); 1297 | printf ("}\n"); 1298 | if (textsides && part == parts && outersides && textoutset) 1299 | textside (1); 1300 | if (coresolid && part == 1) 1301 | printf ("translate([0,0,%lld])cylinder(r=%lld,h=%lld,$fn=%d);\n", scaled (basethickness), scaled (r0 + clearance + (!mazeinside && part < parts ? clearance : 0)), scaled (height - basethickness), W * 4); // Solid core 1302 | if ((mazeoutside && !flip && part == parts) || (!mazeoutside && part + 1 == parts)) 1303 | entrya = 0; // Align for lid alignment 1304 | else if (part < parts && !basewide) 1305 | { // We can position randomly 1306 | int v; 1307 | if (read (f, &v, sizeof (v)) != sizeof (v)) 1308 | err (1, "Read /dev/random"); 1309 | entrya = v % 360; 1310 | } 1311 | // Nubs 1312 | void addnub (double r, int inside) 1313 | { 1314 | double ri = r + (inside ? -mazethickness : mazethickness); 1315 | int W = ((int) ((ri + (inside ? -clearance : clearance)) * 2 * M_PI / mazestep)) / nubs * nubs; 1316 | double da = (double) 2 * M_PI / W / 4; // x angle per 1/4 maze step 1317 | double dz = mazestep / 4 - nubzclearance; 1318 | double my = mazestep * da * 4 * helix / (r * 2 * M_PI); 1319 | if (inside) 1320 | da = -da; 1321 | else if (mirrorinside) 1322 | my = -my; // This is nub outside which is for inside maze 1323 | double a = -da * 1.5; // Centre A 1324 | double z = height - mazestep / 2 - (parkvertical ? 0 : mazestep / 8) - dz * 1.5 - my * 1.5; // Centre Z 1325 | printf ("rotate([0,0,%f])for(a=[0:%f:359])rotate([0,0,a])polyhedron(points=[", entrya, (double) 360 / nubs); 1326 | r += (inside ? nubrclearance : -nubrclearance); // Extra gap 1327 | ri += (inside ? nubrclearance : -nubrclearance); // Extra gap 1328 | for (Z = 0; Z < 4; Z++) 1329 | for (X = 0; X < 4; X++) 1330 | printf ("[%lld,%lld,%lld],", scaled (((X == 1 || X == 2) && (Z == 1 || Z == 2) ? ri : r) * sin (a + da * X)), 1331 | scaled (((X == 1 || X == 2) 1332 | && (Z == 1 || Z == 2) ? ri : r) * cos (a + da * X)), scaled (z + Z * dz + X * my + (Z == 1 1333 | || Z == 1334 | 2 ? nubskew : 1335 | 0))); 1336 | r += (inside ? clearance - nubrclearance : -clearance + nubrclearance); // Back in to wall 1337 | for (Z = 0; Z < 4; Z++) 1338 | for (X = 0; X < 4; X++) 1339 | printf ("[%lld,%lld,%lld],", scaled (r * sin (a + da * X)), scaled (r * cos (a + da * X)), 1340 | scaled (z + Z * dz + X * my + (Z == 1 || Z == 2 ? nubskew : 0))); 1341 | printf ("],faces=["); 1342 | for (Z = 0; Z < 3; Z++) 1343 | for (X = 0; X < 3; X++) 1344 | printf ("[%d,%d,%d],[%d,%d,%d],", Z * 4 + X + 20, Z * 4 + X + 21, Z * 4 + X + 17, Z * 4 + X + 20, Z * 4 + X + 17, 1345 | Z * 4 + X + 16); 1346 | for (Z = 0; Z < 3; Z++) 1347 | printf ("[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],", Z * 4 + 4, Z * 4 + 20, Z * 4 + 16, Z * 4 + 4, Z * 4 + 16, 1348 | Z * 4 + 0, Z * 4 + 23, Z * 4 + 7, Z * 4 + 3, Z * 4 + 23, Z * 4 + 3, Z * 4 + 19); 1349 | for (X = 0; X < 3; X++) 1350 | printf ("[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],[%d,%d,%d],", X + 28, X + 12, X + 13, X + 28, X + 13, X + 29, X + 0, X + 16, 1351 | X + 17, X + 0, X + 17, X + 1); 1352 | printf ("[0,1,5],[0,5,4],[4,5,9],[4,9,8],[8,9,12],[9,13,12],"); 1353 | printf ("[1,2,6],[1,6,5],[5,6,10],[5,10,9],[9,10,14],[9,14,13],"); 1354 | printf ("[2,3,6],[3,7,6],[6,7,11],[6,11,10],[10,11,15],[10,15,14],"); 1355 | printf ("]);\n"); 1356 | } 1357 | if (!mazeinside && part > 1) 1358 | addnub (r0, 1); 1359 | if (!mazeoutside && part < parts) 1360 | addnub (r1, 0); 1361 | printf ("}\n"); 1362 | x += (outersides & 1 ? r3 : r2) + r2 + 5; 1363 | if (++n >= sq) 1364 | { 1365 | n = 0; 1366 | x = 0; 1367 | y += (outersides & 1 ? r3 : r2) * 2 + 5; 1368 | } 1369 | } 1370 | 1371 | printf ("scale(" SCALEI "){\n"); 1372 | if (part) 1373 | box (part); 1374 | else 1375 | for (part = 1; part <= parts; part++) 1376 | box (part); 1377 | printf ("}\n"); 1378 | close (f); 1379 | return 0; 1380 | } 1381 | -------------------------------------------------------------------------------- /webform.cgi: -------------------------------------------------------------------------------- 1 | #!/bin/csh -f 2 | cat << END 3 | Content-Type: text/html 4 | 5 | 6 | 7 | 22 | 23 | 24 |

Puzzle Box

25 |

This makes a customised puzzle box with random maze. Use OpenSCAD to render to an STL for 3D printing.

26 |

See https://www.thingiverse.com/thing:2410748 for examples.

27 |

Do click like on thingiverse. Gifts accepted (just because people have asked how they can do this).

28 |
29 | 30 | 31 | END 32 | ./puzzlebox.cgi --web-form 33 | cat << END 34 |
35 | 36 |
37 |

Some parameters are subject to sanity checks, e.g. nubs set to helix if greater than 1, etc. See comments at top of output for parameters actually used.

38 |

Source code at https://github.com/revk/PuzzleBox

39 |
40 |
By RevK® www.me.uk @TheRealRevK
41 | 42 | 43 | END 44 | --------------------------------------------------------------------------------