project_files/frontlib/model/map.c
author Jens Petersen
Mon, 12 Sep 2022 12:50:37 -0400
branch1.0.0
changeset 15883 9d4ba1912e71
parent 10017 de822cd3df3a
permissions -rw-r--r--
build fix from https://bugzilla.redhat.com/show_bug.cgi?id=1853122
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    20
#include "map.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    22
#include "../util/inihelper.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    23
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    24
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    25
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    26
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    27
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
flib_map *flib_map_create_regular(const char *seed, const char *theme, int templateFilter) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
    if(log_badargs_if2(seed==NULL, theme==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
    flib_map newmap = {0};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
    newmap.mapgen = MAPGEN_REGULAR;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    34
    newmap.name = "+rnd+";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    35
    newmap.seed = (char*)seed;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    36
    newmap.theme = (char*)theme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
    newmap.templateFilter = templateFilter;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
    return flib_map_copy(&newmap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
flib_map *flib_map_create_maze(const char *seed, const char *theme, int mazeSize) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
    if(log_badargs_if2(seed==NULL, theme==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
    flib_map newmap = {0};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
    newmap.mapgen = MAPGEN_MAZE;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
    newmap.name = "+maze+";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
    newmap.seed = (char*)seed;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
    newmap.theme = (char*)theme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
    newmap.mazeSize = mazeSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
    return flib_map_copy(&newmap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
flib_map *flib_map_create_named(const char *seed, const char *name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
    if(log_badargs_if2(seed==NULL, name==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
    flib_map newmap = {0};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
    newmap.mapgen = MAPGEN_NAMED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
    newmap.name = (char*)name;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
    newmap.seed = (char*)seed;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
    return flib_map_copy(&newmap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
flib_map *flib_map_create_drawn(const char *seed, const char *theme, const uint8_t *drawData, size_t drawDataSize) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
    if(log_badargs_if3(seed==NULL, theme==NULL, drawData==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
    flib_map newmap = {0};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
    newmap.mapgen = MAPGEN_DRAWN;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
    newmap.name = "+drawn+";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
    newmap.seed = (char*)seed;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
    newmap.theme = (char*)theme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
    newmap.drawData = (uint8_t*) drawData;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
    newmap.drawDataSize = drawDataSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
    return flib_map_copy(&newmap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    77
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    78
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    79
flib_map *flib_map_copy(const flib_map *map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
    flib_map *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
    if(map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
        flib_map *newmap = flib_calloc(1, sizeof(flib_map));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
        if(newmap) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
            newmap->mapgen = map->mapgen;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
            newmap->drawDataSize = map->drawDataSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
            newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
            newmap->mazeSize = map->mazeSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
            newmap->name = flib_strdupnull(map->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
            newmap->seed = flib_strdupnull(map->seed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
            newmap->templateFilter = map->templateFilter;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91
            newmap->theme = flib_strdupnull(map->theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    92
            if((newmap->drawData || !map->drawData) && (newmap->name || !map->name) && (newmap->seed || !map->seed) && (newmap->theme || !map->theme)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    93
                result = newmap;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    94
                newmap = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    95
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    96
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    97
        flib_map_destroy(newmap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    98
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    99
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   100
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   101
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   102
void flib_map_destroy(flib_map *map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   103
    if(map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   104
        free(map->seed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   105
        free(map->drawData);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   106
        free(map->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   107
        free(map->theme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   108
        free(map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   109
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   110
}