Browse Source

Lotsa shit

Rick Novile 5 months ago
parent
commit
6dab7e8951
13 changed files with 457 additions and 45 deletions
  1. 1
    0
      makefile
  2. 12
    0
      src/common.h
  3. 6
    0
      src/defines.h
  4. 9
    39
      src/main.c
  5. 125
    0
      src/piece.c
  6. 40
    0
      src/piece.h
  7. 1
    0
      src/standard.tvp
  8. 76
    0
      src/string.c
  9. 15
    0
      src/string.h
  10. 25
    6
      src/structs.h
  11. 24
    0
      src/util.c
  12. 5
    0
      src/util.h
  13. 118
    0
      tvp format.txt

+ 1
- 0
makefile View File

@@ -10,6 +10,7 @@ vpath %.c $(SEARCHPATH)
10 10
 vpath %.h $(SEARCHPATH)
11 11
 
12 12
 _OBJS += main.o
13
+_OBJS += piece.o string.o util.o
13 14
 
14 15
 OBJS = $(patsubst %,$(OUT)/%,$(_OBJS))
15 16
 

+ 12
- 0
src/common.h View File

@@ -0,0 +1,12 @@
1
+#ifndef tivien_common_h
2
+#define tivien_common_h
3
+
4
+#include <stdio.h>
5
+#include <stdlib.h>
6
+#include <string.h>
7
+#include <limits.h>
8
+
9
+#include "structs.h"
10
+#include "defines.h"
11
+
12
+#endif

+ 6
- 0
src/defines.h View File

@@ -0,0 +1,6 @@
1
+#ifndef tivien_defines_h
2
+#define tivien_defines_h
3
+
4
+#define CHAR_SIZE sizeof(char)
5
+
6
+#endif

+ 9
- 39
src/main.c View File

@@ -1,43 +1,13 @@
1
-#include <stdio.h>
2
-#include <stdlib.h>
3
-#include <string.h>
4
-
5
-void set_x(char* state, int x) {
6
-    char new_x[3];
7
-    memset(new_x, '\0', sizeof(new_x));
8
-    sprintf(new_x, "%i", x);
9
-    if(x < 10) {
10
-        state[2] = new_x[0];
11
-        state[1] = '0';
12
-        state[0] = '0';
13
-    } else if(x < 100) {
14
-        state[2] = new_x[1];
15
-        state[1] = new_x[0];
16
-        state[0] = '0';
17
-    } else {
18
-        state[2] = new_x[2];
19
-        state[1] = new_x[1];
20
-        state[0] = new_x[0];
21
-    }
22
-}
23
-
24
-int get_x(char* state) {
25
-    char x[3];
26
-    memset(x, '\0', sizeof(x));
27
-    char* ptr;
28
-    strncpy(x, state, 3);
29
-    int output = (int)strtol(x, &ptr, 10);
30
-    return output;
31
-}
1
+//TODO: 
2
+//[Possibly Unneeded Now] merge_int_arr
32 3
 
4
+#include "string.h"
5
+#include "util.h"
6
+#include "piece.h"
7
+#include "structs.h"
8
+#include "defines.h"
9
+#include "common.h"
33 10
 
34 11
 int main(int argc, char** argv) {
35
-    char* prewritten = "00501102000";
36
-    char* state = malloc(sizeof(char) * 11);
37
-    strncpy(state, prewritten, 11);
38
-    printf("%s\n", state);
39
-    set_x(state, 457);
40
-    printf("%s\n", state);
41
-    free(state);
42
-    return 0;
12
+
43 13
 }

+ 125
- 0
src/piece.c View File

