-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgoap.c
203 lines (165 loc) · 5.27 KB
/
goap.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright 2012 Abraham T. Stolk
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#if defined( _MSC_VER )
# define _CRT_SECURE_NO_WARNINGS
# define snprintf _snprintf
#endif
#include "goap.h"
#include "astar.h"
#include <string.h>
#include <stdio.h>
static int idx_for_atomname( actionplanner_t* ap, const char* atomname )
{
int idx;
for ( idx=0; idx < ap->numatoms; ++idx )
if ( !strcmp( ap->atm_names[ idx ], atomname ) ) return idx;
if ( idx < MAXATOMS )
{
ap->atm_names[ idx ] = atomname;
ap->numatoms++;
return idx;
}
return -1;
}
static int idx_for_actionname( actionplanner_t* ap, const char* actionname )
{
int idx;
for ( idx=0; idx < ap->numactions; ++idx )
if ( !strcmp( ap->act_names[ idx ], actionname ) ) return idx;
if ( idx < MAXACTIONS )
{
ap->act_names[ idx ] = actionname;
ap->act_costs[ idx ] = 1; // default cost is 1
ap->numactions++;
return idx;
}
return -1;
}
void goap_actionplanner_clear( actionplanner_t* ap )
{
ap->numatoms = 0;
ap->numactions = 0;
for ( int i=0; i<MAXATOMS; ++i )
{
ap->atm_names[ i ] = 0;
}
for ( int i=0; i<MAXACTIONS; ++i )
{
ap->act_names[ i ] = 0;
ap->act_costs[ i ] = 0;
goap_worldstate_clear( ap->act_pre+i );
goap_worldstate_clear( ap->act_pst+i );
}
}
void goap_worldstate_clear( worldstate_t* ws )
{
ws->values = 0LL;
ws->dontcare = -1LL;
}
bool goap_worldstate_set( actionplanner_t* ap, worldstate_t* ws, const char* atomname, bool value )
{
const int idx = idx_for_atomname( ap, atomname );
if ( idx == -1 ) return false;
ws->values = value ? ( ws->values | ( 1LL << idx ) ) : ( ws->values & ~( 1LL << idx ) );
ws->dontcare &= ~( 1LL << idx );
return true;
}
extern bool goap_set_pre( actionplanner_t* ap, const char* actionname, const char* atomname, bool value )
{
const int actidx = idx_for_actionname( ap, actionname );
const int atmidx = idx_for_atomname( ap, atomname );
if ( actidx == -1 || atmidx == -1 ) return false;
goap_worldstate_set( ap, ap->act_pre+actidx, atomname, value );
return true;
}
bool goap_set_pst( actionplanner_t* ap, const char* actionname, const char* atomname, bool value )
{
const int actidx = idx_for_actionname( ap, actionname );
const int atmidx = idx_for_atomname( ap, atomname );
if ( actidx == -1 || atmidx == -1 ) return false;
goap_worldstate_set( ap, ap->act_pst+actidx, atomname, value );
return true;
}
bool goap_set_cost( actionplanner_t* ap, const char* actionname, int cost )
{
const int actidx = idx_for_actionname( ap, actionname );
if ( actidx == -1 ) return false;
ap->act_costs[ actidx ] = cost;
return true;
}
void goap_worldstate_description( const actionplanner_t* ap, const worldstate_t* ws, char* buf, int sz )
{
int added=0;
for ( int i=0; i<MAXATOMS; ++i )
{
if ( ( ws->dontcare & ( 1LL << i ) ) == 0LL )
{
const char* val = ap->atm_names[ i ];
char upval[ 128 ];
size_t j;
for ( j=0; j<strlen( val ); ++j )
upval[ j ] = ( val[ j ] - 32 );
upval[ j++ ] = 0;
const bool set = ( ( ws->values & ( 1LL << i ) ) != 0LL );
added = snprintf( buf, sz, "%s,", set?upval:val );
buf += added; sz -= added;
}
}
}
void goap_description( actionplanner_t* ap, char* buf, int sz )
{
int added=0;
for ( int a=0; a<ap->numactions; ++a )
{
added=snprintf( buf, sz, "%s:\n", ap->act_names[ a ] );
sz -= added; buf += added;
worldstate_t pre = ap->act_pre[ a ];
worldstate_t pst = ap->act_pst[ a ];
for ( int i=0; i<MAXATOMS; ++i )
if ( ( pre.dontcare & ( 1LL << i ) ) == 0LL )
{
bool v = ( pre.values & ( 1LL << i ) ) != 0LL;
added = snprintf( buf, sz, " %s==%d\n", ap->atm_names[ i ], v );
sz -= added; buf+= added;
}
for ( int i=0; i<MAXATOMS; ++i )
if ( ( pst.dontcare & ( 1LL << i ) ) == 0LL )
{
bool v = ( pst.values & ( 1LL << i ) ) != 0LL;
added = snprintf( buf, sz, " %s:=%d\n", ap->atm_names[ i ], v );
sz -= added; buf+= added;
}
}
}
static worldstate_t goap_do_action( actionplanner_t const* ap, int actionnr, worldstate_t fr )
{
const worldstate_t pst = ap->act_pst[ actionnr ];
const bfield_t unaffected = pst.dontcare;
const bfield_t affected = ( unaffected ^ -1LL );
fr.values = ( fr.values & unaffected ) | ( pst.values & affected );
fr.dontcare &= pst.dontcare;
return fr;
}
int goap_get_possible_state_transitions( actionplanner_t const* ap, worldstate_t fr, worldstate_t* to, const char** actionnames, int* actioncosts, int cnt )
{
int writer=0;
for ( int i=0; i<ap->numactions && writer<cnt; ++i )
{
// see if precondition is met
const worldstate_t pre = ap->act_pre[ i ];
const bfield_t care = ( pre.dontcare ^ -1LL );
const bool met = ( ( pre.values & care ) == ( fr.values & care ) );
if ( met )
{
actionnames[ writer ] = ap->act_names[ i ];
actioncosts[ writer ] = ap->act_costs[ i ];
to[ writer ] = goap_do_action( ap, i, fr );
++writer;
}
}
return writer;
}