Browse Source

Playfield basics

Rick Novile 1 month ago
parent
commit
cef3402c78
11 changed files with 103 additions and 167 deletions
  1. 1
    1
      makefile
  2. 13
    10
      src/bag.c
  3. 1
    1
      src/main.c
  4. 17
    3
      src/piece.c
  5. 46
    0
      src/playfield.c
  6. 15
    0
      src/playfield.h
  7. 0
    17
      src/standard.tvb
  8. 0
    12
      src/standard.tvp
  9. 3
    1
      src/string.c
  10. 7
    4
      src/structs.h
  11. 0
    118
      tvp format.txt

+ 1
- 1
makefile View File

@@ -10,7 +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 bag.o
13
+_OBJS += piece.o string.o util.o bag.o playfield.o
14 14
 
15 15
 OBJS = $(patsubst %,$(OUT)/%,$(_OBJS))
16 16
 

+ 13
- 10
src/bag.c View File

@@ -1,11 +1,14 @@
1 1
 #include "bag.h"
2 2
 
3
+//TODO: get_bag_m, Rewrite get_bag_piece to take a second argument int index representing the index of the piece desired, to allow for access of arbitrary pieces in the bag
4
+
5
+
3 6
 int get_bag_s(Bag* bag) {
4 7
     return substring_to_int(bag->data, 1, 2);
5 8
 }
6 9
 
7 10
 int get_bag_i(Bag* bag) {
8
-    return bag->index;
11
+    return bag->state[0];
9 12
 }
10 13
 
