~tblue/+junk/biosim

« back to all changes in this revision

Viewing changes to src/field.c

  • Committer: Tilman Blumenbach
  • Date: 2010-02-23 17:20:30 UTC
  • Revision ID: tilman@ax86.net-20100223172030-9s951z97trfik0fs
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* field.c - Field functions.
 
2
 *
 
3
 * biosim - A simulator for the predator-prey relationship of aphids and
 
4
 *          ladybugs.
 
5
 *
 
6
 * Copyright (c) 2010, Tilman Blumenbach <tilman@ax86.net>
 
7
 * All rights reserved.
 
8
 *
 
9
 * Redistribution and use in source and binary forms, with or without
 
10
 * modification, are permitted provided that the following conditions are
 
11
 * met:
 
12
 *  - Redistributions of source code must retain the above copyright notice,
 
13
 *    this list of conditions and the following disclaimer.
 
14
 *  - Redistributions in binary form must reproduce the above copyright
 
15
 *    notice, this list of conditions and the following disclaimer in the
 
16
 *    documentation and/or other materials provided with the distribution.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
20
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 
22
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
23
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
24
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 
26
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
27
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
28
 * THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
#include <stdio.h>
 
32
 
 
33
#include "field.h"
 
34
#include "util.h"
 
35
#include "log.h"
 
36
 
 
37
int field_init( field_type *field )
 
38
{
 
39
        unsigned int aphid_chips = 15, ladybug_chips = 15, i;
 
40
        int rand_type, offset;
 
41
 
 
42
        for( i = 0; i < FIELD_SIZE; ++i )
 
43
        {
 
44
                field[i] = TYPE_NONE;
 
45
        }
 
46
 
 
47
        while( aphid_chips || ladybug_chips )
 
48
        {
 
49
                offset = rand_get( 0, FIELD_SIZE - 1 );
 
50
                if( offset == -1 )
 
51
                {
 
52
                        return -1;
 
53
                }
 
54
 
 
55
                LOGF_FILELINE( LOG_DEBUG, "A chips: %d, L chips: %d", aphid_chips, ladybug_chips );
 
56
                LOGF_FILELINE( LOG_DEBUG, "Field: %d -> [%X]", offset, field[offset] );
 
57
 
 
58
                if( field[offset] != TYPE_NONE )
 
59
                {       /* We only want to use empty fields. */
 
60
                        continue;
 
61
                }
 
62
 
 
63
                /* If we only have one kind of chips left, we do not need to get
 
64
                 * a random number.
 
65
                 */
 
66
                if( ! aphid_chips )
 
67
                {
 
68
                        rand_type = TYPE_LADYBUG;
 
69
                }
 
70
                else if( ! ladybug_chips )
 
71
                {
 
72
                        rand_type = TYPE_APHID;
 
73
                }
 
74
                else if( ( rand_type = rand_get( TYPE_NONE + 1, _TYPES_COUNT - 1 ) ) == -1 )
 
75
                {
 
76
                        return -1;
 
77
                }
 
78
 
 
79
                /* Determine which kind of chip we have placed. */
 
80
                switch( rand_type )
 
81
                {
 
82
                        case TYPE_APHID:
 
83
                                --aphid_chips;
 
84
                                break;
 
85
 
 
86
                        case TYPE_LADYBUG:
 
87
                                --ladybug_chips;
 
88
                                break;
 
89
 
 
90
                        default:
 
91
                                /* Should never happen. */
 
92
                                LOGF_FILELINE( LOG_ERROR, "Unhandled field type [%X].", rand_type );
 
93
                                return -1;
 
94
                }
 
95
 
 
96
                field[offset] = rand_type;
 
97
 
 
98
                LOGF_FILELINE( LOG_DEBUG, "Setting type: %d", rand_type );
 
99
        }
 
100
 
 
101
        return 0;
 
102
}
 
103
 
 
104
static void _field_horiz_line( void )
 
105
{
 
106
        unsigned int i;
 
107
 
 
108
        for( i = 0; i < FIELD_ROWS_COLS; ++i )
 
109
        {
 
110
                fputs( "+---", stderr );
 
111
        }
 
112
 
 
113
        fputs( "+\n", stderr );
 
114
}
 
115
 
 
116
void field_dump( const field_type *field )
 
117
{
 
118
        unsigned int i;
 
119
 
 
120
        for( i = 0; i < FIELD_SIZE; ++i )
 
121
        {
 
122
                if( i % FIELD_ROWS_COLS == 0 )
 
123
                {       /* new row */
 
124
                        if( i )
 
125
                        {
 
126
                                fputc( '\n', stderr );
 
127
                        }
 
128
 
 
129
                        _field_horiz_line();
 
130
                        fputc( '|', stderr );
 
131
                }
 
132
 
 
133
                fputc( ' ', stderr );
 
134
 
 
135
                switch( field[i] )
 
136
                {
 
137
                        case TYPE_NONE:
 
138
                                fputc( FIELD_CHR_NONE, stderr );
 
139
                                break;
 
140
 
 
141
                        case TYPE_APHID:
 
142
                                fputc( FIELD_CHR_APHID, stderr );
 
143
                                break;
 
144
 
 
145
                        case TYPE_LADYBUG:
 
146
                                fputc( FIELD_CHR_LADYBUG, stderr );
 
147
                                break;
 
148
 
 
149
                        default:
 
150
                                fputc( FIELD_CHR_UNKNOWN, stderr );
 
151
                                break;
 
152
                }
 
153
 
 
154
                fputs( " |", stderr );
 
155
        }
 
156
 
 
157
        fputc( '\n', stderr );
 
158
        _field_horiz_line();
 
159
}
 
160
 
 
161
int field_set_first( field_type *field, field_type old_type, field_type new_type )
 
162
{
 
163
        unsigned int i;
 
164
 
 
165
        for( i = 0; i < FIELD_SIZE; ++i )
 
166
        {
 
167
                if( field[i] == old_type )
 
168
                {
 
169
                        field[i] = new_type;
 
170
                        return 0;
 
171
                }
 
172
        }
 
173
 
 
174
        /* No field with the desired type found */
 
175
        return -1;
 
176
}