//====================================================================/
//                                                                    /
// KK-Multi Agent Simulator for Java                                  /
//                                                                    /
//====================================================================/

Version = 1.0;



//-------------------------------------------------------
// Component Tree
//-------------------------------------------------------
UNIVERSE {
    Space Streets(0,0),Square_2D(10, 10, Loop, North, 1){
        AgtType RedTurtle(0, 0)[0] {
            Dim X(0, 0) As Double = (0.0);
            Dim Y(0, 0) As Double = (0.0);
            Dim Layer(0, 0) As Integer = (0);
            Dim Direction(0, 0) As Double = (0.0);
            Dim RedNeighbors(0, 0) As AgtSet = ();
            Dim BlueNeighbors(0, 0) As AgtSet = ();
            Dim Num_RedNeighbors(0, 0) As Double = (0.0);
            Dim Num_BlueNeighbors(0, 0) As Double = (0.0);
            Dim Nakama_Rate(0, 0) As Double = (0.0);
        }
        AgtType BlueTurtle(0, 0)[0] {
            Dim X(0, 0) As Double = (0.0);
            Dim Y(0, 0) As Double = (0.0);
            Dim Layer(0, 0) As Integer = (0);
            Dim Direction(0, 0) As Double = (0.0);
            Dim RedNeighbors(0, 0) As AgtSet = ();
            Dim BlueNeighbors(0, 0) As AgtSet = ();
            Dim Num_RedNeighbors(0, 0) As Double = (0.0);
            Dim Num_BlueNeighbors(0, 0) As Double = (0.0);
            Dim Nakama_Rate(0, 0) As Double = (0.0);
        }
    }
    Dim Num_Reds(0, 0) As Integer = (0);
    Dim Num_Blues(0, 0) As Integer = (0);
    Dim Seg_Criteria(0, 0) As Double = (0.0);
}


//-------------------------------------------------------
// Agent Variable Initial Data
//-------------------------------------------------------
Initial_Value {
    UNIVERSE.NUM_REDS = 1(30);
    UNIVERSE.NUM_BLUES = 1(30);
    UNIVERSE.SEG_CRITERIA = 1(0.5);
}


//-------------------------------------------------------
// Agent Rule
//-------------------------------------------------------
#begin_rule UNIVERSE
Univ_Init{

Dim i as integer

// 赤亀を発生させる
For i = 0 to Universe.Num_Reds-1
	CreateAgt(Universe.Streets.RedTurtle)
Next i

// 青亀を発生させる
For i = 0 to Universe.Num_Blues-1
	CreateAgt(Universe.Streets.BlueTurtle)
Next i

Dim RTurtles As AgtSet
Dim BTurtles As AgtSet
DIm ATurtles As AgtSet

// RTurtlesという変数に赤亀のリストを格納する
MakeAgtSet(RTurtles, Universe.Streets.RedTurtle)
// BTurtlesという変数に青亀のリストを格納する
MakeAgtSet(BTurtles, Universe.Streets.BlueTurtle)

//ATurtlesという変数にRTurtlesという変数(赤亀のリスト)の値を加える
MergeAgtSet(ATurtles, RTurtles)
//ATurtlesという変数にBTurtlesという変数(青亀のリスト)の値を加える
MergeAgtSet(ATurtles, BTurtles)

//ATurtlesに含まれているエージェント(全ての亀)をランダムに配置する
RandomPutAgtSetCell(ATurtles, False)

}

Univ_Step_Begin{

}

Univ_Step_End{

}

Univ_Finish{

}
#end_rule UNIVERSE




//-------------------------------------------------------
// Agent Rule
//-------------------------------------------------------
#begin_rule UNIVERSE.STREETS.REDTURTLE
Agt_Init{

}

