Ok, so yeah, if you can't tell from the included snippet, I don't know what I'm doing, just want you to have no illusions about that. IMO not only bad programming, but bad use of byond coding, and most likely a terrifying use of a proc, and a terrible comprehension of OOP.
All of that aside, this proc
does work(ish), albeit with a few minor glitches (see below code). That isn't really the main point of this post, rather I would like to know how to make this code more elegant, proper, and even more generalized, I know I must have some really bad habits (as I have really only ever programmed in applesofts basic way back in the early 80's, spaghetti programming is what I was best at, goto and gosub were my bestest friends) I would like help identifying these problems and perhaps start to correct them. The code is included so you can get a glimpse into how my mind works, and some things I might like to accomplish with this part of the code.
This part of the
Code: is called with spawn(metab) rattai()
And it pertains to a game of survival, albeit for the ratt and its ai (such as it is).
Things the ratt needs to survive:
1) make a nest (find water first and build near it, but not too near)
2) find food, kill it, eat it if hungry, if not, drop it IN the nest
3) drink water when thirsty
4) avoid dangerous mobs (unless too near nest or when
very hungry.)
5) get slight bonuses when close to nest, faster speed, quicker to react in combat, better th (aka THAC0) and more damage.
I think that covers the basics for the ratt and its ai.
proc/rattai() if (get_dist(src,nestloc)<=5) metab=9 init=7 th=20 mindam=2 maxdam=3 else metab=12 init=8 th=22 mindam=1 maxdam=2 if (hp>0&&incombat==0) for (var/mob/M as mob in oview(6)) if (!action&&get_dist(M,src)<5&&!istype(M,/mob/frogg)&&get_dist(M,src)>1) if (get_dist(src,nestloc)<=5&&get_dist(M,nestloc)<=5) step_to(src,M) world<<"[src]: SQUEAK! Get away from my nest!" action=1 else if(food<=2) step_to(src,M) if(get_dist(src,M)<=1) attack(M) action=1 else step_away(src,M) world<<"[src]: Squeak! Run away!" action=1 if (!action&&get_dist(src,nestloc)<=5&&get_dist(M,nestloc)<=5&&get_dist(M,src)<=1) attack(M) action=1 if (!action&&get_dist(M,src)>1&&istype(M,/mob/frogg)&&(food<=7||mouth==0)) step_to(src,M) world<<"[src]: Yum! Get me some! [get_dist(M,src)]" action=1 if (!action&&get_dist(M,src)<=1&&istype(M,/mob/frogg)) world<<"[src]: Gimme those legs!" M.drop() froggs.len-=1 chkfrg() del(M) action=1 for (var/obj/O as obj in oview(5)) if (!action&&mouth==0||food<=7) if (!action&&get_dist(O,src)>1&&istype(O,/obj/frogglegs)) step_to(src,O) action=1 world<<"[src]: Those look yummy!" if (!action&&get_dist(O,src)<=1&&istype(O,/obj/frogglegs)&&food<=7) food+=3 action=1 world<<"[src]: Munch munch.." del(O) if (!action&&get_dist(O,src)<=1&&istype(O,/obj/frogglegs)&&food>7&&(get_dist(O,nestloc)>0 || !(O in nestloc))) O.loc=src mouth=1 action=1 world<<"[src]: mMrmph [O]" if (!action&&!nest&&!waterfound) for (var/turf/T as turf in oview(5)) if (istype(T,/turf/water)) waterfound=1 step_away(src,T) world<<"[src]: I see water" waterloc+=T action=1 break if(!action&&!nest&&waterfound) if(get_dist(src,waterloc)>=6) var/obj/o=new /obj/rattsnest o.loc=loc o.owner=src nestloc=o nest=1 action=1 else spawn(1) while(get_dist(src,waterloc)<6) step_rand(src) sleep(metab) var/obj/o=new /obj/rattsnest o.loc=loc o.owner=src nestloc=o nest=1 action=1 if (!action) step_rand(src) action=1 if (!action&&nest&&mouth==1) if (!get_dist(src,nestloc)) for (var/obj/o in contents) o.loc=nestloc action=1 mouth=0 world<<"[src]: patooey" else step_to(src,nestloc) action=1 if(!action&&food<=7&&mouth==1) mouth=0 food+=3 for (var/obj/o in contents) del(o) action=1 if(!action&&food<=7) for(var/obj/o in nestloc) if(o) if(get_dist(src,nestloc)>0) step_to(src,nestloc) action=1 break else food+=3 del(o) action=1 break if (!action&&water<=1) step_to(src,waterloc) if(get_dist(src,waterloc)<=1) water=watermax action=1 if (!action) step_rand(src) action=1 action=0 spawn(metab) rattai()
|
Problem description: as I stated above, this code does work(ish) there are a few glitches, like the ratt can't change its mind, once its on a path it has no choice but to complete that path and if something disturbs completion errors can occur. (mostly just stuck looping, for example hungry (but not very hungry) and a dangerous mob is in the way of its nest (where it knows it has stored food) can cause a run away and move back towards nest kind of loop, and others along those same lines.
Sorry for such a long post, but I wasn't sure I could get my points across without including as much as I did.
Other than that, the code is really clunky and most of it is un-needed. I'll continue to read through it and let you know of every correction I can offer, and probably find your issue aswell.