@@ -0,0 +1,125 @@
1
+#include "piece.h"
2
+
3
+//Given the piece data and piece name, sets the relevant data for the given piece object
4
+void build_piece(Piece* piece, char* data_str) {
5
+    //piece->name = name;
6
+    int parsed_data[4];
7
+    for(int data_index = 0; data_index <= 3; data_index++) {
8
+        int range_end = data_index * 2;
9
+        int range_start = range_end - 1;
10
+        if(range_start < 0) {
11
+            range_start = 0;
12
+        }
13
+        insert_int(parsed_data, substring_to_int(data_str, range_start, range_end), data_index);
14
+    }
15
+    piece->data = parsed_data;
16
+    size_t matrix_letters = piece->data[2] * piece->data[3] * piece->data[3];
17
+    size_t matrix_size = matrix_letters * CHAR_SIZE;
18
+    piece->matrix = malloc(matrix_size);
19
+    memset(piece->matrix, '\0', matrix_size);
20
+    for(int current_position = 0; current_position < matrix_letters; current_position++) {
21
+        piece->matrix[current_position] = data_str[7 + current_position];
22
+    }
23
+}
24
+
25
+int get_x(Piece* piece) {
26
+    return piece->state[0];
27
+}
28
+
29
+int get_y(Piece* piece) {
30
+    return piece->state[1];
31
+}
32
+
33
+int get_r(Piece* piece) {
34
+    return piece->state[2];
35
+}
36
+
37
+int get_v(Piece* piece) {
38
+    return piece->state[3];
39
+}
40
+
41
+int get_o(Piece* piece) {
42
+    return piece->state[4];
43
+}
44
+
45
+int get_m(Piece* piece) {
46
+    return piece->data[0];
47
+}
48
+
49
+int get_c(Piece* piece) {
50
+    return piece->data[1];
51
+}
52
+
53
+int get_s(Piece* piece) {
54
+    return piece->data[2];
55
+}
56
+int get_z(Piece* piece) {
57
+    return piece->data[3];
58
+}
59
+
60
+void set_x(Piece* piece, int input) {
61
+    piece->state[0] = input;
62
+}
63
+
64
+void set_y(Piece* piece, int input) {
65
+    piece->state[1] = input;
66
+}
67
+
68
+void set_r(Piece* piece, int input) {
69
+    piece->state[2] = input;
70
+}
71
+
72
+void set_v(Piece* piece, int input) {
73
+    piece->state[3] = input;
74
+}
75
+
76
+void set_o(Piece* piece, int input) {
77
+    piece->state[4] = input;
78
+}
79
+
80
+void set_m(Piece* piece, int input) {
81
+    piece->data[0] = input;
82
+}
83
+
84
+void set_c(Piece* piece, int input) {
85
+    piece->data[1] = input;
86
+}
87
+
88
+void set_s(Piece* piece, int input) {
89
+    piece->data[2] = input;
90
+}
91
+
92
+void set_z(Piece* piece, int input) {
93
+    piece->data[3] = input;
94
+}
95
+
96
+void inc_x(Piece* piece) {
97
+    set_x(piece, get_x(piece) + 1);
98
+}
99
+
100
+void inc_y(Piece* piece) {
101
+    set_y(piece, get_y(piece) + 1);
102
+}
103
+
104
+void inc_r(Piece* piece) {
105
+    set_r(piece, get_r(piece) + 1);
106
+}
107
+
108
+void dec_x(Piece* piece) {
109
+    set_x(piece, get_x(piece) - 1);
110
+}
111
+
112
+void dec_y(Piece* piece) {
113
+    set_y(piece, get_y(piece) - 1);
114
+}
115
+
116
+void dec_r(Piece* piece) {
117
+    set_r(piece, get_r(piece) - 1);
118
+}
119
+
120
+int get_cell(Piece* piece, int x, int y, int r) {
121
+    int size = get_z(piece);
122
+    int cell_index = (size * size * r) + (size * y) + x;
123
+    char output = piece->matrix[cell_index] - '0';
124
+    return (int)output;
125
+}

+ 40
- 0
src/piece.h View File