Agt_Step{

//赤亀を数える
MakeOneAgtSetAroundOwnCell(My.RedNeighbors, 1, Universe.Streets.RedTurtle, False)
My.Num_RedNeighbors = CountAgtSet(My.RedNeighbors)

//青亀を数える
MakeOneAgtSetAroundOwnCell(My.BlueNeighbors, 1, Universe.Streets.BlueTurtle, False)
My.Num_BlueNeighbors = CountAgtSet(My.BlueNeighbors)

//仲間率を計算します
If My.Num_RedNeighbors+My.Num_BlueNeighbors == 0 then
	My.Nakama_Rate = 0
else
	My.Nakama_Rate = My.Num_RedNeighbors / (My.Num_RedNeighbors+My.Num_BlueNeighbors)
end if

//不満なら移動します
If My.Nakama_Rate < Universe.Seg_Criteria then
	MoveToSpaceOwnCell(2)
end if



}
#end_rule UNIVERSE.STREETS.REDTURTLE




//-------------------------------------------------------
// Agent Rule
//-------------------------------------------------------
#begin_rule UNIVERSE.STREETS.BLUETURTLE
Agt_Init{


}

Agt_Step{

//赤亀を数える
MakeOneAgtSetAroundOwnCell(My.RedNeighbors, 1, Universe.Streets.RedTurtle, False)
My.Num_RedNeighbors = CountAgtSet(My.RedNeighbors)

//青亀を数える
MakeOneAgtSetAroundOwnCell(My.BlueNeighbors, 1, Universe.Streets.BlueTurtle, False)
My.Num_BlueNeighbors = CountAgtSet(My.BlueNeighbors)

//仲間率を計算します
If My.Num_RedNeighbors+My.Num_BlueNeighbors == 0 then
	My.Nakama_Rate = 0
else
	My.Nakama_Rate = My.Num_BlueNeighbors / (My.Num_RedNeighbors+My.Num_BlueNeighbors)
end if

//不満なら移動します
If My.Nakama_Rate < Universe.Seg_Criteria then
	MoveToSpaceOwnCell(2)
end if

}
#end_rule UNIVERSE.STREETS.BLUETURTLE




//-------------------------------------------------------
// Simulation Definition Division
//-------------------------------------------------------
Single_Simulation {
    Step_Max = 0;
    Time_Max = 0;
    End_Condition = "";
    Exec_Wait = 0;
    Exec_Order = Random;
    Random_Seed = 0;
    Log_File = "", Fixed, Append, 0;
    Replay_Log_File = "", Fixed, Append, 0;
    Redraw_Timing = Step;
    Redraw_Interval = 1;
    Garbage_Interval = 10;
}


//-------------------------------------------------------
// Simulation Loop Definition Division
//-------------------------------------------------------
Repeat_Simulation {
    Value_Change = None;
    Repeat_Max = 1;
}


//-------------------------------------------------------
// 2 Dimension Map Display Definition Division
//-------------------------------------------------------
Map_Output {
    Map_Space_Name = "UNIVERSE.STREETS";
    Map_Name = "Streets";
    Font_Name = "Dialog";
    Font_Char_Set = 81;
    Font_Size = 12.0;
    Font_Style = 0;
    Font_Color = 0, 0, 0;
    Font_Background_Color = 255, 255, 255;
    Title = "Streets";
    Axis_Label = "", "";
    Draw_Range = 0, 0, 9, 9;
    Ruled_Line = Yes;
    Remarks = Yes;
    Legend_Pos = Top;
    Background_Color = 0, 0, 0;
    Background_Transparent = No;
    Target_Layer = 0;
    Coordinate_Display = No;
    Space_Kind = Square_2D;
    BG_Pict = No;
    BG_Type = 0;
    BG_Var_Name = "";
    BG_File_Name = "";
    Disable = No;
    Position_x = 302;
    Position_y = -16;
    Size_x = 792;
    Size_y = 533;
    Origin_Position = 2;
    Output {
        Map_Element_Name = "RedTurtle";
        Map_Element = Agent_Variable;
        Output_Expression = "UNIVERSE.STREETS.REDTURTLE";
        Draw_Line = No;
        Collection_Name = "";
        Line_Type = 0;
        Line_Arrow = 0;
        Line_Width = 0;
        Line_Color = 0, 0, 0;
        Agent_Color = 255,102,102;
        Marker_Id = 1;
        Icon_Type = 0;
        Icon_Var_Name = "";
        Icon_Transparent = No;
        Icon_Enlarg_Reduce = No;
        Icon_File_Name = "";
        Num_Display = No;
        Num_Var_Name = "UNIVERSE.STREETS.REDTURTLE.NUM_BLUENEIGHBORS";
    }
    Output {
        Map_Element_Name = "BlueTurtle";
        Map_Element = Agent_Variable;
        Output_Expression = "UNIVERSE.STREETS.BLUETURTLE";
        Draw_Line = No;
        Collection_Name = "";
        Line_Type = 0;
        Line_Arrow = 0;
        Line_Width = 0;
        Line_Color = 0, 0, 0;
        Agent_Color = 51,153,255;
        Marker_Id = 1;
        Icon_Type = 0;
        Icon_Var_Name = "";
        Icon_Transparent = No;
        Icon_Enlarg_Reduce = No;
        Icon_File_Name = "";
        Num_Display = No;
        Num_Var_Name = "UNIVERSE.STREETS.BLUETURTLE.NUM_REDNEIGHBORS";
    }
}


