Hi, peeps,
This whole week I've been making a roguelike. Everything went fine and swell, until……I had to make the enemies chase the 'main character'. I've tried three ways of doing this…WAY IExplanation : I used point_direction to detect where the character is.**********var dir, dir2, temp;///////////////////////////////////////////////////////////////////////////////temp = 1;while (temp <= movesmax) do { //movesmax = amount dir2 = point_direction(prey.x, prey.y, x, y); //of moves per turn.if dir2 > 0 and dir2 < 90 then dir = 7; //prey - object the if dir2 = 90 then dir = 8; //monster is chasing.if dir2 > 90 and dir2 < 180 then dir = 1;if dir2 = 180 then dir = 2;if dir2 > 180 and dir2 < 270 then dir = 3;if dir2 = 270 then dir = 4;if dir2 > 270 and dir2 < 360 then dir = 5;if dir2 = 0 then dir = 6;switch(dir) {//hor and vercase 4: if (col_check('up')) {self.y -= sprite_height; temp +=1; }case 8: if (col_check('down')){self.y += sprite_height; temp+=1;} case 6: if (col_check('left')) {self.x -= sprite_height; temp +=1; }case 2: if (col_check('right')){self.x += sprite_height; temp +=1; }//diagonalcase 1: if (col_check('up')) and (col_check('right')){self.x += sprite_height; self.y += sprite_height; temp +=1; }case 3: if (col_check('down')) and (col_check('right')){self.x += sprite_height; self.y -= sprite_height; temp +=1; }case 7: if (col_check('left')) and (col_check('up')) {self.x -= sprite_height; self.y += sprite_height; temp +=1; }case 5: if (col_check('left')) and (col_check('down')) {self.x -= sprite_height; self.y -= sprite_height; temp +=1; } } }if temp = movesmax then c_e = true;**********Result:enemy makes strange turns and after a long walk finally reaches the character. Not good enough - he must move directly towards the character.**********WAY IIExplanation:using x and y of enemy to find the prey's coordinates.**********var dir, temp;///////////////////////////////////////////////////////////////////////////////temp = 1;while (temp <= movesmax) do {if prey.x > x+8 and prey.x < sight and prey.y = y then dir = 2;if prey.x < x-8 and prey.x > -sight and prey.y = y then dir = 6;if prey.y > y+8 and prey.y < sight and prey.x = x then dir = 8;if prey.y < y-8 and prey.y > -sight and prey.x = x then dir = 4;//diagonalif prey.x > x+8 and prey.x < sight and prey.y > y+8 and prey.y < sight then dir = 1;if prey.x > x+8 and prey.x < sight and prey.y < y-8 and prey.y > -sight then dir = 3;if prey.x < x-8 and prey.x > -sight and prey.y < y-8 and prey.y > -sight then dir = 5;if prey.x < x-8 and prey.x > -sight and prey.y > y+8 and prey.y < sight then dir = 7;switch(dir) {//hor and vercase 4: if (col_check('up')) {self.y -= sprite_height;temp +=1; }case 8: if (col_check('down')) {self.y += sprite_height;temp+=1;}case 6: if (col_check('left')) {self.x -= sprite_height; temp +=1; }case 2: if (col_check('right')){self.x += sprite_height; temp +=1; }//diagonalcase 1: if (col_check('up')) and (col_check('right')){self.x += sprite_height; self.y += sprite_height; temp +=1; }case 3: if (col_check('down')) and (col_check('right')){self.x += sprite_height; self.y -= sprite_height; temp +=1; }case 7: if (col_check('left')) and (col_check('up')) {self.x -= sprite_height; self.y -= sprite_height; temp +=1; }case 5: if (col_check('left')) and (col_check('down')) {self.x -= sprite_height; self.y += sprite_height; temp +=1; } } }if temp = movesmax then c_e = true;********Result:same as above. Sometimes it even starts walking to the completely opposite direction it should actually walk to. wtf…********WAY IIIExplanation:checking which of the squares around the enemy is closest to the prey, and then moving to it.********var v1, v2, v3, v4, v5, v6, v7, v8, temp, dir;///////////////////////////////////////////////////////////////////////////////temp = 1;while(temp <= movesmax) do {v1 = point_distance(x-8, y-8, prey.x, prey.y);v2 = point_distance(x, y-8, prey.x, prey.y);v3 = point_distance(x+8, y-8, prey.x, prey.y);v4 = point_distance(x-8, y, prey.x, prey.y);v5 = point_distance(x+8, y, prey.x, prey.y);v6 = point_distance(x-8, y+8, prey.x, prey.y);v7 = point_distance(x, y+8, prey.x, prey.y);v8 = point_distance(x+8, y+8, prey.x, prey.y);//mindir = min(v1, v2, v3, v4, v5, v6, v7, v8);switch(dir) {//hor and vercase v2: if (col_check('up')) {self.y -= sprite_height; temp +=1; }case v7: if (col_check('down')) {self.y+=sprite_height;temp+=1;}case v4: if (col_check('left')) {self.x -= sprite_height; temp +=1; }case v5: if (col_check('right')){self.x += sprite_height; temp +=1; }//diagonalcase v3: if (col_check('up')) and (col_check('right')){self.x += sprite_height; self.y -= sprite_height; temp +=1; }case v8: if (col_check('down')) and (col_check('right')){self.x += sprite_height; self.y += sprite_height; temp +=1; }case v1: if (col_check('left')) and (col_check('up')) {self.x -= sprite_height; self.y -= sprite_height; temp +=1; }case v6: if (col_check('left')) and (col_check('down')) {self.x -= sprite_height; self.y += sprite_height; temp +=1; } } }if temp = movesmax then c_e = true;********Result : same as above,just a little more accurate. (col_check checks if the argumented square is free)Could you please show me what's wrong with my codes? :/ I just can't find it…P.S.I WOULD have posted this in the forums, but user registration is forbidden…
lmao, melee STILL hasn't fixed the forums?
In any case, nobody who knows what a GML is goes there anymore.>:/ (josef fritzl)
Have you made sure to stop case leaking?
Er?… Case leaking?… What dos that mean?… :/
switch 1{
case 1://Triggers casecase 2://Gets leaked intobreak;case 3://Break stops leakage}Useful thing when you know about it, evil when you don't. I get annoyed when I find languages don't have case leaking, might as well just be some syntactic sugar for if{} in that caseSo, you mean I should use breaks?
If you don't want all the cases being executed after the true case is found, yes
Heh :D Thank you very much.
I'd help, but my coding style is so different from this… I spread mine out and use lots of brackets to make it easy to read, this is all… Efficient… And compact…
This isn't compact, he could take the spaces out around comparison operators, use a consistent rejection of if then instead of if{} and not use the ugly worded logical ops. Really, operators weren't meant to be letters. Makes mentally parsing tedious. If he was efficient, his col_check() wouldn't use strings but numbers
Also, odd use of while(con) do{} instead of just while (con){}. Never knew GM could do that, seems kind of pointless to let it happen. I'd think it'd be better to leave it be and always just let while(con) do{}until(con) be while(con){do{}until(con)}…ya, that's kinda weird. How did you happen to fall across this hidden syntactic sugar?