@@ -0,0 +1,40 @@
1
+#ifndef tivien_piece_h
2
+#define tivien_piece_h
3
+
4
+#include "common.h"
5
+#include "util.h"
6
+#include "string.h"
7
+
8
+void build_piece(Piece* piece, char* data_str);
9
+
10
+int get_x(Piece* piece);
11
+int get_y(Piece* piece);
12
+int get_r(Piece* piece);
13
+int get_v(Piece* piece);
14
+int get_o(Piece* piece);
15
+int get_m(Piece* piece);
16
+int get_c(Piece* piece);
17
+int get_s(Piece* piece);
18
+int get_z(Piece* piece);
19
+
20
+void set_x(Piece* piece, int input);
21
+void set_y(Piece* piece, int input);
22
+void set_r(Piece* piece, int input);
23
+void set_v(Piece* piece, int input);
24
+void set_o(Piece* piece, int input);
25
+void set_m(Piece* piece, int input);
26
+void set_c(Piece* piece, int input);
27
+void set_s(Piece* piece, int input);
28
+void set_z(Piece* piece, int input);
29
+
30
+void inc_x(Piece* piece);
31
+void inc_y(Piece* piece);
32
+void inc_r(Piece* piece);
33
+
34
+void dec_x(Piece* piece);
35
+void dec_y(Piece* piece);
36
+void dec_r(Piece* piece);
37
+
38
+int get_cell(Piece* piece, int x, int y, int r)
39
+
40
+#endif

src/standard.pcs → src/standard.tvp View File

@@ -1,3 +1,4 @@
1
+i0: 0000000
1 2
 i1: 00601011
2 3
 i2: 00604021100010100111010
3 4
 i3: 0060203000111000010010010

+ 76
- 0
src/string.c View File

@@ -0,0 +1,76 @@
1
+#include "string.h"
2
+
3
+//Takes a string as input, and converts it into the int it represents
4
+//Only functions if all characters in the input are 0-9
5
+int str_to_int(char* input) {
6
+    char* unused;
7
+    int output = (int)strtol(input, &unused, 10);
8
+    return output;
9
+}
10
+
11
+//Returns a char* representing the string for the number
12
+//e.g. an int of 126 is returned as a char* of form {'1', '2', '6'}
13
+char* int_to_str(int input) {
14
+    size_t size = count_characters(input) * CHAR_SIZE;
15
+    char* output = malloc(size);
16
+    memset(output, '\0', size);
17
+    sprintf(output, "%i", input);
18
+    return output;
19
+}
20
+
21
+//Adds char addition to the end of char* string (increasing its length by one)
22
+void add_char(char* string, char addition) {
23
+    size_t letter_count = strlen(string);
24
+    size_t old_size = letter_count * CHAR_SIZE;
25
+    size_t new_size = old_size + CHAR_SIZE;
26
+    char* holder = malloc(old_size);
27
+    memset(holder, '\0', new_size);
28
+    strncpy(holder, string, old_size);
29
+    holder[letter_count] = addition;
30
+    string = realloc(string, new_size);
31
+    strncpy(string, holder, new_size);
32
+}
33
+
34
+
35
+//Returns an array of the values string[low] to string[high]
36
+char* get_substring(char* string, int low, int high) {
37
+    size_t output_size = CHAR_SIZE * (high - low + 1);
38
+    char* output = malloc(output_size);
39
+    memset(output, '\0', output_size);
40
+    for(int current_char = low; current_char <= high; current_char++) {
41
+        output[current_char - low] = string[current_char];
42
+    }
43
+    return output;
44
+}
45
+
46
+//Sets strlen(replacement) values starting from start_index into what's represented in replacement
47
+//e.g., set_substring("hello", "ome", 1) changes string into "homeo"
48
+void set_substring(char* string, char* replacement, int start_index) {
49
+    for(int current_position = start_index; current_position <= strlen(replacement); current_position++) {
50
+        string[current_position] = replacement[current_position - start_index];
51
+    }
52
+} 
53
+
54
+//Gets the substring string[low] to string[high], and then returns its representation as an int
55
+//Only works if the specified substring is 0-9 only
56
+int substring_to_int(char* string, int low, int high) {
57
+    int output;
58
+    char* substring = get_substring(string, low, high);
59
+    output = str_to_int(substring);
60
+    return output;
61
+}
62
+
63
+//Appends addition to the end of destination
64
+void append_str(char* destination, char* addition) {
65
+    size_t dest_size = strlen(destination);
66
+    size_t add_size = strlen(addition);
67
+    size_t new_size = CHAR_SIZE * (dest_size + add_size);
68
+    char* holder = malloc(new_size);
69
+    memset(holder, '\0', new_size);
70
+    strncpy(holder, destination, dest_size);
71
+    for(int current_char = 0; current_char < add_size; current_char++) {
72
+        holder[dest_size + current_char] = addition[current_char];
73
+    }
74
+    destination = realloc(destination, new_size);
75
+    strncpy(destination, holder, new_size);
76
+}