//-------------------------------------------------------
// Control Panel Definition Division
//-------------------------------------------------------
Control_Panel {
    Display_Area = 356, 240, 157, 252;
    Panel_Item {
        Item_Type = Slider;
        Item_Name = "Number_Reds";
        Display_Area = 0, 0, 0, 0;
        Font_Name = "";
        Font_Char_Set = 0;
        Font_Size = 0.0;
        Font_Style = 0;
        Font_Color = 0, 0, 0;
        Font_Background_Color = 0, 0, 0;
        Var_Name = UNIVERSE.NUM_REDS;
        Slider_Range_Max = 50;
        Slider_Value_Min = 0.0;
        Slider_Value_Step = 1.0;
        Slider_Value = "30";
    }
    Panel_Item {
        Item_Type = Slider;
        Item_Name = "Number_Blues";
        Display_Area = 0, 0, 0, 0;
        Font_Name = "";
        Font_Char_Set = 0;
        Font_Size = 0.0;
        Font_Style = 0;
        Font_Color = 0, 0, 0;
        Font_Background_Color = 0, 0, 0;
        Var_Name = UNIVERSE.NUM_BLUES;
        Slider_Range_Max = 50;
        Slider_Value_Min = 0.0;
        Slider_Value_Step = 1.0;
        Slider_Value = "30";
    }
    Panel_Item {
        Item_Type = Slider;
        Item_Name = "Seg_Criteria";
        Display_Area = 0, 0, 0, 0;
        Font_Name = "";
        Font_Char_Set = 0;
        Font_Size = 0.0;
        Font_Style = 0;
        Font_Color = 0, 0, 0;
        Font_Background_Color = 0, 0, 0;
        Var_Name = UNIVERSE.SEG_CRITERIA;
        Slider_Range_Max = 10;
        Slider_Value_Min = 0.0;
        Slider_Value_Step = 0.1;
        Slider_Value = "5";
    }
}


//-------------------------------------------------------
// Space Initial Value Definition Divsion
//-------------------------------------------------------
Space_Initial {
}


//-------------------------------------------------------
// Other Miscellaneous Data Definition Division
//-------------------------------------------------------
Others_Data {
    Indent = 4;
    Rule_Colored = Yes;
    Font_Name = "Dialog";
    Font_Char_Set = 0;
    Font_Size = 12.0;
    Font_Style = 0;
    Font_Color = 0, 0, 0;
    Font_Background_Color = 0, 0, 0;
    Window_Hide_Run = 1;
    Debug_Position_x = 30;
    Debug_Position_y = 14;
    Debug_Size_x = 400;
    Debug_Size_y = 200;
    Console_Position_x = 11;
    Console_Position_y = 517;
    Console_Size_x = 600;
    Console_Size_y = 120;
}