• 1pix agents of prey

    From fir@3:633/280.2 to All on Mon Apr 8 03:18:50 2024


    i can code 1-pixel sized bots on black screen
    (say 100 red bots/agents and 100 green)
    and then spread a 1-pixel yelow food (say 1000)

    then could make competition for example like
    that - in each turn (and you got like 100 maybe
    200 turns on a second) each agnet has given 1.0
    time to spend

    he may use this time in varios way
    1) he may move 1.0 pixel far in chosen direction
    (he may alos move sat 0.7 pixel far and use 0.3
    time for other purposes)
    2) he may change direction - by the cost 0.4 for 360
    degrees or proportionally less ofr part of it
    3) he may look out on surrounding pixels - but the looking
    out has cost say 0.01 time for one pixel look out
    (and also given pixel-agent probably will have a
    radius where he can look at most - say 20 pixels far)
    4) he may dock to food and then drag it like an ant in
    given dorection (maybe by the cost of 2 times more
    than normal move
    5) i also think if it shouldnt be able to drag also
    enemies but enemy could drag also him to

    the goal would be to drag some amount of food
    to his own base (say drawed as a circle on radius 25
    or something like that

    i wonder if coding this owuld make something interesting
    or only something stupid .. right now i coded
    basics of it and get tired..but i eventually could try later

    code of thiose basics below


    #include<stdio.h>
    #include "green-fire.h"
    #include<math.h>

    int IsInsideFrame(int x, int y)
    {
    if(x<0|x>=frame_size_x|y<0|y>=frame_size_y) return 0;
    return 1;
    }
    ///////// MATRIX QUEUE

    typedef void (*AgentEntry)(int) ;
    struct Agent { AgentEntry entry; int type; float x, y,time,angle;
    unsigned color1,color2;};

    Agent* agents = NULL; int agents_size = 0;
    void agents_resize_by_one() { agents=(Agent*)realloc(agents, ++agents_size*sizeof(Agent));}

    void agents_add(AgentEntry entry, int type, float x, float y)
    {
    agents_resize_by_one();
    int l = agents_size-1;

    agents[l].entry = entry;
    agents[l].x = x;
    agents[l].y = y;
    agents[l].angle = rand2(-18000,17999)/100.;

    if(type=='red') agents[l].color1=0xff0000;
    if(type=='green') agents[l].color1=0x00ff00;

    }

    void agents_run_queue()
    {
    for(int i=0; i<agents_size; i++)
    {
    agents[i].time+=1;
    agents[i].entry(i);
    agents[i].time=0;

    }
    }

    void CreateAgents(int amount, int type, AgentEntry entry, float x, float
    y, float r)
    {
    for(int i=0; i<amount; i++)
    agents_add(entry, type, x+rand2(-r,r), y+rand2(-r,r));
    }




    ///////// AGENTS


    float agent_dir_x(int i) { return cos((agents[i].angle-90)*degree360);}
    float agent_dir_y(int i){ return sin((agents[i].angle-90)*degree360);}

    int MoveForward(int i, float time)
    {
    if(time<=0) return 0;
    if(agents[i].time>=time);
    else time = agents[i].time;
    if(time<=0) return 0;

    float new_x = agents[i].x + time*agent_dir_x(i); ;
    float new_y = agents[i].y + time*agent_dir_y(i); ;;

    if(!IsInsideFrame(new_x, new_y)) return 0;

    SetPixelSafe(agents[i].x, agents[i].y, 0);
    agents[i].x = new_x;
    agents[i].y = new_y;
    SetPixelSafe(agents[i].x, agents[i].y, agents[i].color1);

    agents[i].time-=time;
    return 1;

    }

    int ChangeDirection(int i, float delta_angle)
    {
    float time_needed = fabs(delta_angle)/360.*0.4;

    if(time_needed<=0) return 0;
    if(agents[i].time>=time_needed) agents[i].time-=time_needed;
    else return 0;

    agents[i].angle += delta_angle;
    return 1;
    }

    void RedAgentEntryPoint(int i)
    {

    if( MoveForward(i, agents[i].time));
    else ChangeDirection(i, rand2(-180,180));

    }

    void GreenAgentEntryPoint(int i)
    {
    if( MoveForward(i, agents[i].time));
    else ChangeDirection(i, rand2(-180,180));


    }



    void InitializeAgents()
    {
    CreateAgents(100, 'red', RedAgentEntryPoint, 100,100,10);
    CreateAgents(100, 'green', GreenAgentEntryPoint, frame_size_x-100, frame_size_y-100,10);
    }


    void SperadFood(int amount)
    {
    for(int i=0; i<amount; i++)
    {
    int x = rand2(0,frame_size_x-1);
    int y = rand2(0,frame_size_y-1);
    SetPixelSafe(x,y,0xffff00);

    }
    }

    ///////////// WINDOW

    void RunFrame(int advance)
    {
    if(frame_number==0)
    {
    SperadFood(1000);
    InitializeAgents();

    }

    agents_run_queue();
    }

    void MouseMove(int x, int y) {}
    void KeyDown(int key) {}
    void OnResize() { }

    int main(int argc, char* argv[])
    {
    RegisterMouseMove( MouseMove ); RegisterKeyDown( KeyDown );
    RegisterOnResize( OnResize ); RegisterRunFrame( RunFrame );
    SetSleepValue(10); SetScaleOnResize(1);
    SetupWindow4(" MATRIX by fir (spring 2024) ", 10, 10, .9, .9, 300 );
    return 0;
    }

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: i2pn2 (i2pn.org) (3:633/280.2@fidonet)