+ 15
- 0
src/string.h View File

@@ -0,0 +1,15 @@
1
+#ifndef tivien_string_h
2
+#define tivien_string_h
3
+
4
+#include "common.h"
5
+#include "util.h"
6
+
7
+int str_to_int(char* input);
8
+char* int_to_str(int input);
9
+void add_char(char* string, char addition);
10
+char* get_substring(char* string, int low, int high);
11
+void set_substring(char* string, char* replacement, int start_index);
12
+int substring_to_int(char* string, int low, int high);
13
+void append_str(char* destination, char* addition);
14
+
15
+#endif

+ 25
- 6
src/structs.h View File

@@ -2,19 +2,38 @@
2 2
 #define tivien_structs_h
3 3
 
4 4
 typedef struct {
5
-    char* name, matrix;
6
-    int x, y, r, color, state, size, mode;
5
+    //char* name;
6
+    char* matrix;
7
+    int* data;
8
+    int* state;
7 9
 } Piece;
8 10
 
9 11
 typedef struct {
10
-    Piece* pieces;
11
-    int size, max;
12
+    //char* name;
13
+    char* data;
14
+    char* state;
15
+    int* sequence; //The integers here represent the index value of the start of each piece
12 16
 } Bag;
13 17
 
14 18
 typedef struct {
19
+    //char* name;
20
+    char* data;
21
+    char* state;
22
+} Playfield;
23
+
24
+typedef struct {
25
+    char* name;
26
+    char* data;
27
+} Ruleset;
28
+
29
+typedef struct {
30
+    Piece current;
31
+    Piece hold;
15 32
     Bag bag;
16
-    Piece current, hold;
17
-    //Playfield playfield;
33
+    Playfield playfield;
34
+    Ruleset ruleset;
35
+    char* name;
36
+    char* state;
18 37
 } Game;
19 38
 
20 39
 #endif

+ 24
- 0
src/util.c View File

@@ -0,0 +1,24 @@
1
+#include "util.h"
2
+
3
+size_t count_digits(int input) {
4
+    size_t output;
5
+    input = abs(input);
6
+    for(int power_of_ten = 1; power_of_ten <= input; power_of_ten *= 10) {
7
+        output++;
8
+    }
9
+    return output;
10
+}
11
+
12
+size_t count_characters(int input) {
13
+    size_t output;
14
+    if(input < 0) {
15
+        output++;
16
+    }
17
+    output += count_digits(input);
18
+    return output;
19
+}
20
+
21
+//Inserts addition at index in array
22
+void insert_int(int* array, int addition, int index) {
23
+    array[index] = addition;
24
+}

+ 5
- 0
src/util.h View File

@@ -0,0 +1,5 @@
1
+#include "common.h"
2
+
3
+size_t count_digits(int input);
4
+size_t count_characters(int input);
5
+void insert_int(int* array, int addition, int index);

+ 118
- 0
tvp format.txt View File

