# Omnimaga

## Calculator Community => Other Calc-Related Projects and Ideas => TI Z80 => Topic started by: Builderboy on February 28, 2012, 11:29:22 pm

Title: Core Wars
Post by: Builderboy on February 28, 2012, 11:29:22 pm
A few days ago a friend in my math class introduced me to the game Core Wars, and I immediately decided to port it to the calculator!  Core Wars is a two player competitive game, featuring a unique type of gameplay.  The two contestants must each write a program in Redcode, which is the programming language of the Core.  Each program is then inserted into a section of memory called the Core.  If either program stops executing, that player loses.  Of course it is simple to make a program that never terminates when left on its own, the tricky part is that both programs live in the same memory (the Core) and so can influence each other.  The programs must try to find their opposing program and sabotage and destroy it in order to halt it's execution.  It's the greatest battle of a programmers ingenuity, as they pit their own programs to destroy others in The Core!

Redcode:
The programming language of The Core is called RedCode, and is like a simplified version of assembly.  There are only 18 possible instructions, but many different modifiers that make each instruction powerful.  The instructions are formatted in a very specific way that is consistant for all instructions:

Code: [Select]
`NAME A B`
NAME is the name of the instruction.  The name also indicates the type of instruction, and what it does.  The A and B field both hold numbers, and in general data passes from the A field into the B field.  The instructions are specifically:

Code: [Select]
`DAT: If any process tries to execute a DAT command, that process is killed.  A and B field can be used to hold data.MOV: Moves data from A into BADD: Adds A to B and stores the result into BSUB: Subtracts A from B and stores the result into BMUL: Multiplies A and B and stores the result into BDIV: Divides B by A and stores the result into BMOD: Divides B by A and stores the remainder into BJMP: Moves program execution to location A (B is ignored)JMZ: Jumps to location A if B is zeroJMN: Humps to location A if B is not zeroDJN: Decreases B and jumps to A if B is not zeroSPL: Starts a new process at location ASEQ: Skips the next instruction if A and B are equalSNE: Skips the next instruction if A and B are not equalSLT: Skips the next instruction if A is less than BNOP: Does nothing, guess you could use this to hold data as well.`
There is more than these instructions, and A/B than is just told here, and there are some good tutorials that I will LINK (http://vyznev.net/corewar/guide.html#deeper) to in order to help keep this post shorter, and to probably help better explain as well.

One element I do want to cover though is the SPL instruction.  It says that it creates another process at location A, but what does that mean?  Processes in Redcode are handled a very specific way.  If you have two processes, each process will run twice as slow as if you only had one process.  In other words, creating new processes does not give you any more computing power.  If you had 3 processes and your opponent only has 1, then execution might look something like this:

Code: [Select]
`Player 1 Process 1Player 2 Process 1Player 1 Process 2Player 2 Process 1Player 1 Process 3Player 2 Process 1`
As you can see, Player 2 was able to execute 3 instructions in his one process, while each of your processes were only able to execute a single instruction.

Conclusion/Plans:
Now the game hasn't been completed yet, but I do have the majority of the instructions implemented, as well as an assembler and a working interpreter.  I will be updating the tutorial part of this thread as time goes on, but in the meantime feel free to visit This (http://vyznev.net/corewar/guide.html#deeper) tutorial as well.  The syntax is slightly different since we don't have access to the same characters as they do, but I will be posting more on syntax in the future.  For now I will leave you with this screenie of what the game screen will look like!  It features the two program's names, as well as showing how many processes they currently have.  The square in the center is a visual representation of the Core, and shows how the programs move through and modify memory.  The two other squares are each programs private memory areas.

(http://img.removedfromgame.com/imgs/CoreWarsBattle.gif)(http://img.removedfromgame.com/imgs/CoreWarsDebug.gif)
Title: Re: Core Wars
Post by: TIfanx1999 on February 29, 2012, 12:11:06 am
I've neither played nor heard of the original, but this looks really interesting. :)
Title: Re: Core Wars
Post by: Builderboy on March 01, 2012, 08:45:44 pm
Update!  Both the assembler and the interpreter are complete!  Every command is functional and tested (hopefully) and programs can now compete successfully :) Now all that is left to do is to set up the UI, and start having wars!
Title: Re: Core Wars
Post by: LincolnB on March 01, 2012, 09:59:48 pm
Sweet! I read about this game in some book, and it sounded really awesome. Know what'd be cool? Setting this up over global Calcnet and battling fellow omni-and ceme-ians. :D
Title: Re: Core Wars
Post by: Builderboy on March 01, 2012, 10:05:21 pm
It's not played over the link port, so that would be a bit weird methinks :P The programmers write their programs separately, and then only need to get them onto the same calculator in order to run the competition.  I do hope that we can set up a competition though :D
Title: Re: Core Wars
Post by: mrmprog on March 01, 2012, 10:11:32 pm
No way! Just recently I was considering starting something similar to this. I am glad you did though, you will do a much better job than I would have. This game has always intrigued me, and I am very happy to see it for calc.
Title: Re: Core Wars
Post by: shmibs on March 02, 2012, 02:31:24 am
this is brilliant!
i can't wait to test it out against you folks
Title: Re: Core Wars
Post by: Jonius7 on March 02, 2012, 04:12:10 am
Wow! The ingenuity of the original clearly looks translated to the calc version, man, I've never heard Core Wars but sound like quite a game I've never seen before. I'll see how I go as soon as it is available.
Title: Re: Core Wars
Post by: Builderboy on March 02, 2012, 12:30:45 pm
Progress is still chugging along!  I found a bug or two in the interpreter involving post-incrementing on the A operator, and I'm working on fixing that now, as well as getting the UI up to speed.  Memkit is really helping a lot, and the UI should be finished relatively soon.  I will be adding options to provide different core sizes, as well as running multiple games back to back.
Title: Re: Core Wars
Post by: Scipi on March 02, 2012, 12:43:37 pm
It's not played over the link port, so that would be a bit weird methinks :P The programmers write their programs separately, and then only need to get them onto the same calculator in order to run the competition.  I do hope that we can set up a competition though :D

You could probably set up multiplayer so that one calc serves as the host. And the client sends its code across to the host and the host runs the game with both codes.
Title: Re: Core Wars
Post by: Builderboy on March 02, 2012, 12:50:47 pm
It would probably just be easier to send the program using the built in TiOS interface.  You would still be connecting via linkport, but you could disconnect as soon as the program was transferred.  No reason to force the users to be connected through the entire game when they only have to be connected to send the program, especially when it would add a whole bunch of unnecessary linking code.
Title: Re: Core Wars
Post by: Spyro543 on March 02, 2012, 04:52:22 pm
I think the coolest thing is the visual representation of the Core. Are all the on/off pixels bits of data that are equal to 1 or 0?
Title: Re: Core Wars
Post by: Builderboy on March 02, 2012, 04:58:25 pm
Each pixel is a single instruction.  If the instruction is a DAT instruction the pixel is white, and black otherwise.
Title: Re: Core Wars
Post by: Spyro543 on March 02, 2012, 05:02:50 pm
That's cool! Will you have an animated screenshot soon?
Title: Re: Core Wars
Post by: Builderboy on March 02, 2012, 05:08:29 pm
Very soon!  I'm currently working on getting all of the UI elements coded, and after that is done there should be many screenshots to be had ^^
Title: Re: Core Wars
Post by: Builderboy on March 13, 2012, 05:45:56 pm
Alright, the first beta is out!  Probably some bugs that I couldn't find, but it should be mostly workable ^^ Try it out!  I provided some instruction on how to program, but more complete tutorials like the one i posted in the original post are probably much better.
Title: Re: Core Wars
Post by: calcdude84se on March 13, 2012, 09:56:21 pm
Looks very nice. I'll have to try it. Just looking at the README, I think you meant to mention addressing concerning both A and B, not A twice.
Are indirect, indirect predecrement, and indirect postincrement for B /, <, and >, respectively?
Title: Re: Core Wars
Post by: Builderboy on March 13, 2012, 09:58:55 pm
Ah yes It does look like I incorrectly named the last 3 addressing modes, the descriptions are correct though, they refer to the B argument.
Title: Re: Core Wars
Post by: mrmprog on March 14, 2012, 10:05:00 pm
When I try to transfer, it says that the app has a ban signature. Has anyone else encountered this?
Title: Re: Core Wars
Post by: LincolnB on March 14, 2012, 10:13:43 pm
Yep, I have too.
Title: Re: Core Wars
Post by: leafy on March 14, 2012, 10:15:43 pm
Just sign it with wabbitsign and it should be all good.
Title: Re: Core Wars
Post by: Builderboy on March 15, 2012, 01:41:01 am
Alright I signed the app, fixed some issues with the Readme, and re-uploaded to the first post!  I also cross-posted to Cemetech; time to get people learning Redcode so we can have a multi-community battle! :D
Title: Re: Core Wars
Post by: C0deH4cker on March 15, 2012, 10:02:41 am
do you plan on adding support for equates, macros, or for/rof? Great job btw :)
Title: Re: Core Wars
Post by: Builderboy on March 15, 2012, 01:28:09 pm
I Don't plan on  adding either of those first two things, but what is a for/rof?
Title: Re: Core Wars
Post by: Geekboy1011 on March 15, 2012, 01:34:01 pm
imma have to learn this. looks like fun :D

