File indexing completed on 2024-04-28 04:01:57

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                 *
0003 *   SPDX-FileCopyrightText: 2010 University Freiburg <squall.leonhart.cai@gmail.com> *
0004 *                                                                          *
0005 *   SPDX-License-Identifier: GPL-2.0-or-later
0006 ***************************************************************************/
0007 #include "KBlocksAILog.h"
0008 #include <string.h>
0009 
0010 /*****************************************************************
0011 **  screen / file - setting  *************************************
0012 *****************************************************************/
0013 static const int MAX_CHANNEL = 10;
0014 static const int SCREEN_CHANNEL = MAX_CHANNEL;
0015 static int current_channel = SCREEN_CHANNEL;
0016 static FILE *channel[MAX_CHANNEL] = {nullptr};
0017 static bool is_channel_open[MAX_CHANNEL] = {false};
0018 
0019 void set_screen_channel()
0020 {
0021     current_channel = SCREEN_CHANNEL;
0022 }
0023 
0024 void set_channel(int ch)
0025 {
0026     if (abs(ch) >= MAX_CHANNEL) {
0027         set_screen_channel();
0028     } else {
0029         current_channel = ch;
0030     }
0031 }
0032 
0033 void set_screen_file_channel(int ch)
0034 {
0035     set_channel(-ch);
0036 }
0037 
0038 void open_file(const char *fn)
0039 {
0040     open_file(abs(current_channel), fn);
0041 }
0042 
0043 void open_file(int ch, const char *fn)
0044 {
0045     set_channel(ch);
0046     ch = abs(current_channel);
0047     if ((ch == SCREEN_CHANNEL) || (is_channel_open[ch])) {
0048         return;
0049     }
0050     channel[ch] = fopen(fn, "w");
0051     is_channel_open[ch] = true;
0052 }
0053 
0054 void close_file()
0055 {
0056     close_file(abs(current_channel));
0057 }
0058 
0059 void close_file(int ch)
0060 {
0061     set_channel(ch);
0062     ch = abs(current_channel);
0063     if ((ch == SCREEN_CHANNEL) || (!is_channel_open[ch])) {
0064         return;
0065     }
0066     fclose(channel[ch]);
0067     is_channel_open[ch] = false;
0068 }
0069 
0070 void gotoXY(int x, int y)
0071 {
0072 #ifdef DEBUG
0073     char essq[100];
0074     char xstr[100];
0075     char ystr[100];
0076 
0077     sprintf(xstr, "%d", x);
0078     sprintf(ystr, "%d", y);
0079 
0080     essq[0] = '\0';
0081     strcat(essq, "\033[");
0082     strcat(essq, ystr);
0083 
0084     strcat(essq, "d");
0085 
0086     strcat(essq, "\033[");
0087     strcat(essq, xstr);
0088     strcat(essq, "G");
0089 
0090     printf("%s", essq);
0091 #endif
0092 }
0093 bool isScreenChannel(int channel)
0094 {
0095     return ((channel == MAX_CHANNEL) || (channel < 0));
0096 }
0097 
0098 bool isFileChannel(int channel)
0099 {
0100     return ((abs(channel) < MAX_CHANNEL) && (is_channel_open[channel]));
0101 }
0102 
0103 /*****************************************************************
0104 **  print element  ***********************************************
0105 *****************************************************************/
0106 void println()
0107 {
0108 #ifdef DEBUG
0109     if (isScreenChannel(current_channel)) {
0110         printf("\n");
0111     }
0112 #endif
0113 #ifdef LOG
0114     if (isFileChannel(current_channel)) {
0115         fprintf(channel[current_channel], "\n");
0116     }
0117 #endif
0118 }
0119 
0120 void print(const char *s)
0121 {
0122 #ifdef DEBUG
0123     if (isScreenChannel(current_channel)) {
0124         printf("%s", s);
0125     }
0126 #endif
0127 #ifdef LOG
0128     if (isFileChannel(current_channel)) {
0129         fprintf(channel[current_channel], "%s", s);
0130     }
0131 #endif
0132 }
0133 
0134 void println(const char *s)
0135 {
0136     print(s);
0137     println();
0138 }
0139 
0140 void print(int i)
0141 {
0142 #ifdef DEBUG
0143     if (isScreenChannel(current_channel)) {
0144         printf("%d", i);
0145     }
0146 #endif
0147 #ifdef LOG
0148     if (isFileChannel(current_channel)) {
0149         fprintf(channel[current_channel], "%d", i);
0150     }
0151 #endif
0152 }
0153 
0154 void println(int i)
0155 {
0156     print(i);
0157     println();
0158 }
0159 
0160 void print(double d)
0161 {
0162 #ifdef DEBUG
0163     if (isScreenChannel(current_channel)) {
0164         printf("%f", d);
0165     }
0166 #endif
0167 #ifdef LOG
0168     if (isFileChannel(current_channel)) {
0169         fprintf(channel[current_channel], "%f", d);
0170     }
0171 #endif
0172 }
0173 
0174 void println(double d)
0175 {
0176     print(d);
0177     println();
0178 }
0179 /*****************************************************************
0180 **  print object  ************************************************
0181 *****************************************************************/
0182 void println(KBlocksPiece *piece)
0183 {
0184     const char *Piece_Type_Name[7] = {
0185         "PIECE_TYPE_Z", // 0
0186         "PIECE_TYPE_S", // 1
0187         "PIECE_TYPE_I", // 2
0188         "PIECE_TYPE_T", // 3
0189         "PIECE_TYPE_Q", // 4
0190         "PIECE_TYPE_L", // 5
0191         "PIECE_TYPE_J", // 6
0192     };
0193     println("PIECE");
0194     print("Type:"); println(Piece_Type_Name[piece->getType()]);
0195 }
0196 
0197 void println(KBlocksPiece *piece, bool full)
0198 {
0199     println(piece, -1, 0, full);
0200 }
0201 
0202 void println(KBlocksPiece *piece, int x, int y, bool full)
0203 {
0204     if (full) {
0205         if (x != -1) {
0206             gotoXY(x, ++y);
0207         }
0208         println(piece);
0209         if (x != -1) {
0210             gotoXY(x, ++y);
0211         }
0212         println("STATE");
0213         if (x != -1) {
0214             gotoXY(x, ++y);
0215         }
0216         print("Rotation Id   :"); println(piece->getRotation());
0217         if (x != -1) {
0218             gotoXY(x, ++y);
0219         }
0220         print("pos: (");
0221         print(piece->getPosX());
0222         print(",");
0223         print(piece->getPosY());
0224         println(")");
0225         println("Cells: ");
0226         for (int i = 0; i < KBlocksPiece_CellCount; ++i) {
0227             if (x != -1) {
0228                 gotoXY(x, ++y);
0229             }
0230             print("[");
0231             print(piece->getCellPosX(i));
0232             print(",");
0233             print(piece->getCellPosY(i));
0234             println("]");
0235         }
0236     }
0237 }
0238 
0239 void println(KBlocksField *field)
0240 {
0241     println(field, -1, 0);
0242 }
0243 
0244 void println(KBlocksField *field, int x, int y)
0245 {
0246     int w = field->getWidth();
0247     int h = field->getHeight();
0248     for (int j = 0; j < h; ++j) {
0249         if (x != -1) {
0250             gotoXY(x, y + j + 1);
0251         }
0252         print("|");
0253         for (int i = 0; i < w; ++i) {
0254             if (field->getCell(i, j)) {
0255                 print("*");
0256             } else {
0257                 print(" ");
0258             }
0259         }
0260         println("|");
0261     }
0262 }