Cleaning Up CC64

2021-05-03

IanSkinner1982

<- Back


This article is to explain how I made Cookie Clicker C64 item buying more efficient. This probably isn’t the most interesting article, but you may enjoy it nonetheless.
Here is the layout of a page in the store:

1. Print a page in the store
2. Buy certain items depending on the pressed key (goes to sections 4-7)
3. Switch page if + or - is pressed
-------------------------
4. item 1 buying code
5. item 2 buying code
6. item 3 buying code
7. item 4 buying code

Now, this is for 1 page in a store. The store has over 5 pages… with 4 item buying routines each. Too much code, right? Well that's why I came up with an idea to make it better. First, let me explain how the store looks. You have 3 lines for each item. The setup is as follows:

Item Name           Amount Owned           Ex.  Cursors                69
Description of item                             Adds .1 cookies per second
Price of item                                   1000c

I decided that I would put all the building info in a sequential and save it to a few arrays. (I'd have a name array, a price array, a description array and an amount array) It might not make sense why I did this, but you'll see why soon. Instead of printing a whole page, I'd print the values in the array. So...

1. Select a range depending on the page selected. (0-3, 4-7, 8-12, etc)
2. Print the values in the range selected for each array
I will now add a for...next loop to handle all the menu printing. It will go through the range selected and print the desired values.


I have replaced all the page printing code with just 2 parts. (each page display would take 20 or so lines, meaning if I had 5 pages that would be 100 lines. With this new method, I need around 8-10 lines to load the array values, and 9 lines to print any selected page. With 5 pages, that is only 17-19 lines!) I can also add a whole new page just by adding one line of code.
Figuring out how to select an item with this new design was tough. I decided to assign a value to each item of the page. (x) If the input was within 1-4, then you will be sent to the buying routine.

1. Reset x and read keyboard input
2. Exit on a change of the current page or menu
3. Enter buying routine if an item was selected. (if x=1-4)


This ended up taking 18 lines instead of 50 lines for all the pages. Not a big improvement, but it is still good to clean stuff up.
As mentioned earlier, there are four blocks of code for buying items per page. Each bit of code was 6 lines. 6*4*5 is 120 lines. (still assuming I need 5 pages) Yeesh. I have to chop that down. Before, it was like this for each item:

1. Compare price and cookies
2. Subtract price
3. Add gain to gain.
4. Update amount owned.
5. Add 15% to price.
6. Update screen with new price and amount owned.


Now, once you have selected a item, it will do this:

1. Load the item number. (y=i+x-1) (i is the low number in the range)
2. Compare price(y) with your total cookies. (see if you can buy it)
3. Subtract price(y) from your cookies. Add the gain to gain(y). Multiply price(y) by 1.15. Add one to the amount owned
4. Update the price and amount owned on the screen. (this takes up a few more lines to determine where the item you want updated is)

(What is the gain? With Cookie Clicker, an idle game, the game is always adding something to your total amount of cookies. This is your CpS, or in this case, Gain. I made it so each item has its own gain. (in this case, gain(y)) All gains are added, and that is the total gain. The total gain is added to the amount of cookies you have.)


Now we have one routine shared by all items. So instead of 120 lines, we end up with 12 lines to handle all the items. If I were to add a thousand more items, it would still only be 12 lines.

In all, this took a week to figure out, and a few days to debug. In the end I got it to work, but I had to change a few things. All the ideas here still apply though.
Thanks for reading this extremely boring article!