summaryrefslogtreecommitdiffstats
path: root/ksokoban/InternalCollections.cpp
blob: cdc3dbb3d517b3aa436ec6a2ba8f5a7beb87c67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <klocale.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#ifdef USE_LIBZ
#include <zlib.h>
#endif

#include "InternalCollections.h"

#ifndef LEVELS_INCLUDED
#define LEVELS_INCLUDED 1
#include "levels/data.c"
#endif

#define BUFSIZE (128*1024)

// static const int collection_save_id[] = {
//   0, 1, 3, 5, 9, 6, 7, 8, 2, 4
// };

static const int collection_save_id[] = {
  10, 11, 12, 13, 14
};

int
InternalCollections::configCollection2Real (int collection) {
  for (int i=0; i < (int) (sizeof (collection_save_id) / sizeof (int)); i++) {
    if (collection_save_id[i] == collection) return i;
  }
  return 0;
}

int
InternalCollections::realCollection2Config(int collection) {
  assert(collection < (int) (sizeof (collection_save_id) / sizeof (int)));
  return collection_save_id[collection];
}

QString
InternalCollections::collectionName(int _level) {
  switch (_level) {
  case 0:
    return i18n("Sasquatch");
    break;

  case 1:
    return i18n("Mas Sasquatch");
    break;

  case 2:
    return i18n("Sasquatch III");
    break;

  case 3:
    return i18n("Microban (easy)");
    break;

  case 4:
    return i18n("Sasquatch IV");
    break;
  }

  assert(false);
  return QString();
}


InternalCollections::InternalCollections() {
  int datasize, levelnum=0;

#ifdef USE_LIBZ
  data_ = (char *) malloc(BUFSIZE);
  if (data_ == NULL) abort();

  datasize = BUFSIZE;
  uncompress ((unsigned char *) data_, (long unsigned int *) &datasize, level_data_, sizeof (level_data_));
  data_ = (char *) realloc(data_, datasize);
  if (data_ == NULL) abort ();
#else
  datasize = sizeof (level_data_);
  data_ = (char *) malloc(datasize);
  if (data_ == NULL) abort();
  memcpy(data_, level_data_, datasize);
#endif

  int start=0, end=0, name=0;
  enum {NAME, DATA} state=NAME;
  while (end < datasize) {
    switch (state) {
    case NAME:
      if (data_[end] == '\n') {
	data_[end] = '\0';
	state = DATA;
      }
      end++;
      start = end;
      break;

    case DATA:
      if (isalpha(data_[end])) {
// 	collections_.add(new LevelCollection(data_+start, end-start, data_+name, collection_save_id[levelnum]));
	add(new LevelCollection(data_+start, end-start, collectionName(levelnum), collection_save_id[levelnum]));
	//printf("Level found: '%s'\n", data_+name);
	levelnum++;
	name = end;
	state = NAME;
      }
      end++;
      break;

    default:
      assert(0);
    }
  }
  if (state == DATA) {
//     collections_.add(new LevelCollection(data_+start, end-start, data_+name, collection_save_id[levelnum]));
    add(new LevelCollection(data_+start, end-start, collectionName(levelnum), collection_save_id[levelnum]));
    //printf("***Level found: '%s'\n", data_+name);
  }
  //printf("numlevels: %d/%d\n", levelnum+1, collections_.size());
}

InternalCollections::~InternalCollections() {
  for (unsigned i=0; i<collections_.size(); i++) {
    delete collections_[i];
  }

  free(data_);
}

int
InternalCollections::collections() {
  return collections_.size();
}

LevelCollection *
InternalCollections::operator[](int n) {
  return collections_[n];
}

void
InternalCollections::add(LevelCollection* c) {
  unsigned s = collections_.size();
  collections_.resize(s + 1);
  collections_.insert(s, c);
}