Advertisement
Guest User

PrizmCity\main.c

a guest
Sep 16th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 26.12 KB | None | 0 0
  1. #include "include/display.h"
  2. #include "include/keyboard.hpp"
  3. #include "include/string.h"
  4. #include "include/keyboard_syscalls.h"
  5. #include "include/color.h"
  6. #include "include/system.h"
  7. #include "include/stdlib.h"
  8. #include "include/stdio.h"
  9. #include "include/rtc.h"
  10.  
  11. #include "data1.c"
  12. #include "types.c"
  13.  
  14. #define CLEAN_EXIT 0
  15. #define ERROR_EXIT -1
  16. #define MODE_TITLE 1
  17. #define MODE_ABOUT 2
  18. #define MODE_NEW_GAME 3
  19. #define MODE_LOAD_GAME 4
  20. #define MODE_PRE_RUNNING_GAME 5
  21. #define MODE_RUNNING_GAME 6
  22.  
  23. #define D_VRAM (char*)0xA8000000
  24.  
  25.  
  26. int renderloop(int*mode);
  27.     int titlescreenrenderloop(int*cursor_pos, int*outside_mode);
  28.     int aboutscreenrenderloop(int*outside_mode);
  29.     int newgamerenderloop(int*outside_mode, int*SAVE_HANDLE);
  30.     int loadgamerenderloop(int*outside_mode, int*SAVE_HANDLE);
  31.     city_cell*prerunninggamerenderloop(int*outside_mode, city_cell*map);
  32.     int runninggamerenderloop(int*outside_mode, city_cell*map);
  33.    
  34. city_cell*generatemap(int density, int altitude, int climate, int gradualism, int variance);
  35. void drawtilemap(city_cell*map, int xpos, int ypos);
  36. city_cell*access_map_element(city_cell*map, int x, int y);
  37.  
  38. void CopySprite(const char* data, int x, int y, int width, int height);
  39. void CopySpriteMasked(const char* data, int x, int y, int width, int height, int maskcolor);
  40. int GetKeyNB(void);
  41. void GetKeyWaitNone(void);
  42. void GetKeyHold(int key);
  43. void*FillData(void*pointer, unsigned char element, int amount);
  44. unsigned short random(int extra_seed);
  45. void drawprintnum(int x, int y, int num);
  46. void drawprint(int x, int y, char*string);
  47. void rectangle(int x, int y, int width, int height, unsigned short color);
  48. void draw_info_box(int x, int y, int width, int height);
  49. void CopySpriteMasked2(const char* data, int x, int y, int width, int height, int maskcolor);
  50.  
  51.  
  52. static city_data cur_data = {0, 0, 0x00000000};
  53. static int SAVE_HANDLE = -1;
  54.  
  55.  
  56. int main() {
  57.     int escape_errors = CLEAN_EXIT;
  58.     int cur_screen_mode = 1;
  59.    
  60.     Bdisp_EnableColor(1);  
  61.     Bdisp_AllCr_VRAM();
  62.     CopySprite(OMNIMAGA_LOGO, 40, 12, 306, 46);
  63.     CopySprite(CEMETECH_LOGO, 35, 78, 313, 40);
  64.     CopySprite(DALANIAN_SOFTWARE, 112, 138, 159, 66);
  65.    
  66.     Bdisp_PutDisp_DD();
  67.     GetKeyHold(KEY_PRGM_F1);
  68.    
  69.     while(!escape_errors) {
  70.         escape_errors = renderloop(&cur_screen_mode);
  71.     }
  72.     return CLEAN_EXIT;
  73. }
  74.  
  75.  
  76.  
  77. int renderloop(int*mode) {
  78.     int error_log = CLEAN_EXIT;
  79.     int cursorx = 0;
  80.     static int SAVE_HANDLE = -1;
  81.     while(!error_log) {
  82.         switch(*(mode)) {
  83.             case MODE_TITLE:
  84.                 error_log = titlescreenrenderloop(&cursorx, mode);
  85.                 break;
  86.             case MODE_ABOUT:
  87.                 error_log = aboutscreenrenderloop(mode);
  88.                 break;
  89.             case MODE_NEW_GAME:
  90.                 if(cur_data.map) {
  91.                     free(cur_data.map);
  92.                 }
  93.                 error_log = newgamerenderloop(mode, &SAVE_HANDLE);
  94.                 break;
  95.             case MODE_LOAD_GAME:
  96.                 error_log = loadgamerenderloop(mode, &SAVE_HANDLE);
  97.                 break;
  98.             case MODE_PRE_RUNNING_GAME:
  99.                 cur_data.map = prerunninggamerenderloop(mode, cur_data.map);
  100.                 error_log = (cur_data.map)?(CLEAN_EXIT):(ERROR_EXIT);
  101.                 break;
  102.             case MODE_RUNNING_GAME:
  103.                 error_log = runninggamerenderloop(mode, cur_data.map);
  104.                 break;
  105.             default:
  106.                 error_log = ERROR_EXIT;
  107.                 break;
  108.         }
  109.     }
  110.     if(SAVE_HANDLE >= 0) {
  111.         Bfile_CloseFile_OS(SAVE_HANDLE);
  112.         SAVE_HANDLE = -1;
  113.     }
  114.     free(cur_data.map);
  115.     return error_log;
  116. }
  117.  
  118. city_cell*prerunninggamerenderloop(int*outside_mode, city_cell*map){
  119.     int choice[5] = {2, 2, 2, 2, 2};
  120.     int done = 0;
  121.     int cur_choice = 0;
  122.     while(!done) {
  123.         GetKeyWaitNone();
  124.         Bdisp_AllCr_VRAM();
  125.         PrintXY(1, 1, "  Choose Map Params:", 0x0, TEXT_COLOR_BLUE);
  126.         PrintXY(2, 3, "  Density     :", 0x0, TEXT_COLOR_BLACK);
  127.         PrintXY(2, 4, "  Altitude    :", 0x0, TEXT_COLOR_BLACK);
  128.         PrintXY(2, 5, "  Climate     :", 0x0, TEXT_COLOR_BLACK);
  129.         PrintXY(2, 6, "  Gradualism  :", 0x0, TEXT_COLOR_BLACK);
  130.         PrintXY(2, 7, "  Variance    :", 0x0, TEXT_COLOR_BLACK);
  131.         PrintXY(2, 8, "  Empty Map", 0x0, TEXT_COLOR_BLACK);
  132.         for(int i = 0; i < 5; i++) {
  133.             switch(choice[i]) {
  134.                 case 1:
  135.                     PrintXY(15, i+3, "  1", 0x0, TEXT_COLOR_BLACK);
  136.                     break;
  137.                 case 2:
  138.                     PrintXY(15, i+3, "  2", 0x0, TEXT_COLOR_BLACK);
  139.                     break;
  140.                 case 3:
  141.                     PrintXY(15, i+3, "  3", 0x0, TEXT_COLOR_BLACK);
  142.                     break;
  143.             }
  144.         }
  145.         PrintXY(1, cur_choice+3, "  >", 0x0, TEXT_COLOR_PURPLE);
  146.         Bdisp_PutDisp_DD();
  147.         switch(GetKeyNB()) {
  148.             case KEY_PRGM_UP:
  149.                 cur_choice--;
  150.                 if(cur_choice < 0) cur_choice = 5;
  151.                 break;
  152.             case KEY_PRGM_DOWN:
  153.                 cur_choice++;
  154.                 if(cur_choice > 5) cur_choice = 0;
  155.                 break;
  156.             case KEY_PRGM_F1:
  157.                 if(cur_choice == 5) {
  158.                     if(map) { free(map); }
  159.                     map = FillData(malloc(sizeof(city_cell)*3600), 0x00, sizeof(city_cell)*3600);
  160.                     done--; } else {
  161.                     done++;
  162.                 }
  163.                 break;
  164.             case KEY_PRGM_LEFT:
  165.                 if(choice[cur_choice] > 1) choice[cur_choice]--;
  166.                 break;
  167.             case KEY_PRGM_RIGHT:
  168.                 if(choice[cur_choice] < 3) choice[cur_choice]++;
  169.                 break;
  170.         }
  171.        
  172.     }
  173.     if(done != -1) {
  174.         map = generatemap(choice[0], choice[1], choice[2], choice[3], choice[4]);
  175.     }
  176.     if(!map) {
  177.         Bdisp_AllCr_VRAM();
  178.         PrintXY(1, 1, "  *BIG* error :(", 0x0, TEXT_COLOR_RED);
  179.         Bdisp_PutDisp_DD();
  180.         OS_InnerWait_ms(2000);
  181.         *(outside_mode) = MODE_TITLE;
  182.         return 0x00000000;
  183.     }
  184.     *(outside_mode) = MODE_RUNNING_GAME;
  185.     return map;
  186. }
  187.  
  188.  
  189.  
  190. int runninggamerenderloop(int*outside_mode, city_cell*map){
  191.     int exit_sequence = 0;
  192.     int xpos = 0;
  193.     int ypos = 0;
  194.     int cur_xpos = 0;
  195.     int cur_ypos = 0;
  196.     int HUD_option = 0;
  197.     int HUD_option_2 = 0;
  198.     int HUD_x = 0;
  199.     int HUD_y = 0;
  200.     int HUD_x2 = 0;
  201.     int HUD_y2 = 0;
  202.     int s = 0;
  203.     while(!exit_sequence) {
  204.         switch(GetKeyNB()) {
  205.             case KEY_PRGM_EXIT:
  206.                 *(outside_mode) = MODE_TITLE;
  207.                 return CLEAN_EXIT;
  208.                 break;
  209.             case KEY_PRGM_LEFT:
  210.                 cur_xpos -= (cur_xpos>0);
  211.                 while(cur_xpos < (xpos+2) && xpos > 0 ) { xpos--; }
  212.                 break;
  213.             case KEY_PRGM_RIGHT:
  214.                 cur_xpos += (cur_xpos<59);
  215.                 while(cur_xpos > (xpos+18) && xpos < 40 ) { xpos++; }
  216.                 break;
  217.             case KEY_PRGM_UP:
  218.                 cur_ypos -= (cur_ypos>0);
  219.                 while(cur_ypos < (ypos+2) && ypos > 0 ) { ypos--; }
  220.                 break;
  221.             case KEY_PRGM_DOWN:
  222.                 cur_ypos += (cur_ypos<59);
  223.                 while(cur_ypos > (ypos+8) && ypos < 50 ) { ypos++; }
  224.                 break;
  225.             case KEY_PRGM_F1:
  226.                 HUD_option = 1;
  227.                 break;
  228.             case KEY_PRGM_F2:
  229.                 HUD_option = 2;
  230.                 break;
  231.             case KEY_PRGM_F3:
  232.                 HUD_option = 3;
  233.                 break;
  234.             case KEY_PRGM_F4:
  235.                 HUD_option = 4;
  236.                 break;
  237.             case KEY_PRGM_F5:
  238.                 HUD_option = 5;
  239.                 break;
  240.             case KEY_PRGM_F6:
  241.                 HUD_option = 6;
  242.                 break;
  243.             case KEY_PRGM_1:
  244.                 HUD_option_2 = 1;
  245.                 break;
  246.             case KEY_PRGM_2:
  247.                 HUD_option_2 = 2;
  248.                 break;
  249.             case KEY_PRGM_3:
  250.                 HUD_option_2 = 3;
  251.                 break;
  252.             case KEY_PRGM_4:
  253.                 HUD_option_2 = 4;
  254.                 break;
  255.             case KEY_PRGM_5:
  256.                 HUD_option_2 = 5;
  257.                 break;
  258.             case KEY_PRGM_6:
  259.                 HUD_option_2 = 6;
  260.                 break;
  261.             case KEY_PRGM_7:
  262.                 HUD_option_2 = 7;
  263.                 break;
  264.             case KEY_PRGM_8:
  265.                 HUD_option_2 = 8;
  266.                 break;
  267.         }
  268.        
  269.         HUD_x = (HUD_option-1)*26+163;
  270.         HUD_y = ((HUD_option-1)%2)*14+9;
  271.         HUD_x2 = ((HUD_option_2-1)%2)*26+325;
  272.         HUD_y2 = (HUD_option_2-1)*13+58;
  273.  
  274.         drawtilemap(map, xpos, ypos);
  275.         CopySpriteMasked(map_cursor, 16*(cur_xpos - xpos), 16*(cur_ypos - ypos)+56-8, 16, 16, 0xFFFF);
  276.         CopySprite(HUD_top, 0, 0, 384, 56);
  277.         CopySprite(HUD_right, 384-64, 56, 64, 216-56);
  278.         drawprintnum(31, 11, cur_data.money);
  279.         drawprintnum(31, 32, cur_data.population);
  280.         if(HUD_option) {
  281.             CopySpriteMasked2(HUD_option_trans, HUD_x, HUD_y, 24, 24, 0xFFFF);
  282.             if(HUD_option_2) {
  283.                 CopySpriteMasked2(HUD_option_trans, HUD_x2, HUD_y2, 24, 24, 0xFFFF);
  284.             }
  285.         } else {
  286.             HUD_option_2 = 0;
  287.         }
  288.         switch(HUD_option) {
  289.             case 1:
  290.                 CopySpriteMasked(HUD_options_1, 384-64+5, 58, 50, 63, 0xFFFF);
  291.                 switch(HUD_option_2) {
  292.                     case 1:
  293.                         if(GetKeyNB() == KEY_PRGM_SHIFT) {
  294.                             access_map_element(map, cur_xpos, cur_ypos)->occupied_type = residential;
  295.                             access_map_element(map, cur_xpos, cur_ypos)->occupied = 1;
  296.                         }
  297.                         break;             
  298.                     case 2:
  299.                         if(GetKeyNB() == KEY_PRGM_SHIFT) {
  300.                             access_map_element(map, cur_xpos, cur_ypos)->occupied_type = commercial;
  301.                             access_map_element(map, cur_xpos, cur_ypos)->occupied = 1;
  302.                         }
  303.                         break;
  304.                     case 3:
  305.                         if(GetKeyNB() == KEY_PRGM_SHIFT) {
  306.                             access_map_element(map, cur_xpos, cur_ypos)->occupied_type = industrial;
  307.                             access_map_element(map, cur_xpos, cur_ypos)->occupied = 1;
  308.                         }
  309.                         break;
  310.                     case 4:
  311.                         if(GetKeyNB() == KEY_PRGM_SHIFT) {
  312.                             access_map_element(map, cur_xpos, cur_ypos)->occupied = 0;
  313.                         }
  314.                         break;
  315.                 }
  316.                 break;
  317.             case 2:
  318.                 CopySpriteMasked(HUD_options_2, 384-64+5, 58, 50, 115, 0xFFFF);
  319.                 break;
  320.             case 3:
  321.                 CopySpriteMasked(HUD_options_3, 384-64+5, 58, 50, 50, 0xFFFF);
  322.                 break;
  323.             case 6:
  324.                 draw_info_box(60, 110, 180, 42);
  325.                 drawprint(68, 118, "press f1 to save\nor f2 to cancel\0");
  326.                 Bdisp_PutDisp_DD();
  327.                 while(!s) {
  328.                     switch(GetKeyNB()) {
  329.                         case KEY_PRGM_F1:
  330.                             realloc(map, sizeof(city_cell)*3600+5000);
  331.                             memcpy(sizeof(city_cell)*3600+map, &(cur_data.money), sizeof(int));
  332.                             memcpy(sizeof(city_cell)*3600+sizeof(int)+map, &(cur_data.population), sizeof(int));
  333.                             Bfile_WriteFile_OS(SAVE_HANDLE, (const char*) map, sizeof(city_cell)*3600+5000);
  334.                             s++;
  335.                             draw_info_box(60, 152, 120, 28);
  336.                             if(SAVE_HANDLE > 0) {
  337.                                 drawprint(68, 160, "data saved\0");
  338.                             } else {
  339.                                 drawprint(68, 160, "save error\0");
  340.                             }
  341.                             Bdisp_PutDisp_DD();
  342.                             GetKeyHold(KEY_PRGM_F1);
  343.                             break;
  344.                         case KEY_PRGM_F2:
  345.                             s++;
  346.                             break;             
  347.                     }              
  348.                 }
  349.                 s = 0;
  350.                 HUD_option = 0;
  351.                 break;
  352.         }
  353.        
  354.         if(HUD_option_2) {
  355.             switch(HUD_option) {
  356.                 case 1:
  357.                     if(HUD_option_2 > 4) { HUD_option_2 = 0; }
  358.                     break;
  359.                 case 3:
  360.                     if(HUD_option_2 > 3) { HUD_option_2 = 0; }
  361.                     break;
  362.             }
  363.         }
  364.         if(HUD_option) {
  365.             CopySpriteMasked2(HUD_option_trans, HUD_x, HUD_y, 24, 24, 0xFFFF);
  366.             if(HUD_option_2) {
  367.                 CopySpriteMasked2(HUD_option_trans, HUD_x2, HUD_y2, 24, 24, 0xFFFF);
  368.             }
  369.         } else {
  370.             HUD_option_2 = 0;
  371.         }
  372.         Bdisp_PutDisp_DD();
  373.     }
  374.     return CLEAN_EXIT;
  375. }
  376.  
  377. void drawtilemap(city_cell*map, int xpos, int ypos) {
  378.     city_cell*cur_element = 0x00000000;
  379.     const char*tile = 0x00000000;
  380.     for(int j = 0; j < 10 ; j++) {
  381.         for(int i = 0; i < 20; i++) {
  382.             cur_element = access_map_element(map, xpos+i, ypos+j);
  383.             if(cur_element->occupied) {
  384.                 switch(cur_element->occupied_type) {
  385.                     case utility:
  386.                         break;
  387.                     case residential:
  388.                     case commercial:
  389.                     case industrial:
  390.                         tile = ((cur_element->occupied_type-1)*512)+zoning_tiles;
  391.                         break;
  392.                     case school:
  393.                         break;
  394.                     case police:
  395.                         break;
  396.                     case firestation:
  397.                         break;
  398.                     case hospital:
  399.                         break;
  400.                     case road:
  401.                         break;
  402.                     case political:
  403.                         break;
  404.                     case park:
  405.                         break;
  406.                     case landmark:
  407.                         break;
  408.                     case ruins:
  409.                         break;
  410.                 }
  411.             } else {
  412.                 switch(cur_element->natural_cell){
  413.                     case grass:
  414.                         tile = grass_tile;
  415.                         break;
  416.                     case water:
  417.                         tile = water_tile;
  418.                         break;
  419.                     case dirt:
  420.                         tile = dirt_tile;
  421.                         break;
  422.                     case sand:
  423.                         tile = sand_tile;
  424.                         break;
  425.                     case tree:
  426.                         tile = tree_tile;
  427.                         break;
  428.                     case mountain:
  429.                         tile = mountain_tile;
  430.                         break;
  431.  
  432.                 }
  433.             }
  434.             CopySprite(tile, i*16, j*16+56, 16, 16);
  435.             tile = 0x00000000;
  436.         }  
  437.     }
  438.    
  439. }
  440.  
  441. city_cell*generatemap(int density, int altitude, int climate, int gradualism, int variance){
  442.     city_cell*cur_cell = 0x00000000;
  443.     int surroundings[25];
  444.     Bdisp_AllCr_VRAM();
  445.     int loop_index = 0;
  446.     int cell_in_q = 0;
  447.     int types[6];
  448.     int cur_max_type = 0;
  449.     PrintXY(1, 1, "  Generating Map.", 0x0, TEXT_COLOR_BLUE);
  450.     PrintXY(1, 2, "  Please Wait...", 0x0, TEXT_COLOR_BLUE);
  451.     Bdisp_PutDisp_DD();
  452.     city_cell*map = FillData(malloc(sizeof(city_cell)*3600), 0x00, sizeof(city_cell)*3600);
  453.     if(!map) return 0x00000000;
  454.     PrintXY(2, 5, "  Debouncing Buildings", 0x0, random(0)%7);
  455.     Bdisp_PutDisp_DD();
  456.     for(int j = 0; j < 60; j++) {
  457.         for(int i = 0; i < 60; i++) {
  458.            
  459.             if(!i) { switch(j) {
  460.                 case 9:
  461.                     PrintXY(2, 5, "                          ", 0x0, TEXT_COLOR_WHITE);
  462.                     PrintXY(2, 5, "  Parsing Mountains", 0x0, random(0)%7);
  463.                     Bdisp_PutDisp_DD();
  464.                     break;
  465.                 case 17:
  466.                     PrintXY(2, 5, "                          ", 0x0, TEXT_COLOR_WHITE);
  467.                     PrintXY(2, 5, "  Cleaning Water", 0x0, random(0)%7);
  468.                     Bdisp_PutDisp_DD();
  469.                     break;
  470.                 case 26:
  471.                     PrintXY(2, 5, "                          ", 0x0, TEXT_COLOR_WHITE);
  472.                     PrintXY(2, 5, "  Seeding Trees", 0x0, random(0)%7);
  473.                     Bdisp_PutDisp_DD();
  474.                     break;
  475.                 case 34:
  476.                     PrintXY(2, 5, "                          ", 0x0, TEXT_COLOR_WHITE);
  477.                     PrintXY(2, 5, "  Adding Gatorade", 0x0, random(0)%7);
  478.                     Bdisp_PutDisp_DD();
  479.                     break;
  480.                 case 45:
  481.                     PrintXY(2, 5, "                          ", 0x0, TEXT_COLOR_WHITE);
  482.                     PrintXY(2, 5, "  Tapping Supplies", 0x0, random(0)%7);
  483.                     Bdisp_PutDisp_DD();
  484.                     break;
  485.                 case 55:
  486.                     PrintXY(2, 5, "                          ", 0x0, TEXT_COLOR_WHITE);
  487.                     PrintXY(2, 5, "  Printing Currency", 0x0, random(0)%7);
  488.                     Bdisp_PutDisp_DD();
  489.                     break;
  490.             } }
  491.             for(int k = -1; k <= 1; k++) {
  492.                 for(int l = -1; l <= 1; l++) {
  493.                     if(j + k <= 0 || j + k >= 59 || i + l <= 0 || i + l >= 59) {
  494.                         surroundings[loop_index++] = -1;
  495.                         continue;
  496.                     }
  497.                     surroundings[loop_index++] = access_map_element(map, i+l, j+k)->natural_cell;
  498.                 }
  499.             }
  500.             loop_index = 0;
  501.             cur_cell = access_map_element(map, i, j);
  502.             if(!i && !j){
  503.                 switch(altitude) {
  504.                     case 1:
  505.                         cur_cell->natural_cell = water;
  506.                         break;
  507.                     case 2:
  508.                         cur_cell->natural_cell = grass;
  509.                         break;
  510.                     case 3:
  511.                         cur_cell->natural_cell = dirt;
  512.                         break;
  513.                 }
  514.                 continue;
  515.             }
  516.             if(!(random(0)%(density+1))) {
  517.                 for(int i = 0; i < 4; i++) {
  518.                     if(surroundings[i]<0) {
  519.                         types[(altitude==1)?(1):((altitude==2)?(0):(2))]++;
  520.                         continue;
  521.                     }
  522.                     types[surroundings[i]]++;
  523.                 }
  524.                
  525.                 if(altitude == 1) {
  526.                     cur_cell->natural_cell = water;
  527.                     continue;
  528.                 }
  529.                
  530.                 for(int i = 1; i < 6; i++) {
  531.                     cur_max_type = (types[cur_max_type]>types[i])?(cur_max_type):(i);
  532.                 }
  533.                 cell_in_q = cur_max_type;
  534.                 cur_max_type = 0;
  535.                
  536.                
  537.                 switch(cell_in_q) {
  538.                     case grass:
  539.                         if(random(RTC_GetTicks())%(2*climate)) {
  540.                             cur_cell->natural_cell = tree; } else {
  541.                             cur_cell->natural_cell = grass;
  542.                         }
  543.                         break;
  544.                     case water:
  545.                         cur_cell->natural_cell = water;
  546.                         break;
  547.                     case dirt:
  548.                         if(random(RTC_GetTicks())%3){
  549.                             cur_cell->natural_cell = dirt;
  550.                         } else {
  551.                             cur_cell->natural_cell = mountain;
  552.                         }
  553.                         break;
  554.                     case sand:
  555.                         cur_cell->natural_cell = water;
  556.                         break;
  557.                     case tree:
  558.                         if(random(RTC_GetTicks())%(2*(climate*climate))) {
  559.                             cur_cell->natural_cell = tree; } else {
  560.                             cur_cell->natural_cell = grass;
  561.                         }
  562.                         break;
  563.                     case mountain:
  564.                         if(random(RTC_GetTicks())%(altitude+2)) {
  565.                             cur_cell->natural_cell = dirt; } else {
  566.                             cur_cell->natural_cell = mountain;
  567.                         }
  568.                         break;
  569.                 }  
  570.             } else {
  571.                
  572.                 if(!(random(RTC_GetTicks())%(2*gradualism))) {
  573.                     switch(altitude) {
  574.                         case 1:
  575.                             cur_cell->natural_cell = water;
  576.                             break;
  577.                         case 2:
  578.                             cur_cell->natural_cell = (random(RTC_GetTicks())%(2*climate))?(tree):(grass);
  579.                             break;
  580.                         case 3:
  581.                             cur_cell->natural_cell = (random(RTC_GetTicks())%2)?(dirt):(mountain);
  582.                             break;         
  583.                     }
  584.                 } else {
  585.                     switch(altitude) {
  586.                         case 1:
  587.                             cur_cell->natural_cell = water;
  588.                             break;
  589.                         case 2:
  590.                             cur_cell->natural_cell = grass;
  591.                             break;
  592.                         case 3:
  593.                             cur_cell->natural_cell = dirt;
  594.                             break;
  595.                    
  596.                    
  597.                     }
  598.                 }
  599.                 cell_in_q = 0;
  600.  
  601.             }
  602.             OS_InnerWait_ms(random(RTC_GetTicks())%64);
  603.            
  604.         }
  605.     }
  606.    
  607.     int width, height, x, y, selection;
  608.    
  609.    
  610.     for(int k = 0; k < (variance+(density-2)); k++) {  
  611.         switch(altitude) {
  612.             case 1:
  613.                 width = 16 + (random(RTC_GetTicks())%16);
  614.                 OS_InnerWait_ms(((random(0)+1)*(random(0)+1)*(random(0)+1)*(random(0)+1)*64)%1024);
  615.                 height = 16 + (random(RTC_GetTicks())%16);
  616.                 OS_InnerWait_ms(((random(0)+1)*(random(0)+1)*(random(0)+1)*(random(0)+1)*64)%1024);
  617.                 x = random(RTC_GetTicks()) % (60-width);
  618.                 OS_InnerWait_ms(((random(0)+1)*(random(0)+1)*(random(0)+1)*(random(0)+1)*64)%1024);
  619.                 y = random(RTC_GetTicks()) % (60-height);
  620.                 selection = random(RTC_GetTicks())%4;
  621.                 for(int j = y; j < y+height; j++) {
  622.                     for(int i = x; i < x+width; i++) {
  623.                         if(island_seed[selection][((j-y)*16)/height][((i-x)*16)/width] != 1) {
  624.                             access_map_element(map, i, j)->natural_cell = island_seed[selection][((j-y)*16)/height][((i-x)*16)/width];
  625.                         }
  626.                        
  627.                     }
  628.                 }
  629.                 break;
  630.             case 2:
  631.             case 3:
  632.                 width = 16 + (random(RTC_GetTicks())%16);
  633.                 OS_InnerWait_ms(((random(0)+1)*(random(0)+1)*(random(0)+1)*(random(0)+1)*64)%1024);
  634.                 height = 16 + (random(RTC_GetTicks())%16);
  635.                 OS_InnerWait_ms(((random(0)+1)*(random(0)+1)*(random(0)+1)*(random(0)+1)*64)%1024);
  636.                 x = random(RTC_GetTicks()) % (60-width);
  637.                 OS_InnerWait_ms(((random(0)+1)*(random(0)+1)*(random(0)+1)*(random(0)+1)*64)%1024);
  638.                 y = random(RTC_GetTicks()) % (60-height);
  639.                 selection = random(RTC_GetTicks())%2;
  640.                 for(int j = y; j < y+height; j++) {
  641.                     for(int i = x; i < x+width; i++) {
  642.                         if(lake_seed[selection][((j-y)*16)/height][((i-x)*16)/width] != 4) {
  643.                             access_map_element(map, i, j)->natural_cell = lake_seed[selection][((j-y)*16)/height][((i-x)*16)/width];
  644.                         }
  645.                     }
  646.                 }
  647.                 break;
  648.         }
  649.     }
  650.     Bdisp_AllCr_VRAM();
  651.     return map;
  652. }
  653.  
  654. city_cell*access_map_element(city_cell*map, int x, int y) {
  655.     return (y*60+x+map);
  656. }
  657.  
  658. int titlescreenrenderloop(int*cursor_pos, int*outside_mode) {
  659.     *(cursor_pos) %= 3;
  660.     switch(GetKeyNB()) {
  661.         case KEY_PRGM_UP:
  662.             --(*(cursor_pos));
  663.             *(cursor_pos)=(*(cursor_pos) < 0)?(2):(*(cursor_pos));
  664.             break;
  665.         case KEY_PRGM_DOWN:
  666.             ++(*(cursor_pos));
  667.             *(cursor_pos)=(*(cursor_pos) > 2)?(0):(*(cursor_pos));
  668.             break;
  669.         case KEY_PRGM_F1:
  670.             *(outside_mode) = ((*(cursor_pos)==2)?(MODE_ABOUT):((*(cursor_pos)==1)?(MODE_LOAD_GAME):(MODE_NEW_GAME)));
  671.             break;
  672.     }
  673.     GetKeyWaitNone();
  674.     memcpy(D_VRAM, TitleScreen, LCD_HEIGHT_PX * LCD_WIDTH_PX * 2);
  675.     CopySpriteMasked(cursor01, 250, 25*(*(cursor_pos))+105, 24, 24, 0xFFFF);
  676.     Bdisp_PutDisp_DD();
  677.    
  678.     return CLEAN_EXIT;
  679. }
  680.  
  681. int aboutscreenrenderloop(int*outside_mode) {
  682.     while(GetKeyNB() != KEY_PRGM_NONE) { }
  683.     memcpy(D_VRAM, AboutScreen, LCD_HEIGHT_PX * LCD_WIDTH_PX * 2);
  684.     Bdisp_PutDisp_DD();
  685.     GetKeyHold(KEY_PRGM_F1);
  686.     *(outside_mode) = MODE_TITLE;
  687.     return CLEAN_EXIT;
  688. }
  689.  
  690. int newgamerenderloop(int*outside_mode, int*SAVE_HANDLE) {
  691.     int decision = 0;
  692.     int size_needed = (sizeof(city_cell)*3600)+5000;
  693.     unsigned short*file_name = malloc(64);
  694.     Bdisp_AllCr_VRAM();
  695.     PrintXY(2, 2, "  Start New Game?", 0x0, TEXT_COLOR_BLACK);
  696.     Bdisp_PutDisp_DD();
  697.     OS_InnerWait_ms(1000);
  698.     PrintXY(5, 4, "  F1 -- YES", 0x0, TEXT_COLOR_GREEN);
  699.     PrintXY(5, 5, "  F2 -- NO", 0x0, TEXT_COLOR_RED);
  700.     Bdisp_PutDisp_DD();
  701.     while(!decision) {
  702.         switch(GetKeyNB()) {
  703.             case KEY_PRGM_F1:
  704.                 *(outside_mode) = MODE_PRE_RUNNING_GAME;
  705.                 GetKeyWaitNone();
  706.                 decision++;
  707.                 Bfile_StrToName_ncpy(file_name, (const unsigned char*) "\\\\fls0\\city.pcc\0", 64);
  708.                 *(SAVE_HANDLE) = Bfile_OpenFile_OS(file_name, 0x03);
  709.                 if(*(SAVE_HANDLE) < 0) {
  710.                     Bdisp_AllCr_VRAM();
  711.                     PrintXY(2, 2, "  No previous file!", 0x0, TEXT_COLOR_BLACK);
  712.                     PrintXY(2, 3, "  Making new file", 0x0, TEXT_COLOR_BLACK);
  713.                     OS_InnerWait_ms(1000);
  714.                     Bdisp_PutDisp_DD();
  715.                     PrintXY(3, 4, "  Please wait...", 0x0, TEXT_COLOR_GREEN);
  716.                     Bdisp_PutDisp_DD();
  717.                     OS_InnerWait_ms(500);
  718.                     Bfile_CreateEntry_OS(file_name, 1, &size_needed);
  719.                     *(SAVE_HANDLE) = Bfile_OpenFile_OS(file_name, 0x03);
  720.                     if(*(SAVE_HANDLE) < 0) {
  721.                         Bdisp_AllCr_VRAM();
  722.                         PrintXY(1, 2, "  FATAL CREATION ERROR", 0x0, TEXT_COLOR_RED);
  723.                         PrintXY(2, 4, "  Successful Troll", 0x0, TEXT_COLOR_CYAN);
  724.                         PrintXY(2, 5, "  Is Successful :(", 0x0, TEXT_COLOR_CYAN);
  725.                         Bdisp_PutDisp_DD();
  726.                         GetKeyHold(KEY_PRGM_F1);
  727.                         *(outside_mode) = MODE_TITLE;
  728.                         free(file_name);
  729.                         return CLEAN_EXIT;
  730.                     }
  731.                     cur_data.money = 50000;
  732.                     cur_data.population = 0;
  733.                     // TODO: setup/load stuff
  734.                 }
  735.                 break;
  736.             case KEY_PRGM_F2:
  737.                 *(outside_mode) = MODE_TITLE;
  738.                 GetKeyWaitNone();
  739.                 decision--;
  740.                 break;
  741.         }
  742.     }
  743.     if(*(SAVE_HANDLE) >= 0) {
  744.         Bfile_CloseFile_OS(*(SAVE_HANDLE));
  745.         *(SAVE_HANDLE) = -1;
  746.     }
  747.     free(file_name);
  748.     return CLEAN_EXIT;
  749. }
  750.  
  751. int loadgamerenderloop(int*outside_mode, int*SAVE_HANDLE) {
  752.     Bdisp_AllCr_VRAM();
  753.     unsigned short*file_name = malloc(64);
  754.     Bfile_StrToName_ncpy(file_name, (const unsigned char*) "\\\\fls0\\city.pcc\0", 64);
  755.     PrintXY(1, 2, "  Attempt to get file", 0x0, TEXT_COLOR_BLACK);
  756.     PrintXY(1, 3, "  from \'city.pcc\' ", 0x0, TEXT_COLOR_BLACK);
  757.     Bdisp_PutDisp_DD();
  758.     for(int i = 17; i <= 19; i++) {
  759.         OS_InnerWait_ms(500);
  760.         PrintXY(i, 3, "  .", 0x0, TEXT_COLOR_BLACK);
  761.         Bdisp_PutDisp_DD();
  762.     }
  763.     OS_InnerWait_ms(500);
  764.     *(SAVE_HANDLE) = Bfile_OpenFile_OS(file_name, 0x03);
  765.     if(*(SAVE_HANDLE) < 0) {
  766.         PrintXY(2, 5, "  ERROR: NONE EXISTS!", 0x0, TEXT_COLOR_RED);
  767.         Bdisp_PutDisp_DD();
  768.         GetKeyHold(KEY_PRGM_F1);
  769.         *(outside_mode) = MODE_TITLE;
  770.     } else {
  771.         PrintXY(2, 5, "  LOAD SUCCESSFUL.", 0x0, TEXT_COLOR_GREEN);
  772.         Bdisp_PutDisp_DD();
  773.         GetKeyHold(KEY_PRGM_F1);
  774.         *(outside_mode) = MODE_TITLE;
  775.         // TODO: load stuff
  776.     }
  777.     if(*(SAVE_HANDLE) >= 0) {
  778.         Bfile_CloseFile_OS(*(SAVE_HANDLE));
  779.         *(SAVE_HANDLE) = -1;
  780.     }
  781.     free(file_name);
  782.     return CLEAN_EXIT;
  783. }
  784.  
  785.  
  786.  
  787.  
  788. void*FillData(void*pointer, unsigned char element, int amount){
  789.     void*pointer_ref = pointer;
  790.     for(int i = amount; i > 0; i--) {
  791.         *((unsigned char*) pointer++) = element;
  792.     }
  793.     return pointer_ref;
  794. }
  795.  
  796.  
  797. int PRGM_GetKey(){
  798.     unsigned char buffer[12];
  799.     PRGM_GetKey_OS( buffer );
  800.     return ( buffer[1] & 0x0F ) * 10 + ( ( buffer[2] & 0xF0 ) >> 4 );
  801. }
  802.  
  803. int GetKeyNB() {
  804.     int key;
  805.     key = PRGM_GetKey();
  806.     if (key == KEY_PRGM_MENU) {
  807.         GetKey(&key);
  808.         Bdisp_EnableColor(1);
  809.         Bdisp_PutDisp_DD();
  810.     }
  811.     return key;
  812. }
  813.  
  814. void GetKeyHold(int key) {
  815.     while(GetKeyNB() != key) { }
  816.     while(GetKeyNB() != KEY_PRGM_NONE) { }
  817.  
  818. }
  819.  
  820. void GetKeyWaitNone() {
  821.     while(GetKeyNB() != KEY_PRGM_NONE) { } 
  822. }
  823.  
  824.  
  825. void CopySprite(const char* data, int x, int y, int width, int height) {
  826.    char* VRAM = (char*)0xA8000000;
  827.    VRAM += 2*(LCD_WIDTH_PX*y + x);
  828.    for(int j=y; j<y+height; j++) {
  829.       memcpy(VRAM,data,2*width);
  830.       VRAM += 2*LCD_WIDTH_PX;
  831.       data += 2*width;
  832.    }
  833. }
  834.  
  835. void CopySpriteMasked(const char* data, int x, int y, int width, int height, int maskcolor) {
  836.    char* VRAM = (char*)0xA8000000;
  837.    VRAM += 2*(LCD_WIDTH_PX*y + x);
  838.    for(int j=y; j<y+height; j++) {
  839.       for(int i=x; i<x+width;  i++) {
  840.          if ((((((int)(*data))&0x000000FF)<<8) | ((((int)(*(data+1))))&0x000000FF)) != maskcolor) {
  841.             *(VRAM++) = *(data++);
  842.             *(VRAM++) = *(data++);
  843.          } else { VRAM += 2; data += 2; }
  844.       }
  845.       VRAM += 2*(LCD_WIDTH_PX-width);
  846.    }
  847. }
  848.  
  849. void CopySpriteMasked2(const char* data, int x, int y, int width, int height, int maskcolor) {
  850.    char* VRAM = (char*)0xA8000000;
  851.    VRAM += 2*(LCD_WIDTH_PX*y + x);
  852.    for(int j=y; j<y+height; j++) {
  853.       for(int i=x; i<x+width;  i++) {
  854.          if ((((((int)(*data))&0x000000FF)<<8) | ((((int)(*(data+1))))&0x000000FF)) != maskcolor) {
  855.             *(VRAM) = (((int)(*(data++) + (*(VRAM))))/2);
  856.             VRAM++;
  857.             *(VRAM) = (((int)(*(data++) + (*(VRAM))))/2);
  858.             VRAM++;
  859.          } else { VRAM += 2; data += 2; }
  860.       }
  861.       VRAM += 2*(LCD_WIDTH_PX-width);
  862.    }
  863. }
  864.  
  865. unsigned short random(int extra_seed) {
  866.     int seed = 0;
  867.     int seed2 = 0;
  868.     for(int i = 0; i < 32; i++ ){
  869.         seed <<= 1;
  870.         seed |= (RTC_GetTicks()%2);
  871.     }
  872.     for(int i = 0; i < 32; i++ ){
  873.         seed2 <<= 1;
  874.         seed2 |= (RTC_GetTicks()%16);
  875.     }
  876.     seed ^= seed2;
  877.     seed = (( 0x41C64E6D*seed ) + 0x3039);
  878.     seed2 = (( 0x1045924A*seed2 ) + 0x5023);
  879.     extra_seed = (extra_seed)?(( 0xF201B35C*extra_seed ) + 0xD018):(extra_seed);
  880.     seed ^= seed2;
  881.     if(extra_seed){ seed ^= extra_seed; }
  882.     return ((seed >> 16) ^ seed) >> 16;
  883. }
  884.  
  885. void drawprintnum(int x, int y, int num) {
  886.     int digits[10] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
  887.     int pos_num = (num<0)?(-num):(num);
  888.     int num_digits = 0;
  889.     for(int i = 0; i < 10; i++) {
  890.         if(!pos_num) { break; }
  891.         digits[i] = pos_num%10;
  892.         pos_num = (pos_num - (pos_num%10)) / 10;
  893.         num_digits++;      
  894.     }
  895.     if(num<0) {
  896.         CopySpriteMasked(glyph_nums + 2400, x, y, 10, 12, 0xFFFF);
  897.         x += 12;
  898.     } else if (num == 0) {
  899.         CopySpriteMasked(glyph_nums, x, y, 10, 12, 0xFFFF);
  900.         return;
  901.     }
  902.     for(int i = 0; i < num_digits; i++) {
  903.         CopySpriteMasked((digits[i]*240)+glyph_nums, x, y, 10, 12, 0xFFFF);
  904.         x += 12;
  905.     }
  906. }
  907.  
  908. void drawprint(int x, int y, char*string) {
  909.     int i = 0;
  910.     int len = 0;
  911.     int s = 0;
  912.     int xs = x;
  913.     while(1) {
  914.         if(*(string+(i++))) {
  915.             len++;
  916.         } else {
  917.             break;
  918.         }
  919.     }
  920.     for(int j = 0; j < len; j++) {
  921.         switch(*(string+j)) {
  922.             case 0:
  923.                 return;
  924.                 break;
  925.             case ' ':
  926.                 x += 10;
  927.                 s++;
  928.                 break;
  929.             case '0':
  930.             case '1':
  931.             case '2':
  932.             case '3':
  933.             case '4':
  934.             case '5':
  935.             case '6':
  936.             case '7':
  937.             case '8':
  938.             case '9':
  939.                 s++;
  940.                 drawprintnum(x, y, *(string+j)-'0');
  941.                 x += 10;
  942.                 break;
  943.             case '\n':
  944.                 s++;
  945.                 y += 16;
  946.                 x = xs;
  947.                 break;
  948.         }
  949.         if(s) {
  950.             s = 0;
  951.             continue;
  952.         }
  953.         CopySpriteMasked((*(string+j) - 'a')*192+glyph_chars, x, y, 8, 12, 0xFFFF);
  954.         x += 10;
  955.     }
  956. }
  957.  
  958. void rectangle(int x, int y, int width, int height, unsigned short color) {
  959.     unsigned short*VRAM = (unsigned short*)0xA8000000;
  960.     for(int j = y; j < y+height; j++) {
  961.         for(int i = x; i < x+width; i++) {
  962.             *(j*LCD_WIDTH_PX+i+VRAM) = color;          
  963.         }
  964.     }
  965. }
  966.  
  967. void draw_info_box(int x, int y, int width, int height) {
  968.     rectangle(x, y, width, height, 0xFD10);
  969.     rectangle(x+2, y+2, width-4, height-4, 0xFF85);
  970. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement