1
/* field.c - Field functions.
3
* biosim - A simulator for the predator-prey relationship of aphids and
6
* Copyright (c) 2010, Tilman Blumenbach <tilman@ax86.net>
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions are
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.
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.
37
int field_init( field_type *field )
39
unsigned int aphid_chips = 15, ladybug_chips = 15, i;
40
int rand_type, offset;
42
for( i = 0; i < FIELD_SIZE; ++i )
47
while( aphid_chips || ladybug_chips )
49
offset = rand_get( 0, FIELD_SIZE - 1 );
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] );
58
if( field[offset] != TYPE_NONE )
59
{ /* We only want to use empty fields. */
63
/* If we only have one kind of chips left, we do not need to get
68
rand_type = TYPE_LADYBUG;
70
else if( ! ladybug_chips )
72
rand_type = TYPE_APHID;
74
else if( ( rand_type = rand_get( TYPE_NONE + 1, _TYPES_COUNT - 1 ) ) == -1 )
79
/* Determine which kind of chip we have placed. */
91
/* Should never happen. */
92
LOGF_FILELINE( LOG_ERROR, "Unhandled field type [%X].", rand_type );
96
field[offset] = rand_type;
98
LOGF_FILELINE( LOG_DEBUG, "Setting type: %d", rand_type );
104
static void _field_horiz_line( void )
108
for( i = 0; i < FIELD_ROWS_COLS; ++i )
110
fputs( "+---", stderr );
113
fputs( "+\n", stderr );
116
void field_dump( const field_type *field )
120
for( i = 0; i < FIELD_SIZE; ++i )
122
if( i % FIELD_ROWS_COLS == 0 )
126
fputc( '\n', stderr );
130
fputc( '|', stderr );
133
fputc( ' ', stderr );
138
fputc( FIELD_CHR_NONE, stderr );
142
fputc( FIELD_CHR_APHID, stderr );
146
fputc( FIELD_CHR_LADYBUG, stderr );
150
fputc( FIELD_CHR_UNKNOWN, stderr );
154
fputs( " |", stderr );
157
fputc( '\n', stderr );
161
int field_set_first( field_type *field, field_type old_type, field_type new_type )
165
for( i = 0; i < FIELD_SIZE; ++i )
167
if( field[i] == old_type )
174
/* No field with the desired type found */