11 14
 int get_bag_q(Bag* bag) {
@@ -13,14 +16,14 @@ int get_bag_q(Bag* bag) {
13 16
 }
14 17
 
15 18
 void set_bag_i(Bag* bag, int input) {
16
-    bag->index = input;
19
+    bag->state[0] = input;
17 20
 }
18 21
 
19 22
 void inc_bag_i(Bag* bag) {
20
-    bag->index++;
23
+    bag->state[0]++;
21 24
 }
22 25
 
23
-char* get_piece(Bag* bag) {
26
+char* get_bag_piece(Bag* bag) {
24 27
     int piece_index = get_bag_q(bag);
25 28
     int piece_states = substring_to_int(bag->data, piece_index + 3, piece_index + 4);
26 29
     int piece_size = substring_to_int(bag->data, piece_index + 5, piece_index + 6);
@@ -29,20 +32,20 @@ char* get_piece(Bag* bag) {
29 32
     return piece_str;
30 33
 }
31 34
 
32
-char* draw_piece(Bag* bag) {
35
+char* draw_bag_piece(Bag* bag) {
33 36
     int size = get_bag_s(bag);
34 37
     int index = get_bag_i(bag);
35
-    char* piece = get_piece(bag);
38
+    char* piece = get_bag_piece(bag);
36 39
     if(index + 1 >= size) {
37 40
         set_bag_i(bag, 0);
38
-        randomize_sequence(bag);
41
+        randomize_bag_sequence(bag);
39 42
     } else {
40 43
         inc_bag_i(bag);
41 44
     }
42 45
     return piece;
43 46
 }
44 47
 
45
-void randomize_sequence(Bag* bag) {
48
+void randomize_bag_sequence(Bag* bag) {
46 49
     int size = get_bag_s(bag);
47 50
     for(int first_index = 0; first_index < size - 1; first_index++) {
48 51
         int second_index = first_index + rand() / (RAND_MAX / (size - first_index) + 1);
@@ -53,7 +56,7 @@ void randomize_sequence(Bag* bag) {
53 56
 }
54 57
 
55 58
 void build_bag(Bag* bag, char* input) {
56
-    bag->index = 0;
59
+    bag->state[0] = 0;
57 60
     bag->data = input;
58 61
     int bag_size = get_bag_s(bag);
59 62
     bag->sequence = malloc(sizeof(int) * bag_size);
@@ -64,5 +67,5 @@ void build_bag(Bag* bag, char* input) {
64 67
         bag->sequence[current_piece] = current_index;
65 68
         current_index += states * size * size + 7;
66 69
     }
67
-    randomize_sequence(bag);
70
+    randomize_bag_sequence(bag);
68 71
 }

+ 1
- 1
src/main.c View File

@@ -21,7 +21,7 @@
21 21
 
22 22
 int main(int argc, char** argv) {
23 23
     srand(time(NULL));
24
-    char* test_bag_data = "0030070403001111000010010011000111100110010010005040301011100001001101000011101001011001000401021111";
24
+    char* test_bag_data = "007004010211110060404000011110000000000100010001000100000000011110000010001000100010000504030101110000100110100001110100101100100020403011110000010011001000011110100110010001040311001100000101101000011001101011010000304031001110000110100100001110010100101100070403001111000010010011000111100110010010";
25 25
     Bag test_bag;
26 26
     build_bag(&test_bag, test_bag_data);
27 27
     char* test_piece = get_piece(&test_bag);

+ 17
- 3
src/piece.c View File

@@ -1,9 +1,22 @@
1 1
 #include "piece.h"
2 2
 
3
+//Piece Data Values
4
+//m : Mode Switch
5
+//c : Color/Texture
6
+//s : Rotation States Count
7
+//z : Bounding Box Size
8
+
9
+//Piece State Values
10
+//x : x-position
11
+//y : y-position
12
+//r : Current Rotation State
13
+//v : Mode Override
14
+//o : Color/Texture Override
15
+
3 16
 //TODO: Add rotation functions to piece.h
4 17
 
5 18
 //Given the piece data and piece name, sets the relevant data for the given piece object
6
-void build_piece(Piece* piece, char* data_str) {
19
+void build_piece(Piece* piece, char* input) {
7 20
     //piece->name = name;
8 21
     int parsed_data[4];
9 22
     for(int data_index = 0; data_index <= 3; data_index++) {
@@ -12,15 +25,16 @@ void build_piece(Piece* piece, char* data_str) {
12 25
         if(range_start < 0) {
13 26
             range_start = 0;
14 27
         }
15
-        insert_int(parsed_data, substring_to_int(data_str, range_start, range_end), data_index);
28
+        insert_int(parsed_data, substring_to_int(input, range_start, range_end), data_index);
16 29
     }
17 30
     piece->data = parsed_data;
18 31
     size_t matrix_letters = piece->data[2] * piece->data[3] * piece->data[3];
19 32
     size_t matrix_size = matrix_letters * CHAR_SIZE;
20 33
     piece->matrix = malloc(matrix_size);
21 34
     memset(piece->matrix, '\0', matrix_size);
35
+    //TODO: Fix this for loop's existence. This should probably be a call to strncpy() or similar
22 36
     for(int current_position = 0; current_position < matrix_letters; current_position++) {
23
-        piece->matrix[current_position] = data_str[7 + current_position];
37
+        piece->matrix[current_position] = input[7 + current_position];
24 38
     }
25 39
 }
26 40
 

+ 46
- 0
src/playfield.c View File

@@ -0,0 +1,46 @@
1
+#include "playfield.h"
2
+ 
3
+//Playfield Data Values
4
+//m : Mode Switch; currently unused
5
+//w : Width 
6
+//h : Height 
7
+
8
+int get_playfield_m(Playfield* playfield) {
9
+    return playfield->data[0];
10
+}
11
+
12
+int get_playfield_w(Playfield* playfield) {
13
+    return playfield->data[1];
14
+}
15
+
16
+int get_playfield_h(Playfield* playfield) {
17
+    return playfield->data[2];
18
+}
19
+
20
+int get_playfield_cell(Playfield* playfield, int x, int y) {
21
+    if(!(x >= get_playfield_w(playfield) || y >= get_playfield_h(playfield))) {
22
+        int pos = get_playfield_w(playfield) * y + x;
23
+        return substring_to_int(playfield->state, pos, pos);
24
+    }
25
+    return -1;
26
+}
27
+
28
+void set_playfield_cell(Playfield* playfield, int x, int y, int input) {
29
+    if(!(x >= get_playfield_w(playfield) || y >= get_playfield_h(playfield))) {
30
+        int pos = get_playfield_w(playfield) * y + x;
31
+        //TODO: Would be better with an "int_to_char" function instead
32
+        playfield->state[pos] = int_to_str(input)[0];
33
+    }
34
+}
35
+
36
+void build_playfield(Playfield* playfield, char* input) {
37
+    //TODO: Would be better with a "char_to_int" function instead
38
+    //ADD: Actually... would it?
39
+    playfield->data[0] = substring_to_int(input, 0, 0);
40
+    playfield->data[1] = substring_to_int(input, 1, 3);
41
+    playfield->data[2] = substring_to_int(input, 4, 6);
42
+    int playfield_size = CHAR_SIZE * get_playfield_w(playfield) * get_playfield_h(playfield);
43
+    playfield->state = malloc(playfield_size);
44
+    memset(playfield->state, '\0', playfield_size);
45
+}
46
+

+ 15
- 0
src/playfield.h View File

@@ -0,0 +1,15 @@
1
+#ifndef tivien_playfield_h
2
+#define tivien_playfield_h
3
+
4
+#include "string.h"
5
+#include "common.h"
6
+
7
+int get_playfield_m(Playfield* playfield);
8
+int get_playfield_w(Playfield* playfield);
9
+int get_playfield_h(Playfield* playfield);
10
+
11
+int get_playfield_cell(Playfield* playfield, int x, int y);
12
+void set_playfield_cell(Playfield* playfield, int x, int y, int input);
13
+
14
+void build_playfield(Playfield* playfield, char* input);
15
+#endif

+ 0
- 17
src/standard.tvb View File

@@ -1,17 +0,0 @@
1
-MSSDDD...DD/IIQqQqQq
2
-
3
-M - Mode
4
-SS - Size 
5
-DDD...DD - Piece
6
-II - Index
7
-QqQqQq - Sequence
8
-
9
-0 03 0070403001111000010010011000111100110010010 0050403010111000010011010000111010010110010 00401021111
10
-
11
-start @ 3
12
-get_substring[6..7] = states
13
-get_substring[8..9] = size
14
-
15
-
16
-
17
-3 A B

+ 0
- 12
src/standard.tvp View File

@@ -1,12 +0,0 @@
1
-i0: 0000000
2
-i1: 00601011
3
-i2: 00604021100010100111010
4
-i3: 0060203000111000010010010
5
-l3: 00704021011111011010111
6
-o4: 00401021111
7
-i4: 00604040000111100000000001000100010001000000000111100000100010001000100
8
-t4: 0050403010111000010011010000111010010110010
9
-s4: 0020403011110000010011001000011110100110010
10
-z4: 0010403110011000001011010000110011010110100
11
-j4: 0030403100111000011010010000111001010010110
12
-l4: 0070403001111000010010011000111100110010010

+ 3
- 1
src/string.c View File

@@ -1,5 +1,7 @@
1 1
 #include "string.h"
2 2
 
3
+//TODO: char_to_int (cf. playfield.c::build_playfield), int_to_char (cf. playfield.c::set_playfield_pos)
4
+
3 5
 //Takes a string as input, and converts it into the int it represents
4 6
 //Only functions if all characters in the input are 0-9
5 7
 int str_to_int(char* input) {
@@ -31,7 +33,7 @@ void add_char(char* string, char addition) {
31 33
     strncpy(string, holder, new_size);
32 34
 }
33 35
 
34
-//Returns an array of the values string[low] to string[high]
36
+//Returns an array of the chars string[low] to string[high]
35 37
 char* get_substring(char* string, int low, int high) {
36 38
     size_t output_size = CHAR_SIZE * (high - low + 1);
37 39
     char* output = malloc(output_size);

+ 7
- 4
src/structs.h View File

@@ -1,6 +1,9 @@
1 1
 #ifndef tivien_structs_h
2 2
 #define tivien_structs_h
3 3
 
4
+//Data: Does *not* change; Is required for building an item
5
+//State: *Does* change; Is not required for building an item
6
+
4 7
 typedef struct {
5 8
     //char* name;
6 9
     char* matrix;
@@ -11,13 +14,13 @@ typedef struct {
11 14
 typedef struct {
12 15
     //char* name;
13 16
     char* data;
14
-    int index;
17
+    int* state; //Currently only contains the index, but int* just for future proofing
15 18
     int* sequence; //The integers here represent the index value of the start of each piece
16 19
 } Bag;
17 20
 
18 21
 typedef struct {
19 22
     //char* name;
20
-    char* data;
23
+    int* data;
21 24
     char* state;
22 25
 } Playfield;
23 26
 
@@ -31,8 +34,8 @@ typedef struct {
31 34
     Piece hold;
32 35
     Bag bag;
33 36
     Playfield playfield;
34
-    Ruleset ruleset;
35
-    char* name;
37
+    //Ruleset ruleset;
38
+    //char* name;
36 39
     char* state;
37 40
 } Game;
38 41
 

+ 0
- 118
tvp format.txt View File

@@ -1,118 +0,0 @@
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