builder you should add gcn support so we can play OVER TEH INTERWEBZZZZ
Title: Re: Core Wars
Post by: Builderboy on March 15, 2012, 01:40:10 pm
Lol I've said it before and I'll say it again, there is no need for real time program sending, because the users don't write programs in real time.  You could just as easily send your program to a friend and run it on their calculator :P
Title: Re: Core Wars
Post by: Geekboy1011 on March 15, 2012, 01:41:53 pm
but you could automate the sending over gcn watch it in real time with both parties and have it announce it to irc leik a boss tho :D
Title: Re: Core Wars
Post by: Builderboy on March 15, 2012, 01:43:10 pm
I could, but I will work on putting more useful things in the limited app space :P
Title: Re: Core Wars
Post by: Geekboy1011 on March 15, 2012, 01:45:56 pm
Fiiiiiinnnnnneeee :P
Title: Re: Core Wars
Post by: Stefan Bauwens on March 15, 2012, 03:17:27 pm
This looks quite interesting. But quite complicated looking at it though. But I'm sure if someone puts his mind to it, he can accomplish anything(I know, it's a silly quote). :)
Title: Re: Core Wars
Post by: hellninjas on March 15, 2012, 05:49:15 pm
Quite the accomplishment builderboy!
"I still like fruit ninja though :D"
But this definitely has caught my eye... So i will download!/me wonders if core wars is on the ipod app store o.o
Title: Re: Core Wars
Post by: C0deH4cker on March 15, 2012, 10:36:52 pm
Quite the accomplishment builderboy!
"I still like fruit ninja though :D"
But this definitely has caught my eye... So i will download!/me wonders if core wars is on the ipod app store o.o

Sadly, there isnt, but I managed to port pMars to iOS. You need to be jailbroken to run it though. I will upload to my repo in a little bit. iNinjas Repo (http://ininjas.com/repo/).
Title: Re: Core Wars
Post by: C0deH4cker on March 17, 2012, 04:51:38 pm
I uploaded it to the repo. It is a package called pMARS. To visually view the core it takes quite a few steps:

2. Download all of the packages in the X Windows section, and also get WindowMaker.
3. Get iSSH from the AppStore (or somewhere else...).
4. Add a new configuration. Set the following fields:
Description: root
Host: 127.0.0.1
Port: 22
Password: alpine (unless you changed this)
5. Click Save and then open the new connection.
6. Type: cd /var/root/src/redcode/warriors
7. Press the 'X' in the top-right.
8. Press the other 'X' that appears in the top-right. When it asks you to start the X server, say yes.
9. Press the 'X in the top-right.
10. Press on the connection to "root".
11. Type: export DISPLAY=127.0.0.1:0
12. Type: wmaker &
Ignore any warnings it prints.
13. Type: pmars aeka.red rave.red
14. The programs are now fighting. To view the progress:
15. Press the 'X' in the top-right corner.
16. Press the other 'X' that appears in the top-right corner.
17. Enjoy!

Since that is an extremely long process, I will record a video of pMARS in action.
Title: Re: Core Wars
Post by: Scipi on March 17, 2012, 11:12:36 pm
We really should have a ([semi]-regular) tournament for this on Omni and Cemetech. (Maybe even Omni vs Cemetech) XD

I'm gonna start looking into this on my spare time and really get into it hopefully. :P
Title: Re: Core Wars
Post by: hellninjas on March 19, 2012, 11:56:01 am
Codehacker, now i have something to do when i get home, or am at school :P
Tanks a bunch! :D
Title: Re: Core Wars
Post by: C0deH4cker on March 19, 2012, 04:03:15 pm
no problemo ;)
Title: Re: Core Wars
Post by: mrmprog on March 20, 2012, 01:20:12 pm
Could any of you guys who are familier with redcode post some of your own warriors? I undertstand the language, but I dont quite have the perfect "grasp" that I want:)
Title: Re: Core Wars
Post by: Builderboy on March 21, 2012, 12:29:54 am
I myself don't have any good examples with me, but maybe you could check the internet!  Core wars is a direct port, so any program you find on the web should theoretically work in my program too, as long as it doesn't depend on a specific core size
Title: Re: Core Wars
Post by: mrmprog on March 22, 2012, 01:22:06 pm
Ok, thanks. Is it ok to post our own warriors here, or should we make a new thread? By the way, how do you store each memory location?
Title: Re: Core Wars
Post by: Builderboy on March 22, 2012, 01:52:05 pm
You can make a new thread, and people can learn from everybody's examples ^^ And Storing to memory locations is done in general by the MOV command, which moves an instruction from one location into another.  Different addressing modes and instruction modifiers can restrict and alter this behavior as well.
Title: Re: Core Wars
Post by: shkaboinka on March 22, 2012, 01:59:06 pm
I've been thinking of making a game based on this idea: It would involve robots moving around on a grid which would have "instructions" in various locations. The instructions would be collected into a program, which can then be dropped onto the grid as an instruction of it's own. There might be multiple robots that can influence eachother ... I haven't decided the objective though :/
Title: Re: Core Wars
Post by: Builderboy on March 22, 2012, 03:28:13 pm
Sounds interesting, you should make a thread about it if it gets off the ground :)
Title: Re: Core Wars
Post by: Spyro543 on March 22, 2012, 03:39:55 pm
I've been thinking of making a game based on this idea: It would involve robots moving around on a grid which would have "instructions" in various locations. The instructions would be collected into a program, which can then be dropped onto the grid as an instruction of it's own. There might be multiple robots that can influence eachother ... I haven't decided the objective though :/
I've been working on a game like this...but it's for computer.
Title: Re: Core Wars
Post by: DJ Omnimaga on March 22, 2012, 08:17:30 pm
Seems interesting, but unfortunately despite having looked at Youtube videos and reading explanations, I have no clue what it does exactly and the goal. X.x Hopefully it's fun for other people, though.
Title: Re: Core Wars
Post by: Builderboy on March 26, 2012, 08:27:39 pm
We should start a mini contest using Core Warriors!  Who would be interested?
Title: Re: Core Wars
Post by: hellninjas on March 26, 2012, 08:28:56 pm
I thought one had already started? I might be mistaken, but sure i'll participate ONCE I FIGURE OUT THIS FREAGIN LANGUAGE!
Title: Re: Core Wars
Post by: Scipi on March 26, 2012, 08:33:28 pm
We should hold a monthly tournament, definitely. :D