@@ -0,0 +1,118 @@
1
+.tvp Format:
2
+
3
+A .tvp file represents a set of pieces, with each line representing a different piece.
4
+Each piece is required to have a name (in the name area) and a full standard area (even if it's just full of 0s!)
5
+For more specialized behaviour, the standard area can be followed by a /, and then followed with an override state.
6
+This override state is simply an actual game logic state written out, which will be forced on the piece when spawned.
7
+Since it's the exact same format as the in-game logic states, some areas are unused in .tvp, but for ease of processing, all
8
+parts are required to be filled in some way (even if all 0s, indicating default state) if the override is used
9
+
10
+nnnnn: MCCSSZZDDDDDDDDD...DDD/XXXYYYRRVOO
11
+
12
+-NAME AREA-
13
+nnnn: Piece name (can be any length)
14
+
15
+-STANDARD AREA-
16
+M: Mode Switch
17
+    0 - Default Mode
18
+    1 - Colored Mode (Changes Data section to parse values 0-9 then a-z to specify block-by-block color/texture)
19
+CC: Piece Color/Texture (default mode)
20
+    0 - Blank
21
+    1 - Red 
22
+    2 - Green 
23
+    3 - Blue 
24
+    4 - Yellow 
25
+    5 - Purple 
26
+    6 - Cyan 
27
+    7 - Orange 
28
+SS - Number of unique rotation states (Almost always 1, 2, or 4)
29
+ZZ - Width/Height of Bounding Square (Only one number since it's a square)
30
+DDDD... - Data section, contains piece data (varies in length according to state count and size)
31
+
32
+-OVERRIDE AREA (NOT required)-
33
+XXX: Spawn X-coordinate
34
+YYY: Spawn Y-coordinate
35
+RR: Spawn rotation state
36
+V: Mode override (irrelvant for .tvp; should always be 0)
37
+OO: Color override (irrelevant for .tvp; should always be 00)
38
+
39
+
40
+given x, y, r (rotation state) coordinates, all starting at 0:
41
+always: add 7 (due to header at start of string)
42
+for each x: add 1
43
+for each y: add piece.size
44
+for each r: add piece.size * piece.size
45
+
46
+size of "piece" in data: 8 + (SS * ZZ * ZZ)
47
+size of "state" in data: 11
48
+
49
+mode m = data[0]
50
+color c = data[1..2]
51
+states s = data[3..4]
52
+size z = data[5..6]
53
+matrix m = data[7..(states * size * size)]
54
+
55
+x = state[0..2]
56
+y = state[3..5]
57
+r = state[6..7]
58
+mode-override v = state[8]
59
+color-override o = state[9..10]
60
+
61
+spawn x = 5 - (ZZ/2) + (ZZ%2)
62
+note: integer division in C returns the integer part of the division result (e.g. 3/2 = 1), hence the +(ZZ%2)
63
+
64
+
65
+
66
+ char* test = "00604040000111100000000001000100010001000000000111100000100010001000100";
67
+    char get[3];
68
+    memset(get, '\0', sizeof(get));
69
+    char* ptr;
70
+    strncpy(get, test + 5, 2);
71
+    int end = (int)strtol(get, &ptr, 10);
72
+    printf("%i\n",end);
73
+
74
+//prints "4"
75
+
76
+potential editable rules:
77
+Spawn Delay (on/off, length)
78
+Autoshift Delay (on/off, length)
79
+Autorepeat (on/off, length)
80
+IRS +l
81
+IHS +l 
82
+lock delay +l
83
+initial g
84
+max g
85
+move reset (on/off, length, something re: infinity)
86
+next (on/off, amount)
87
+hold (on/off)
88
+ghost
89
+
90
+
91
+bag: MSSDDD...DD/IIQqQqQq..QqQq 
92
+
93
+M - mode 
94
+S - size (number of pieces) 
95
+D - pieces 
96
+I - current index 
97
+Qq - piece sequence (double digits, each "Qq" is one 2 digit number)
98
+
99
+
100
+
101
+playfield: MWWWHHH/DDD...DD 
102
+
103
+M - mode
104
+W - width 
105
+H - height 
106
+D - playfield data (if M > 0, then DdDdDd...DdDd; double digits like Qq above)
107
+
108
+
109
+
110
+game: MPPPPPPPPPPRRR...RRBBB...BB/CCC..CCHHH...HHFFF...FF
111
+
112
+M - mode 
113
+P - points 
114
+R - ruleset 
115
+B - bag w/ state data (or blank state if none)
116
+C - current piece in play w/ state data (or i0 if none)
117
+H - hold piece (or i0 if none)
118
+F - playfield w/ state data

Loading…
Cancel
Save