This is what I eventually came up with, almost done I just need to make him spawn a grain bag into the stock. Just putting it here for future reference and if people need some inspiration.
void handleFarmingBehavior(object farmer){
string phase = GetLocalString(farmer, VAR_ACTIVITY_PHASE);
string phase_status = GetLocalString(farmer, VAR_PHASE_STATUS);
PrintString("FARMING PHASE:" + phase);
if(phase == PHASE_INITIALIZE){
PrintString("ENTERING:" + phase);
//farmer should go to a farm waypoint that is not occupied yet
//once reached upgrade phase to PHASE_FARM
object farmWp = GetNearestObjectByTag("DM_WP_FARM", farmer);
if(farmWp != OBJECT_INVALID){
PrintString("Valid farmWp");
location farmLocation = GetLocation(farmWp);
PrintString("phase status: " + phase_status);
if(phase_status == ""){
AssignCommand(farmer, ClearAllActions());
AssignCommand(farmer,ActionMoveToLocation(farmLocation));
AssignCommand(farmer, ActionPlayAnimation(ANIMATION_LOOPING_SHOVELING, 1.0, 300.0));
SetLocalString(farmer, VAR_PHASE_STATUS, STATE_RUNNING);
}else if(phase_status == STATE_RUNNING){
object creature = GetFirstObjectInShape(SHAPE_CUBE, 2.5, farmLocation, FALSE, OBJECT_TYPE_CREATURE);
if(creature != OBJECT_INVALID && creature == farmer){
SetLocalString(farmer, VAR_ACTIVITY_PHASE, PHASE_FARM);
SetLocalString(farmer, VAR_PHASE_STATUS, "");
}else{
//the farmer probably had a problem and never reached the farming point lets try again
AssignCommand(farmer, ClearAllActions());
AssignCommand(farmer,ActionMoveToLocation(farmLocation));
AssignCommand(farmer, ActionPlayAnimation(ANIMATION_LOOPING_SHOVELING, 1.0, 300.0));
}
}
}else{
//no farming possibility set phase to completed so cleanup can happen
SetLocalString(farmer, VAR_ACTIVITY_PHASE, PHASE_COMPLETED);
SetLocalString(farmer, VAR_PHASE_STATUS, "");
}
}
if(phase == PHASE_FARM){
AssignCommand(farmer, ActionPlayAnimation(ANIMATION_LOOPING_SHOVELING, 1.0, 300.0));
if(phase_status == ""){
SetLocalInt(farmer, VAR_PHASE_DURATION, 5);
SetLocalString(farmer, VAR_PHASE_STATUS, STATE_RUNNING);
}else if(phase_status == STATE_RUNNING){
int duration = GetLocalInt(farmer, VAR_PHASE_DURATION);
if(duration == 0){
SetLocalString(farmer, VAR_ACTIVITY_PHASE, PHASE_DELIVER);
SetLocalString(farmer, VAR_PHASE_STATUS, "");
}
}
}
if(phase == PHASE_DELIVER){
object stockLocationObject = GetNearestObjectByTag("DM_VILLAGE_STOCK", farmer);
location stockLocation = GetLocation(stockLocationObject);
if(phase_status == ""){
AssignCommand(farmer, ClearAllActions());
AssignCommand(farmer,ActionMoveToLocation(stockLocation));
SetLocalString(farmer, VAR_PHASE_STATUS, STATE_RUNNING);
}else if(phase_status == STATE_RUNNING){
object creature = GetFirstObjectInShape(SHAPE_CUBE, 2.5, stockLocation, FALSE, OBJECT_TYPE_CREATURE);
if(creature != OBJECT_INVALID && creature == farmer){
//TODO: we need to put a grain item in the chest
//Finish the farmers activity by putting the phase to completed
SetLocalString(farmer, VAR_ACTIVITY_PHASE, PHASE_COMPLETED);
SetLocalString(farmer, VAR_PHASE_STATUS, "");
}else{
//the farmer probably had a problem and never reached the delivery point lets try again
AssignCommand(farmer, ClearAllActions());
AssignCommand(farmer,ActionMoveToLocation(stockLocation));
}
}
}
if(phase == PHASE_COMPLETED){
if(phase_status == ""){
AssignCommand(farmer, ClearAllActions());
AssignCommand(farmer,ActionRandomWalk());
SetLocalString(farmer, VAR_PHASE_STATUS, STATE_RUNNING);
}
}
return;
}