(What if we had several sections as well with modified rules? Say, processes in one might all run at the same time no matter how many a program has?)
Title: Re: Core Wars
Post by: Builderboy on March 26, 2012, 08:47:55 pm
Nope, no tournament has been started yet, although a thread has been started where people have been sharing their creations for fun.  And Homer, the issue I see with that rule-set is that games would be too short.  Within 22 frames a program could have completely filled the entire core (6 to reach 64 processes, 16 to fill the core) and there becomes no opportunity cost to creating processes.
Title: Re: Core Wars
Post by: Scipi on March 26, 2012, 10:03:18 pm
Ah, true. I suggested that because I would like to personally see something emerge I like to call an "Unpacker" which would basically deploy itself as several subprograms and could even have code to restore itself if it becomes damaged. (XD)

Although I guess that is still possible to do, it would just take a long time to run.
Title: Re: Core Wars
Post by: hellninjas on March 27, 2012, 12:34:50 am
/me runs Imp, NEVER ENDING BATTLE!
Title: Re: Core Wars
Post by: mrmprog on March 27, 2012, 09:00:48 pm
Does anyone else get random text when exiting? I will post a screenshot if needed.
Title: Re: Core Wars
Post by: C0deH4cker on March 28, 2012, 02:09:43 pm
Is there no limit on number of instructions run in a battle? It sucks to be battling two warriors where one of them uses an imp, and then it just freezes when the imp takes over.
Title: Re: Core Wars
Post by: Builderboy on March 28, 2012, 02:10:47 pm
If over 9000 instructions are executed, the game is taken to be a draw.  I might have to lower that limit :P
Title: Re: Core Wars
Post by: mrmprog on March 28, 2012, 07:53:38 pm
Yeah, is it possible for us to have customizable process limits/instruction limits? And btw, is it just me, or is the "start in random location" thing not random for the first program? It seems like it always starts at the top corner, no where else. This has led to some strange behavior. Also, I would be interested in a tournament.
Title: Re: Core Wars
Post by: Builderboy on March 28, 2012, 10:00:31 pm
Customizable process limits?  What do you mean by that?  Also since positions are relative, the first program starting at the 'begining' of the core is irrelevant
Title: Re: Core Wars
Post by: calcdude84se on March 28, 2012, 10:12:09 pm
I think he means the number of times SPL (Is that it? I mean the instruction used to "fork".) can be executed successfully.
Title: Re: Core Wars
Post by: Builderboy on March 29, 2012, 12:00:40 am
Ah I see, and that unfortunately is very difficult to change, as it affects a lot of different memory locations.  I made it as large as I could before starting to eat into even more memory gained from appvars.  Remember that each process takes 2 full bytes, so with a max of 64 processes, that is already 256 bytes of data.
Title: Re: Core Wars
Post by: jsj795 on March 29, 2012, 12:15:24 am
Trying to learn to write RedCode so I can actually play this :P There's a lot to cover :O
Title: Re: Core Wars
Post by: mrmprog on March 29, 2012, 01:20:21 pm
What I meant was the actually being able to lower the limit on threads :P Is it possible to make the debugger able to look at other memory locations in the core, and have a mode to constantly execute the program, instead of having to step?
Title: Re: Core Wars
Post by: Builderboy on March 29, 2012, 02:05:13 pm
Ah I see, yeah that would be possible, I'll implement that ^^
Title: Re: Core Wars
Post by: C0deH4cker on March 29, 2012, 04:21:14 pm
Maybe a way in the debugger to scroll through the core?
Title: Re: Core Wars
Post by: Builderboy on March 29, 2012, 04:22:48 pm
Yeah thats what mrmprog suggested
Title: Re: Core Wars
Post by: mrmprog on March 29, 2012, 09:37:45 pm
Is it possible to make a system that defends against imps? I am working on a warrior that would be really cool, but it must be able to always stop imps. I would have posted in the warriors thread, but I was the last to post there :P
Title: Re: Core Wars
Post by: Builderboy on March 29, 2012, 11:33:03 pm
The best way to defend against imps is to continuously decrement the B field of the memory location 2 instructions above your program.  If an imp hits that, it will fail to copy its statement to the next instruction and then die.
Title: Re: Core Wars
Post by: mrmprog on March 30, 2012, 11:30:45 am
So, how exactly would I decrement the B field? Sorry, Im pretty bad with redcode so far.
Title: Re: Core Wars
Post by: Builderboy on March 30, 2012, 01:10:53 pm
If you wanted a program that did nothing but kill an Imp, you would write something like this:

JMP 0 )-2

The JMP command always jumps to the location specified by the first argument (0) so this instruction will keep being executed over and over again.  The second argument isn't used for the JMP command, but we can still use it.  The ) addressing mode decrements the B field of whatever it points to, so )-2 decrements the B field of the instruction 2 behind the current one.
Title: Re: Core Wars
Post by: mrmprog on March 30, 2012, 07:18:12 pm
Thanks! Using that, I made a nifty warrior that bombs with MOV 0 1s, and then turns into just an imp gate when it is done bombing. It seems to be surprisingly effective (or maybe my test subjects are just really bad). Do you plan make some sort of development environment, maybe as a exasperated program? Also, I noticed that occasionally, it will "skip" through rounds, as if the enemy warrior is killed instantly. It is difficult to replicate this though.
Title: Re: Core Wars
Post by: Builderboy on March 30, 2012, 07:26:26 pm
What kind of developemtn environment do you mean?  And I'm not sure if exasperated is the word you are thinking of :P

And yeah I have had that bug happen to me as well, I am working on trying to figure out why it happens
Title: Re: Core Wars
Post by: mrmprog on March 31, 2012, 12:15:35 am
Auto correct fail... x.x I meant external. By a development environment, I mean something that will allow us to not have to type out the commands, like a redcode editor. I was just thinking it could make creation of programs easier.
Title: Re: Core Wars
Post by: Builderboy on March 31, 2012, 12:57:44 am
How else would you enter the commands?
Title: Re: Core Wars
Post by: mrmprog on March 31, 2012, 11:20:23 am
I mean a separate program, in which you can navigate a menu to select the commands you want, instead of typing them. The program would then save the warrior as a program.
Title: Re: Core Wars
Post by: Builderboy on March 31, 2012, 01:17:59 pm
I personally would think that having to select a token from a menu of 20 would be a lot more annoying than just simply typing it out yourself.  Either way, there probably won't be a special editor for core wars, as I was never even able to get my Basic editor to work properly D:
Title: Re: Core Wars
Post by: mrmprog on April 01, 2012, 02:01:09 pm
Hmm, you are probably right. The more I type the commands, the faster I get :) Will it be possible in future updates to do this?
Code: [Select]
`Jmp I+2`I think it would make writing some things a little easier, unless it requires major changes on your part.
Title: Re: Core Wars
Post by: Builderboy on April 01, 2012, 02:50:13 pm
It could probably be done, but maybe instead the syntax would be 2+I, as that would make it a bit easier to parse